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;
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations