From 76dc892491948adae5e5e62cf94448967e8d865b Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Sun, 6 Dec 2015 07:15:03 -0800 Subject: Fixes bad POM file with ONOS commit 8c68536972f63069c263635c9d9f4f31d7f3e9a2 Change-Id: I7adb5a2d3738d53dbc41db7577768b0e7ced5450 Signed-off-by: Ashlee Young --- .../net/edgeservice/impl/EdgeManager.java | 55 ++++---- .../onosproject/net/flow/impl/FlowRuleManager.java | 2 +- .../flowobjective/impl/FlowObjectiveManager.java | 9 +- .../net/intent/impl/ObjectiveTracker.java | 7 +- .../impl/compiler/MplsPathIntentCompiler.java | 31 +++-- .../OpticalConnectivityIntentCompiler.java | 12 +- .../net/link/impl/BasicLinkOperator.java | 4 + .../newresource/impl/ResourceDeviceListener.java | 103 +++++++++++++- .../net/newresource/impl/ResourceLinkListener.java | 153 --------------------- .../net/newresource/impl/ResourceRegistrar.java | 11 +- 10 files changed, 173 insertions(+), 214 deletions(-) delete mode 100644 framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceLinkListener.java (limited to 'framework/src/onos/core/net/src/main/java/org/onosproject') diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java index e992f7a4..cd7335d6 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java @@ -27,10 +27,10 @@ import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.ReferenceCardinality; import org.apache.felix.scr.annotations.Service; import org.onosproject.event.AbstractListenerManager; -import org.onosproject.event.Event; import org.onosproject.net.ConnectPoint; import org.onosproject.net.DeviceId; import org.onosproject.net.device.DeviceEvent; +import org.onosproject.net.device.DeviceListener; import org.onosproject.net.device.DeviceService; import org.onosproject.net.edge.EdgePortEvent; import org.onosproject.net.edge.EdgePortListener; @@ -38,17 +38,16 @@ import org.onosproject.net.edge.EdgePortService; import org.onosproject.net.flow.DefaultTrafficTreatment; import org.onosproject.net.flow.TrafficTreatment; import org.onosproject.net.link.LinkEvent; +import org.onosproject.net.link.LinkListener; +import org.onosproject.net.link.LinkService; import org.onosproject.net.packet.DefaultOutboundPacket; import org.onosproject.net.packet.OutboundPacket; import org.onosproject.net.packet.PacketService; import org.onosproject.net.topology.Topology; -import org.onosproject.net.topology.TopologyEvent; -import org.onosproject.net.topology.TopologyListener; import org.onosproject.net.topology.TopologyService; import org.slf4j.Logger; import java.nio.ByteBuffer; -import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -73,7 +72,9 @@ public class EdgeManager private final Map> connectionPoints = Maps.newConcurrentMap(); - private final TopologyListener topologyListener = new InnerTopologyListener(); + private final LinkListener linkListener = new InnerLinkListener(); + + private final DeviceListener deviceListener = new InnerDeviceListener(); @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected PacketService packetService; @@ -84,17 +85,23 @@ public class EdgeManager @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected TopologyService topologyService; + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected LinkService linkService; + @Activate public void activate() { eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry); - topologyService.addListener(topologyListener); + deviceService.addListener(deviceListener); + linkService.addListener(linkListener); + loadAllEdgePorts(); log.info("Started"); } @Deactivate public void deactivate() { eventDispatcher.removeSink(EdgePortEvent.class); - topologyService.removeListener(topologyListener); + deviceService.removeListener(deviceListener); + linkService.removeListener(linkListener); log.info("Stopped"); } @@ -142,31 +149,27 @@ public class EdgeManager return new DefaultOutboundPacket(point.deviceId(), builder.build(), data); } - // Internal listener for topo events used to keep our edge-port cache - // up to date. - private class InnerTopologyListener implements TopologyListener { + private class InnerLinkListener implements LinkListener { + @Override - public void event(TopologyEvent event) { - topology = event.subject(); - List triggers = event.reasons(); - if (triggers != null) { - triggers.forEach(reason -> { - if (reason instanceof DeviceEvent) { - processDeviceEvent((DeviceEvent) reason); - } else if (reason instanceof LinkEvent) { - processLinkEvent((LinkEvent) reason); - } - }); - } else { - //FIXME special case of preexisting edgeport & no triggerless events could cause this to never hit and - //never discover an edgeport that should have been discovered. - loadAllEdgePorts(); - } + public void event(LinkEvent event) { + topology = topologyService.currentTopology(); + processLinkEvent(event); + } + } + + private class InnerDeviceListener implements DeviceListener { + + @Override + public void event(DeviceEvent event) { + topology = topologyService.currentTopology(); + processDeviceEvent(event); } } // Initial loading of the edge port cache. private void loadAllEdgePorts() { + topology = topologyService.currentTopology(); deviceService.getAvailableDevices().forEach(d -> deviceService.getPorts(d.id()) .forEach(p -> addEdgePort(new ConnectPoint(d.id(), p.number())))); } diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/flow/impl/FlowRuleManager.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/flow/impl/FlowRuleManager.java index 5958d1f5..63ee03ec 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/flow/impl/FlowRuleManager.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/flow/impl/FlowRuleManager.java @@ -436,7 +436,7 @@ public class FlowRuleManager log.debug("Adding rule in store, but not on switch {}", rule); flowMissing(rule); } catch (Exception e) { - log.debug("Can't add missing flow rule {}", e.getMessage()); + log.debug("Can't add missing flow rule:", e); continue; } } diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java index 5ecdc7a2..33200b1a 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/flowobjective/impl/FlowObjectiveManager.java @@ -16,7 +16,6 @@ package org.onosproject.net.flowobjective.impl; import com.google.common.collect.Maps; -import com.google.common.collect.Sets; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.Deactivate; @@ -53,9 +52,11 @@ import org.onosproject.net.group.GroupService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collections; import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import static com.google.common.base.Preconditions.checkNotNull; @@ -228,8 +229,10 @@ public class FlowObjectiveManager implements FlowObjectiveService { flowObjectiveStore.getNextGroup(fwd.nextId()) == null) { log.trace("Queuing forwarding objective for nextId {}", fwd.nextId()); // TODO: change to computeIfAbsent - Set pnext = pendingForwards.putIfAbsent(fwd.nextId(), - Sets.newHashSet(new PendingNext(deviceId, fwd))); + Set newset = Collections.newSetFromMap( + new ConcurrentHashMap()); + newset.add(new PendingNext(deviceId, fwd)); + Set pnext = pendingForwards.putIfAbsent(fwd.nextId(), newset); if (pnext != null) { pnext.add(new PendingNext(deviceId, fwd)); } diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/ObjectiveTracker.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/ObjectiveTracker.java index ebf681a2..ff711a02 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/ObjectiveTracker.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/ObjectiveTracker.java @@ -34,6 +34,7 @@ import org.onosproject.net.HostId; import org.onosproject.net.Link; import org.onosproject.net.LinkKey; import org.onosproject.net.NetworkResource; +import org.onosproject.net.PortNumber; import org.onosproject.net.device.DeviceEvent; import org.onosproject.net.device.DeviceListener; import org.onosproject.net.device.DeviceService; @@ -302,11 +303,11 @@ public class ObjectiveTracker implements ObjectiveTrackerService { private class InternalResourceListener implements ResourceListener { @Override public void event(ResourceEvent event) { - Optional> linkEvent = event.subject().components().stream() + Optional> deviceEvent = event.subject().components().stream() .map(Object::getClass) - .filter(x -> x == LinkKey.class) + .filter(x -> x == PortNumber.class) .findFirst(); - if (linkEvent.isPresent()) { + if (deviceEvent.isPresent()) { executorService.execute(() -> { if (delegate == null) { return; diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java index 718c7bbf..5549918c 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/MplsPathIntentCompiler.java @@ -15,6 +15,7 @@ */ package org.onosproject.net.intent.impl.compiler; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; import org.apache.felix.scr.annotations.Activate; @@ -59,9 +60,9 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.google.common.base.Preconditions.checkNotNull; import static org.onosproject.net.LinkKey.linkKey; @@ -120,11 +121,16 @@ public class MplsPathIntentCompiler implements IntentCompiler { return Collections.emptyMap(); } - List resources = labels.entrySet().stream() - .map(x -> ResourcePath.discrete(linkKey(x.getKey().src(), x.getKey().src()), x.getValue())) - .collect(Collectors.toList()); + // for short term solution: same label is used for both directions + // TODO: introduce the concept of Tx and Rx resources of a port + Set resources = labels.entrySet().stream() + .flatMap(x -> Stream.of( + ResourcePath.discrete(x.getKey().src().deviceId(), x.getKey().src().port(), x.getValue()), + ResourcePath.discrete(x.getKey().dst().deviceId(), x.getKey().dst().port(), x.getValue()) + )) + .collect(Collectors.toSet()); List allocations = - resourceService.allocate(intent.id(), resources); + resourceService.allocate(intent.id(), ImmutableList.copyOf(resources)); if (allocations.isEmpty()) { Collections.emptyMap(); } @@ -135,20 +141,23 @@ public class MplsPathIntentCompiler implements IntentCompiler { private Map findMplsLabels(Set links) { Map labels = new HashMap<>(); for (LinkKey link : links) { - Optional label = findMplsLabel(link); - if (label.isPresent()) { - labels.put(link, label.get()); + Set forward = findMplsLabel(link.src()); + Set backward = findMplsLabel(link.dst()); + Set common = Sets.intersection(forward, backward); + if (common.isEmpty()) { + continue; } + labels.put(link, common.iterator().next()); } return labels; } - private Optional findMplsLabel(LinkKey link) { - return resourceService.getAvailableResources(ResourcePath.discrete(link)).stream() + private Set findMplsLabel(ConnectPoint cp) { + return resourceService.getAvailableResources(ResourcePath.discrete(cp.deviceId(), cp.port())).stream() .filter(x -> x.last() instanceof MplsLabel) .map(x -> (MplsLabel) x.last()) - .findFirst(); + .collect(Collectors.toSet()); } private MplsLabel getMplsLabel(Map labels, LinkKey link) { diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java index 2941ddba..e017ac58 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/OpticalConnectivityIntentCompiler.java @@ -57,9 +57,9 @@ import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.google.common.base.Preconditions.checkArgument; -import static org.onosproject.net.LinkKey.linkKey; /** * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. @@ -182,7 +182,10 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler lambdaResources = path.links().stream() - .map(x -> ResourcePath.discrete(linkKey(x.src(), x.dst()))) + .flatMap(x -> Stream.of( + ResourcePath.discrete(x.src().deviceId(), x.src().port()), + ResourcePath.discrete(x.dst().deviceId(), x.dst().port()) + )) .map(x -> x.child(minLambda)) .collect(Collectors.toList()); @@ -197,7 +200,10 @@ public class OpticalConnectivityIntentCompiler implements IntentCompiler findCommonLambdasOverLinks(List links) { return links.stream() - .map(x -> ResourcePath.discrete(linkKey(x.src(), x.dst()))) + .flatMap(x -> Stream.of( + ResourcePath.discrete(x.src().deviceId(), x.src().port()), + ResourcePath.discrete(x.dst().deviceId(), x.dst().port()) + )) .map(resourceService::getAvailableResources) .map(x -> Iterables.filter(x, r -> r.last() instanceof IndexedLambda)) .map(x -> Iterables.transform(x, r -> (IndexedLambda) r.last())) diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java index 801092f4..ff74dbde 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/link/impl/BasicLinkOperator.java @@ -38,6 +38,7 @@ import org.slf4j.Logger; public final class BasicLinkOperator implements ConfigOperator { private static final long DEF_BANDWIDTH = -1L; + private static final double DEF_METRIC = -1; private static final Duration DEF_DURATION = Duration.ofNanos(-1L); private static final Logger log = getLogger(BasicLinkOperator.class); @@ -77,6 +78,9 @@ public final class BasicLinkOperator implements ConfigOperator { */ public static SparseAnnotations combine(BasicLinkConfig cfg, SparseAnnotations an) { DefaultAnnotations.Builder b = DefaultAnnotations.builder(); + if (cfg.metric() != DEF_METRIC) { + b.set(AnnotationKeys.METRIC, String.valueOf(cfg.metric())); + } if (cfg.latency() != DEF_DURATION) { b.set(AnnotationKeys.LATENCY, cfg.latency().toString()); } diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java index 4fb0d7ba..bfc6a995 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceDeviceListener.java @@ -16,19 +16,32 @@ package org.onosproject.net.newresource.impl; import com.google.common.collect.Lists; +import org.onlab.packet.MplsLabel; +import org.onlab.packet.VlanId; +import org.onlab.util.ItemNotFoundException; import org.onosproject.net.Device; +import org.onosproject.net.DeviceId; import org.onosproject.net.Port; import org.onosproject.net.OchPort; +import org.onosproject.net.OchSignal; +import org.onosproject.net.PortNumber; import org.onosproject.net.TributarySlot; import org.onosproject.net.OduSignalType; +import org.onosproject.net.behaviour.LambdaQuery; +import org.onosproject.net.behaviour.MplsQuery; +import org.onosproject.net.behaviour.VlanQuery; import org.onosproject.net.device.DeviceEvent; import org.onosproject.net.device.DeviceListener; +import org.onosproject.net.driver.DriverHandler; +import org.onosproject.net.driver.DriverService; import org.onosproject.net.newresource.ResourceAdminService; import org.onosproject.net.newresource.ResourcePath; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Collections; import java.util.List; +import java.util.SortedSet; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -42,12 +55,19 @@ final class ResourceDeviceListener implements DeviceListener { private static final Logger log = LoggerFactory.getLogger(ResourceDeviceListener.class); + private static final int MAX_VLAN_ID = VlanId.MAX_VLAN; + private static final List ENTIRE_VLAN_IDS = getEntireVlans(); + + private static final int MAX_MPLS_LABEL = 1048576; + private static final List ENTIRE_MPLS_LABELS = getEntireMplsLabels(); + private static final int TOTAL_ODU2_TRIBUTARY_SLOTS = 8; private static final int TOTAL_ODU4_TRIBUTARY_SLOTS = 80; private static final List ENTIRE_ODU2_TRIBUTARY_SLOTS = getEntireOdu2TributarySlots(); private static final List ENTIRE_ODU4_TRIBUTARY_SLOTS = getEntireOdu4TributarySlots(); private final ResourceAdminService adminService; + private final DriverService driverService; private final ExecutorService executor; /** @@ -56,8 +76,10 @@ final class ResourceDeviceListener implements DeviceListener { * @param adminService instance invoked to register resources * @param executor executor used for processing resource registration */ - ResourceDeviceListener(ResourceAdminService adminService, ExecutorService executor) { + ResourceDeviceListener(ResourceAdminService adminService, DriverService driverService, + ExecutorService executor) { this.adminService = checkNotNull(adminService); + this.driverService = checkNotNull(driverService); this.executor = checkNotNull(executor); } @@ -95,6 +117,26 @@ final class ResourceDeviceListener implements DeviceListener { executor.submit(() -> { adminService.registerResources(portPath); + // for VLAN IDs + if (isVlanEnabled(device.id(), port.number())) { + adminService.registerResources(Lists.transform(ENTIRE_VLAN_IDS, portPath::child)); + } + + // for MPLS labels + if (isMplsEnabled(device.id(), port.number())) { + adminService.registerResources(Lists.transform(ENTIRE_MPLS_LABELS, portPath::child)); + } + + // for Lambdas + SortedSet lambdas = queryLambdas(device.id(), port.number()); + if (!lambdas.isEmpty()) { + adminService.registerResources(lambdas.stream() + .map(portPath::child) + .collect(Collectors.toList())); + } + + // for Tributary slots + // TODO: need to define Behaviour to make a query about OCh port switch (port.type()) { case OCH: // register ODU TributarySlots against the OCH port @@ -124,15 +166,68 @@ final class ResourceDeviceListener implements DeviceListener { executor.submit(() -> adminService.unregisterResources(resource)); } + private SortedSet queryLambdas(DeviceId did, PortNumber port) { + try { + DriverHandler handler = driverService.createHandler(did); + if (handler == null) { + return Collections.emptySortedSet(); + } + LambdaQuery query = handler.behaviour(LambdaQuery.class); + return query.queryLambdas(port); + } catch (ItemNotFoundException e) { + return Collections.emptySortedSet(); + } + } + + private boolean isVlanEnabled(DeviceId device, PortNumber port) { + try { + DriverHandler handler = driverService.createHandler(device); + if (handler == null) { + return false; + } + + VlanQuery query = handler.behaviour(VlanQuery.class); + return query != null && query.isEnabled(port); + } catch (ItemNotFoundException e) { + return false; + } + } + + private boolean isMplsEnabled(DeviceId device, PortNumber port) { + try { + DriverHandler handler = driverService.createHandler(device); + if (handler == null) { + return false; + } + + MplsQuery query = handler.behaviour(MplsQuery.class); + return query != null && query.isEnabled(port); + } catch (ItemNotFoundException e) { + return false; + } + } + + private static List getEntireVlans() { + return IntStream.range(0, MAX_VLAN_ID) + .mapToObj(x -> VlanId.vlanId((short) x)) + .collect(Collectors.toList()); + } + + private static List getEntireMplsLabels() { + // potentially many objects are created + return IntStream.range(0, MAX_MPLS_LABEL) + .mapToObj(MplsLabel::mplsLabel) + .collect(Collectors.toList()); + } + private static List getEntireOdu2TributarySlots() { return IntStream.rangeClosed(1, TOTAL_ODU2_TRIBUTARY_SLOTS) - .mapToObj(x -> TributarySlot.of(x)) + .mapToObj(TributarySlot::of) .collect(Collectors.toList()); } private static List getEntireOdu4TributarySlots() { return IntStream.rangeClosed(1, TOTAL_ODU4_TRIBUTARY_SLOTS) - .mapToObj(x -> TributarySlot.of(x)) + .mapToObj(TributarySlot::of) .collect(Collectors.toList()); } - } diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceLinkListener.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceLinkListener.java deleted file mode 100644 index 9d2e06f5..00000000 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceLinkListener.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Copyright 2015 Open Networking Laboratory - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.onosproject.net.newresource.impl; - -import com.google.common.collect.Lists; -import org.onlab.packet.MplsLabel; -import org.onlab.packet.VlanId; -import org.onlab.util.ItemNotFoundException; -import org.onosproject.net.ConnectPoint; -import org.onosproject.net.Link; -import org.onosproject.net.LinkKey; -import org.onosproject.net.behaviour.MplsQuery; -import org.onosproject.net.behaviour.VlanQuery; -import org.onosproject.net.driver.DriverHandler; -import org.onosproject.net.driver.DriverService; -import org.onosproject.net.link.LinkEvent; -import org.onosproject.net.link.LinkListener; -import org.onosproject.net.newresource.ResourceAdminService; -import org.onosproject.net.newresource.ResourcePath; - -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.function.Predicate; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import static com.google.common.base.Preconditions.checkNotNull; - -/** - * An implementation of LinkListener registering links as resources. - */ -final class ResourceLinkListener implements LinkListener { - - private static final int TOTAL_VLANS = 1024; - private static final List ENTIRE_VLAN_IDS = getEntireVlans(); - - private static final int TOTAL_MPLS_LABELS = 1048576; - private static final List ENTIRE_MPLS_LABELS = getEntireMplsLabels(); - - private final ResourceAdminService adminService; - private final DriverService driverService; - private final ExecutorService executor; - - /** - * Creates an instance with the specified ResourceAdminService and ExecutorService. - * - * @param adminService instance invoked to register resources - * @param driverService driver service instance - * @param executor executor used for processing resource registration - */ - ResourceLinkListener(ResourceAdminService adminService, DriverService driverService, ExecutorService executor) { - this.adminService = checkNotNull(adminService); - this.driverService = checkNotNull(driverService); - this.executor = checkNotNull(executor); - } - - @Override - public void event(LinkEvent event) { - Link link = event.subject(); - switch (event.type()) { - case LINK_ADDED: - registerLinkResource(link); - break; - case LINK_REMOVED: - unregisterLinkResource(link); - break; - default: - break; - } - } - - private void registerLinkResource(Link link) { - executor.submit(() -> { - // register the link - LinkKey linkKey = LinkKey.linkKey(link); - adminService.registerResources(ResourcePath.discrete(linkKey)); - - ResourcePath linkPath = ResourcePath.discrete(linkKey); - // register VLAN IDs against the link - if (isEnabled(link, this::isVlanEnabled)) { - adminService.registerResources(Lists.transform(ENTIRE_VLAN_IDS, linkPath::child)); - } - - // register MPLS labels against the link - if (isEnabled(link, this::isMplsEnabled)) { - adminService.registerResources(Lists.transform(ENTIRE_MPLS_LABELS, linkPath::child)); - } - }); - } - - private void unregisterLinkResource(Link link) { - LinkKey linkKey = LinkKey.linkKey(link); - executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(linkKey))); - } - - private boolean isEnabled(Link link, Predicate predicate) { - return predicate.test(link.src()) && predicate.test(link.dst()); - } - - private boolean isVlanEnabled(ConnectPoint cp) { - try { - DriverHandler handler = driverService.createHandler(cp.deviceId()); - if (handler == null) { - return false; - } - - VlanQuery query = handler.behaviour(VlanQuery.class); - return query != null && query.isEnabled(cp.port()); - } catch (ItemNotFoundException e) { - return false; - } - } - - private boolean isMplsEnabled(ConnectPoint cp) { - try { - DriverHandler handler = driverService.createHandler(cp.deviceId()); - if (handler == null) { - return false; - } - - MplsQuery query = handler.behaviour(MplsQuery.class); - return query != null && query.isEnabled(cp.port()); - } catch (ItemNotFoundException e) { - return false; - } - } - - private static List getEntireVlans() { - return IntStream.range(0, TOTAL_VLANS) - .mapToObj(x -> VlanId.vlanId((short) x)) - .collect(Collectors.toList()); - } - - private static List getEntireMplsLabels() { - // potentially many objects are created - return IntStream.range(0, TOTAL_MPLS_LABELS) - .mapToObj(MplsLabel::mplsLabel) - .collect(Collectors.toList()); - } -} diff --git a/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java b/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java index 143f8c2b..e8042661 100644 --- a/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java +++ b/framework/src/onos/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceRegistrar.java @@ -24,8 +24,6 @@ import org.apache.felix.scr.annotations.ReferenceCardinality; import org.onosproject.net.device.DeviceListener; import org.onosproject.net.device.DeviceService; import org.onosproject.net.driver.DriverService; -import org.onosproject.net.link.LinkListener; -import org.onosproject.net.link.LinkService; import org.onosproject.net.newresource.ResourceAdminService; import java.util.concurrent.ExecutorService; @@ -49,25 +47,18 @@ public final class ResourceRegistrar { @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) protected DeviceService deviceService; - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) - protected LinkService linkService; - private DeviceListener deviceListener; - private LinkListener linkListener; private final ExecutorService executor = Executors.newSingleThreadExecutor(groupedThreads("onos/resource", "registrar")); @Activate public void activate() { - deviceListener = new ResourceDeviceListener(adminService, executor); + deviceListener = new ResourceDeviceListener(adminService, driverService, executor); deviceService.addListener(deviceListener); - linkListener = new ResourceLinkListener(adminService, driverService, executor); - linkService.addListener(linkListener); } @Deactivate public void deactivate() { deviceService.removeListener(deviceListener); - linkService.removeListener(linkListener); } } -- cgit 1.2.3-korg