Search in sources :

Example 6 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)

Example 7 with ISwitchManager

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

the class FlowsResource method getFlows.

@Get("json")
@SuppressWarnings("unchecked")
public Map<String, Object> getFlows() {
    Map<String, Object> response = new HashMap<>();
    String switchId = (String) this.getRequestAttributes().get("switch_id");
    LOGGER.debug("Get flows for switch: {}", switchId);
    ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes().get(ISwitchManager.class.getCanonicalName());
    try {
        OFFlowStatsReply replay = switchManager.dumpFlowTable(DatapathId.of(switchId));
        LOGGER.debug("OF_STATS: {}", replay);
        if (replay != null) {
            for (OFFlowStatsEntry entry : replay.getEntries()) {
                String key = String.format("flow-0x%s", Long.toHexString(entry.getCookie().getValue()).toUpperCase());
                response.put(key, buildFlowStat(entry));
            }
        }
    } catch (IllegalArgumentException exception) {
        String messageString = "No such switch";
        LOGGER.error("{}: {}", messageString, switchId, exception);
        MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, System.currentTimeMillis(), ErrorType.PARAMETERS_INVALID.toString(), messageString, exception.getMessage());
        response.putAll(MAPPER.convertValue(responseMessage, Map.class));
    }
    return response;
}
Also used : OFFlowStatsEntry(org.projectfloodlight.openflow.protocol.OFFlowStatsEntry) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) HashMap(java.util.HashMap) MessageError(org.openkilda.messaging.error.MessageError) OFFlowStatsReply(org.projectfloodlight.openflow.protocol.OFFlowStatsReply) Get(org.restlet.resource.Get)

Example 8 with ISwitchManager

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

the class MetersResource method getMeters.

// FIXME(surabujin): is it used anywhere?
@Get("json")
@SuppressWarnings("unchecked")
public Map<Long, Object> getMeters() {
    Map<Long, Object> response = new HashMap<>();
    String switchId = (String) this.getRequestAttributes().get("switch_id");
    logger.debug("Get meters for switch: {}", switchId);
    ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes().get(ISwitchManager.class.getCanonicalName());
    try {
        OFMeterConfigStatsReply replay = switchManager.dumpMeters(DatapathId.of(switchId));
        logger.debug("Meters from switch {} received: {}", switchId, replay);
        if (replay != null) {
            for (OFMeterConfig entry : replay.getEntries()) {
                response.put(entry.getMeterId(), entry);
            }
        }
    } catch (IllegalArgumentException | SwitchOperationException exception) {
        String messageString = "No such switch";
        logger.error("{}: {}", messageString, switchId, exception);
        MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, System.currentTimeMillis(), ErrorType.PARAMETERS_INVALID.toString(), messageString, exception.getMessage());
        response.putAll(MAPPER.convertValue(responseMessage, Map.class));
    }
    return response;
}
Also used : SwitchOperationException(org.openkilda.floodlight.switchmanager.SwitchOperationException) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) OFMeterConfigStatsReply(org.projectfloodlight.openflow.protocol.OFMeterConfigStatsReply) HashMap(java.util.HashMap) MessageError(org.openkilda.messaging.error.MessageError) OFMeterConfig(org.projectfloodlight.openflow.protocol.OFMeterConfig) Get(org.restlet.resource.Get)

Aggregations

ISwitchManager (org.openkilda.floodlight.switchmanager.ISwitchManager)8 SwitchOperationException (org.openkilda.floodlight.switchmanager.SwitchOperationException)4 MessageError (org.openkilda.messaging.error.MessageError)3 InfoMessage (org.openkilda.messaging.info.InfoMessage)3 DatapathId (org.projectfloodlight.openflow.types.DatapathId)3 HashMap (java.util.HashMap)2 ErrorData (org.openkilda.messaging.error.ErrorData)2 ErrorMessage (org.openkilda.messaging.error.ErrorMessage)2 SwitchRulesResponse (org.openkilda.messaging.info.switches.SwitchRulesResponse)2 Get (org.restlet.resource.Get)2 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 FloodlightModuleException (net.floodlightcontroller.core.module.FloodlightModuleException)1 Message (org.openkilda.messaging.Message)1 CommandData (org.openkilda.messaging.command.CommandData)1 CommandMessage (org.openkilda.messaging.command.CommandMessage)1 BaseInstallFlow (org.openkilda.messaging.command.flow.BaseInstallFlow)1 RemoveFlow (org.openkilda.messaging.command.flow.RemoveFlow)1 ConnectModeRequest (org.openkilda.messaging.command.switches.ConnectModeRequest)1 DeleteRulesAction (org.openkilda.messaging.command.switches.DeleteRulesAction)1