Search in sources :

Example 6 with CommandData

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");
}
Also used : ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) Message(org.openkilda.messaging.Message) CommandMessage(org.openkilda.messaging.command.CommandMessage) MessageError(org.openkilda.messaging.error.MessageError) IOException(java.io.IOException) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) CommandData(org.openkilda.messaging.command.CommandData) CommandMessage(org.openkilda.messaging.command.CommandMessage) Post(org.restlet.resource.Post) Put(org.restlet.resource.Put)

Example 7 with CommandData

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);
    }
}
Also used : FlowsGetRequest(org.openkilda.messaging.command.flow.FlowsGetRequest) Set(java.util.Set) FlowUpdateRequest(org.openkilda.messaging.command.flow.FlowUpdateRequest) InfoMessage(org.openkilda.messaging.info.InfoMessage) Message(org.openkilda.messaging.Message) CommandMessage(org.openkilda.messaging.command.CommandMessage) FlowIdStatusPayload(org.openkilda.messaging.payload.flow.FlowIdStatusPayload) FlowDeleteRequest(org.openkilda.messaging.command.flow.FlowDeleteRequest) FlowCreateRequest(org.openkilda.messaging.command.flow.FlowCreateRequest) FlowGetRequest(org.openkilda.messaging.command.flow.FlowGetRequest) IOException(java.io.IOException) CommandMessage(org.openkilda.messaging.command.CommandMessage) FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) InfoMessage(org.openkilda.messaging.info.InfoMessage) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData) InfoData(org.openkilda.messaging.info.InfoData) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) CommandData(org.openkilda.messaging.command.CommandData) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData) FlowPathRequest(org.openkilda.messaging.command.flow.FlowPathRequest) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Example 8 with CommandData

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());
    }
}
Also used : DiscoverIslCommandData(org.openkilda.messaging.command.discovery.DiscoverIslCommandData) Values(org.apache.storm.tuple.Values) CommandMessage(org.openkilda.messaging.command.CommandMessage) Commands(org.openkilda.simulator.classes.Commands) CommandData(org.openkilda.messaging.command.CommandData) DiscoverPathCommandData(org.openkilda.messaging.command.discovery.DiscoverPathCommandData) DiscoverIslCommandData(org.openkilda.messaging.command.discovery.DiscoverIslCommandData) DiscoverPathCommandData(org.openkilda.messaging.command.discovery.DiscoverPathCommandData)

Example 9 with CommandData

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;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) CommandData(org.openkilda.messaging.command.CommandData) Flow(org.openkilda.messaging.model.Flow) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) ImmutablePair(org.openkilda.messaging.model.ImmutablePair) PortInfoData(org.openkilda.messaging.info.event.PortInfoData) FlowCache(org.openkilda.pce.cache.FlowCache) PathNode(org.openkilda.messaging.info.event.PathNode) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MutablePair(org.apache.commons.lang3.tuple.MutablePair) FlowCreateRequest(org.openkilda.messaging.command.flow.FlowCreateRequest) PortChangeType(org.openkilda.messaging.info.event.PortChangeType) NetworkCache(org.openkilda.pce.cache.NetworkCache) FlowState(org.openkilda.messaging.payload.flow.FlowState) LogManager(org.apache.logging.log4j.LogManager) ArrayList(java.util.ArrayList) CommandData(org.openkilda.messaging.command.CommandData)

Example 10 with CommandData

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;
}
Also used : FlowCreateRequest(org.openkilda.messaging.command.flow.FlowCreateRequest) CommandData(org.openkilda.messaging.command.CommandData)

Aggregations

CommandData (org.openkilda.messaging.command.CommandData)10 CommandMessage (org.openkilda.messaging.command.CommandMessage)6 Values (org.apache.storm.tuple.Values)5 Message (org.openkilda.messaging.Message)5 IOException (java.io.IOException)4 FlowCreateRequest (org.openkilda.messaging.command.flow.FlowCreateRequest)4 DiscoverIslCommandData (org.openkilda.messaging.command.discovery.DiscoverIslCommandData)3 BaseInstallFlow (org.openkilda.messaging.command.flow.BaseInstallFlow)3 ErrorMessage (org.openkilda.messaging.error.ErrorMessage)3 InfoMessage (org.openkilda.messaging.info.InfoMessage)3 DiscoverPathCommandData (org.openkilda.messaging.command.discovery.DiscoverPathCommandData)2 FlowDeleteRequest (org.openkilda.messaging.command.flow.FlowDeleteRequest)2 FlowGetRequest (org.openkilda.messaging.command.flow.FlowGetRequest)2 FlowPathRequest (org.openkilda.messaging.command.flow.FlowPathRequest)2 FlowUpdateRequest (org.openkilda.messaging.command.flow.FlowUpdateRequest)2 FlowsGetRequest (org.openkilda.messaging.command.flow.FlowsGetRequest)2 RemoveFlow (org.openkilda.messaging.command.flow.RemoveFlow)2 InfoData (org.openkilda.messaging.info.InfoData)2 IslInfoData (org.openkilda.messaging.info.event.IslInfoData)2 FlowState (org.openkilda.messaging.payload.flow.FlowState)2