Search in sources :

Example 41 with DeviceService

use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.

the class AddTestFlowsCommand method doExecute.

@Override
protected void doExecute() {
    FlowRuleService flowService = get(FlowRuleService.class);
    DeviceService deviceService = get(DeviceService.class);
    CoreService coreService = get(CoreService.class);
    ApplicationId appId = coreService.registerApplication("onos.test.flow.installer");
    int flowsPerDevice = Integer.parseInt(flows);
    int num = Integer.parseInt(numOfRuns);
    ArrayList<Long> results = Lists.newArrayList();
    Iterable<Device> devices = deviceService.getDevices();
    TrafficTreatment treatment = DefaultTrafficTreatment.builder().setOutput(PortNumber.portNumber(RandomUtils.nextInt(MAX_OUT_PORT))).build();
    TrafficSelector.Builder sbuilder;
    FlowRuleOperations.Builder rules = FlowRuleOperations.builder();
    FlowRuleOperations.Builder remove = FlowRuleOperations.builder();
    for (Device d : devices) {
        for (long i = 0; i < flowsPerDevice; i++) {
            sbuilder = DefaultTrafficSelector.builder();
            sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i)).matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt()));
            int randomPriority = RandomUtils.nextInt(FlowRule.MAX_PRIORITY - FlowRule.MIN_PRIORITY + 1) + FlowRule.MIN_PRIORITY;
            FlowRule addRule = DefaultFlowRule.builder().forDevice(d.id()).withSelector(sbuilder.build()).withTreatment(treatment).withPriority(randomPriority).fromApp(appId).makeTemporary(10).build();
            FlowRule removeRule = DefaultFlowRule.builder().forDevice(d.id()).withSelector(sbuilder.build()).withTreatment(treatment).withPriority(randomPriority).fromApp(appId).makeTemporary(10).build();
            rules.add(addRule);
            remove.remove(removeRule);
        }
    }
    // close stages
    rules.newStage();
    remove.newStage();
    for (int i = 0; i < num; i++) {
        printProgress("Run %d:", i);
        latch = new CountDownLatch(2);
        final CountDownLatch addSuccess = new CountDownLatch(1);
        printProgress("..batch add request");
        Stopwatch add = Stopwatch.createStarted();
        flowService.apply(rules.build(new FlowRuleOperationsContext() {

            private final Stopwatch timer = Stopwatch.createStarted();

            @Override
            public void onSuccess(FlowRuleOperations ops) {
                timer.stop();
                printProgress("..add success");
                results.add(timer.elapsed(TimeUnit.MILLISECONDS));
                if (results.size() == num) {
                    if (outputJson()) {
                        print("%s", json(new ObjectMapper(), true, results));
                    } else {
                        printTime(true, results);
                    }
                }
                latch.countDown();
                addSuccess.countDown();
            }
        }));
        try {
            addSuccess.await();
            // wait until all flows reaches ADDED state
            while (!Streams.stream(flowService.getFlowEntriesById(appId)).allMatch(fr -> fr.state() == FlowEntryState.ADDED)) {
                Thread.sleep(100);
            }
            add.stop();
            printProgress("..completed %d ± 100 ms", add.elapsed(TimeUnit.MILLISECONDS));
        } catch (InterruptedException e1) {
            printProgress("Interrupted");
            Thread.currentThread().interrupt();
        }
        printProgress("..cleaning up");
        flowService.apply(remove.build(new FlowRuleOperationsContext() {

            @Override
            public void onSuccess(FlowRuleOperations ops) {
                latch.countDown();
            }
        }));
        try {
            latch.await();
            while (!Iterables.isEmpty(flowService.getFlowEntriesById(appId))) {
                Thread.sleep(500);
            }
        } catch (InterruptedException e) {
            printProgress("Interrupted.");
            Thread.currentThread().interrupt();
        }
    }
    if (outputJson()) {
        print("%s", json(new ObjectMapper(), true, results));
    } else {
        printTime(true, results);
    }
}
Also used : FlowRuleOperationsContext(org.onosproject.net.flow.FlowRuleOperationsContext) Iterables(com.google.common.collect.Iterables) FlowEntryState(org.onosproject.net.flow.FlowEntry.FlowEntryState) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) Stopwatch(com.google.common.base.Stopwatch) CoreService(org.onosproject.core.CoreService) PortNumber(org.onosproject.net.PortNumber) DeviceService(org.onosproject.net.device.DeviceService) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Command(org.apache.karaf.shell.api.action.Command) RandomUtils(org.apache.commons.lang.math.RandomUtils) FlowRuleService(org.onosproject.net.flow.FlowRuleService) TrafficSelector(org.onosproject.net.flow.TrafficSelector) Lists(com.google.common.collect.Lists) ApplicationId(org.onosproject.core.ApplicationId) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) FlowRuleOperations(org.onosproject.net.flow.FlowRuleOperations) Device(org.onosproject.net.Device) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Argument(org.apache.karaf.shell.api.action.Argument) Streams(com.google.common.collect.Streams) TimeUnit(java.util.concurrent.TimeUnit) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractShellCommand(org.onosproject.cli.AbstractShellCommand) FlowRule(org.onosproject.net.flow.FlowRule) Service(org.apache.karaf.shell.api.action.lifecycle.Service) MacAddress(org.onlab.packet.MacAddress) FlowRuleOperations(org.onosproject.net.flow.FlowRuleOperations) Device(org.onosproject.net.Device) DeviceService(org.onosproject.net.device.DeviceService) Stopwatch(com.google.common.base.Stopwatch) CoreService(org.onosproject.core.CoreService) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) CountDownLatch(java.util.concurrent.CountDownLatch) TrafficSelector(org.onosproject.net.flow.TrafficSelector) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) DefaultFlowRule(org.onosproject.net.flow.DefaultFlowRule) FlowRule(org.onosproject.net.flow.FlowRule) FlowRuleService(org.onosproject.net.flow.FlowRuleService) ApplicationId(org.onosproject.core.ApplicationId) FlowRuleOperationsContext(org.onosproject.net.flow.FlowRuleOperationsContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 42 with DeviceService

use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.

the class AllocationsCommand method doExecute.

@Override
protected void doExecute() {
    deviceService = get(DeviceService.class);
    resourceService = get(ResourceService.class);
    if (typeStrings != null) {
        typesToPrint = new HashSet<>(Arrays.asList(typeStrings));
    } else {
        typesToPrint = Collections.emptySet();
    }
    if (intentStrings != null) {
        intentsToPrint = new HashSet<>(Arrays.asList(intentStrings));
    } else {
        intentsToPrint = Collections.emptySet();
    }
    if (deviceIdStr != null && portNumberStr != null) {
        DeviceId deviceId = deviceId(deviceIdStr);
        PortNumber portNumber = PortNumber.fromString(portNumberStr);
        printAllocation(deviceId, portNumber, 0);
    } else if (deviceIdStr != null) {
        DeviceId deviceId = deviceId(deviceIdStr);
        printAllocation(deviceId, 0);
    } else {
        printAllocation();
    }
}
Also used : DeviceId(org.onosproject.net.DeviceId) DeviceService(org.onosproject.net.device.DeviceService) ResourceService(org.onosproject.net.resource.ResourceService) PortNumber(org.onosproject.net.PortNumber)

Example 43 with DeviceService

use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.

the class DeviceIdCompleter method complete.

@Override
public int complete(Session session, CommandLine commandLine, List<String> candidates) {
    // Delegate string completer
    StringsCompleter delegate = new StringsCompleter();
    // Fetch our service and feed it's offerings to the string completer
    DeviceService service = AbstractShellCommand.get(DeviceService.class);
    Iterator<Device> it = service.getDevices().iterator();
    SortedSet<String> strings = delegate.getStrings();
    while (it.hasNext()) {
        strings.add(it.next().id().toString());
    }
    // Now let the completer do the work for figuring out what to offer.
    return delegate.complete(session, commandLine, candidates);
}
Also used : StringsCompleter(org.apache.karaf.shell.support.completers.StringsCompleter) Device(org.onosproject.net.Device) DeviceService(org.onosproject.net.device.DeviceService)

Example 44 with DeviceService

use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.

the class DevicePortStateCommand method doExecute.

@Override
protected void doExecute() {
    DeviceService deviceService = get(DeviceService.class);
    DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
    Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
    if (dev == null) {
        print(" %s", "Device does not exist");
        return;
    }
    PortNumber pnum = PortNumber.fromString(portNumber);
    Port p = deviceService.getPort(dev.id(), pnum);
    if (p == null) {
        print(" %s", "Port does not exist");
        return;
    }
    if ("enable".equals(portState)) {
        deviceAdminService.changePortState(dev.id(), p.number(), true);
    } else if ("disable".equals(portState)) {
        deviceAdminService.changePortState(dev.id(), p.number(), false);
    } else {
        print(" %s", "State must be enable or disable");
    }
}
Also used : Device(org.onosproject.net.Device) DeviceAdminService(org.onosproject.net.device.DeviceAdminService) Port(org.onosproject.net.Port) DeviceService(org.onosproject.net.device.DeviceService) PortNumber(org.onosproject.net.PortNumber)

Example 45 with DeviceService

use of org.onosproject.net.device.DeviceService in project onos by opennetworkinglab.

the class AnnotatePortCommand method doExecute.

@Override
protected void doExecute() {
    DeviceService deviceService = get(DeviceService.class);
    NetworkConfigService netcfgService = get(NetworkConfigService.class);
    ConnectPoint connectPoint = ConnectPoint.deviceConnectPoint(port);
    if (deviceService.getPort(connectPoint) == null) {
        print("Port %s does not exist.", port);
        return;
    }
    if (removeCfg && key == null) {
        // remove whole port annotation config
        netcfgService.removeConfig(connectPoint, PortAnnotationConfig.class);
        print("Annotation Config about %s removed", connectPoint);
        return;
    }
    if (key == null) {
        print("[ERROR] Annotation key not specified.");
        return;
    }
    PortAnnotationConfig cfg = netcfgService.addConfig(connectPoint, PortAnnotationConfig.class);
    if (removeCfg) {
        // remove config about entry
        cfg.annotation(key);
    } else {
        // add remove request config
        cfg.annotation(key, value);
    }
    cfg.apply();
}
Also used : PortAnnotationConfig(org.onosproject.net.config.basics.PortAnnotationConfig) NetworkConfigService(org.onosproject.net.config.NetworkConfigService) DeviceService(org.onosproject.net.device.DeviceService) ConnectPoint(org.onosproject.net.ConnectPoint)

Aggregations

DeviceService (org.onosproject.net.device.DeviceService)187 Device (org.onosproject.net.Device)75 DeviceId (org.onosproject.net.DeviceId)73 Port (org.onosproject.net.Port)59 ConnectPoint (org.onosproject.net.ConnectPoint)42 PortNumber (org.onosproject.net.PortNumber)40 List (java.util.List)30 Collectors (java.util.stream.Collectors)24 Set (java.util.Set)23 AbstractHandlerBehaviour (org.onosproject.net.driver.AbstractHandlerBehaviour)19 Logger (org.slf4j.Logger)19 ArrayList (java.util.ArrayList)18 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)17 Optional (java.util.Optional)17 Test (org.junit.Test)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 Collections (java.util.Collections)15 VirtualNetwork (org.onosproject.incubator.net.virtual.VirtualNetwork)15 DriverHandler (org.onosproject.net.driver.DriverHandler)15 Collection (java.util.Collection)14