Search in sources :

Example 11 with ISwitchManager

use of org.openkilda.floodlight.switchmanager.ISwitchManager 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)

Example 12 with ISwitchManager

use of org.openkilda.floodlight.switchmanager.ISwitchManager in project open-kilda by telstra.

the class RecordHandler method doDeleteFlow.

/**
 * Removes flow.
 *
 * @param message command message for flow installation
 */
private void doDeleteFlow(final CommandMessage message, String replyToTopic, Destination replyDestination) throws FlowCommandException {
    RemoveFlow command = (RemoveFlow) message.getData();
    logger.debug("deleting a flow: {}", command);
    DatapathId dpid = DatapathId.of(command.getSwitchId());
    ISwitchManager switchManager = context.getSwitchManager();
    try {
        switchManager.deleteFlow(dpid, command.getId(), command.getCookie());
        Integer meterId = meterPool.deallocate(command.getSwitchId(), command.getId());
        if (meterId != null) {
            switchManager.deleteMeter(dpid, meterId);
        }
        message.setDestination(replyDestination);
        context.getKafkaProducer().postMessage(replyToTopic, message);
    } catch (SwitchOperationException e) {
        throw new FlowCommandException(command.getId(), ErrorType.DELETION_FAILURE, e);
    }
}
Also used : SwitchOperationException(org.openkilda.floodlight.switchmanager.SwitchOperationException) RemoveFlow(org.openkilda.messaging.command.flow.RemoveFlow) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) DatapathId(org.projectfloodlight.openflow.types.DatapathId)

Example 13 with ISwitchManager

use of org.openkilda.floodlight.switchmanager.ISwitchManager in project open-kilda by telstra.

the class RecordHandler method doConnectMode.

private void doConnectMode(final CommandMessage message, String replyToTopic, Destination replyDestination) {
    ConnectModeRequest request = (ConnectModeRequest) message.getData();
    if (request.getMode() != null)
        logger.debug("Setting CONNECT MODE to '{}'", request.getMode());
    else
        logger.debug("Getting CONNECT MODE");
    ISwitchManager switchManager = context.getSwitchManager();
    ConnectModeRequest.Mode result = switchManager.connectMode(request.getMode());
    logger.debug("CONNECT MODE is now '{}'", result);
    ConnectModeResponse response = new ConnectModeResponse(result);
    InfoMessage infoMessage = new InfoMessage(response, System.currentTimeMillis(), message.getCorrelationId(), replyDestination);
    context.getKafkaProducer().postMessage(replyToTopic, infoMessage);
}
Also used : ConnectModeRequest(org.openkilda.messaging.command.switches.ConnectModeRequest) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) InfoMessage(org.openkilda.messaging.info.InfoMessage) ConnectModeResponse(org.openkilda.messaging.info.switches.ConnectModeResponse)

Example 14 with ISwitchManager

use of org.openkilda.floodlight.switchmanager.ISwitchManager in project open-kilda by telstra.

the class RecordHandler method doInstallSwitchRules.

