Yi Tseng
Committed by Luca Prete

[ONOS-5238] Add neighbour message handler for vpls

Change-Id: Iaef04233402864874af0c83231117c279e946d64
...@@ -100,12 +100,13 @@ public class IntentInstaller { ...@@ -100,12 +100,13 @@ public class IntentInstaller {
100 .filter(cp -> !cp.equals(src)) 100 .filter(cp -> !cp.equals(src))
101 .collect(Collectors.toSet()); 101 .collect(Collectors.toSet());
102 Key brcKey = buildKey(PREFIX_BROADCAST, src, vlanId); 102 Key brcKey = buildKey(PREFIX_BROADCAST, src, vlanId);
103 - if (intentService.getIntent(brcKey) == null) { 103 +
104 - SinglePointToMultiPointIntent brcIntent = 104 + if (intentService.getIntent(brcKey) == null && dsts.size() > 0) {
105 - buildBrcIntent(brcKey, src, dsts, vlanId); 105 + intents.add(buildBrcIntent(brcKey, src, dsts, vlanId));
106 - intents.add(brcIntent);
107 } 106 }
108 - if (mac != null && countMacInCPoints(cPoints) > 1) { 107 +
108 + if (mac != null && countMacInCPoints(cPoints) > 1 &&
109 + dsts.size() > 0) {
109 Key uniKey = buildKey(PREFIX_UNICAST, src, vlanId); 110 Key uniKey = buildKey(PREFIX_UNICAST, src, vlanId);
110 if (intentService.getIntent(uniKey) == null) { 111 if (intentService.getIntent(uniKey) == null) {
111 MultiPointToSinglePointIntent uniIntent = 112 MultiPointToSinglePointIntent uniIntent =
......
...@@ -38,7 +38,6 @@ import org.onosproject.net.host.HostEvent; ...@@ -38,7 +38,6 @@ import org.onosproject.net.host.HostEvent;
38 import org.onosproject.net.host.HostListener; 38 import org.onosproject.net.host.HostListener;
39 import org.onosproject.net.host.HostService; 39 import org.onosproject.net.host.HostService;
40 import org.onosproject.net.intent.IntentService; 40 import org.onosproject.net.intent.IntentService;
41 -import org.onosproject.routing.IntentSynchronizationAdminService;
42 import org.onosproject.routing.IntentSynchronizationService; 41 import org.onosproject.routing.IntentSynchronizationService;
43 import org.slf4j.Logger; 42 import org.slf4j.Logger;
44 43
...@@ -52,7 +51,8 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -52,7 +51,8 @@ import static org.slf4j.LoggerFactory.getLogger;
52 */ 51 */
53 @Component(immediate = true) 52 @Component(immediate = true)
54 public class Vpls { 53 public class Vpls {
55 - private static final String VPLS_APP = "org.onosproject.vpls"; 54 + protected static final String VPLS_APP = "org.onosproject.vpls";
55 +
56 private final Logger log = getLogger(getClass()); 56 private final Logger log = getLogger(getClass());
57 57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
...@@ -73,9 +73,6 @@ public class Vpls { ...@@ -73,9 +73,6 @@ public class Vpls {
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected IntentSynchronizationService intentSynchronizer; 74 protected IntentSynchronizationService intentSynchronizer;
75 75
76 - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 - protected IntentSynchronizationAdminService intentSynchronizerAdmin;
78 -
79 private final HostListener hostListener = new InternalHostListener(); 76 private final HostListener hostListener = new InternalHostListener();
80 77
81 private final InternalInterfaceListener interfaceListener 78 private final InternalInterfaceListener interfaceListener
...@@ -85,6 +82,7 @@ public class Vpls { ...@@ -85,6 +82,7 @@ public class Vpls {
85 82
86 private ApplicationId appId; 83 private ApplicationId appId;
87 84
85 +
88 @Activate 86 @Activate
89 public void activate() { 87 public void activate() {
90 appId = coreService.registerApplication(VPLS_APP); 88 appId = coreService.registerApplication(VPLS_APP);
...@@ -102,12 +100,13 @@ public class Vpls { ...@@ -102,12 +100,13 @@ public class Vpls {
102 100
103 setupConnectivity(); 101 setupConnectivity();
104 102
105 - log.info("Started"); 103 + log.debug("Activated");
106 } 104 }
107 105
108 @Deactivate 106 @Deactivate
109 public void deactivate() { 107 public void deactivate() {
110 - log.info("Stopped"); 108 + intentSynchronizer.removeIntentsByAppId(appId);
109 + log.debug("Deactivated");
111 } 110 }
112 111
113 protected void setupConnectivity() { 112 protected void setupConnectivity() {
...@@ -130,6 +129,7 @@ public class Vpls { ...@@ -130,6 +129,7 @@ public class Vpls {
130 * hosts attached. 129 * hosts attached.
131 */ 130 */
132 intentInstaller.installIntents(confHostPresentCPoint); 131 intentInstaller.installIntents(confHostPresentCPoint);
132 +
133 } 133 }
134 134
135 /** 135 /**
...@@ -146,21 +146,20 @@ public class Vpls { ...@@ -146,21 +146,20 @@ public class Vpls {
146 interfaceService.getInterfaces() 146 interfaceService.getInterfaces()
147 .stream() 147 .stream()
148 .filter(intf -> intf.ipAddressesList().isEmpty()) 148 .filter(intf -> intf.ipAddressesList().isEmpty())
149 - .forEach(intf -> confCPointsByVlan.put(intf.vlan(), 149 + .forEach(intf -> confCPointsByVlan.put(intf.vlan(), intf.connectPoint()));
150 - intf.connectPoint()));
151 return confCPointsByVlan; 150 return confCPointsByVlan;
152 } 151 }
153 152
154 /** 153 /**
155 - * Checks if for any ConnectPoint configured there's an host present 154 + * Checks if for any ConnectPoint configured there's an host presents
156 - * and in case it associate them together. 155 + * and in case it associates them together.
157 * 156 *
158 - * @param confCPointsByVlan the configured ConnectPoints grouped by vlan id 157 + * @param confCPointsByVlan the configured ConnectPoints grouped by VLAN Id
159 * @return the configured ConnectPoints with eventual hosts associated. 158 * @return the configured ConnectPoints with eventual hosts associated.
160 */ 159 */
161 protected SetMultimap<VlanId, Pair<ConnectPoint, MacAddress>> pairAvailableHosts( 160 protected SetMultimap<VlanId, Pair<ConnectPoint, MacAddress>> pairAvailableHosts(
162 SetMultimap<VlanId, ConnectPoint> confCPointsByVlan) { 161 SetMultimap<VlanId, ConnectPoint> confCPointsByVlan) {
163 - log.debug("Binding connected hosts mac addresses"); 162 + log.debug("Binding connected hosts MAC addresses");
164 163
165 SetMultimap<VlanId, Pair<ConnectPoint, MacAddress>> confHostPresentCPoint = 164 SetMultimap<VlanId, Pair<ConnectPoint, MacAddress>> confHostPresentCPoint =
166 HashMultimap.create(); 165 HashMultimap.create();
...@@ -171,9 +170,10 @@ public class Vpls { ...@@ -171,9 +170,10 @@ public class Vpls {
171 return confHostPresentCPoint; 170 return confHostPresentCPoint;
172 } 171 }
173 172
173 + // Bind VLAN Id with hosts and connect points
174 private void bindMacAddr(Map.Entry<VlanId, ConnectPoint> e, 174 private void bindMacAddr(Map.Entry<VlanId, ConnectPoint> e,
175 SetMultimap<VlanId, Pair<ConnectPoint, 175 SetMultimap<VlanId, Pair<ConnectPoint,
176 - MacAddress>> confHostPresentCPoint) { 176 + MacAddress>> confHostPresentCPoint) {
177 VlanId vlanId = e.getKey(); 177 VlanId vlanId = e.getKey();
178 ConnectPoint cp = e.getValue(); 178 ConnectPoint cp = e.getValue();
179 Set<Host> connectedHosts = hostService.getConnectedHosts(cp); 179 Set<Host> connectedHosts = hostService.getConnectedHosts(cp);
......
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.vpls;
18 +
19 +import com.google.common.collect.Maps;
20 +
21 +import org.apache.felix.scr.annotations.Activate;
22 +import org.apache.felix.scr.annotations.Component;
23 +import org.apache.felix.scr.annotations.Deactivate;
24 +import org.apache.felix.scr.annotations.Reference;
25 +import org.apache.felix.scr.annotations.ReferenceCardinality;
26 +import org.onosproject.core.ApplicationId;
27 +import org.onosproject.core.CoreService;
28 +import org.onosproject.incubator.net.intf.Interface;
29 +import org.onosproject.incubator.net.intf.InterfaceEvent;
30 +import org.onosproject.incubator.net.intf.InterfaceListener;
31 +import org.onosproject.incubator.net.intf.InterfaceService;
32 +import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
33 +import org.onosproject.incubator.net.neighbour.NeighbourMessageHandler;
34 +import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
35 +import org.onosproject.net.Host;
36 +import org.onosproject.net.host.HostService;
37 +import org.slf4j.Logger;
38 +
39 +import java.util.Map;
40 +
41 +import static org.slf4j.LoggerFactory.getLogger;
42 +
43 +/**
44 + * Handles neighbour messages for VPLS use case.
45 + * Handlers will be changed automatically by interface or network configuration
46 + * events.
47 + */
48 +@Component(immediate = true)
49 +public class VplsNeighbourHandler {
50 +
51 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 + protected CoreService coreService;
53 +
54 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
55 + protected InterfaceService interfaceService;
56 +
57 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
58 + protected NeighbourResolutionService neighbourService;
59 +
60 + private VplsInterfaceListener interfaceListener
61 + = new VplsInterfaceListener();
62 +
63 + private VplsNeighbourMessageHandler neighbourHandler =
64 + new VplsNeighbourMessageHandler();
65 +
66 + private final Logger log = getLogger(getClass());
67 +
68 + private Map<Interface, NeighbourMessageHandler> neighbourHandlers =
69 + Maps.newHashMap();
70 +
71 + private ApplicationId appId;
72 +
73 +
74 + @Activate
75 + protected void activate() {
76 + appId = coreService.registerApplication(Vpls.VPLS_APP);
77 + interfaceService.addListener(interfaceListener);
78 +
79 + interfaceService.getInterfaces().forEach(intf -> {
80 + neighbourHandlers.put(intf, neighbourHandler);
81 +
82 + neighbourService.registerNeighbourHandler(intf, neighbourHandler, appId);
83 + });
84 +
85 + log.debug("Activated");
86 + }
87 +
88 + @Deactivate
89 + protected void deactivate() {
90 + interfaceService.removeListener(interfaceListener);
91 + neighbourHandlers.entrySet().forEach(e -> {
92 + neighbourService.unregisterNeighbourHandler(e.getKey(), e.getValue(), appId);
93 + });
94 + log.debug("Deactivated");
95 + }
96 +
97 + private void configNeighbourHandler(Interface intf,
98 + NeighbourMessageHandler handler,
99 + InterfaceEvent.Type eventType) {
100 + switch (eventType) {
101 + case INTERFACE_ADDED:
102 + neighbourHandlers.put(intf, handler);
103 + neighbourService.registerNeighbourHandler(intf, handler, appId);
104 + break;
105 + case INTERFACE_REMOVED:
106 + neighbourHandlers.remove(intf, handler);
107 + neighbourService.unregisterNeighbourHandler(intf, handler, appId);
108 + break;
109 + case INTERFACE_UPDATED:
110 + break;
111 + default:
112 + break;
113 + }
114 + }
115 +
116 + /**
117 + * Handler for neighbour messages.
118 + */
119 + private class VplsNeighbourMessageHandler implements NeighbourMessageHandler {
120 +
121 + @Override
122 + public void handleMessage(NeighbourMessageContext context,
123 + HostService hostService) {
124 +
125 + switch (context.type()) {
126 + case REQUEST:
127 + interfaceService.getInterfacesByVlan(context.vlan())
128 + .stream()
129 + .map(Interface::connectPoint)
130 + .forEach(context::proxy);
131 + break;
132 + case REPLY:
133 + hostService.getHostsByMac(context.dstMac())
134 + .stream()
135 + .filter(host -> host.vlan().equals(context.vlan()))
136 + .map(Host::location)
137 + .forEach(context::proxy);
138 + break;
139 +
140 + default:
141 + log.warn("Unknown context type: {}", context.type());
142 + break;
143 + }
144 + }
145 + }
146 +
147 + /**
148 + * Listener for interface configuration events.
149 + */
150 + private class VplsInterfaceListener implements InterfaceListener {
151 +
152 + @Override
153 + public void event(InterfaceEvent event) {
154 + configNeighbourHandler(event.subject(), neighbourHandler, event.type());
155 + }
156 + }
157 +
158 +}
...@@ -15,13 +15,13 @@ ...@@ -15,13 +15,13 @@
15 */ 15 */
16 package org.onosproject.vpls; 16 package org.onosproject.vpls;
17 17
18 -import java.util.ArrayList;
19 import java.util.Collections; 18 import java.util.Collections;
20 import java.util.List; 19 import java.util.List;
21 import java.util.Set; 20 import java.util.Set;
22 import java.util.concurrent.atomic.AtomicLong; 21 import java.util.concurrent.atomic.AtomicLong;
23 import java.util.stream.Collectors; 22 import java.util.stream.Collectors;
24 23
24 +import com.google.common.collect.Lists;
25 import org.junit.After; 25 import org.junit.After;
26 import org.junit.Before; 26 import org.junit.Before;
27 import org.junit.Test; 27 import org.junit.Test;
...@@ -170,7 +170,7 @@ public class VplsTest { ...@@ -170,7 +170,7 @@ public class VplsTest {
170 vpls.intentService = intentService; 170 vpls.intentService = intentService;
171 vpls.interfaceService = interfaceService; 171 vpls.interfaceService = interfaceService;
172 vpls.intentSynchronizer = intentSynchronizer; 172 vpls.intentSynchronizer = intentSynchronizer;
173 - vpls.intentSynchronizerAdmin = intentSynchronizer; 173 +
174 } 174 }
175 175
176 @After 176 @After
...@@ -230,7 +230,7 @@ public class VplsTest { ...@@ -230,7 +230,7 @@ public class VplsTest {
230 public void testActivateNoHosts() { 230 public void testActivateNoHosts() {
231 vpls.activate(); 231 vpls.activate();
232 232
233 - List<Intent> expectedIntents = new ArrayList<>(); 233 + List<Intent> expectedIntents = Lists.newArrayList();
234 expectedIntents.addAll(generateVlanOneBrc()); 234 expectedIntents.addAll(generateVlanOneBrc());
235 expectedIntents.addAll(generateVlanTwoBrc()); 235 expectedIntents.addAll(generateVlanTwoBrc());
236 236
...@@ -263,7 +263,7 @@ public class VplsTest { ...@@ -263,7 +263,7 @@ public class VplsTest {
263 263
264 vpls.activate(); 264 vpls.activate();
265 265
266 - List<Intent> expectedIntents = new ArrayList<>(); 266 + List<Intent> expectedIntents = Lists.newArrayList();
267 expectedIntents.addAll(generateVlanOneBrc()); 267 expectedIntents.addAll(generateVlanOneBrc());
268 expectedIntents.addAll(generateVlanOneUni()); 268 expectedIntents.addAll(generateVlanOneUni());
269 expectedIntents.addAll(generateVlanTwoBrc()); 269 expectedIntents.addAll(generateVlanTwoBrc());
...@@ -307,7 +307,7 @@ public class VplsTest { ...@@ -307,7 +307,7 @@ public class VplsTest {
307 hostsAvailable.forEach(host -> 307 hostsAvailable.forEach(host ->
308 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host))); 308 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host)));
309 309
310 - List<Intent> expectedIntents = new ArrayList<>(); 310 + List<Intent> expectedIntents = Lists.newArrayList();
311 expectedIntents.addAll(generateVlanOneBrc()); 311 expectedIntents.addAll(generateVlanOneBrc());
312 expectedIntents.addAll(generateVlanOneUni()); 312 expectedIntents.addAll(generateVlanOneUni());
313 expectedIntents.addAll(generateVlanTwoBrc()); 313 expectedIntents.addAll(generateVlanTwoBrc());
...@@ -343,7 +343,7 @@ public class VplsTest { ...@@ -343,7 +343,7 @@ public class VplsTest {
343 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host)); 343 hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host));
344 }); 344 });
345 345
346 - List<Intent> expectedIntents = new ArrayList<>(); 346 + List<Intent> expectedIntents = Lists.newArrayList();
347 expectedIntents.addAll(generateVlanOneBrc()); 347 expectedIntents.addAll(generateVlanOneBrc());
348 expectedIntents.addAll(generateVlanTwoBrc()); 348 expectedIntents.addAll(generateVlanTwoBrc());
349 349
...@@ -382,7 +382,7 @@ public class VplsTest { ...@@ -382,7 +382,7 @@ public class VplsTest {
382 private List<SinglePointToMultiPointIntent> generateVlanOneBrc() { 382 private List<SinglePointToMultiPointIntent> generateVlanOneBrc() {
383 Key key = null; 383 Key key = null;
384 384
385 - List<SinglePointToMultiPointIntent> intents = new ArrayList<>(); 385 + List<SinglePointToMultiPointIntent> intents = Lists.newArrayList();
386 386
387 // Building sp2mp intent for H1 - VLAN1 387 // Building sp2mp intent for H1 - VLAN1
388 key = Key.of((PREFIX_BROADCAST + "-" + DID1 + "-" + P1 + "-" + VLAN1), 388 key = Key.of((PREFIX_BROADCAST + "-" + DID1 + "-" + P1 + "-" + VLAN1),
...@@ -410,7 +410,7 @@ public class VplsTest { ...@@ -410,7 +410,7 @@ public class VplsTest {
410 private List<MultiPointToSinglePointIntent> generateVlanOneUni() { 410 private List<MultiPointToSinglePointIntent> generateVlanOneUni() {
411 Key key = null; 411 Key key = null;
412 412
413 - List<MultiPointToSinglePointIntent> intents = new ArrayList<>(); 413 + List<MultiPointToSinglePointIntent> intents = Lists.newArrayList();
414 414
415 // Building mp2sp intent for H1 - VLAN1 415 // Building mp2sp intent for H1 - VLAN1
416 key = Key.of((PREFIX_UNICAST + "-" + DID1 + "-" + P1 + "-" + VLAN1), 416 key = Key.of((PREFIX_UNICAST + "-" + DID1 + "-" + P1 + "-" + VLAN1),
...@@ -438,7 +438,7 @@ public class VplsTest { ...@@ -438,7 +438,7 @@ public class VplsTest {
438 private List<SinglePointToMultiPointIntent> generateVlanTwoBrc() { 438 private List<SinglePointToMultiPointIntent> generateVlanTwoBrc() {
439 Key key = null; 439 Key key = null;
440 440
441 - List<SinglePointToMultiPointIntent> intents = new ArrayList<>(); 441 + List<SinglePointToMultiPointIntent> intents = Lists.newArrayList();
442 442
443 // Building sp2mp intent for H4 - VLAN2 443 // Building sp2mp intent for H4 - VLAN2
444 key = Key.of((PREFIX_BROADCAST + "-" + DID4 + "-" + P1 + "-" + VLAN2), 444 key = Key.of((PREFIX_BROADCAST + "-" + DID4 + "-" + P1 + "-" + VLAN2),
...@@ -466,7 +466,7 @@ public class VplsTest { ...@@ -466,7 +466,7 @@ public class VplsTest {
466 private List<MultiPointToSinglePointIntent> generateVlanTwoUni() { 466 private List<MultiPointToSinglePointIntent> generateVlanTwoUni() {
467 Key key = null; 467 Key key = null;
468 468
469 - List<MultiPointToSinglePointIntent> intents = new ArrayList<>(); 469 + List<MultiPointToSinglePointIntent> intents = Lists.newArrayList();
470 470
471 // Building mp2sp intent for H4 - VLAN2 471 // Building mp2sp intent for H4 - VLAN2
472 key = Key.of((PREFIX_UNICAST + "-" + DID4 + "-" + P1 + "-" + VLAN2), 472 key = Key.of((PREFIX_UNICAST + "-" + DID4 + "-" + P1 + "-" + VLAN2),
......