Java Code Examples for org.onosproject.net.flow.FlowRuleService#applyFlowRules()

The following examples show how to use org.onosproject.net.flow.FlowRuleService#applyFlowRules() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OplinkPowerConfigUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Sets specified channle attenuation.
 *
 * @param portNum the port number
 * @param och channel signal
 * @param power attenuation in 0.01 dB
 */
private void setChannelAttenuation(PortNumber portNum, OchSignal och, long power) {
    FlowEntry flowEntry = findFlow(portNum, och);
    if (flowEntry == null) {
        log.warn("Target channel power not set");
        return;
    }
    final DriverHandler handler = behaviour.handler();
    for (Instruction ins : flowEntry.treatment().allInstructions()) {
        if (ins.type() != Instruction.Type.EXTENSION) {
            continue;
        }
        ExtensionTreatment ext = ((Instructions.ExtensionInstructionWrapper) ins).extensionInstruction();
        if (ext.type() == ExtensionTreatmentType.ExtensionTreatmentTypes.OPLINK_ATTENUATION.type()) {
            ((OplinkAttenuation) ext).setAttenuation((int) power);
            FlowRuleService service = handler.get(FlowRuleService.class);
            service.applyFlowRules(flowEntry);
            return;
        }
    }
    addAttenuation(flowEntry, power);
}
 
Example 2
Source File: OplinkPowerConfigUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Replace flow with new flow containing Oplink attenuation extension instruction. Also resets metrics.
 *
 * @param flowEntry flow entry
 * @param power power value
 */
private void addAttenuation(FlowEntry flowEntry, long power) {
    FlowRule.Builder flowBuilder = new DefaultFlowRule.Builder()
            .withCookie(flowEntry.id().value())
            .withPriority(flowEntry.priority())
            .forDevice(flowEntry.deviceId())
            .forTable(flowEntry.tableId());
    if (flowEntry.isPermanent()) {
        flowBuilder.makePermanent();
    } else {
        flowBuilder.makeTemporary(flowEntry.timeout());
    }
    flowBuilder.withSelector(flowEntry.selector());
    // Copy original instructions and add attenuation instruction
    TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
    flowEntry.treatment().allInstructions().forEach(ins -> treatmentBuilder.add(ins));
    final DriverHandler handler = behaviour.handler();
    treatmentBuilder.add(Instructions.extension(new OplinkAttenuation((int) power), handler.data().deviceId()));
    flowBuilder.withTreatment(treatmentBuilder.build());

    FlowRuleService service = handler.get(FlowRuleService.class);
    service.applyFlowRules(flowBuilder.build());
}
 
Example 3
Source File: FlowsWebResource.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new flow rules. Creates and installs a new flow rules.<br>
 * Flow rule criteria and instruction description:
 * https://wiki.onosproject.org/display/ONOS/Flow+Rules
 *
 * @param appId application id
 * @param stream flow rules JSON
 * @return status of the request - CREATED if the JSON is correct,
 * BAD_REQUEST if the JSON is invalid
 * @onos.rsModel FlowsBatchPost
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createFlows(@QueryParam("appId") String appId, InputStream stream) {
    FlowRuleService service = get(FlowRuleService.class);
    ObjectNode root = mapper().createObjectNode();
    ArrayNode flowsNode = root.putArray(FLOWS);
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
        ArrayNode flowsArray = nullIsIllegal((ArrayNode) jsonTree.get(FLOWS),
                                             FLOW_ARRAY_REQUIRED);

        if (appId != null) {
            flowsArray.forEach(flowJson -> ((ObjectNode) flowJson).put("appId", appId));
        }

        List<FlowRule> rules = codec(FlowRule.class).decode(flowsArray, this);

        service.applyFlowRules(rules.toArray(new FlowRule[rules.size()]));
        rules.forEach(flowRule -> {
            ObjectNode flowNode = mapper().createObjectNode();
            flowNode.put(DEVICE_ID, flowRule.deviceId().toString())
                    .put(FLOW_ID, Long.toString(flowRule.id().value()));
            flowsNode.add(flowNode);
        });
    } catch (IOException ex) {
        throw new IllegalArgumentException(ex);
    }
    return Response.ok(root).build();
}
 
Example 4
Source File: FlowsWebResource.java    From onos with Apache License 2.0 5 votes vote down vote up
/**
 * Creates new flow rule. Creates and installs a new flow rule for the
 * specified device. <br>
 * Flow rule criteria and instruction description:
 * https://wiki.onosproject.org/display/ONOS/Flow+Rules
 *
 * @param deviceId device identifier
 * @param appId    application identifier
 * @param stream   flow rule JSON
 * @return status of the request - CREATED if the JSON is correct,
 * BAD_REQUEST if the JSON is invalid
 * @onos.rsModel FlowsPost
 */
