Search in sources :

Example 1 with BaseInstallFlow

use of org.openkilda.messaging.command.flow.BaseInstallFlow in project open-kilda by telstra.

the class SpeakerBolt method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Tuple tuple) {
    String request = tuple.getString(0);
    Values values = null;
    try {
        Message message = MAPPER.readValue(request, Message.class);
        if (!Destination.WFM_TRANSACTION.equals(message.getDestination())) {
            return;
        }
        logger.debug("Request tuple={}", tuple);
        if (message instanceof CommandMessage) {
            CommandData data = ((CommandMessage) message).getData();
            if (data instanceof BaseInstallFlow) {
                Long transactionId = ((BaseInstallFlow) data).getTransactionId();
                String switchId = ((BaseInstallFlow) data).getSwitchId();
                String flowId = ((BaseInstallFlow) data).getId();
                logger.debug("Flow install message: {}={}, switch-id={}, {}={}, {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId, request);
                message.setDestination(Destination.TOPOLOGY_ENGINE);
                values = new Values(MAPPER.writeValueAsString(message), switchId, flowId, transactionId);
                // FIXME(surabujin): looks like TE ignore this messages
                outputCollector.emit(StreamType.CREATE.toString(), tuple, values);
            } else if (data instanceof RemoveFlow) {
                Long transactionId = ((RemoveFlow) data).getTransactionId();
                String switchId = ((RemoveFlow) data).getSwitchId();
                String flowId = ((RemoveFlow) data).getId();
                logger.debug("Flow remove message: {}={}, switch-id={}, {}={}, {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId, request);
                message.setDestination(Destination.TOPOLOGY_ENGINE);
                values = new Values(MAPPER.writeValueAsString(message), switchId, flowId, transactionId);
                outputCollector.emit(StreamType.DELETE.toString(), tuple, values);
            } else {
                logger.debug("Skip undefined command message: {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), request);
            }
        } else if (message instanceof ErrorMessage) {
            String flowId = ((ErrorMessage) message).getData().getErrorDescription();
            FlowState status = FlowState.DOWN;
            // TODO: Should add debug message if receiving ErrorMessage.
            if (flowId != null) {
                logger.error("Flow error message: {}={}, {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), Utils.FLOW_ID, flowId, request);
                values = new Values(flowId, status);
                outputCollector.emit(StreamType.STATUS.toString(), tuple, values);
            } else {
                logger.debug("Skip error message without flow-id: {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), request);
            }
        } else {
            // TODO: should this be a warn or error? Probably, after refactored / specific
            // topics
            logger.debug("Skip undefined message: {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), request);
        }
    } catch (IOException exception) {
        logger.error("\n\nCould not deserialize message={}", request, exception);
    } finally {
        logger.debug("Speaker message ack: component={}, stream={}, tuple={}, values={}", tuple.getSourceComponent(), tuple.getSourceStreamId(), tuple, values);
        outputCollector.ack(tuple);
    }
}
Also used : FlowState(org.openkilda.messaging.payload.flow.FlowState) Message(org.openkilda.messaging.Message) CommandMessage(org.openkilda.messaging.command.CommandMessage) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) RemoveFlow(org.openkilda.messaging.command.flow.RemoveFlow) Values(org.apache.storm.tuple.Values) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) IOException(java.io.IOException) CommandData(org.openkilda.messaging.command.CommandData) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) CommandMessage(org.openkilda.messaging.command.CommandMessage)

Example 2 with BaseInstallFlow

use of org.openkilda.messaging.command.flow.BaseInstallFlow in project open-kilda by telstra.

the class TopologyEngineBolt method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(Tuple tuple) {
    String request = tuple.getString(0);
    Values values = null;
    try {
        Message message = MAPPER.readValue(request, Message.class);
        if (!Destination.WFM.equals(message.getDestination())) {
            return;
        }
        logger.debug("Request tuple={}", tuple);
        if (message instanceof CommandMessage) {
            CommandData data = ((CommandMessage) message).getData();
            if (data instanceof BaseInstallFlow) {
                BaseInstallFlow installData = (BaseInstallFlow) data;
                Long transactionId = UUID.randomUUID().getLeastSignificantBits();
                installData.setTransactionId(transactionId);
                String switchId = installData.getSwitchId();
                String flowId = installData.getId();
                logger.debug("Flow install message: {}={}, switch-id={}, {}={}, {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId, request);
                // FIXME(surabujin): send here and in TE
                message.setDestination(Destination.CONTROLLER);
                values = new Values(MAPPER.writeValueAsString(message), switchId, flowId, transactionId);
                outputCollector.emit(StreamType.CREATE.toString(), tuple, values);
            } else if (data instanceof RemoveFlow) {
                RemoveFlow removeData = (RemoveFlow) data;
                Long transactionId = UUID.randomUUID().getLeastSignificantBits();
                removeData.setTransactionId(transactionId);
                String switchId = removeData.getSwitchId();
                String flowId = removeData.getId();
                logger.debug("Flow remove message: {}={}, switch-id={}, {}={}, {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), switchId, Utils.FLOW_ID, flowId, Utils.TRANSACTION_ID, transactionId, request);
                message.setDestination(Destination.CONTROLLER);
                values = new Values(MAPPER.writeValueAsString(message), switchId, flowId, transactionId);
                outputCollector.emit(StreamType.DELETE.toString(), tuple, values);
            } else {
                logger.debug("Skip undefined command message: {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), request);
            }
        } else if (message instanceof InfoMessage) {
            values = new Values(message);
            logger.debug("Flow response message: {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), request);
            outputCollector.emit(StreamType.RESPONSE.toString(), tuple, values);
        } else if (message instanceof ErrorMessage) {
            String flowId = ((ErrorMessage) message).getData().getErrorDescription();
            logger.error("Flow error message: {}={}, {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), Utils.FLOW_ID, flowId, request);
            values = new Values(message, flowId);
            outputCollector.emit(StreamType.STATUS.toString(), tuple, values);
        } else {
            logger.debug("Skip undefined message: {}={}, message={}", Utils.CORRELATION_ID, message.getCorrelationId(), request);
        }
    } catch (IOException exception) {
        logger.error("Could not deserialize message={}", request, exception);
    } finally {
        logger.debug("Topology-Engine message ack: component={}, stream={}, tuple={}, values={}", tuple.getSourceComponent(), tuple.getSourceStreamId(), tuple, values);
        outputCollector.ack(tuple);
    }
}
Also used : InfoMessage(org.openkilda.messaging.info.InfoMessage) Message(org.openkilda.messaging.Message) CommandMessage(org.openkilda.messaging.command.CommandMessage) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) RemoveFlow(org.openkilda.messaging.command.flow.RemoveFlow) InfoMessage(org.openkilda.messaging.info.InfoMessage) Values(org.apache.storm.tuple.Values) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) IOException(java.io.IOException) CommandData(org.openkilda.messaging.command.CommandData) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) CommandMessage(org.openkilda.messaging.command.CommandMessage)

Example 3 with BaseInstallFlow

use of org.openkilda.messaging.command.flow.BaseInstallFlow in project open-kilda by telstra.

the class RecordHandler method doSyncRulesRequest.

/**
 * Installs missed flows on the switch.
 * @param message with list of flows.
 */
private void doSyncRulesRequest(final CommandMessage message) {
    InstallMissedFlowsRequest request = (InstallMissedFlowsRequest) message.getData();
    final String switchId = request.getSwitchId();
    logger.debug("Processing rules to be updated for switch {}", switchId);
    for (BaseInstallFlow command : request.getFlowCommands()) {
        logger.debug("Processing command for switch {} {}", switchId, command);
        try {
            if (command instanceof InstallIngressFlow) {
                installIngressFlow((InstallIngressFlow) command);
            } else if (command instanceof InstallEgressFlow) {
                installEgressFlow((InstallEgressFlow) command);
            } else if (command instanceof InstallTransitFlow) {
                installTransitFlow((InstallTransitFlow) command);
            } else if (command instanceof InstallOneSwitchFlow) {
                installOneSwitchFlow((InstallOneSwitchFlow) command);
            }
        } catch (SwitchOperationException e) {
            logger.error("Error during flow installation", e);
        }
    }
}
Also used : InstallIngressFlow(org.openkilda.messaging.command.flow.InstallIngressFlow) SwitchOperationException(org.openkilda.floodlight.switchmanager.SwitchOperationException) InstallTransitFlow(org.openkilda.messaging.command.flow.InstallTransitFlow) InstallMissedFlowsRequest(org.openkilda.messaging.command.switches.InstallMissedFlowsRequest) InstallOneSwitchFlow(org.openkilda.messaging.command.flow.InstallOneSwitchFlow) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) InstallEgressFlow(org.openkilda.messaging.command.flow.InstallEgressFlow)

Example 4 with BaseInstallFlow

use of org.openkilda.messaging.command.flow.BaseInstallFlow in project open-kilda by telstra.

the class CommandBuilderImpl method buildInstallCommandFromSegment.

private List<BaseInstallFlow> buildInstallCommandFromSegment(FlowPath flowPath, PathSegment segment, boolean switchRulesContainsFlowPathCookie, boolean switchRulesContainsMirrorCookie) {
    if (segment.getSrcSwitchId().equals(segment.getDestSwitchId())) {
        log.warn("One-switch flow segment {} is provided", flowPath.getCookie());
        return new ArrayList<>();
    }
    Optional<Flow> foundFlow = flowRepository.findById(flowPath.getFlow().getFlowId());
    if (!foundFlow.isPresent()) {
        log.warn("Flow with id {} was not found", flowPath.getFlow().getFlowId());
        return new ArrayList<>();
    }
    Flow flow = foundFlow.get();
    EncapsulationResources encapsulationResources = getEncapsulationResources(flowPath, flow);
    if (segment.getDestSwitchId().equals(flowPath.getDestSwitchId())) {
        List<BaseInstallFlow> commands = new ArrayList<>();
        if (switchRulesContainsMirrorCookie) {
            MirrorConfig mirrorConfig = makeMirrorConfig(flowPath, flow.getDestSwitchId(), flow.getDestPort());
            commands.add(flowCommandFactory.buildInstallEgressMirrorFlow(flowPath, segment.getDestPort(), encapsulationResources, segment.isDestWithMultiTable(), mirrorConfig));
        }
        if (switchRulesContainsFlowPathCookie) {
            commands.add(flowCommandFactory.buildInstallEgressFlow(flowPath, segment.getDestPort(), encapsulationResources, segment.isDestWithMultiTable()));
        }
        return commands;
    } else {
        int segmentIdx = flowPath.getSegments().indexOf(segment);
        if (segmentIdx < 0 || segmentIdx + 1 == flowPath.getSegments().size()) {
            log.warn("Paired segment for switch {} and cookie {} has not been found", segment.getDestSwitchId(), flowPath.getCookie());
            return new ArrayList<>();
        }
        PathSegment foundPairedFlowSegment = flowPath.getSegments().get(segmentIdx + 1);
        return Collections.singletonList(flowCommandFactory.buildInstallTransitFlow(flowPath, segment.getDestSwitchId(), segment.getDestPort(), foundPairedFlowSegment.getSrcPort(), encapsulationResources, segment.isDestWithMultiTable()));
    }
}
Also used : EncapsulationResources(org.openkilda.wfm.share.flow.resources.EncapsulationResources) MirrorConfig(org.openkilda.model.MirrorConfig) ArrayList(java.util.ArrayList) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) PathSegment(org.openkilda.model.PathSegment) InstallServer42Flow(org.openkilda.messaging.command.flow.InstallServer42Flow) BaseFlow(org.openkilda.messaging.command.flow.BaseFlow) Flow(org.openkilda.model.Flow) RemoveFlow(org.openkilda.messaging.command.flow.RemoveFlow) BaseInstallFlow(org.openkilda.messaging.command.flow.BaseInstallFlow) InstallSharedFlow(org.openkilda.messaging.command.flow.InstallSharedFlow)

Example 5 with BaseInstallFlow

use of org.openkilda.messaging.command.flow.BaseInstallFlow in project open-kilda by telstra.

the class FlowResource method installFlow.

/**
 * Installing flow.
 *
 * @param json the json from request.
 * @return json response.
 * @throws JsonProcessingException if response can't be wrote to string.
 */
@Post("json")
@Put("json")
public String installFlow(String json) throws JsonProcessingException {
    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(CorrelationContext.getId(), 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(CorrelationContext.getId(), 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(CorrelationContext.getId(), 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)

Aggregations

BaseInstallFlow (org.openkilda.messaging.command.flow.BaseInstallFlow)6 IOException (java.io.IOException)3 Message (org.openkilda.messaging.Message)3 CommandData (org.openkilda.messaging.command.CommandData)3 CommandMessage (org.openkilda.messaging.command.CommandMessage)3 RemoveFlow (org.openkilda.messaging.command.flow.RemoveFlow)3 ArrayList (java.util.ArrayList)2 Values (org.apache.storm.tuple.Values)2 ErrorMessage (org.openkilda.messaging.error.ErrorMessage)2 ISwitchManager (org.openkilda.floodlight.switchmanager.ISwitchManager)1 SwitchOperationException (org.openkilda.floodlight.switchmanager.SwitchOperationException)1 BaseFlow (org.openkilda.messaging.command.flow.BaseFlow)1 InstallEgressFlow (org.openkilda.messaging.command.flow.InstallEgressFlow)1 InstallIngressFlow (org.openkilda.messaging.command.flow.InstallIngressFlow)1 InstallOneSwitchFlow (org.openkilda.messaging.command.flow.InstallOneSwitchFlow)1 InstallServer42Flow (org.openkilda.messaging.command.flow.InstallServer42Flow)1 InstallServer42FlowBuilder (org.openkilda.messaging.command.flow.InstallServer42Flow.InstallServer42FlowBuilder)1 InstallSharedFlow (org.openkilda.messaging.command.flow.InstallSharedFlow)1 InstallTransitFlow (org.openkilda.messaging.command.flow.InstallTransitFlow)1 InstallMissedFlowsRequest (org.openkilda.messaging.command.switches.InstallMissedFlowsRequest)1