private void doInstallSwitchRules(final CommandMessage message, String replyToTopic, Destination replyDestination) {
    SwitchRulesInstallRequest request = (SwitchRulesInstallRequest) message.getData();
    logger.debug("Installing rules on '{}' switch: action={}", request.getSwitchId(), request.getInstallRulesAction());
    DatapathId dpid = DatapathId.of(request.getSwitchId());
    ISwitchManager switchManager = context.getSwitchManager();
    InstallRulesAction installAction = request.getInstallRulesAction();
    List<Long> installedRules = new ArrayList<>();
    try {
        if (installAction == InstallRulesAction.INSTALL_DROP) {
            switchManager.installDropFlow(dpid);
            installedRules.add(ISwitchManager.DROP_RULE_COOKIE);
        } else if (installAction == InstallRulesAction.INSTALL_BROADCAST) {
            switchManager.installVerificationRule(dpid, true);
            installedRules.add(ISwitchManager.VERIFICATION_BROADCAST_RULE_COOKIE);
        } else if (installAction == InstallRulesAction.INSTALL_UNICAST) {
            // TODO: this isn't always added (ie if OF1.2). Is there a better response?
            switchManager.installVerificationRule(dpid, false);
            installedRules.add(ISwitchManager.VERIFICATION_UNICAST_RULE_COOKIE);
        } else {
            switchManager.installDefaultRules(dpid);
            installedRules.addAll(asList(ISwitchManager.DROP_RULE_COOKIE, ISwitchManager.VERIFICATION_BROADCAST_RULE_COOKIE, ISwitchManager.VERIFICATION_UNICAST_RULE_COOKIE));
        }
        SwitchRulesResponse response = new SwitchRulesResponse(installedRules);
        InfoMessage infoMessage = new InfoMessage(response, System.currentTimeMillis(), message.getCorrelationId(), replyDestination);
        context.getKafkaProducer().postMessage(replyToTopic, infoMessage);
    } catch (SwitchOperationException e) {
        ErrorData errorData = new ErrorData(ErrorType.CREATION_FAILURE, e.getMessage(), request.getSwitchId());
        ErrorMessage error = new ErrorMessage(errorData, System.currentTimeMillis(), message.getCorrelationId(), replyDestination);
        context.getKafkaProducer().postMessage(replyToTopic, error);
    }
}
Also used : SwitchOperationException(org.openkilda.floodlight.switchmanager.SwitchOperationException) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) SwitchRulesInstallRequest(org.openkilda.messaging.command.switches.SwitchRulesInstallRequest) InstallRulesAction(org.openkilda.messaging.command.switches.InstallRulesAction) InfoMessage(org.openkilda.messaging.info.InfoMessage) SwitchRulesResponse(org.openkilda.messaging.info.switches.SwitchRulesResponse) DatapathId(org.projectfloodlight.openflow.types.DatapathId) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) ErrorData(org.openkilda.messaging.error.ErrorData)

Example 15 with ISwitchManager

use of org.openkilda.floodlight.switchmanager.ISwitchManager in project open-kilda by telstra.

the class RecordHandler method doDeleteSwitchRules.

