Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 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.HashMultimap; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.ImmutableSetMultimap; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 23 | import com.google.common.collect.SetMultimap; |
| 24 | import com.google.common.collect.Sets; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.packet.Ethernet; |
| 29 | import org.onlab.packet.IpAddress; |
| 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onlab.packet.VlanId; |
| 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.incubator.net.intf.Interface; |
| 34 | import org.onosproject.incubator.net.intf.InterfaceListener; |
| 35 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 36 | import org.onosproject.incubator.net.neighbour.NeighbourHandlerRegistration; |
| 37 | import org.onosproject.incubator.net.neighbour.NeighbourMessageContext; |
| 38 | import org.onosproject.incubator.net.neighbour.NeighbourMessageHandler; |
| 39 | import org.onosproject.incubator.net.neighbour.NeighbourMessageType; |
| 40 | import org.onosproject.incubator.net.neighbour.NeighbourProtocol; |
| 41 | import org.onosproject.incubator.net.neighbour.NeighbourResolutionService; |
| 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.DefaultHost; |
| 44 | import org.onosproject.net.DeviceId; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 45 | import org.onosproject.net.EncapsulationType; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 46 | import org.onosproject.net.Host; |
| 47 | import org.onosproject.net.HostId; |
| 48 | import org.onosproject.net.HostLocation; |
| 49 | import org.onosproject.net.PortNumber; |
| 50 | import org.onosproject.net.host.HostService; |
| 51 | import org.onosproject.net.host.HostServiceAdapter; |
| 52 | import org.onosproject.net.provider.ProviderId; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 53 | |
| 54 | import java.util.Collection; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 55 | import java.util.HashMap; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 56 | import java.util.Map; |
| 57 | import java.util.Set; |
| 58 | import java.util.stream.Collectors; |
| 59 | |
| 60 | import static junit.framework.TestCase.assertEquals; |
| 61 | |
| 62 | /** |
| 63 | * Tests the the {@link VplsNeighbourHandler} class. |
| 64 | */ |
| 65 | public class VplsNeighbourHandlerTest { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 66 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 67 | private static final String IFACES_NOT_EXPECTED = |
| 68 | "The interfaces reached by the packet are not equal to the " + |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 69 | "interfaces expected."; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 70 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 71 | private static final DeviceId DID1 = getDeviceId(1); |
| 72 | private static final DeviceId DID2 = getDeviceId(2); |
| 73 | private static final DeviceId DID3 = getDeviceId(3); |
| 74 | private static final DeviceId DID4 = getDeviceId(4); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 75 | private static final DeviceId DID5 = getDeviceId(5); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 76 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 77 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 78 | private static final PortNumber P2 = PortNumber.portNumber(2); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 79 | private static final PortNumber P3 = PortNumber.portNumber(3); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 80 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 81 | private static final ConnectPoint OF1P1 = new ConnectPoint(DID1, P1); |
| 82 | private static final ConnectPoint OF2P1 = new ConnectPoint(DID2, P1); |
| 83 | private static final ConnectPoint OF3P1 = new ConnectPoint(DID3, P1); |
| 84 | private static final ConnectPoint OF4P1 = new ConnectPoint(DID4, P1); |
| 85 | private static final ConnectPoint OF4P2 = new ConnectPoint(DID4, P2); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 86 | private static final ConnectPoint OF4P3 = new ConnectPoint(DID4, P3); |
| 87 | private static final ConnectPoint OF5P1 = new ConnectPoint(DID5, P1); |
| 88 | private static final ConnectPoint OF5P2 = new ConnectPoint(DID5, P2); |
| 89 | private static final ConnectPoint OF5P3 = new ConnectPoint(DID5, P3); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 90 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 91 | private static final String VPLS1 = "vpls1"; |
| 92 | private static final String VPLS2 = "vpls2"; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 93 | private static final String VPLS3 = "vpls3"; |
| 94 | private static final String VPLS4 = "vpls4"; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 95 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 96 | private static final VlanId VLAN100 = VlanId.vlanId("100"); |
| 97 | private static final VlanId VLAN200 = VlanId.vlanId("200"); |
| 98 | private static final VlanId VLAN300 = VlanId.vlanId("300"); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 99 | private static final VlanId VLAN400 = VlanId.vlanId("400"); |
| 100 | private static final VlanId VLAN_NONE = VlanId.NONE; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 101 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 102 | private static final Interface V100H1 = |
| 103 | new Interface("v100h1", OF1P1, null, null, VLAN100); |
| 104 | private static final Interface V100H2 = |
| 105 | new Interface("v100h2", OF4P1, null, null, VLAN100); |
| 106 | private static final Interface V200H1 = |
| 107 | new Interface("v200h1", OF4P2, null, null, VLAN200); |
| 108 | private static final Interface V200H2 = |
| 109 | new Interface("v200h2", OF2P1, null, null, VLAN200); |
| 110 | private static final Interface V300H1 = |
| 111 | new Interface("v300h1", OF3P1, null, null, VLAN300); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 112 | private static final Interface V400H1 = |
| 113 | new Interface("v400h1", OF5P1, null, null, VLAN400); |
| 114 | private static final Interface VNONEH1 = |
| 115 | new Interface("vNoneh1", OF5P2, null, null, VLAN_NONE); |
| 116 | private static final Interface VNONEH2 = |
| 117 | new Interface("vNoneh2", OF5P3, null, null, VLAN_NONE); |
| 118 | private static final Interface VNONEH3 = |
| 119 | new Interface("vNoneh3", OF4P3, null, null, VLAN_NONE); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 120 | |
| 121 | private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 122 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 123 | private static final MacAddress MAC3 = MacAddress.valueOf("00:00:00:00:00:03"); |
| 124 | private static final MacAddress MAC4 = MacAddress.valueOf("00:00:00:00:00:04"); |
| 125 | private static final MacAddress MAC5 = MacAddress.valueOf("00:00:00:00:00:05"); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 126 | private static final MacAddress MAC6 = MacAddress.valueOf("00:00:00:00:00:06"); |
| 127 | private static final MacAddress MAC7 = MacAddress.valueOf("00:00:00:00:00:07"); |
| 128 | private static final MacAddress MAC8 = MacAddress.valueOf("00:00:00:00:00:08"); |
| 129 | private static final MacAddress MAC9 = MacAddress.valueOf("00:00:00:00:00:09"); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 130 | |
| 131 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 132 | |
| 133 | private final Host v100Host1 = makeHost(MAC1, VLAN100, OF1P1); |
| 134 | private final Host v100Host2 = makeHost(MAC2, VLAN100, OF4P1); |
| 135 | private final Host v200Host1 = makeHost(MAC3, VLAN200, OF4P2); |
| 136 | private final Host v200Host2 = makeHost(MAC5, VLAN200, OF2P1); |
| 137 | private final Host v300Host1 = makeHost(MAC4, VLAN300, OF3P1); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 138 | private final Host v400Host1 = makeHost(MAC6, VLAN400, OF5P1); |
| 139 | private final Host vNoneHost1 = makeHost(MAC7, VLAN_NONE, OF5P2); |
| 140 | private final Host vNoneHost2 = makeHost(MAC8, VLAN_NONE, OF5P3); |
| 141 | private final Host vNoneHost3 = makeHost(MAC9, VLAN_NONE, OF4P3); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 142 | |
| 143 | private final Set<Host> availableHosts = ImmutableSet.of(v100Host1, |
| 144 | v100Host2, |
| 145 | v200Host1, |
| 146 | v300Host1, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 147 | v200Host2, |
| 148 | v400Host1, |
| 149 | vNoneHost1, |
| 150 | vNoneHost2, |
| 151 | vNoneHost3); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 152 | |
| 153 | private final Set<Interface> availableInterfaces = |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 154 | ImmutableSet.of(V100H1, V100H2, V200H1, V200H2, V300H1, |
| 155 | V400H1, VNONEH1, VNONEH2, VNONEH3); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 156 | |
| 157 | private VplsNeighbourHandler vplsNeighbourHandler; |
| 158 | |
| 159 | private HostService hostService; |
| 160 | |
| 161 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 162 | * Sets up 4 VPLS. |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 163 | * VPLS 1 contains 3 hosts: v100h1, v200h1 and v300h1 |
| 164 | * VPLS 2 contains 2 hosts: v100h2, v200h2 |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 165 | * VPLS 3 contains 2 hosts: vNoneh1, vNoneh2 |
| 166 | * VPLS 4 contains 2 hosts: v400h1, vNoneh3 |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 167 | */ |
| 168 | @Before |
| 169 | public void setUp() { |
| 170 | vplsNeighbourHandler = new VplsNeighbourHandler(); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 171 | SetMultimap<String, Interface> ifacesByVpls = |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 172 | HashMultimap.create(); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 173 | ifacesByVpls.put(VPLS1, V100H1); |
| 174 | ifacesByVpls.put(VPLS1, V200H1); |
| 175 | ifacesByVpls.put(VPLS1, V300H1); |
| 176 | ifacesByVpls.put(VPLS2, V100H2); |
| 177 | ifacesByVpls.put(VPLS2, V200H2); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 178 | ifacesByVpls.put(VPLS3, VNONEH1); |
| 179 | ifacesByVpls.put(VPLS3, VNONEH2); |
| 180 | ifacesByVpls.put(VPLS4, V400H1); |
| 181 | ifacesByVpls.put(VPLS4, VNONEH3); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 182 | HashMap<String, EncapsulationType> encap = Maps.newHashMap(); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 183 | vplsNeighbourHandler.vplsConfigService = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 184 | new TestVplsConfigService(ifacesByVpls, encap); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 185 | vplsNeighbourHandler.interfaceService = |
| 186 | new TestInterfaceService(); |
| 187 | vplsNeighbourHandler.neighbourService = |
| 188 | new TestNeighbourService(); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 189 | hostService = new TestHostService(); |
| 190 | } |
| 191 | |
| 192 | @After |
| 193 | public void tearDown() { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 197 | * Sends request messages to all hosts in VPLS 1. |
| 198 | * Request messages should be received from other hosts in VPLS 1. |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 199 | */ |
| 200 | @Test |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 201 | public void vpls1RequestMessage() { |
| 202 | // Request messages from v100h1 (VPLS 1) should be received by v200h1 and v300h1 |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 203 | TestMessageContext requestMessage = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 204 | makeBroadcastRequestContext(v100Host1); |
| 205 | Set<Interface> expectInterfaces = ImmutableSet.of(V200H1, V300H1); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 206 | vplsNeighbourHandler.handleRequest(requestMessage); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 207 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 208 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 209 | // Request messages from v200h1 (VPLS 1) should be received by v100h1 and v300h1 |
| 210 | requestMessage = makeBroadcastRequestContext(v200Host1); |
| 211 | expectInterfaces = ImmutableSet.of(V100H1, V300H1); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 212 | vplsNeighbourHandler.handleRequest(requestMessage); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 213 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 214 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 215 | // Request from v300h1 (VPLS 1) should be received by v100h1 and v200h1 |
| 216 | requestMessage = makeBroadcastRequestContext(v300Host1); |
| 217 | expectInterfaces = ImmutableSet.of(V100H1, V200H1); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 218 | vplsNeighbourHandler.handleRequest(requestMessage); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 219 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 223 | * Sends request messages to all hosts in VPLS 2. |
| 224 | * Request messages should be received from other hosts in VPLS 2. |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 225 | */ |
| 226 | @Test |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 227 | public void vpls2RequestMessage() { |
| 228 | // Request messages from v100h2 (VPLS 2) should be received by v200h2 |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 229 | TestMessageContext requestMessage = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 230 | makeBroadcastRequestContext(v100Host2); |
| 231 | Set<Interface> expectInterfaces = ImmutableSet.of(V200H2); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 232 | vplsNeighbourHandler.handleRequest(requestMessage); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 233 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 234 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 235 | // Request messages from v200h2 (VPLS 2) should be received by v100h2 |
| 236 | requestMessage = makeBroadcastRequestContext(v200Host2); |
| 237 | expectInterfaces = ImmutableSet.of(V100H2); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 238 | vplsNeighbourHandler.handleRequest(requestMessage); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 239 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 243 | * Tests correct connection between untagged interfaces. |
| 244 | * |
| 245 | * Sends request messages to all hosts in VPLS 3. |
| 246 | * Request messages should be received from other hosts in VPLS 3. |
| 247 | */ |
| 248 | @Test |
| 249 | public void vpls3RequestMessage() { |
| 250 | // Request messages from vNoneHost1 (VPLS 3) should be received by vNoneHost2 |
| 251 | TestMessageContext requestMessage = |
| 252 | makeBroadcastRequestContext(vNoneHost1); |
| 253 | Set<Interface> expectInterfaces = ImmutableSet.of(VNONEH2); |
| 254 | vplsNeighbourHandler.handleRequest(requestMessage); |
| 255 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
| 256 | |
| 257 | // Request messages from vNoneh2 (VPLS 3) should be received by vNoneh1 |
| 258 | requestMessage = makeBroadcastRequestContext(vNoneHost2); |
| 259 | expectInterfaces = ImmutableSet.of(VNONEH1); |
| 260 | vplsNeighbourHandler.handleRequest(requestMessage); |
| 261 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Tests correct connection between tagged and untagged interfaces. |
| 266 | * |
| 267 | * Sends request messages to all hosts in VPLS 4. |
| 268 | * Request messages should be received from other hosts in VPLS 4. |
| 269 | */ |
| 270 | @Test |
| 271 | public void vpls4RequestMessage() { |
| 272 | // Request messages from v400Host1 (VPLS 4) should be received by vNoneHost3 |
| 273 | TestMessageContext requestMessage = |
| 274 | makeBroadcastRequestContext(v400Host1); |
| 275 | Set<Interface> expectInterfaces = ImmutableSet.of(VNONEH3); |
| 276 | vplsNeighbourHandler.handleRequest(requestMessage); |
| 277 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
| 278 | |
| 279 | // Request messages from vNoneHost3 (VPLS 4) should be received by v400Host1 |
| 280 | requestMessage = makeBroadcastRequestContext(vNoneHost3); |
| 281 | expectInterfaces = ImmutableSet.of(V400H1); |
| 282 | vplsNeighbourHandler.handleRequest(requestMessage); |
| 283 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, requestMessage.forwardResults); |
| 284 | } |
| 285 | |
| 286 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 287 | * Sends reply messages to hosts in VPLS 1. |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 288 | * Reply messages should be received by the host with MAC address equal to |
| 289 | * the dstMac of the message context. |
| 290 | */ |
| 291 | @Test |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 292 | public void vpls1ReplyMessage() { |
| 293 | // Reply messages from v100h1 (VPLS 1) should be received by v200h1 |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 294 | TestMessageContext replyMessage = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 295 | makeReplyContext(v100Host1, v200Host1); |
| 296 | Set<Interface> expectInterfaces = ImmutableSet.of(V200H1); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 297 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 298 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 299 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 300 | // Reply messages from v200h1 (VPLS 1) should be received by v300h1 |
| 301 | replyMessage = makeReplyContext(v200Host1, v300Host1); |
| 302 | expectInterfaces = ImmutableSet.of(V300H1); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 303 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 304 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 305 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 306 | // Reply messages from v300h1 (VPLS 1) should be received by v100h1 |
| 307 | replyMessage = makeReplyContext(v300Host1, v100Host1); |
| 308 | expectInterfaces = ImmutableSet.of(V100H1); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 309 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 310 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 314 | * Sends reply messages to hosts in VPLS 2. |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 315 | * Reply messages should be received by the host with MAC address equal to |
| 316 | * the dstMac of the message context. |
| 317 | */ |
| 318 | @Test |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 319 | public void vpls2ReplyMessage() { |
| 320 | // Reply messages from v100h2 (VPLS 2) should be received by v200h2 |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 321 | TestMessageContext replyMessage = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 322 | makeReplyContext(v100Host2, v200Host2); |
| 323 | Set<Interface> expectInterfaces = ImmutableSet.of(V200H2); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 324 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 325 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 326 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 327 | // Reply messages from v200h2 (VPLS 2) should be received by v100h2 |
| 328 | replyMessage = makeReplyContext(v200Host2, v100Host2); |
| 329 | expectInterfaces = ImmutableSet.of(V100H2); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 330 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 331 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 335 | * Sends reply messages to hosts in VPLS 3. |
| 336 | * Reply messages should be received by the host with MAC address equal to |
| 337 | * the dstMac of the message context. |
| 338 | */ |
| 339 | @Test |
| 340 | public void vpls3ReplyMessage() { |
| 341 | // Reply messages from vNoneh1 (VPLS 3) should be received by vNoneh2 |
| 342 | TestMessageContext replyMessage = |
| 343 | makeReplyContext(vNoneHost1, vNoneHost2); |
| 344 | Set<Interface> expectInterfaces = ImmutableSet.of(VNONEH2); |
| 345 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
| 346 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
| 347 | |
| 348 | // Reply messages from vNoneh2 (VPLS 3) should be received by vNoneh1 |
| 349 | replyMessage = makeReplyContext(vNoneHost2, vNoneHost1); |
| 350 | expectInterfaces = ImmutableSet.of(VNONEH1); |
| 351 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
| 352 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Sends reply messages to hosts in VPLS 4. |
| 357 | * Reply messages should be received by the host with MAC address equal to |
| 358 | * the dstMac of the message context. |
| 359 | */ |
| 360 | @Test |
| 361 | public void vpls4ReplyMessage() { |
| 362 | // Reply messages from v400h1 (VPLS 4) should be received by vNoneh3 |
| 363 | TestMessageContext replyMessage = |
| 364 | makeReplyContext(v400Host1, vNoneHost3); |
| 365 | Set<Interface> expectInterfaces = ImmutableSet.of(VNONEH3); |
| 366 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
| 367 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
| 368 | |
| 369 | // Reply messages from vNoneh3 (VPLS 4) should be received by v400h1 |
| 370 | replyMessage = makeReplyContext(vNoneHost3, v400Host1); |
| 371 | expectInterfaces = ImmutableSet.of(V400H1); |
| 372 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
| 373 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
| 374 | } |
| 375 | |
| 376 | /** |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 377 | * Sends wrong reply messages to hosts. |
| 378 | * The source and the destination MAC addresses are not set on any host of the VPLS. |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 379 | * The reply messages will not be received by any hosts. |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 380 | */ |
| 381 | @Test |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 382 | public void wrongReplyMessage() { |
| 383 | // Reply message from v100h1 (VPLS 1) to v100h2 (VPLS 2). |
| 384 | // Forward results should be empty |
| 385 | TestMessageContext replyMessage = makeReplyContext(v100Host1, v100Host2); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 386 | Set<Interface> expectInterfaces = ImmutableSet.of(); |
| 387 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 388 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 389 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 390 | // Reply message from v200h2 (VPLS 2) to v300h1 (VPLS 1). |
| 391 | // Forward results should be empty |
| 392 | replyMessage = makeReplyContext(v200Host2, v300Host1); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 393 | expectInterfaces = ImmutableSet.of(); |
| 394 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 395 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 396 | |
| 397 | // Reply message from vNoneh1 (VPLS 3) to v400h1 (VPLS 4). |
| 398 | // Forward results should be empty |
| 399 | replyMessage = makeReplyContext(vNoneHost1, v400Host1); |
| 400 | expectInterfaces = ImmutableSet.of(); |
| 401 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
| 402 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
| 403 | |
| 404 | // Reply message from vNoneh3 (VPLS 4) to vNoneH2 (VPLS 3). |
| 405 | // Forward results should be empty |
| 406 | replyMessage = makeReplyContext(vNoneHost3, vNoneHost2); |
| 407 | expectInterfaces = ImmutableSet.of(); |
| 408 | vplsNeighbourHandler.handleReply(replyMessage, hostService); |
| 409 | assertEquals(IFACES_NOT_EXPECTED, expectInterfaces, replyMessage.forwardResults); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Returns the device Id of the ith device. |
| 414 | * |
| 415 | * @param i the device to get the Id of |
| 416 | * @return the device Id |
| 417 | */ |
| 418 | private static DeviceId getDeviceId(int i) { |
| 419 | return DeviceId.deviceId("" + i); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | private Host makeHost(MacAddress mac, VlanId vlan, ConnectPoint cp) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 423 | return new DefaultHost(PID, |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 424 | HostId.hostId(mac, vlan), |
| 425 | mac, |
| 426 | vlan, |
| 427 | new HostLocation(cp, 0), |
| 428 | Sets.newHashSet()); |
| 429 | } |
| 430 | |
| 431 | private TestMessageContext makeBroadcastRequestContext(Host host) { |
| 432 | return new TestMessageContext(host.location(), |
| 433 | host.mac(), |
| 434 | MacAddress.BROADCAST, |
| 435 | host.vlan(), |
| 436 | NeighbourMessageType.REQUEST); |
| 437 | } |
| 438 | |
| 439 | private TestMessageContext makeReplyContext(Host src, Host dst) { |
| 440 | return new TestMessageContext(src.location(), |
| 441 | src.mac(), |
| 442 | dst.mac(), |
| 443 | src.vlan(), |
| 444 | NeighbourMessageType.REPLY); |
| 445 | } |
| 446 | |
| 447 | private class TestMessageContext implements NeighbourMessageContext { |
| 448 | |
| 449 | |
| 450 | private final NeighbourMessageType type; |
| 451 | private final MacAddress srcMac; |
| 452 | private final MacAddress dstMac; |
| 453 | private final ConnectPoint inPort; |
| 454 | private final VlanId vlanId; |
| 455 | |
| 456 | public Set<Interface> forwardResults; |
| 457 | |
| 458 | /** |
| 459 | * Creates new neighbour message context for test. |
| 460 | * |
| 461 | * @param inPort the input port |
| 462 | * @param srcMac the source Mac |
| 463 | * @param dstMac the destination Mac |
| 464 | * @param vlanId the VLAN Id |
| 465 | * @param type the message context type |
| 466 | */ |
| 467 | public TestMessageContext( |
| 468 | ConnectPoint inPort, |
| 469 | MacAddress srcMac, |
| 470 | MacAddress dstMac, |
| 471 | VlanId vlanId, |
| 472 | NeighbourMessageType type) { |
| 473 | |
| 474 | this.inPort = inPort; |
| 475 | this.srcMac = srcMac; |
| 476 | this.dstMac = dstMac; |
| 477 | this.vlanId = vlanId; |
| 478 | this.type = type; |
| 479 | |
| 480 | this.forwardResults = Sets.newHashSet(); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 481 | } |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 482 | |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 483 | @Override |
| 484 | public ConnectPoint inPort() { |
| 485 | return inPort; |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public NeighbourMessageType type() { |
| 490 | return type; |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public VlanId vlan() { |
| 495 | return vlanId; |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public MacAddress srcMac() { |
| 500 | return srcMac; |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public MacAddress dstMac() { |
| 505 | return dstMac; |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public IpAddress target() { |
| 510 | return null; |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public IpAddress sender() { |
| 515 | return null; |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public void forward(ConnectPoint outPort) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | @Override |
| 523 | public void forward(Interface outIntf) { |
| 524 | forwardResults.add(outIntf); |
| 525 | } |
| 526 | |
| 527 | @Override |
| 528 | public void reply(MacAddress targetMac) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | @Override |
| 532 | public void flood() { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | @Override |
| 536 | public void drop() { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public Ethernet packet() { |
| 541 | return null; |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public NeighbourProtocol protocol() { |
| 546 | return null; |
| 547 | } |
| 548 | } |
| 549 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 550 | private class TestVplsConfigService extends VplsConfigServiceAdapter { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 551 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 552 | private final SetMultimap<String, Interface> ifacesByVplsName; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 553 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 554 | public TestVplsConfigService(SetMultimap<String, Interface> ifacesByVplsName, |
| 555 | HashMap<String, EncapsulationType> encapByVplsName) { |
| 556 | this.ifacesByVplsName = ifacesByVplsName; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 560 | public void addVpls(String vplsName, Set<String> ifaceNames, String encap) { |
| 561 | if (!ifacesByVplsName.containsKey(vplsName)) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 562 | ifaceNames.forEach(ifaceName -> { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 563 | availableInterfaces.forEach(iface -> { |
| 564 | if (iface.name().equals(ifaceName)) { |
| 565 | ifacesByVplsName.put(vplsName, iface); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 566 | } |
| 567 | }); |
| 568 | }); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 573 | public void removeVpls(String vplsName) { |
| 574 | if (ifacesByVplsName.containsKey(vplsName)) { |
| 575 | ifacesByVplsName.removeAll(vplsName); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 580 | public void addIface(String vplsName, String ifaceName) { |
| 581 | availableInterfaces.forEach(intf -> { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 582 | if (intf.name().equals(ifaceName)) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 583 | ifacesByVplsName.put(vplsName, intf); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 584 | } |
| 585 | }); |
| 586 | } |
| 587 | |
| 588 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 589 | public void removeIface(String ifaceName) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 590 | SetMultimap<String, Interface> toBeRemoved = HashMultimap.create(); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 591 | ifacesByVplsName.entries().forEach(e -> { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 592 | if (e.getValue().name().equals(ifaceName)) { |
| 593 | toBeRemoved.put(e.getKey(), e.getValue()); |
| 594 | } |
| 595 | }); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 596 | toBeRemoved.entries() |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 597 | .forEach(e -> ifacesByVplsName.remove(e.getKey(), |
| 598 | e.getValue())); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 602 | public void cleanVplsConfig() { |
| 603 | ifacesByVplsName.clear(); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 607 | public Set<Interface> allIfaces() { |
| 608 | return ImmutableSet.copyOf(ifacesByVplsName.values()); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 612 | public Set<Interface> ifaces(String name) { |
| 613 | return ifacesByVplsName.get(name) |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 614 | .stream() |
| 615 | .collect(Collectors.toSet()); |
| 616 | } |
| 617 | |
| 618 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 619 | public Set<String> vplsNames() { |
| 620 | return ifacesByVplsName.keySet(); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 624 | public SetMultimap<String, Interface> ifacesByVplsName() { |
| 625 | return ImmutableSetMultimap.copyOf(ifacesByVplsName); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 629 | public SetMultimap<String, Interface> ifacesByVplsName(VlanId vlan, |
| 630 | ConnectPoint connectPoint) { |
| 631 | String vplsName = |
| 632 | ifacesByVplsName.entries().stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 633 | .filter(e -> e.getValue().connectPoint().equals(connectPoint)) |
| 634 | .filter(e -> e.getValue().vlan().equals(vlan)) |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame^] | 635 | .map(Map.Entry::getKey) |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 636 | .findFirst() |
| 637 | .orElse(null); |
| 638 | SetMultimap<String, Interface> result = HashMultimap.create(); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 639 | if (vplsName != null && |
| 640 | ifacesByVplsName.containsKey(vplsName)) { |
| 641 | ifacesByVplsName.get(vplsName) |
| 642 | .forEach(intf -> result.put(vplsName, intf)); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 643 | return result; |
| 644 | } |
| 645 | return null; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | class TestHostService extends HostServiceAdapter { |
| 650 | @Override |
| 651 | public Set<Host> getHostsByMac(MacAddress mac) { |
| 652 | return availableHosts.stream() |
| 653 | .filter(host -> host.mac().equals(mac)) |
| 654 | .collect(Collectors.toSet()); |
| 655 | } |
| 656 | |
| 657 | @Override |
| 658 | public Iterable<Host> getHosts() { |
| 659 | return availableHosts; |
| 660 | } |
| 661 | |
| 662 | @Override |
| 663 | public Set<Host> getHostsByVlan(VlanId vlanId) { |
| 664 | return availableHosts.stream() |
| 665 | .filter(host -> host.vlan().equals(vlanId)) |
| 666 | .collect(Collectors.toSet()); |
| 667 | } |
| 668 | |
| 669 | @Override |
| 670 | public int getHostCount() { |
| 671 | return availableHosts.size(); |
| 672 | } |
| 673 | |
| 674 | @Override |
| 675 | public Host getHost(HostId hostId) { |
| 676 | return availableHosts.stream() |
| 677 | .filter(host -> host.id().equals(hostId)) |
| 678 | .findFirst() |
| 679 | .orElse(null); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | private class TestNeighbourService implements NeighbourResolutionService { |
| 684 | private SetMultimap<ConnectPoint, NeighbourHandlerRegistration> handlerRegs; |
| 685 | |
| 686 | public TestNeighbourService() { |
| 687 | handlerRegs = HashMultimap.create(); |
| 688 | } |
| 689 | |
| 690 | @Override |
| 691 | public void registerNeighbourHandler(ConnectPoint connectPoint, |
| 692 | NeighbourMessageHandler handler, |
| 693 | ApplicationId appId) { |
| 694 | Interface intf = |
| 695 | new Interface(null, connectPoint, null, null, null); |
| 696 | |
| 697 | NeighbourHandlerRegistration reg = |
| 698 | new HandlerRegistration(handler, intf, appId); |
| 699 | |
| 700 | handlerRegs.put(connectPoint, reg); |
| 701 | } |
| 702 | |
| 703 | @Override |
| 704 | public void registerNeighbourHandler(Interface intf, |
| 705 | NeighbourMessageHandler handler, |
| 706 | ApplicationId appId) { |
| 707 | NeighbourHandlerRegistration reg = |
| 708 | new HandlerRegistration(handler, intf, appId); |
| 709 | handlerRegs.put(intf.connectPoint(), reg); |
| 710 | } |
| 711 | |
| 712 | @Override |
| 713 | public void unregisterNeighbourHandler(ConnectPoint connectPoint, |
| 714 | NeighbourMessageHandler handler, |
| 715 | ApplicationId appId) { |
| 716 | handlerRegs.removeAll(connectPoint); |
| 717 | } |
| 718 | |
| 719 | @Override |
| 720 | public void unregisterNeighbourHandler(Interface intf, |
| 721 | NeighbourMessageHandler handler, |
| 722 | ApplicationId appId) { |
| 723 | handlerRegs.removeAll(intf.connectPoint()); |
| 724 | } |
| 725 | |
| 726 | @Override |
| 727 | public void unregisterNeighbourHandlers(ApplicationId appId) { |
| 728 | handlerRegs.clear(); |
| 729 | } |
| 730 | |
| 731 | @Override |
| 732 | public Map<ConnectPoint, Collection<NeighbourHandlerRegistration>> getHandlerRegistrations() { |
| 733 | return handlerRegs.asMap(); |
| 734 | } |
| 735 | |
| 736 | private class HandlerRegistration implements NeighbourHandlerRegistration { |
| 737 | private final Interface intf; |
| 738 | private final NeighbourMessageHandler handler; |
| 739 | private final ApplicationId appId; |
| 740 | |
| 741 | /** |
| 742 | * Creates a new registration handler. |
| 743 | * |
| 744 | * @param handler the neighbour message handler |
| 745 | * @param intf the interface |
| 746 | */ |
| 747 | public HandlerRegistration(NeighbourMessageHandler handler, |
| 748 | Interface intf, |
| 749 | ApplicationId appId) { |
| 750 | this.intf = intf; |
| 751 | this.handler = handler; |
| 752 | this.appId = appId; |
| 753 | } |
| 754 | |
| 755 | @Override |
| 756 | public Interface intf() { |
| 757 | return intf; |
| 758 | } |
| 759 | |
| 760 | @Override |
| 761 | public NeighbourMessageHandler handler() { |
| 762 | return handler; |
| 763 | } |
| 764 | |
| 765 | @Override |
| 766 | public ApplicationId appId() { |
| 767 | return appId; |
| 768 | } |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
| 772 | class TestInterfaceService implements InterfaceService { |
| 773 | |
| 774 | @Override |
| 775 | public void addListener(InterfaceListener listener) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | @Override |
| 779 | public void removeListener(InterfaceListener listener) { |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | @Override |
| 783 | public Set<Interface> getInterfaces() { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 784 | return availableInterfaces; |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | @Override |
| 788 | public Interface getInterfaceByName(ConnectPoint connectPoint, |
| 789 | String name) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 790 | return availableInterfaces.stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 791 | .filter(intf -> intf.name().equals(name)) |
| 792 | .findFirst() |
| 793 | .orElse(null); |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | @Override |
| 797 | public Set<Interface> getInterfacesByPort(ConnectPoint port) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 798 | return availableInterfaces.stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 799 | .filter(intf -> intf.connectPoint().equals(port)) |
| 800 | .collect(Collectors.toSet()); |
| 801 | } |
| 802 | |
| 803 | @Override |
| 804 | public Set<Interface> getInterfacesByIp(IpAddress ip) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 805 | return availableInterfaces.stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 806 | .filter(intf -> intf.ipAddressesList().contains(ip)) |
| 807 | .collect(Collectors.toSet()); |
| 808 | } |
| 809 | |
| 810 | @Override |
| 811 | public Set<Interface> getInterfacesByVlan(VlanId vlan) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 812 | return availableInterfaces.stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 813 | .filter(intf -> intf.vlan().equals(vlan)) |
| 814 | .collect(Collectors.toSet()); |
| 815 | } |
| 816 | |
| 817 | @Override |
| 818 | public Interface getMatchingInterface(IpAddress ip) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 819 | return availableInterfaces.stream() |
Yong-hwan Kim | fbf653c | 2016-09-21 10:05:22 -0700 | [diff] [blame] | 820 | .filter(intf -> intf.ipAddressesList().contains(ip)) |
| 821 | .findFirst() |
| 822 | .orElse(null); |
| 823 | } |
| 824 | } |
| 825 | } |