Search in sources :

Example 11 with MessageException

use of org.openkilda.messaging.error.MessageException in project open-kilda by telstra.

the class BasicService method validateInfoMessage.

/**
 * Validates info message.
 *
 * @param correlationId     request correlation id
 * @param requestMessage    request command message
 * @param responseMessage   response info message
 * @return parsed response InfoData
 */
default InfoData validateInfoMessage(final Message requestMessage, final Message responseMessage, final String correlationId) {
    InfoData data;
    if (responseMessage != null) {
        if (responseMessage instanceof ErrorMessage) {
            ErrorData error = ((ErrorMessage) responseMessage).getData();
            logger.error("Response message is error: {}={}, command={}, error={}", CORRELATION_ID, correlationId, requestMessage, error);
            throw new MessageException((ErrorMessage) responseMessage);
        } else if (responseMessage instanceof InfoMessage) {
            InfoMessage info = (InfoMessage) responseMessage;
            data = info.getData();
            if (data == null) {
                String errorMessage = "Response message data is empty";
                logger.error("{}: {}={}, command={}, info={}", errorMessage, CORRELATION_ID, correlationId, requestMessage, info);
                throw new MessageException(responseMessage.getCorrelationId(), responseMessage.getTimestamp(), INTERNAL_ERROR, errorMessage, requestMessage.toString());
            }
        } else {
            String errorMessage = "Response message type is unexpected";
            logger.error("{}: {}:{}, command={}, message={}", errorMessage, CORRELATION_ID, correlationId, requestMessage, responseMessage);
            throw new MessageException(responseMessage.getCorrelationId(), responseMessage.getTimestamp(), INTERNAL_ERROR, errorMessage, requestMessage.toString());
        }
    } else {
        String errorMessage = "Response message is empty";
        logger.error("{}: {}={}, command={}", errorMessage, CORRELATION_ID, correlationId, requestMessage);
        throw new MessageException(DEFAULT_CORRELATION_ID, System.currentTimeMillis(), INTERNAL_ERROR, errorMessage, requestMessage.toString());
    }
    return data;
}
Also used : MessageException(org.openkilda.messaging.error.MessageException) InfoData(org.openkilda.messaging.info.InfoData) InfoMessage(org.openkilda.messaging.info.InfoMessage) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) ErrorData(org.openkilda.messaging.error.ErrorData)

Example 12 with MessageException

use of org.openkilda.messaging.error.MessageException in project open-kilda by telstra.

the class FlowServiceImpl method createFlow.

/**
 * {@inheritDoc}
 */
@Override
public Set<CommandMessage> createFlow(final FlowPayload payload, final String correlationId) {
    Switch source = switchRepository.findByName(payload.getSource().getSwitchId());
    Switch destination = switchRepository.findByName(payload.getDestination().getSwitchId());
    if (source == null || destination == null) {
        logger.error("Switches not found: source={}, destination={}", payload.getSource().getSwitchId(), payload.getDestination().getSwitchId());
        throw new MessageException(ErrorType.NOT_FOUND, System.currentTimeMillis());
    }
    List<Isl> path = islRepository.getPath(source.getName(), destination.getName());
    if (path == null || path.isEmpty()) {
        logger.error("Path not found: source={}, destination={}", payload.getSource().getSwitchId(), payload.getDestination().getSwitchId());
        throw new MessageException(ErrorType.NOT_FOUND, System.currentTimeMillis());
    }
    List<Isl> sortedPath = sortPath(source.getName(), path);
    logger.debug("Path found: {}", sortedPath);
    int directVlanId = transitVlanIdPool.allocate();
    int reverseVlanId = transitVlanIdPool.allocate();
    long cookie = getCookie();
    Flow direct = buildFlow(path, source, destination, payload, directVlanId, cookie | DIRECT_FLOW_COOKIE);
    Flow reverse = buildFlow(path, destination, source, payload, reverseVlanId, cookie | REVERSE_FLOW_COOKIE);
    flowRepository.save(direct);
    logger.debug("Flow stored: flow={}", direct);
    flowRepository.save(reverse);
    logger.debug("Flow stored: flow={}", reverse);
    Set<CommandMessage> response = new HashSet<>();
    response.addAll(direct.getInstallationCommands(sortedPath, correlationId));
    response.addAll(reverse.getInstallationCommands(sortedPath, correlationId));
    logger.debug("Flows create command message list: {}", response);
    return response;
}
Also used : Isl(org.openkilda.topology.domain.Isl) Switch(org.openkilda.topology.domain.Switch) MessageException(org.openkilda.messaging.error.MessageException) Flow(org.openkilda.topology.domain.Flow) CommandMessage(org.openkilda.messaging.command.CommandMessage) HashSet(java.util.HashSet)

