blob: ff09167cb99ef86a7475cdb874bd6b24155c7209 [file] [log] [blame]
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -07003 *
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 */
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070016package org.onosproject.vpls;
17
18import com.google.common.collect.HashMultimap;
19import com.google.common.collect.ImmutableSet;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070020import com.google.common.collect.SetMultimap;
21import com.google.common.collect.Sets;
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.Ethernet;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.incubator.net.intf.Interface;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070031import org.onosproject.incubator.net.neighbour.NeighbourHandlerRegistration;
32import org.onosproject.incubator.net.neighbour.NeighbourMessageContext;
33import org.onosproject.incubator.net.neighbour.NeighbourMessageHandler;
34import org.onosproject.incubator.net.neighbour.NeighbourMessageType;
35import org.onosproject.incubator.net.neighbour.NeighbourProtocol;
36import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
37import org.onosproject.net.ConnectPoint;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070038import org.onosproject.net.Host;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070039import org.onosproject.net.host.HostService;
Yi Tsengf4e13e32017-03-30 15:38:39 -070040import org.onosproject.vpls.api.VplsData;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070041
42import java.util.Collection;
43import java.util.Map;
44import java.util.Set;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070045
46import static junit.framework.TestCase.assertEquals;
Yi Tsengf4e13e32017-03-30 15:38:39 -070047import static junit.framework.TestCase.assertTrue;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070048
49/**
Yi Tsengf4e13e32017-03-30 15:38:39 -070050 * Tests the the {@link VplsNeighbourHandler}.
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070051 */
Yi Tsengf4e13e32017-03-30 15:38:39 -070052public class VplsNeighbourHandlerTest extends VplsTest {
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070053
Luca Prete092e8952016-10-26 16:25:56 +020054 private static final String IFACES_NOT_EXPECTED =
55 "The interfaces reached by the packet are not equal to the " +
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010056 "interfaces expected.";
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070057 private VplsNeighbourHandler vplsNeighbourHandler;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070058 private HostService hostService;
59
60 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010061 * Sets up 4 VPLS.
Luca Prete092e8952016-10-26 16:25:56 +020062 * VPLS 1 contains 3 hosts: v100h1, v200h1 and v300h1
63 * VPLS 2 contains 2 hosts: v100h2, v200h2
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +010064 * VPLS 3 contains 2 hosts: vNoneh1, vNoneh2
65 * VPLS 4 contains 2 hosts: v400h1, vNoneh3
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070066 */
67 @Before
68 public void setUp() {
69 vplsNeighbourHandler = new VplsNeighbourHandler();
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070070 hostService = new TestHostService();
Yi Tsengf4e13e32017-03-30 15:38:39 -070071 vplsNeighbourHandler.vplsStore = new TestVplsStore();
72 vplsNeighbourHandler.interfaceService = new TestInterfaceService();
73 vplsNeighbourHandler.neighbourService = new TestNeighbourService();
74 vplsNeighbourHandler.coreService = new TestCoreService();
75 vplsNeighbourHandler.configService = new TestConfigService();
76
77 // Init VPLS store
78 VplsData vplsData = VplsData.of(VPLS1);
79 vplsData.addInterfaces(ImmutableSet.of(V100H1, V200H1, V300H1));
80 vplsNeighbourHandler.vplsStore.addVpls(vplsData);
81
82 vplsData = VplsData.of(VPLS2);
83 vplsData.addInterfaces(ImmutableSet.of(V100H2, V200H2));
84 vplsNeighbourHandler.vplsStore.addVpls(vplsData);
85
86 vplsData = VplsData.of(VPLS3);
87 vplsData.addInterfaces(ImmutableSet.of(VNONEH1, VNONEH2));
88 vplsNeighbourHandler.vplsStore.addVpls(vplsData);
89
90 vplsData = VplsData.of(VPLS4);
91 vplsData.addInterfaces(ImmutableSet.of(V400H1, VNONEH3));
92 vplsNeighbourHandler.vplsStore.addVpls(vplsData);
93
94 vplsNeighbourHandler.activate();
95
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -070096 }
97
98 @After
99 public void tearDown() {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700100 vplsNeighbourHandler.deactivate();
101 }
102
103 /**
104 * Registers neighbour handler to all available interfaces.
105 */
106 @Test
107 public void testConfigNeighbourHandler() {
108 vplsNeighbourHandler.configNeighbourHandler();
109 assertEquals(9, vplsNeighbourHandler.neighbourService.getHandlerRegistrations().size());
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700110 }
111
112 /**
Luca Prete092e8952016-10-26 16:25:56 +0200113 * Sends request messages to all hosts in VPLS 1.
114 * Request messages should be received from other hosts in VPLS 1.
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700115 */
116 @Test
Luca Prete092e8952016-10-26 16:25:56 +0200117 public void vpls1RequestMessage() {
118 // Request messages from v100h1 (VPLS 1) should be received by v200h1 and v300h1
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700119 TestMessageContext requestMessage =
Yi Tsengf4e13e32017-03-30 15:38:39 -0700120 makeBroadcastRequestContext(V100HOST1);
Luca Prete092e8952016-10-26 16:25:56 +0200121 Set<Interface> expectInterfaces = ImmutableSet.of(V200H1, V300H1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700122 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200123 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700124
Luca Prete092e8952016-10-26 16:25:56 +0200125 // Request messages from v200h1 (VPLS 1) should be received by v100h1 and v300h1
Yi Tsengf4e13e32017-03-30 15:38:39 -0700126 requestMessage = makeBroadcastRequestContext(V200HOST1);
Luca Prete092e8952016-10-26 16:25:56 +0200127 expectInterfaces = ImmutableSet.of(V100H1, V300H1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700128 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200129 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700130
Luca Prete092e8952016-10-26 16:25:56 +0200131 // Request from v300h1 (VPLS 1) should be received by v100h1 and v200h1
Yi Tsengf4e13e32017-03-30 15:38:39 -0700132 requestMessage = makeBroadcastRequestContext(V300HOST1);
Luca Prete092e8952016-10-26 16:25:56 +0200133 expectInterfaces = ImmutableSet.of(V100H1, V200H1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700134 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200135 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700136 }
137
138 /**
Luca Prete092e8952016-10-26 16:25:56 +0200139 * Sends request messages to all hosts in VPLS 2.
140 * Request messages should be received from other hosts in VPLS 2.
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700141 */
142 @Test
Luca Prete092e8952016-10-26 16:25:56 +0200143 public void vpls2RequestMessage() {
144 // Request messages from v100h2 (VPLS 2) should be received by v200h2
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700145 TestMessageContext requestMessage =
Yi Tsengf4e13e32017-03-30 15:38:39 -0700146 makeBroadcastRequestContext(V100HOST2);
Luca Prete092e8952016-10-26 16:25:56 +0200147 Set<Interface> expectInterfaces = ImmutableSet.of(V200H2);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700148 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200149 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700150
Luca Prete092e8952016-10-26 16:25:56 +0200151 // Request messages from v200h2 (VPLS 2) should be received by v100h2
Yi Tsengf4e13e32017-03-30 15:38:39 -0700152 requestMessage = makeBroadcastRequestContext(V200HOST2);
Luca Prete092e8952016-10-26 16:25:56 +0200153 expectInterfaces = ImmutableSet.of(V100H2);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700154 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200155 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700156 }
157
158 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100159 * Tests correct connection between untagged interfaces.
160 *
161 * Sends request messages to all hosts in VPLS 3.
162 * Request messages should be received from other hosts in VPLS 3.
163 */
164 @Test
165 public void vpls3RequestMessage() {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700166 // Request messages from VNONEHOST1 (VPLS 3) should be received by VNONEHOST2
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100167 TestMessageContext requestMessage =
Yi Tsengf4e13e32017-03-30 15:38:39 -0700168 makeBroadcastRequestContext(VNONEHOST1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100169 Set<Interface> expectInterfaces = ImmutableSet.of(VNONEH2);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700170 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100171 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
172
173 // Request messages from vNoneh2 (VPLS 3) should be received by vNoneh1
Yi Tsengf4e13e32017-03-30 15:38:39 -0700174 requestMessage = makeBroadcastRequestContext(VNONEHOST2);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100175 expectInterfaces = ImmutableSet.of(VNONEH1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700176 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100177 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
178 }
179
180 /**
181 * Tests correct connection between tagged and untagged interfaces.
182 *
183 * Sends request messages to all hosts in VPLS 4.
184 * Request messages should be received from other hosts in VPLS 4.
185 */
186 @Test
187 public void vpls4RequestMessage() {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700188 // Request messages from V400HOST1 (VPLS 4) should be received by VNONEHOST3
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100189 TestMessageContext requestMessage =
Yi Tsengf4e13e32017-03-30 15:38:39 -0700190 makeBroadcastRequestContext(V400HOST1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100191 Set<Interface> expectInterfaces = ImmutableSet.of(VNONEH3);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700192 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100193 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
194
Yi Tsengf4e13e32017-03-30 15:38:39 -0700195 // Request messages from VNONEHOST3 (VPLS 4) should be received by V400HOST1
196 requestMessage = makeBroadcastRequestContext(VNONEHOST3);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100197 expectInterfaces = ImmutableSet.of(V400H1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700198 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100199 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
200 }
201
202 /**
Luca Prete092e8952016-10-26 16:25:56 +0200203 * Sends reply messages to hosts in VPLS 1.
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700204 * Reply messages should be received by the host with MAC address equal to
205 * the dstMac of the message context.
206 */
207 @Test
Luca Prete092e8952016-10-26 16:25:56 +0200208 public void vpls1ReplyMessage() {
209 // Reply messages from v100h1 (VPLS 1) should be received by v200h1
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700210 TestMessageContext replyMessage =
Yi Tsengf4e13e32017-03-30 15:38:39 -0700211 makeReplyContext(V100HOST1, V200HOST1);
Luca Prete092e8952016-10-26 16:25:56 +0200212 Set<Interface> expectInterfaces = ImmutableSet.of(V200H1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700213 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200214 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700215
Luca Prete092e8952016-10-26 16:25:56 +0200216 // Reply messages from v200h1 (VPLS 1) should be received by v300h1
Yi Tsengf4e13e32017-03-30 15:38:39 -0700217 replyMessage = makeReplyContext(V200HOST1, V300HOST1);
Luca Prete092e8952016-10-26 16:25:56 +0200218 expectInterfaces = ImmutableSet.of(V300H1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700219 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200220 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700221
Luca Prete092e8952016-10-26 16:25:56 +0200222 // Reply messages from v300h1 (VPLS 1) should be received by v100h1
Yi Tsengf4e13e32017-03-30 15:38:39 -0700223 replyMessage = makeReplyContext(V300HOST1, V100HOST1);
Luca Prete092e8952016-10-26 16:25:56 +0200224 expectInterfaces = ImmutableSet.of(V100H1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700225 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200226 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700227 }
228
229 /**
Luca Prete092e8952016-10-26 16:25:56 +0200230 * Sends reply messages to hosts in VPLS 2.
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700231 * Reply messages should be received by the host with MAC address equal to
232 * the dstMac of the message context.
233 */
234 @Test
Luca Prete092e8952016-10-26 16:25:56 +0200235 public void vpls2ReplyMessage() {
236 // Reply messages from v100h2 (VPLS 2) should be received by v200h2
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700237 TestMessageContext replyMessage =
Yi Tsengf4e13e32017-03-30 15:38:39 -0700238 makeReplyContext(V100HOST2, V200HOST2);
Luca Prete092e8952016-10-26 16:25:56 +0200239 Set<Interface> expectInterfaces = ImmutableSet.of(V200H2);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700240 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200241 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700242
Luca Prete092e8952016-10-26 16:25:56 +0200243 // Reply messages from v200h2 (VPLS 2) should be received by v100h2
Yi Tsengf4e13e32017-03-30 15:38:39 -0700244 replyMessage = makeReplyContext(V200HOST2, V100HOST2);
Luca Prete092e8952016-10-26 16:25:56 +0200245 expectInterfaces = ImmutableSet.of(V100H2);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700246 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200247 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700248 }
249
250 /**
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100251 * Sends reply messages to hosts in VPLS 3.
252 * Reply messages should be received by the host with MAC address equal to
253 * the dstMac of the message context.
254 */
255 @Test
256 public void vpls3ReplyMessage() {
257 // Reply messages from vNoneh1 (VPLS 3) should be received by vNoneh2
258 TestMessageContext replyMessage =
Yi Tsengf4e13e32017-03-30 15:38:39 -0700259 makeReplyContext(VNONEHOST1, VNONEHOST2);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100260 Set<Interface> expectInterfaces = ImmutableSet.of(VNONEH2);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700261 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100262 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
263
264 // Reply messages from vNoneh2 (VPLS 3) should be received by vNoneh1
Yi Tsengf4e13e32017-03-30 15:38:39 -0700265 replyMessage = makeReplyContext(VNONEHOST2, VNONEHOST1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100266 expectInterfaces = ImmutableSet.of(VNONEH1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700267 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100268 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
269 }
270
271 /**
272 * Sends reply messages to hosts in VPLS 4.
273 * Reply messages should be received by the host with MAC address equal to
274 * the dstMac of the message context.
275 */
276 @Test
277 public void vpls4ReplyMessage() {
278 // Reply messages from v400h1 (VPLS 4) should be received by vNoneh3
279 TestMessageContext replyMessage =
Yi Tsengf4e13e32017-03-30 15:38:39 -0700280 makeReplyContext(V400HOST1, VNONEHOST3);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100281 Set<Interface> expectInterfaces = ImmutableSet.of(VNONEH3);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700282 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100283 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
284
285 // Reply messages from vNoneh3 (VPLS 4) should be received by v400h1
Yi Tsengf4e13e32017-03-30 15:38:39 -0700286 replyMessage = makeReplyContext(VNONEHOST3, V400HOST1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100287 expectInterfaces = ImmutableSet.of(V400H1);
Yi Tsengf4e13e32017-03-30 15:38:39 -0700288 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100289 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
290 }
291
292 /**
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700293 * Sends wrong reply messages to hosts.
294 * The source and the destination MAC addresses are not set on any host of the VPLS.
Luca Prete092e8952016-10-26 16:25:56 +0200295 * The reply messages will not be received by any hosts.
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700296 */
297 @Test
Luca Prete092e8952016-10-26 16:25:56 +0200298 public void wrongReplyMessage() {
299 // Reply message from v100h1 (VPLS 1) to v100h2 (VPLS 2).
300 // Forward results should be empty
Yi Tsengf4e13e32017-03-30 15:38:39 -0700301 TestMessageContext replyMessage = makeReplyContext(V100HOST1, V100HOST2);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700302 Set<Interface> expectInterfaces = ImmutableSet.of();
Yi Tsengf4e13e32017-03-30 15:38:39 -0700303 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200304 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700305
Luca Prete092e8952016-10-26 16:25:56 +0200306 // Reply message from v200h2 (VPLS 2) to v300h1 (VPLS 1).
307 // Forward results should be empty
Yi Tsengf4e13e32017-03-30 15:38:39 -0700308 replyMessage = makeReplyContext(V200HOST2, V300HOST1);
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700309 expectInterfaces = ImmutableSet.of();
Yi Tsengf4e13e32017-03-30 15:38:39 -0700310 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Luca Prete092e8952016-10-26 16:25:56 +0200311 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100312
313 // Reply message from vNoneh1 (VPLS 3) to v400h1 (VPLS 4).
314 // Forward results should be empty
Yi Tsengf4e13e32017-03-30 15:38:39 -0700315 replyMessage = makeReplyContext(VNONEHOST1, V400HOST1);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100316 expectInterfaces = ImmutableSet.of();
Yi Tsengf4e13e32017-03-30 15:38:39 -0700317 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100318 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
319
320 // Reply message from vNoneh3 (VPLS 4) to vNoneH2 (VPLS 3).
321 // Forward results should be empty
Yi Tsengf4e13e32017-03-30 15:38:39 -0700322 replyMessage = makeReplyContext(VNONEHOST3, VNONEHOST2);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100323 expectInterfaces = ImmutableSet.of();
Yi Tsengf4e13e32017-03-30 15:38:39 -0700324 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
Carolina Fernandezb1cef5c2016-11-22 19:18:40 +0100325 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
Luca Prete092e8952016-10-26 16:25:56 +0200326 }
327
328 /**
Yi Tsengf4e13e32017-03-30 15:38:39 -0700329 * Sends reply and request message from a host which not related to any VPLS.
Luca Prete092e8952016-10-26 16:25:56 +0200330 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700331 @Test
332 public void testVplsNotfound() {
333 TestMessageContext replyMessage = makeReplyContext(V300HOST2, V100HOST1);
334 Set<Interface> expectInterfaces = ImmutableSet.of();
335 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(replyMessage);
336 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults);
337 assertTrue(replyMessage.dropped());
338
339 TestMessageContext requestMessage = makeBroadcastRequestContext(V300HOST2);
340 ((TestNeighbourService) vplsNeighbourHandler.neighbourService).sendNeighourMessage(requestMessage);
341 assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults);
342 assertTrue(requestMessage.dropped());
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700343 }
344
Yi Tsengf4e13e32017-03-30 15:38:39 -0700345 /**
346 * Generates broadcast request message context by given source host.
347 *
348 * @param host the source host
349 * @return the request message context
350 */
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700351 private TestMessageContext makeBroadcastRequestContext(Host host) {
352 return new TestMessageContext(host.location(),
353 host.mac(),
354 MacAddress.BROADCAST,
355 host.vlan(),
356 NeighbourMessageType.REQUEST);
357 }
358
Yi Tsengf4e13e32017-03-30 15:38:39 -0700359 /**
360 * Generates reply message context by given source and destination host.
361 *
362 * @param src the source host
363 * @param dst the destination host
364 * @return the reply message context
365 */
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700366 private TestMessageContext makeReplyContext(Host src, Host dst) {
367 return new TestMessageContext(src.location(),
368 src.mac(),
369 dst.mac(),
370 src.vlan(),
371 NeighbourMessageType.REPLY);
372 }
373
Yi Tsengf4e13e32017-03-30 15:38:39 -0700374 /**
375 * Test message context.
376 */
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700377 private class TestMessageContext implements NeighbourMessageContext {
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700378 private final NeighbourMessageType type;
379 private final MacAddress srcMac;
380 private final MacAddress dstMac;
381 private final ConnectPoint inPort;
382 private final VlanId vlanId;
Yi Tsengf4e13e32017-03-30 15:38:39 -0700383 private boolean dropped = false;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700384 public Set<Interface> forwardResults;
385
386 /**
387 * Creates new neighbour message context for test.
388 *
389 * @param inPort the input port
390 * @param srcMac the source Mac
391 * @param dstMac the destination Mac
392 * @param vlanId the VLAN Id
393 * @param type the message context type
394 */
395 public TestMessageContext(
396 ConnectPoint inPort,
397 MacAddress srcMac,
398 MacAddress dstMac,
399 VlanId vlanId,
400 NeighbourMessageType type) {
401
402 this.inPort = inPort;
403 this.srcMac = srcMac;
404 this.dstMac = dstMac;
405 this.vlanId = vlanId;
406 this.type = type;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700407 this.forwardResults = Sets.newHashSet();
Yi Tsengf4e13e32017-03-30 15:38:39 -0700408 this.dropped = false;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700409 }
Luca Prete092e8952016-10-26 16:25:56 +0200410
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700411 @Override
412 public ConnectPoint inPort() {
413 return inPort;
414 }
415
416 @Override
417 public NeighbourMessageType type() {
418 return type;
419 }
420
421 @Override
422 public VlanId vlan() {
423 return vlanId;
424 }
425
426 @Override
427 public MacAddress srcMac() {
428 return srcMac;
429 }
430
431 @Override
432 public MacAddress dstMac() {
433 return dstMac;
434 }
435
436 @Override
437 public IpAddress target() {
438 return null;
439 }
440
441 @Override
442 public IpAddress sender() {
443 return null;
444 }
445
446 @Override
447 public void forward(ConnectPoint outPort) {
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700448 }
449
Yi Tsengf4e13e32017-03-30 15:38:39 -0700450 /**
451 * Records all forward network interface information.
452 * @param outIntf output interface
453 */
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700454 @Override
455 public void forward(Interface outIntf) {
456 forwardResults.add(outIntf);
457 }
458
459 @Override
460 public void reply(MacAddress targetMac) {
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700461 }
462
463 @Override
464 public void flood() {
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700465 }
466
467 @Override
468 public void drop() {
Yi Tsengf4e13e32017-03-30 15:38:39 -0700469 this.dropped = true;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700470 }
471
472 @Override
473 public Ethernet packet() {
474 return null;
475 }
476
477 @Override
478 public NeighbourProtocol protocol() {
479 return null;
480 }
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700481
Yi Tsengf4e13e32017-03-30 15:38:39 -0700482 public boolean dropped() {
483 return dropped;
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700484 }
485 }
486
Yi Tsengf4e13e32017-03-30 15:38:39 -0700487 /**
488 * Test neighbour service; records all registrations between neighbour
489 * message handler and interfaces.
490 */
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700491 private class TestNeighbourService implements NeighbourResolutionService {
492 private SetMultimap<ConnectPoint, NeighbourHandlerRegistration> handlerRegs;
493
494 public TestNeighbourService() {
495 handlerRegs = HashMultimap.create();
496 }
497
498 @Override
499 public void registerNeighbourHandler(ConnectPoint connectPoint,
500 NeighbourMessageHandler handler,
501 ApplicationId appId) {
502 Interface intf =
503 new Interface(null, connectPoint, null, null, null);
504
505 NeighbourHandlerRegistration reg =
506 new HandlerRegistration(handler, intf, appId);
507
508 handlerRegs.put(connectPoint, reg);
509 }
510
511 @Override
512 public void registerNeighbourHandler(Interface intf,
513 NeighbourMessageHandler handler,
514 ApplicationId appId) {
515 NeighbourHandlerRegistration reg =
516 new HandlerRegistration(handler, intf, appId);
517 handlerRegs.put(intf.connectPoint(), reg);
518 }
519
520 @Override
521 public void unregisterNeighbourHandler(ConnectPoint connectPoint,
522 NeighbourMessageHandler handler,
523 ApplicationId appId) {
524 handlerRegs.removeAll(connectPoint);
525 }
526
527 @Override
528 public void unregisterNeighbourHandler(Interface intf,
529 NeighbourMessageHandler handler,
530 ApplicationId appId) {
531 handlerRegs.removeAll(intf.connectPoint());
532 }
533
534 @Override
535 public void unregisterNeighbourHandlers(ApplicationId appId) {
536 handlerRegs.clear();
537 }
538
539 @Override
540 public Map<ConnectPoint, Collection<NeighbourHandlerRegistration>> getHandlerRegistrations() {
541 return handlerRegs.asMap();
542 }
543
Yi Tsengf4e13e32017-03-30 15:38:39 -0700544 /**
545 * Sends neighbour message context to all handler which related to the
546 * context.
547 *
548 * @param context the neighbour message context
549 */
550 public void sendNeighourMessage(NeighbourMessageContext context) {
551 ConnectPoint connectPoint = context.inPort();
552 VlanId vlanId = context.vlan();
553 Collection<NeighbourHandlerRegistration> registrations = handlerRegs.get(connectPoint);
554 registrations.forEach(reg -> {
555 if (reg.intf().vlan().equals(vlanId)) {
556 reg.handler().handleMessage(context, hostService);
557 }
558 });
559 }
560
561 /**
562 * Test handler registration.
563 */
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700564 private class HandlerRegistration implements NeighbourHandlerRegistration {
565 private final Interface intf;
566 private final NeighbourMessageHandler handler;
567 private final ApplicationId appId;
568
569 /**
570 * Creates a new registration handler.
571 *
572 * @param handler the neighbour message handler
573 * @param intf the interface
574 */
575 public HandlerRegistration(NeighbourMessageHandler handler,
576 Interface intf,
577 ApplicationId appId) {
578 this.intf = intf;
579 this.handler = handler;
580 this.appId = appId;
581 }
582
583 @Override
584 public Interface intf() {
585 return intf;
586 }
587
588 @Override
589 public NeighbourMessageHandler handler() {
590 return handler;
591 }
592
593 @Override
594 public ApplicationId appId() {
595 return appId;
596 }
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700597 }
598 }
Yong-hwan Kimfbf653c2016-09-21 10:05:22 -0700599}