private void doDeleteSwitchRules(final CommandMessage message, String replyToTopic, Destination replyDestination) {
    SwitchRulesDeleteRequest request = (SwitchRulesDeleteRequest) message.getData();
    logger.debug("Deleting rules from '{}' switch: action={}", request.getSwitchId(), request.getDeleteRulesAction());
    DatapathId dpid = DatapathId.of(request.getSwitchId());
    ISwitchManager switchManager = context.getSwitchManager();
    DeleteRulesAction deleteAction = request.getDeleteRulesAction();
    List<Long> removedRules = new ArrayList<>();
    try {
        /*
             * This first part .. we are either deleting one rule, or all non-default rules (the else)
             */
        List<Long> toRemove = new ArrayList<>();
        if (deleteAction == DeleteRulesAction.ONE) {
            toRemove.add(request.getOneCookie());
        } else if (deleteAction == DeleteRulesAction.REMOVE_DROP) {
            toRemove.add(ISwitchManager.DROP_RULE_COOKIE);
        } else if (deleteAction == DeleteRulesAction.REMOVE_BROADCAST) {
            toRemove.add(ISwitchManager.VERIFICATION_BROADCAST_RULE_COOKIE);
        } else if (deleteAction == DeleteRulesAction.REMOVE_UNICAST) {
            toRemove.add(ISwitchManager.VERIFICATION_UNICAST_RULE_COOKIE);
        } else if (deleteAction == DeleteRulesAction.REMOVE_DEFAULTS || deleteAction == DeleteRulesAction.REMOVE_ADD) {
            toRemove.add(ISwitchManager.DROP_RULE_COOKIE);
            toRemove.add(ISwitchManager.VERIFICATION_BROADCAST_RULE_COOKIE);
            toRemove.add(ISwitchManager.VERIFICATION_UNICAST_RULE_COOKIE);
        }
        // toRemove is > 0 only if we are trying to delete base rule(s).
        if (toRemove.size() > 0) {
            removedRules.addAll(switchManager.deleteRuleWithCookie(dpid, toRemove));
            if (deleteAction == DeleteRulesAction.REMOVE_ADD) {
                switchManager.installDefaultRules(dpid);
            }
        } else {
            removedRules.addAll(switchManager.deleteAllNonDefaultRules(dpid));
            /*
                 * The Second part - only for a subset of actions.
                 */
            if (deleteAction == DeleteRulesAction.DROP) {
                List<Long> removedDefaultRules = switchManager.deleteDefaultRules(dpid);
                // Return removedDefaultRules as a part of the result list.
                removedRules.addAll(removedDefaultRules);
            } else if (deleteAction == DeleteRulesAction.DROP_ADD) {
                switchManager.deleteDefaultRules(dpid);
                switchManager.installDefaultRules(dpid);
            } else if (deleteAction == DeleteRulesAction.OVERWRITE) {
                switchManager.installDefaultRules(dpid);
            }
        }
        SwitchRulesResponse response = new SwitchRulesResponse(removedRules);
        InfoMessage infoMessage = new InfoMessage(response, System.currentTimeMillis(), message.getCorrelationId(), replyDestination);
        context.getKafkaProducer().postMessage(replyToTopic, infoMessage);
    } catch (SwitchOperationException e) {
        ErrorData errorData = new ErrorData(ErrorType.DELETION_FAILURE, e.getMessage(), request.getSwitchId());
        ErrorMessage error = new ErrorMessage(errorData, System.currentTimeMillis(), message.getCorrelationId(), replyDestination);
        context.getKafkaProducer().postMessage(replyToTopic, error);
    }
}
Also used : SwitchOperationException(org.openkilda.floodlight.switchmanager.SwitchOperationException) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) InfoMessage(org.openkilda.messaging.info.InfoMessage) SwitchRulesResponse(org.openkilda.messaging.info.switches.SwitchRulesResponse) DeleteRulesAction(org.openkilda.messaging.command.switches.DeleteRulesAction) SwitchRulesDeleteRequest(org.openkilda.messaging.command.switches.SwitchRulesDeleteRequest) DatapathId(org.projectfloodlight.openflow.types.DatapathId) ErrorMessage(org.openkilda.messaging.error.ErrorMessage) ErrorData(org.openkilda.messaging.error.ErrorData)

Aggregations

ISwitchManager (org.openkilda.floodlight.switchmanager.ISwitchManager)23 DatapathId (org.projectfloodlight.openflow.types.DatapathId)15 InfoMessage (org.openkilda.messaging.info.InfoMessage)11 SwitchOperationException (org.openkilda.floodlight.error.SwitchOperationException)9 UnsupportedSwitchOperationException (org.openkilda.floodlight.error.UnsupportedSwitchOperationException)9 SwitchNotFoundException (org.openkilda.floodlight.error.SwitchNotFoundException)6 Cookie (org.openkilda.model.cookie.Cookie)5 FlowSharedSegmentCookie (org.openkilda.model.cookie.FlowSharedSegmentCookie)5 PortColourCookie (org.openkilda.model.cookie.PortColourCookie)5 IOException (java.io.IOException)4 IKafkaProducerService (org.openkilda.floodlight.service.kafka.IKafkaProducerService)4 ErrorData (org.openkilda.messaging.error.ErrorData)4 MessageError (org.openkilda.messaging.error.MessageError)4 SwitchRulesResponse (org.openkilda.messaging.info.switches.SwitchRulesResponse)4 SwitchId (org.openkilda.model.SwitchId)4 IOFSwitch (net.floodlightcontroller.core.IOFSwitch)3 SwitchOperationException (org.openkilda.floodlight.switchmanager.SwitchOperationException)3 Message (org.openkilda.messaging.Message)3 ConnectModeRequest (org.openkilda.messaging.command.switches.ConnectModeRequest)3 DeleteRulesCriteria (org.openkilda.messaging.command.switches.DeleteRulesCriteria)3