Example 13 with MessageException

use of org.openkilda.messaging.error.MessageException in project open-kilda by telstra.

the class SwitchServiceImpl method deactivate.

/**
 * {@inheritDoc}
 */
@Override
public Switch deactivate(final SwitchInfoData data) {
    String name = data.getSwitchId();
    String state = SwitchStateType.INACTIVE.toString().toLowerCase();
    logger.debug("Switch deactivating: switch-id={}", name);
    Switch sw = switchRepository.findByName(name);
    if (sw == null) {
        throw new MessageException(ErrorType.NOT_FOUND, System.currentTimeMillis());
    }
    sw.setState(state);
    sw.setLabels(state, data.getDescription());
    switchRepository.save(sw);
    return sw;
}
Also used : Switch(org.openkilda.topology.domain.Switch) MessageException(org.openkilda.messaging.error.MessageException)

Example 14 with MessageException

use of org.openkilda.messaging.error.MessageException in project open-kilda by telstra.

the class SwitchServiceImpl method activate.

/**
 * {@inheritDoc}
 */
@Override
public Switch activate(final SwitchInfoData data) {
    String name = data.getSwitchId();
    String state = SwitchStateType.ACTIVE.toString().toLowerCase();
    logger.debug("Switch activating: switch-id={}", name);
    Switch sw = switchRepository.findByName(name);
    if (sw == null) {
        throw new MessageException(ErrorType.NOT_FOUND, System.currentTimeMillis());
    }
    sw.setState(state);
    sw.setLabels(state, data.getDescription());
    switchRepository.save(sw);
    return sw;
}
Also used : Switch(org.openkilda.topology.domain.Switch) MessageException(org.openkilda.messaging.error.MessageException)

Example 15 with MessageException

use of org.openkilda.messaging.error.MessageException in project open-kilda by telstra.

the class KafkaMessageProducer method send.

/**
 * {@inheritDoc}
 */
@Override
public void send(final String topic, final Object object) {
    ListenableFuture<SendResult<String, String>> future;
    String message;
    try {
        message = MAPPER.writeValueAsString(object);
    } catch (JsonProcessingException exception) {
        String errorMessage = "Unable to serialize object";
        logger.error("{}: object={}", errorMessage, object, exception);
        throw new MessageException(DEFAULT_CORRELATION_ID, System.currentTimeMillis(), DATA_INVALID, errorMessage, object.toString());
    }
    future = kafkaTemplate.send(topic, message);
    future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {

        @Override
        public void onSuccess(SendResult<String, String> result) {
            logger.debug("Message sent: topic={}, message={}", topic, message);
        }

        @Override
        public void onFailure(Throwable exception) {
            logger.error("Unable to send message: topic={}, message={}", topic, message, exception);
        }
    });
    try {
        SendResult<String, String> result = future.get(TIMEOUT, TimeUnit.MILLISECONDS);
        logger.debug("Record sent: record={}, metadata={}", result.getProducerRecord(), result.getRecordMetadata());
    } catch (TimeoutException | ExecutionException | InterruptedException exception) {
        String errorMessage = "Unable to send message";
        logger.error("{}: topic={}, message={}", errorMessage, topic, message, exception);
        throw new MessageException(DEFAULT_CORRELATION_ID, System.currentTimeMillis(), INTERNAL_ERROR, errorMessage, message);
    }
}
Also used : MessageException(org.openkilda.messaging.error.MessageException) SendResult(org.springframework.kafka.support.SendResult) ExecutionException(java.util.concurrent.ExecutionException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

MessageException (org.openkilda.messaging.error.MessageException)15 InfoMessage (org.openkilda.messaging.info.InfoMessage)6 Switch (org.openkilda.topology.domain.Switch)6 Values (org.apache.storm.tuple.Values)4 PathInfoData (org.openkilda.messaging.info.event.PathInfoData)4 Flow (org.openkilda.messaging.model.Flow)4 UnroutablePathException (org.openkilda.pce.provider.UnroutablePathException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ErrorData (org.openkilda.messaging.error.ErrorData)2 ErrorMessage (org.openkilda.messaging.error.ErrorMessage)2 InfoData (org.openkilda.messaging.info.InfoData)2 Flow (org.openkilda.topology.domain.Flow)2 Isl (org.openkilda.topology.domain.Isl)2 FlowValidationException (org.openkilda.wfm.topology.flow.validation.FlowValidationException)2 FlowValidator (org.openkilda.wfm.topology.flow.validation.FlowValidator)2 SendResult (org.springframework.kafka.support.SendResult)2 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 CommandMessage (org.openkilda.messaging.command.CommandMessage)1