use of org.openkilda.messaging.command.CommandData in project open-kilda by telstra.
the class FlowResource method installFlow.
@Post("json")
@Put("json")
public String installFlow(String json) throws IOException {
ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes().get(ISwitchManager.class.getCanonicalName());
Message message;
try {
message = MAPPER.readValue(json, Message.class);
} catch (IOException exception) {
String messageString = "Received JSON is not valid for TPN";
logger.error("{}: {}", messageString, json, exception);
MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, now(), ErrorType.DATA_INVALID.toString(), messageString, exception.getMessage());
return MAPPER.writeValueAsString(responseMessage);
}
if (!(message instanceof CommandMessage)) {
String messageString = "Json payload message is not an instance of CommandMessage";
logger.error("{}: class={}, data={}", messageString, message.getClass().getCanonicalName(), json);
MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, now(), ErrorType.DATA_INVALID.toString(), messageString, message.getClass().getCanonicalName());
return MAPPER.writeValueAsString(responseMessage);
}
CommandMessage cmdMessage = (CommandMessage) message;
CommandData data = cmdMessage.getData();
if (!(data instanceof BaseInstallFlow)) {
String messageString = "Json payload data is not an instance of CommandData";
logger.error("{}: class={}, data={}", messageString, data.getClass().getCanonicalName(), json);
MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, now(), ErrorType.DATA_INVALID.toString(), messageString, data.getClass().getCanonicalName());
return MAPPER.writeValueAsString(responseMessage);
}
return MAPPER.writeValueAsString("ok");
}
use of org.openkilda.messaging.command.CommandData in project open-kilda by telstra.
the class KafkaMessageConsumer method receive.
/**
* Receives messages from WorkFlowManager queue.
*
* @param record the message object instance
*/
@KafkaListener(topics = "kilda-test")
public void receive(final String record) {
logger.debug("message received: {}", record);
try {
Message message = MAPPER.readValue(record, Message.class);
if (message.getDestination() == null || Destination.TOPOLOGY_ENGINE.equals(message.getDestination())) {
if (message instanceof CommandMessage) {
CommandData data = ((CommandMessage) message).getData();
if (data instanceof FlowCreateRequest) {
FlowPayload payload = ((FlowCreateRequest) data).getPayload();
logger.debug("FlowCreateRequest: {}", payload);
Set<CommandMessage> commands = flowService.createFlow(payload, message.getCorrelationId());
for (CommandMessage response : commands) {
kafkaMessageProducer.send(topic, response);
}
logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
} else if (data instanceof FlowDeleteRequest) {
FlowIdStatusPayload payload = ((FlowDeleteRequest) data).getPayload();
logger.debug("FlowDeleteRequest: {}", payload);
Set<CommandMessage> commands = flowService.deleteFlow(payload, message.getCorrelationId());
for (CommandMessage response : commands) {
kafkaMessageProducer.send(topic, response);
}
logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
} else if (data instanceof FlowUpdateRequest) {
FlowPayload payload = ((FlowUpdateRequest) data).getPayload();
logger.debug("FlowUpdateRequest: {}", payload);
Set<CommandMessage> commands = flowService.updateFlow(payload, message.getCorrelationId());
for (CommandMessage response : commands) {
kafkaMessageProducer.send(topic, response);
}
logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
} else if (data instanceof FlowGetRequest) {
FlowIdStatusPayload payload = ((FlowGetRequest) data).getPayload();
logger.debug("FlowGetRequest: {}", payload);
InfoMessage response = flowService.getFlow(payload, message.getCorrelationId());
kafkaMessageProducer.send(topic, response);
logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
} else if (data instanceof FlowsGetRequest) {
FlowIdStatusPayload payload = ((FlowsGetRequest) data).getPayload();
logger.debug("FlowsGetRequest: {}", payload);
InfoMessage response = flowService.getFlows(payload, message.getCorrelationId());
kafkaMessageProducer.send(topic, response);
logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
} else if (data instanceof FlowPathRequest) {
FlowIdStatusPayload payload = ((FlowPathRequest) data).getPayload();
logger.debug("FlowPathRequest: {}", payload);
InfoMessage response = flowService.pathFlow(payload, message.getCorrelationId());
kafkaMessageProducer.send(topic, response);
logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
} else {
logger.error("Unexpected command message data type: {}", data);
}
} else if (message instanceof InfoMessage) {
InfoData data = ((InfoMessage) message).getData();
if (data instanceof SwitchInfoData) {
SwitchInfoData payload = (SwitchInfoData) data;
switch(payload.getState()) {
case ADDED:
switchService.add(payload);
break;
case ACTIVATED:
switchService.activate(payload);
break;
case DEACTIVATED:
switchService.deactivate(payload);
break;
case REMOVED:
switchService.remove(payload);
break;
case CHANGED:
default:
break;
}
} else if (data instanceof IslInfoData) {
IslInfoData payload = (IslInfoData) data;
islService.discoverLink(payload);
} else {
logger.debug("Unexpected info message data type: {}", data);
}
}
} else {
logger.debug("Skip message: {}", message);
}
} catch (IOException exception) {
logger.error("Could not deserialize message: {}", record, exception);
}
}
use of org.openkilda.messaging.command.CommandData in project open-kilda by telstra.
the class CommandBolt method processCommand.
protected void processCommand(Tuple tuple) throws Exception {
CommandMessage command = Utils.MAPPER.readValue(getJson(tuple), CommandMessage.class);
if (command.getDestination() == Destination.CONTROLLER) {
CommandData data = command.getData();
Commands switchCommand;
String sw;
if (data instanceof DiscoverIslCommandData) {
switchCommand = Commands.DO_DISCOVER_ISL_COMMAND;
sw = ((DiscoverIslCommandData) data).getSwitchId();
} else if (data instanceof DiscoverPathCommandData) {
switchCommand = Commands.DO_DISCOVER_PATH_COMMAND;
sw = ((DiscoverPathCommandData) data).getSrcSwitchId();
} else if (data instanceof InstallIngressFlow) {
switchCommand = Commands.DO_INSTALL_INGRESS_FLOW;
sw = ((InstallIngressFlow) data).getSwitchId();
} else if (data instanceof InstallEgressFlow) {
switchCommand = Commands.DO_INSTALL_EGRESS_FLOW;
sw = ((InstallEgressFlow) data).getSwitchId();
} else if (data instanceof InstallTransitFlow) {
switchCommand = Commands.DO_INSTALL_TRANSIT_FLOW;
sw = ((InstallTransitFlow) data).getSwitchId();
} else if (data instanceof InstallOneSwitchFlow) {
switchCommand = Commands.DO_INSTALL_ONESWITCH_FLOW;
sw = ((InstallOneSwitchFlow) data).getSwitchId();
} else if (data instanceof RemoveFlow) {
switchCommand = Commands.DO_DELETE_FLOW;
sw = ((RemoveFlow) data).getSwitchId();
} else {
logger.error("unknown data type: {}", data.toString());
throw new Exception("Unknown command {}".format(data.getClass().getSimpleName()));
}
List<Integer> taskIDs = collector.emit(SimulatorTopology.COMMAND_BOLT_STREAM, tuple, new Values(sw.toLowerCase(), switchCommand.name(), command.getData()));
// logger.info("{}: {} - {}", switchCommand.name(), sw, command.getData().toString());
}
}
use of org.openkilda.messaging.command.CommandData in project open-kilda by telstra.
the class CacheWarmingService method getFlowCommands.
public List<CommandData> getFlowCommands(String switchId) {
List<CommandData> result = new ArrayList<>();
predefinedFlows.stream().filter(flow -> switchId.equals(flow.getLeft().getSourceSwitch())).forEach(pair -> {
CommandData command = getFlowCommandIfNeeded(pair.getLeft());
if (Objects.nonNull(command)) {
result.add(command);
}
});
return result;
}
use of org.openkilda.messaging.command.CommandData in project open-kilda by telstra.
the class CacheWarmingService method getFlowCommandIfNeeded.
private CommandData getFlowCommandIfNeeded(Flow flow) {
CommandData request = null;
if (flow.getState() == FlowState.CACHED && isFlowSwitchesUp(flow)) {
request = new FlowCreateRequest(flow);
LOGGER.info("Created flow create request for flowId {}", flow.getFlowId());
predefinedFlows.stream().filter(item -> item.getLeft().equals(flow)).findFirst().ifPresent(pair -> pair.setRight(CachedFlowState.CREATED));
}
return request;
}
Aggregations