@POST
@Path("{deviceId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createFlow(@PathParam("deviceId") String deviceId,
                           @QueryParam("appId") String appId,
                           InputStream stream) {
    FlowRuleService service = get(FlowRuleService.class);
    try {
        ObjectNode jsonTree = readTreeFromStream(mapper(), stream);
        JsonNode specifiedDeviceId = jsonTree.get("deviceId");
        if (specifiedDeviceId != null &&
                !specifiedDeviceId.asText().equals(deviceId)) {
            throw new IllegalArgumentException(
                    "Invalid deviceId in flow creation request");
        }
        jsonTree.put("deviceId", deviceId);

        if (appId != null) {
            jsonTree.put("appId", appId);
        }

        FlowRule rule = codec(FlowRule.class).decode(jsonTree, this);
        service.applyFlowRules(rule);
        UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
                .path("flows")
                .path(deviceId)
                .path(Long.toString(rule.id().value()));

        return Response
                .created(locationBuilder.build())
                .build();
    } catch (IOException ex) {
        throw new IllegalArgumentException(ex);
    }
}
 
Example 5
Source File: OplinkOpticalPowerConfig.java    From onos with Apache License 2.0 5 votes vote down vote up
private boolean setChannelTargetPower(PortNumber port, OchSignal channel, long power) {
    log.debug("Set port{} channel{} attenuation.", port, channel.channelSpacing());
    FlowRuleService service = handler().get(FlowRuleService.class);
    Iterable<FlowEntry> entries = service.getFlowEntries(data().deviceId());
    for (FlowEntry entry : entries) {
        OplinkCrossConnect crossConnect = OplinkOpticalUtility.fromFlowRule(this, entry);
        // The channel port might be input port or output port.
        if ((port.equals(crossConnect.getInPort()) || port.equals(crossConnect.getOutPort())) &&
                channel.spacingMultiplier() == crossConnect.getChannel()) {
            log.debug("Flow is found, modify the flow with attenuation.");
            // Modify attenuation in treatment
            TrafficTreatment treatment = DefaultTrafficTreatment.builder()
                    .setOutput(crossConnect.getOutPort())
                    .extension(new OplinkAttenuation((int) power), data().deviceId())
                    .build();
            // Apply the new flow rule
            service.applyFlowRules(DefaultFlowRule.builder()
                    .forDevice(data().deviceId())
                    .makePermanent()
                    .withSelector(entry.selector())
                    .withTreatment(treatment)
                    .withPriority(entry.priority())
                    .withCookie(entry.id().value())
                    .build());
            return true;
        }
    }
    return false;
}
 
Example 6
Source File: PortWaveLengthCommand.java    From onos with Apache License 2.0 4 votes vote down vote up
@Override
protected void doExecute() throws Exception {
    if (operation.equals("edit-config")) {
        FlowRuleService flowService = get(FlowRuleService.class);
        DeviceService deviceService = get(DeviceService.class);
        CoreService coreService = get(CoreService.class);
        ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPointString);

        TrafficSelector.Builder trafficSelectorBuilder = DefaultTrafficSelector.builder();
        TrafficTreatment.Builder trafficTreatmentBuilder = DefaultTrafficTreatment.builder();
        FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder();


        // an empty traffic selector
        TrafficSelector trafficSelector = trafficSelectorBuilder.matchInPort(cp.port()).build();
        OchSignal ochSignal;
        if (parameter.contains("/")) {
            ochSignal = createOchSignal();
        } else if (parameter.matches("-?\\d+(\\.\\d+)?")) {
            ochSignal = createOchSignalFromWavelength(deviceService, cp);
        } else {
            print("Signal or wavelength %s are in uncorrect format");
            return;
        }
        if (ochSignal == null) {
            print("Error in creating OchSignal");
            return;
        }
        TrafficTreatment trafficTreatment = trafficTreatmentBuilder
                .add(Instructions.modL0Lambda(ochSignal))
                .add(Instructions.createOutput(deviceService.getPort(cp).number()))
                .build();

        Device device = deviceService.getDevice(cp.deviceId());
        int priority = 100;
        ApplicationId appId = coreService.registerApplication("org.onosproject.optical-model");
        log.info(appId.name());
        FlowRule addFlow = flowRuleBuilder
                .withPriority(priority)
                .fromApp(appId)
                .withTreatment(trafficTreatment)
                .withSelector(trafficSelector)
                .forDevice(device.id())
                .makePermanent()
                .build();
        flowService.applyFlowRules(addFlow);
        String msg = String.format("Setting wavelength %s", ochSignal.centralFrequency().asGHz());
        print(msg);
    } else {
        print("Operation %s are not supported now.", operation);
    }

}