Sho SHIMIZU
Committed by Gerrit Code Review

ONOS-2742: Register entire VLAN ID space when a link is discovered

Change-Id: Ia00a1e732e3cf458a899baa25307de813cca4aef
...@@ -15,14 +15,23 @@ ...@@ -15,14 +15,23 @@
15 */ 15 */
16 package org.onosproject.net.newresource.impl; 16 package org.onosproject.net.newresource.impl;
17 17
18 +import org.onlab.packet.VlanId;
19 +import org.onlab.util.ItemNotFoundException;
20 +import org.onosproject.net.ConnectPoint;
18 import org.onosproject.net.Link; 21 import org.onosproject.net.Link;
19 import org.onosproject.net.LinkKey; 22 import org.onosproject.net.LinkKey;
23 +import org.onosproject.net.behaviour.VlanQuery;
24 +import org.onosproject.net.driver.DriverHandler;
25 +import org.onosproject.net.driver.DriverService;
20 import org.onosproject.net.link.LinkEvent; 26 import org.onosproject.net.link.LinkEvent;
21 import org.onosproject.net.link.LinkListener; 27 import org.onosproject.net.link.LinkListener;
22 import org.onosproject.net.newresource.ResourceAdminService; 28 import org.onosproject.net.newresource.ResourceAdminService;
23 import org.onosproject.net.newresource.ResourcePath; 29 import org.onosproject.net.newresource.ResourcePath;
24 30
31 +import java.util.List;
25 import java.util.concurrent.ExecutorService; 32 import java.util.concurrent.ExecutorService;
33 +import java.util.stream.Collectors;
34 +import java.util.stream.IntStream;
26 35
27 import static com.google.common.base.Preconditions.checkNotNull; 36 import static com.google.common.base.Preconditions.checkNotNull;
28 37
...@@ -31,7 +40,11 @@ import static com.google.common.base.Preconditions.checkNotNull; ...@@ -31,7 +40,11 @@ import static com.google.common.base.Preconditions.checkNotNull;
31 */ 40 */
32 final class ResourceLinkListener implements LinkListener { 41 final class ResourceLinkListener implements LinkListener {
33 42
43 + private static final int TOTAL_VLANS = 1024;
44 + private static final List<VlanId> ENTIRE_VLAN_IDS = getEntireVlans();
45 +
34 private final ResourceAdminService adminService; 46 private final ResourceAdminService adminService;
47 + private final DriverService driverService;
35 private final ExecutorService executor; 48 private final ExecutorService executor;
36 49
37 /** 50 /**
...@@ -40,8 +53,9 @@ final class ResourceLinkListener implements LinkListener { ...@@ -40,8 +53,9 @@ final class ResourceLinkListener implements LinkListener {
40 * @param adminService instance invoked to register resources 53 * @param adminService instance invoked to register resources
41 * @param executor executor used for processing resource registration 54 * @param executor executor used for processing resource registration
42 */ 55 */
43 - ResourceLinkListener(ResourceAdminService adminService, ExecutorService executor) { 56 + ResourceLinkListener(ResourceAdminService adminService, DriverService driverService, ExecutorService executor) {
44 this.adminService = checkNotNull(adminService); 57 this.adminService = checkNotNull(adminService);
58 + this.driverService = checkNotNull(driverService);
45 this.executor = checkNotNull(executor); 59 this.executor = checkNotNull(executor);
46 } 60 }
47 61
...@@ -61,12 +75,47 @@ final class ResourceLinkListener implements LinkListener { ...@@ -61,12 +75,47 @@ final class ResourceLinkListener implements LinkListener {
61 } 75 }
62 76
63 private void registerLinkResource(Link link) { 77 private void registerLinkResource(Link link) {
64 - LinkKey linkKey = LinkKey.linkKey(link); 78 + executor.submit(() -> {
65 - executor.submit(() -> adminService.registerResources(ResourcePath.ROOT, linkKey)); 79 + // register the link
80 + LinkKey linkKey = LinkKey.linkKey(link);
81 + adminService.registerResources(ResourcePath.ROOT, linkKey);
82 +
83 + // register VLAN IDs against the link
84 + if (isVlanEnabled(link)) {
85 + adminService.registerResources(new ResourcePath(linkKey), ENTIRE_VLAN_IDS);
86 + }
87 + });
66 } 88 }
67 89
68 private void unregisterLinkResource(Link link) { 90 private void unregisterLinkResource(Link link) {
69 LinkKey linkKey = LinkKey.linkKey(link); 91 LinkKey linkKey = LinkKey.linkKey(link);
70 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, linkKey)); 92 executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, linkKey));
71 } 93 }
94 +
95 + private boolean isVlanEnabled(Link link) {
96 + ConnectPoint src = link.src();
97 + ConnectPoint dst = link.dst();
98 +
99 + return isVlanEnabled(src) && isVlanEnabled(dst);
100 + }
101 +
102 + private boolean isVlanEnabled(ConnectPoint cp) {
103 + try {
104 + DriverHandler handler = driverService.createHandler(cp.deviceId());
105 + if (handler == null) {
106 + return false;
107 + }
108 +
109 + VlanQuery query = handler.behaviour(VlanQuery.class);
110 + return query != null && query.isEnabled(cp.port());
111 + } catch (ItemNotFoundException e) {
112 + return false;
113 + }
114 + }
115 +
116 + private static List<VlanId> getEntireVlans() {
117 + return IntStream.range(0, TOTAL_VLANS)
118 + .mapToObj(x -> VlanId.vlanId((short) x))
119 + .collect(Collectors.toList());
120 + }
72 } 121 }
......
...@@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Reference; ...@@ -23,6 +23,7 @@ import org.apache.felix.scr.annotations.Reference;
23 import org.apache.felix.scr.annotations.ReferenceCardinality; 23 import org.apache.felix.scr.annotations.ReferenceCardinality;
24 import org.onosproject.net.device.DeviceListener; 24 import org.onosproject.net.device.DeviceListener;
25 import org.onosproject.net.device.DeviceService; 25 import org.onosproject.net.device.DeviceService;
26 +import org.onosproject.net.driver.DriverService;
26 import org.onosproject.net.link.LinkListener; 27 import org.onosproject.net.link.LinkListener;
27 import org.onosproject.net.link.LinkService; 28 import org.onosproject.net.link.LinkService;
28 import org.onosproject.net.newresource.ResourceAdminService; 29 import org.onosproject.net.newresource.ResourceAdminService;
...@@ -43,6 +44,9 @@ public final class ResourceRegistrar { ...@@ -43,6 +44,9 @@ public final class ResourceRegistrar {
43 protected ResourceAdminService adminService; 44 protected ResourceAdminService adminService;
44 45
45 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 + protected DriverService driverService;
48 +
49 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
46 protected DeviceService deviceService; 50 protected DeviceService deviceService;
47 51
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
...@@ -57,7 +61,7 @@ public final class ResourceRegistrar { ...@@ -57,7 +61,7 @@ public final class ResourceRegistrar {
57 public void activate() { 61 public void activate() {
58 deviceListener = new ResourceDeviceListener(adminService, executor); 62 deviceListener = new ResourceDeviceListener(adminService, executor);
59 deviceService.addListener(deviceListener); 63 deviceService.addListener(deviceListener);
60 - linkListener = new ResourceLinkListener(adminService, executor); 64 + linkListener = new ResourceLinkListener(adminService, driverService, executor);
61 linkService.addListener(linkListener); 65 linkService.addListener(linkListener);
62 } 66 }
63 67
......