Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 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 | package org.onosproject.vpls; |
| 17 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 18 | import com.google.common.collect.HashMultimap; |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSetMultimap; |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import com.google.common.collect.Maps; |
| 23 | import com.google.common.collect.SetMultimap; |
| 24 | import com.google.common.collect.Sets; |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 25 | import org.junit.After; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 26 | import org.junit.Before; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 27 | import org.junit.Test; |
| 28 | import org.onlab.packet.Ip4Address; |
| 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.VlanId; |
| 31 | import org.onosproject.TestApplicationId; |
| 32 | import org.onosproject.app.ApplicationService; |
| 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
| 35 | import org.onosproject.core.IdGenerator; |
| 36 | import org.onosproject.incubator.net.intf.Interface; |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 37 | import org.onosproject.incubator.net.intf.InterfaceListener; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 38 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 39 | import org.onosproject.net.ConnectPoint; |
| 40 | import org.onosproject.net.DefaultHost; |
| 41 | import org.onosproject.net.DeviceId; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 42 | import org.onosproject.net.EncapsulationType; |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 43 | import org.onosproject.net.FilteredConnectPoint; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 44 | import org.onosproject.net.Host; |
| 45 | import org.onosproject.net.HostId; |
| 46 | import org.onosproject.net.HostLocation; |
| 47 | import org.onosproject.net.PortNumber; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 48 | import org.onosproject.net.config.NetworkConfigService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 49 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 50 | import org.onosproject.net.flow.TrafficSelector; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 51 | import org.onosproject.net.flow.criteria.Criterion; |
| 52 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 53 | import org.onosproject.net.host.HostEvent; |
| 54 | import org.onosproject.net.host.HostListener; |
| 55 | import org.onosproject.net.host.HostService; |
| 56 | import org.onosproject.net.host.HostServiceAdapter; |
| 57 | import org.onosproject.net.intent.Intent; |
| 58 | import org.onosproject.net.intent.IntentService; |
| 59 | import org.onosproject.net.intent.IntentServiceAdapter; |
| 60 | import org.onosproject.net.intent.IntentUtils; |
| 61 | import org.onosproject.net.intent.Key; |
| 62 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 63 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
| 64 | import org.onosproject.net.provider.ProviderId; |
| 65 | import org.onosproject.routing.IntentSynchronizationAdminService; |
| 66 | import org.onosproject.routing.IntentSynchronizationService; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 67 | import org.onosproject.vpls.config.VplsConfigService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 68 | |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 69 | import java.util.Collection; |
| 70 | import java.util.Collections; |
| 71 | import java.util.HashMap; |
| 72 | import java.util.HashSet; |
| 73 | import java.util.List; |
| 74 | import java.util.Map; |
| 75 | import java.util.Set; |
| 76 | import java.util.concurrent.atomic.AtomicLong; |
| 77 | import java.util.stream.Collectors; |
| 78 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 79 | import static java.lang.String.format; |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 80 | import static org.easymock.EasyMock.*; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 81 | import static org.junit.Assert.assertEquals; |
| 82 | import static org.junit.Assert.assertTrue; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 83 | import static org.onosproject.net.EncapsulationType.*; |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 84 | import static org.onosproject.vpls.IntentInstaller.*; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 85 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Tests for the {@link Vpls} class. |
| 88 | */ |
| 89 | public class VplsTest { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 90 | private static final String APP_NAME = "org.onosproject.vpls"; |
| 91 | private static final ApplicationId APPID = TestApplicationId.create(APP_NAME); |
| 92 | private static final String DASH = "-"; |
| 93 | private static final int PRIORITY_OFFSET = 1000; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 94 | private static final String VPLS1 = "vpls1"; |
| 95 | private static final String VPLS2 = "vpls2"; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 96 | private static final String VPLS3 = "vpls3"; |
| 97 | private static final String VPLS4 = "vpls4"; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 98 | |
| 99 | private static final PortNumber P1 = PortNumber.portNumber(1); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 100 | private static final PortNumber P2 = PortNumber.portNumber(2); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 101 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 102 | private static final DeviceId DID1 = getDeviceId(1); |
| 103 | private static final DeviceId DID2 = getDeviceId(2); |
| 104 | private static final DeviceId DID3 = getDeviceId(3); |
| 105 | private static final DeviceId DID4 = getDeviceId(4); |
| 106 | private static final DeviceId DID5 = getDeviceId(5); |
| 107 | private static final DeviceId DID6 = getDeviceId(6); |
| 108 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 109 | private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1); |
| 110 | private static final ConnectPoint CP2 = new ConnectPoint(DID2, P1); |
| 111 | private static final ConnectPoint CP3 = new ConnectPoint(DID3, P1); |
| 112 | private static final ConnectPoint CP4 = new ConnectPoint(DID4, P1); |
| 113 | private static final ConnectPoint CP5 = new ConnectPoint(DID5, P1); |
| 114 | private static final ConnectPoint CP6 = new ConnectPoint(DID6, P1); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 115 | private static final ConnectPoint CP7 = new ConnectPoint(DID4, P2); |
| 116 | private static final ConnectPoint CP8 = new ConnectPoint(DID3, P2); |
| 117 | private static final ConnectPoint CP9 = new ConnectPoint(DID5, P1); |
| 118 | private static final ConnectPoint CP10 = new ConnectPoint(DID5, P2); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 119 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 120 | private static final VlanId VLAN100 = VlanId.vlanId((short) 100); |
| 121 | private static final VlanId VLAN200 = VlanId.vlanId((short) 200); |
| 122 | private static final VlanId VLAN300 = VlanId.vlanId((short) 300); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 123 | private static final VlanId VLAN400 = VlanId.vlanId((short) 400); |
| 124 | private static final VlanId VLAN_NONE = VlanId.NONE; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 125 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 126 | private static final MacAddress MAC1 = getMac(1); |
| 127 | private static final MacAddress MAC2 = getMac(2); |
| 128 | private static final MacAddress MAC3 = getMac(3); |
| 129 | private static final MacAddress MAC4 = getMac(4); |
| 130 | private static final MacAddress MAC5 = getMac(5); |
| 131 | private static final MacAddress MAC6 = getMac(6); |
| 132 | private static final MacAddress MAC7 = getMac(7); |
| 133 | private static final MacAddress MAC8 = getMac(8); |
| 134 | private static final MacAddress MAC9 = getMac(9); |
| 135 | private static final MacAddress MAC10 = getMac(10); |
| 136 | private static final MacAddress MAC11 = getMac(11); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 137 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 138 | private static final Ip4Address IP1 = Ip4Address.valueOf("192.168.1.1"); |
| 139 | private static final Ip4Address IP2 = Ip4Address.valueOf("192.168.1.2"); |
| 140 | |
| 141 | private static final HostId HID1 = HostId.hostId(MAC1, VLAN100); |
| 142 | private static final HostId HID2 = HostId.hostId(MAC2, VLAN100); |
| 143 | private static final HostId HID3 = HostId.hostId(MAC3, VLAN200); |
| 144 | private static final HostId HID4 = HostId.hostId(MAC4, VLAN200); |
| 145 | private static final HostId HID5 = HostId.hostId(MAC5, VLAN300); |
| 146 | private static final HostId HID6 = HostId.hostId(MAC6, VLAN300); |
| 147 | private static final HostId HID7 = HostId.hostId(MAC7, VLAN300); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 148 | private static final HostId HID8 = HostId.hostId(MAC8, VLAN400); |
| 149 | private static final HostId HID9 = HostId.hostId(MAC9); |
| 150 | private static final HostId HID10 = HostId.hostId(MAC10); |
| 151 | private static final HostId HID11 = HostId.hostId(MAC11); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 152 | |
| 153 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 154 | |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 155 | private static IdGenerator idGenerator; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 156 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 157 | private static final Interface V100H1 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 158 | new Interface("v100h1", CP1, null, null, VLAN100); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 159 | private static final Interface V100H2 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 160 | new Interface("v100h2", CP2, null, null, VLAN100); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 161 | private static final Interface V200H1 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 162 | new Interface("v200h1", CP3, null, null, VLAN200); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 163 | private static final Interface V200H2 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 164 | new Interface("v200h2", CP4, null, null, VLAN200); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 165 | private static final Interface V300H1 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 166 | new Interface("v300h1", CP5, null, null, VLAN300); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 167 | private static final Interface V300H2 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 168 | new Interface("v300h2", CP6, null, null, VLAN300); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 169 | private static final Interface V400H1 = |
| 170 | new Interface("v400h1", CP7, null, null, VLAN400); |
| 171 | |
| 172 | private static final Interface VNONEH1 = |
| 173 | new Interface("vNoneh1", CP8, null, null, VLAN_NONE); |
| 174 | private static final Interface VNONEH2 = |
| 175 | new Interface("vNoneh2", CP9, null, null, VLAN_NONE); |
| 176 | private static final Interface VNONEH3 = |
| 177 | new Interface("vNoneh3", CP10, null, null, VLAN_NONE); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 178 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 179 | private static final Host V100HOST1 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 180 | new DefaultHost(PID, HID1, MAC1, VLAN100, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 181 | getLocation(1), Collections.singleton(IP1)); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 182 | private static final Host V100HOST2 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 183 | new DefaultHost(PID, HID2, MAC2, VLAN100, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 184 | getLocation(2), Sets.newHashSet()); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 185 | private static final Host V200HOST1 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 186 | new DefaultHost(PID, HID3, MAC3, VLAN200, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 187 | getLocation(3), Collections.singleton(IP2)); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 188 | private static final Host V200HOST2 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 189 | new DefaultHost(PID, HID4, MAC4, VLAN200, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 190 | getLocation(4), Sets.newHashSet()); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 191 | private static final Host V300HOST1 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 192 | new DefaultHost(PID, HID5, MAC5, VLAN300, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 193 | getLocation(5), Sets.newHashSet()); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 194 | private static final Host V300HOST2 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 195 | new DefaultHost(PID, HID6, MAC6, VLAN300, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 196 | getLocation(6), Sets.newHashSet()); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 197 | private static final Host V300HOST3 = |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 198 | new DefaultHost(PID, HID7, MAC7, VLAN300, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 199 | getLocation(7), Sets.newHashSet()); |
| 200 | private static final Host V400HOST1 = |
| 201 | new DefaultHost(PID, HID8, MAC8, VLAN400, |
| 202 | getLocation(4, 2), Sets.newHashSet()); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 203 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 204 | private static final Host VNONEHOST1 = |
| 205 | new DefaultHost(PID, HID9, MAC9, VlanId.NONE, |
| 206 | getLocation(3, 2), Sets.newHashSet()); |
| 207 | private static final Host VNONEHOST2 = |
| 208 | new DefaultHost(PID, HID10, MAC10, VlanId.NONE, |
| 209 | getLocation(5, 1), Sets.newHashSet()); |
| 210 | private static final Host VNONEHOST3 = |
| 211 | new DefaultHost(PID, HID11, MAC11, VlanId.NONE, |
| 212 | getLocation(5, 2), Sets.newHashSet()); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 213 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 214 | private static final Set<Interface> AVAILABLE_INTERFACES = |
| 215 | ImmutableSet.of(V100H1, V100H2, V200H1, V200H2, V300H1, V300H2, |
| 216 | V400H1, VNONEH1, VNONEH2); |
| 217 | |
| 218 | private static final Set<Host> AVAILABLE_HOSTS = |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 219 | ImmutableSet.of(V100HOST1, V100HOST2, V200HOST1, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 220 | V200HOST2, V300HOST1, V300HOST2, V300HOST3, |
| 221 | VNONEHOST1, VNONEHOST2, |
| 222 | V400HOST1, VNONEHOST3); |
| 223 | |
| 224 | private SetMultimap<String, Interface> interfacesByVpls = HashMultimap.create(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 225 | |
| 226 | private ApplicationService applicationService; |
| 227 | private CoreService coreService; |
| 228 | private HostListener hostListener; |
| 229 | private NetworkConfigService configService; |
| 230 | private Set<Host> hostsAvailable; |
| 231 | private HostService hostService; |
| 232 | private IntentService intentService; |
| 233 | private InterfaceService interfaceService; |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 234 | private VplsConfigService vplsConfigService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 235 | private Vpls vpls; |
| 236 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 237 | @Before |
| 238 | public void setUp() throws Exception { |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 239 | idGenerator = new TestIdGenerator(); |
| 240 | Intent.bindIdGenerator(idGenerator); |
| 241 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 242 | applicationService = createMock(ApplicationService.class); |
| 243 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 244 | configService = createMock(NetworkConfigService.class); |
| 245 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 246 | coreService = createMock(CoreService.class); |
| 247 | expect(coreService.registerApplication(APP_NAME)) |
| 248 | .andReturn(APPID); |
| 249 | replay(coreService); |
| 250 | |
| 251 | hostsAvailable = Sets.newHashSet(); |
| 252 | hostService = new TestHostService(hostsAvailable); |
| 253 | |
| 254 | intentService = new TestIntentService(); |
| 255 | |
| 256 | TestIntentSynchronizer intentSynchronizer = |
| 257 | new TestIntentSynchronizer(intentService); |
| 258 | |
| 259 | interfaceService = createMock(InterfaceService.class); |
Luca Prete | a885482 | 2016-04-26 16:30:55 -0700 | [diff] [blame] | 260 | interfaceService.addListener(anyObject(InterfaceListener.class)); |
| 261 | expectLastCall().anyTimes(); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 262 | addIfaceConfig(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 263 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 264 | interfacesByVpls.put(VPLS1, V100H1); |
| 265 | interfacesByVpls.put(VPLS1, V200H1); |
| 266 | interfacesByVpls.put(VPLS1, V300H1); |
| 267 | interfacesByVpls.put(VPLS2, V100H2); |
| 268 | interfacesByVpls.put(VPLS2, V200H2); |
| 269 | interfacesByVpls.put(VPLS2, V300H2); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 270 | interfacesByVpls.put(VPLS3, VNONEH1); |
| 271 | interfacesByVpls.put(VPLS3, VNONEH2); |
| 272 | interfacesByVpls.put(VPLS4, V400H1); |
| 273 | interfacesByVpls.put(VPLS4, VNONEH3); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 274 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 275 | Map<String, EncapsulationType> encapByVpls = new HashMap<>(); |
| 276 | encapByVpls.put(VPLS1, VLAN); |
| 277 | encapByVpls.put(VPLS2, NONE); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 278 | encapByVpls.put(VPLS3, NONE); |
| 279 | encapByVpls.put(VPLS4, NONE); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 280 | |
| 281 | vplsConfigService = new TestVplsConfigService(interfacesByVpls, encapByVpls); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 282 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 283 | vpls = new Vpls(); |
| 284 | vpls.applicationService = applicationService; |
| 285 | vpls.coreService = coreService; |
| 286 | vpls.hostService = hostService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 287 | vpls.vplsConfigService = vplsConfigService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 288 | vpls.intentService = intentService; |
| 289 | vpls.interfaceService = interfaceService; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 290 | vpls.configService = configService; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 291 | vpls.intentSynchronizer = intentSynchronizer; |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 292 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Ray Milkey | 6e968eb | 2016-06-29 09:37:27 -0700 | [diff] [blame] | 295 | @After |
| 296 | public void tearDown() { |
| 297 | Intent.unbindIdGenerator(idGenerator); |
| 298 | } |
| 299 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 300 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 301 | * Creates the interface configuration: |
| 302 | * On devices 1 and 2 is configured an interface on port 1 with vlan 100. |
| 303 | * On device 3 is configured an interface on port 3 with no vlan. |
| 304 | * On devices 3 and 4 is configured an interface on port 1 with vlan 200. |
| 305 | * On device 4 is an interface configured on port 2 with vlan 400. |
| 306 | * On device 5 are configured two interfaces on port 1 and 2 with no vlan. |
| 307 | * On device 5 and 6 is configured an interface on port 1 with vlan 300. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 308 | */ |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 309 | private void addIfaceConfig() { |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 310 | Set<Interface> interfaces = ImmutableSet.copyOf(AVAILABLE_INTERFACES); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 311 | Set<Interface> vlanOneSet = ImmutableSet.of(V100H1, V100H2); |
| 312 | Set<Interface> vlanTwoSet = ImmutableSet.of(V200H1, V200H2); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 313 | Set<Interface> vlanThreeSet = ImmutableSet.of(VNONEH1, VNONEH2); |
| 314 | Set<Interface> vlanFourSet = ImmutableSet.of(V400H1, VNONEH3); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 315 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 316 | AVAILABLE_INTERFACES.forEach(intf -> { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 317 | expect(interfaceService.getInterfacesByPort(intf.connectPoint())) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 318 | .andReturn(Sets.newHashSet(intf)).anyTimes(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 319 | }); |
| 320 | expect(interfaceService.getInterfacesByVlan(VLAN100)) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 321 | .andReturn(vlanOneSet).anyTimes(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 322 | expect(interfaceService.getInterfacesByVlan(VLAN200)) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 323 | .andReturn(vlanTwoSet).anyTimes(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 324 | expect(interfaceService.getInterfacesByVlan(VLAN300)) |
| 325 | .andReturn(vlanThreeSet).anyTimes(); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 326 | expect(interfaceService.getInterfacesByVlan(VLAN400)) |
| 327 | .andReturn(vlanFourSet).anyTimes(); |
| 328 | expect(interfaceService.getInterfacesByVlan(VlanId.NONE)) |
| 329 | .andReturn(vlanFourSet).anyTimes(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 330 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 331 | |
| 332 | replay(interfaceService); |
| 333 | } |
| 334 | |
| 335 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 336 | * Seven ports are configured with VLANs, while three ports are not. No hosts are |
| 337 | * registered by the HostService. |
| 338 | * |
| 339 | * The first three ports have an interface configured on VPLS 1, |
| 340 | * the other three on VPLS 2. Two ports are defined for VPLS 3, while |
| 341 | * the two remaining ports are configured on VPLS 4. |
| 342 | * |
| 343 | * The number of intents expected is 10: three for VPLS 1, three for VPLS 2, |
| 344 | * two for VPLS 3, two for VPLS 4. Eight MP2SP intents. |
| 345 | * Checks if the number of intents submitted to the intent framework is |
| 346 | * equal to the number of intents expected and if all intents are equivalent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 347 | */ |
| 348 | @Test |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 349 | public void activateNoHosts() { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 350 | vpls.activate(); |
| 351 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 352 | List<Intent> expectedIntents = Lists.newArrayList(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 353 | Set<FilteredConnectPoint> fcPoints; |
| 354 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 355 | fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1)); |
| 356 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 357 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 358 | fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2)); |
| 359 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 360 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 361 | fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2)); |
| 362 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE)); |
| 363 | |
| 364 | fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3)); |
| 365 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE)); |
| 366 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 367 | checkIntents(expectedIntents); |
| 368 | } |
| 369 | |
| 370 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 371 | * Ten ports are configured with VLANs and ten hosts are registered by the |
| 372 | * HostService. |
| 373 | * |
| 374 | * The first three ports have an interface configured on VPLS 1, |
| 375 | * the other three on VPLS 2, two on VPLS3 and two on VPLS4. |
| 376 | * |
| 377 | * The number of intents expected is twenty: six |
| 378 | * for VPLS 1, six for VPLS 2. four for VPLS 3, four for VPLS 4. |
| 379 | * That is ten sp2mp intents, ten mp2sp intents. For VPLS 1 |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 380 | * IPs are added to demonstrate this doesn't influence the number of intents |
| 381 | * created. Checks if the number of intents submitted to the intent |
| 382 | * framework is equal to the number of intents expected and if all intents |
| 383 | * are equivalent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 384 | */ |
| 385 | @Test |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 386 | public void tenInterfacesConfiguredHostsPresent() { |
| 387 | hostsAvailable.addAll(AVAILABLE_HOSTS); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 388 | |
| 389 | vpls.activate(); |
| 390 | |
Yi Tseng | 28767f0 | 2016-09-13 04:27:20 -0700 | [diff] [blame] | 391 | List<Intent> expectedIntents = Lists.newArrayList(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 392 | Set<FilteredConnectPoint> fcPoints; |
| 393 | Set<Host> hosts; |
| 394 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 395 | fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1)); |
| 396 | hosts = ImmutableSet.of(V100HOST1, V200HOST1, V300HOST1); |
| 397 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN)); |
| 398 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS1, VLAN)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 399 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 400 | fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2)); |
| 401 | hosts = ImmutableSet.of(V100HOST2, V200HOST2, V300HOST2); |
| 402 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE)); |
| 403 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS2, NONE)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 404 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 405 | fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2)); |
| 406 | hosts = ImmutableSet.of(VNONEHOST1, VNONEHOST2); |
| 407 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE)); |
| 408 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS3, NONE)); |
| 409 | |
| 410 | fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3)); |
| 411 | hosts = ImmutableSet.of(V400HOST1, VNONEHOST3); |
| 412 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE)); |
| 413 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS4, NONE)); |
| 414 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 415 | checkIntents(expectedIntents); |
| 416 | } |
| 417 | |
| 418 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 419 | * Ten ports are configured; seven have VLANs and three do not. |
| 420 | * Initially, no hosts are registered by the HostService. |
| 421 | * |
| 422 | * The first three ports have an interface configured on |
| 423 | * VPLS 1, three have an interface configured on VPLS 2, two have an |
| 424 | * interface configured on VPLS 3 and two have an interface configured |
| 425 | * on VPLS 4, three have an interface configure. When the |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 426 | * module starts up, three hosts attached to device one, two and three - |
| 427 | * port 1, are registered by the HostService and events are sent to the |
| 428 | * application. sp2mp intents are created for all interfaces configured and |
| 429 | * mp2sp intents are created only for the hosts attached. |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 430 | * |
| 431 | * The number of intents expected is seventeen: six for VPLS 1, |
| 432 | * three for VPLS 2, four for VPLS3 and four for VPLS4. |
| 433 | * Ten sp2mp intents, seven mp2sp intents. IPs are added on the first two |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 434 | * hosts only to demonstrate this doesn't influence the number of intents |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 435 | * created. |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 436 | * An additional host is added on device seven - port 1, to demonstrate that |
| 437 | * the application does not generate intents, even if the interface uses the |
| 438 | * same VLAN Id of the other interfaces configured for the specifc VPLS. |
| 439 | * Checks if the number of intents submitted to the intent framework is equal |
| 440 | * to the number of intents expected and if all intents are equivalent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 441 | */ |
| 442 | @Test |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 443 | public void tenInterfacesThreeHostEventsSameVpls() { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 444 | vpls.activate(); |
| 445 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 446 | List<Intent> expectedIntents = Lists.newArrayList(); |
| 447 | Set<FilteredConnectPoint> fcPoints; |
| 448 | Set<Host> hosts; |
| 449 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 450 | hostsAvailable.addAll(Sets.newHashSet(V100HOST1, V200HOST1, |
| 451 | V300HOST1, V300HOST3, |
| 452 | VNONEHOST1, VNONEHOST2, |
| 453 | V400HOST1, VNONEHOST3)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 454 | hostsAvailable.forEach(host -> |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 455 | hostListener.event(new HostEvent(HostEvent.Type.HOST_ADDED, host))); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 456 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 457 | fcPoints = buildFCPoints(ImmutableSet.of(V100H1, V200H1, V300H1)); |
| 458 | hosts = ImmutableSet.of(V100HOST1, V200HOST1, V300HOST1); |
| 459 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS1, VLAN)); |
| 460 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS1, VLAN)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 461 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 462 | fcPoints = buildFCPoints(ImmutableSet.of(V100H2, V200H2, V300H2)); |
| 463 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS2, NONE)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 464 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 465 | fcPoints = buildFCPoints(ImmutableSet.of(VNONEH1, VNONEH2)); |
| 466 | hosts = ImmutableSet.of(VNONEHOST1, VNONEHOST2); |
| 467 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS3, NONE)); |
| 468 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS3, NONE)); |
| 469 | |
| 470 | fcPoints = buildFCPoints(ImmutableSet.of(V400H1, VNONEH3)); |
| 471 | hosts = ImmutableSet.of(V400HOST1, VNONEHOST3); |
| 472 | expectedIntents.addAll(generateVplsBrc(fcPoints, VPLS4, NONE)); |
| 473 | expectedIntents.addAll(generateVplsUni(fcPoints, hosts, VPLS4, NONE)); |
| 474 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 475 | checkIntents(expectedIntents); |
| 476 | } |
| 477 | |
| 478 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 479 | * Generates a list of the expected sp2mp intents for a VPLS. |
| 480 | * |
| 481 | * @param fcPoints the filtered connect point |
| 482 | * @param name the name of the VPLS |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 483 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 484 | * @return the list of expected sp2mp intents for the given VPLS |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 485 | */ |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 486 | private List<SinglePointToMultiPointIntent> |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 487 | generateVplsBrc(Set<FilteredConnectPoint> fcPoints, String name, EncapsulationType encap) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 488 | List<SinglePointToMultiPointIntent> intents = Lists.newArrayList(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 489 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 490 | fcPoints.forEach(point -> { |
| 491 | Set<FilteredConnectPoint> otherPoints = |
| 492 | fcPoints.stream() |
| 493 | .filter(fcp -> !fcp.equals(point)) |
| 494 | .collect(Collectors.toSet()); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 495 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 496 | Key brckey = buildKey(PREFIX_BROADCAST, |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 497 | point.connectPoint(), |
| 498 | name, |
| 499 | MacAddress.BROADCAST); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 500 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 501 | intents.add(buildBrcIntent(brckey, point, otherPoints, encap)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 502 | }); |
| 503 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 504 | return intents; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 508 | * Generates a list of expected mp2sp intents for a given VPLS. |
| 509 | * |
| 510 | * @param fcPoints the filtered connect point |
| 511 | * @param hosts the hosts |
| 512 | * @param name the name of the VPLS |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 513 | * @param encap the encapsulation type |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 514 | * @return the list of expected mp2sp intents for the given VPLS |
| 515 | */ |
| 516 | private List<MultiPointToSinglePointIntent> |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 517 | generateVplsUni(Set<FilteredConnectPoint> fcPoints, Set<Host> hosts, |
| 518 | String name, EncapsulationType encap) { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 519 | List<MultiPointToSinglePointIntent> intents = Lists.newArrayList(); |
| 520 | |
| 521 | hosts.forEach(host -> { |
| 522 | FilteredConnectPoint hostPoint = getHostPoint(host, fcPoints); |
| 523 | |
| 524 | Set<FilteredConnectPoint> otherPoints = |
| 525 | fcPoints.stream() |
| 526 | .filter(fcp -> !fcp.equals(hostPoint)) |
| 527 | .collect(Collectors.toSet()); |
| 528 | |
| 529 | Key uniKey = buildKey(PREFIX_UNICAST, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 530 | host.location(), name, host.mac()); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 531 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 532 | intents.add(buildUniIntent(uniKey, otherPoints, hostPoint, host, encap)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 533 | }); |
| 534 | |
| 535 | return intents; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Checks if the number of intents submitted to the intent framework is equal |
| 540 | * to the number of intents expected and if all intents are equivalent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 541 | * |
| 542 | * @param intents the list of intents expected |
| 543 | */ |
| 544 | private void checkIntents(List<Intent> intents) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 545 | assertEquals("The number of intents submitted differs from the number" + |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 546 | " of intents expected. ", |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 547 | intents.size(), intentService.getIntentCount()); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 548 | |
| 549 | for (Intent intentOne : intents) { |
| 550 | boolean found = false; |
| 551 | for (Intent intentTwo : intentService.getIntents()) { |
| 552 | if (intentOne.key().equals(intentTwo.key())) { |
| 553 | found = true; |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 554 | assertTrue(format("The intent submitted is different from" + |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 555 | " the intent expected. %s %s", |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 556 | intentOne, intentTwo), |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 557 | IntentUtils.intentsAreEqual(intentOne, intentTwo)); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 558 | break; |
| 559 | } |
| 560 | } |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 561 | assertTrue("The intent submitted is not equal to any of the expected" + |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 562 | " intents. ", found); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 567 | * Builds a broadcast intent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 568 | * |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 569 | * @param key the key to identify the intent |
| 570 | * @param src the ingress connect point |
| 571 | * @param dsts the egress connect points |
| 572 | * @return the generated single-point to multi-point intent |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 573 | */ |
| 574 | private SinglePointToMultiPointIntent buildBrcIntent(Key key, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 575 | FilteredConnectPoint src, |
| 576 | Set<FilteredConnectPoint> dsts, |
| 577 | EncapsulationType encap) { |
| 578 | SinglePointToMultiPointIntent.Builder intentBuilder; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 579 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 580 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 581 | .matchEthDst(MacAddress.BROADCAST) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 582 | .build(); |
| 583 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 584 | intentBuilder = SinglePointToMultiPointIntent.builder() |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 585 | .appId(APPID) |
| 586 | .key(key) |
| 587 | .selector(selector) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 588 | .filteredIngressPoint(src) |
| 589 | .filteredEgressPoints(dsts) |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 590 | .constraints(PARTIAL_FAILURE_CONSTRAINT) |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 591 | .priority(PRIORITY_OFFSET); |
| 592 | |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 593 | setEncap(intentBuilder, PARTIAL_FAILURE_CONSTRAINT, encap); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 594 | |
| 595 | return intentBuilder.build(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 599 | * Builds a unicast intent. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 600 | * |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 601 | * @param key the key to identify the intent |
| 602 | * @param srcs the ingress connect points |
| 603 | * @param dst the egress connect point |
| 604 | * @param host the destination Host |
| 605 | * @return the generated multi-point to single-point intent |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 606 | */ |
| 607 | private MultiPointToSinglePointIntent buildUniIntent(Key key, |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 608 | Set<FilteredConnectPoint> srcs, |
| 609 | FilteredConnectPoint dst, |
| 610 | Host host, |
| 611 | EncapsulationType encap) { |
| 612 | MultiPointToSinglePointIntent.Builder intentBuilder; |
| 613 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 614 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 615 | .matchEthDst(host.mac()) |
| 616 | .build(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 617 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 618 | intentBuilder = MultiPointToSinglePointIntent.builder() |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 619 | .appId(APPID) |
| 620 | .key(key) |
| 621 | .selector(selector) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 622 | .filteredIngressPoints(srcs) |
| 623 | .filteredEgressPoint(dst) |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 624 | .constraints(PARTIAL_FAILURE_CONSTRAINT) |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 625 | .priority(PRIORITY_OFFSET); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 626 | |
Luca Prete | 1346564 | 2017-01-04 10:31:48 -0800 | [diff] [blame] | 627 | setEncap(intentBuilder, PARTIAL_FAILURE_CONSTRAINT, encap); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 628 | |
| 629 | return intentBuilder.build(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 633 | * Returns the filtered connect point associated to a given host. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 634 | * |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 635 | * @param host the target host |
| 636 | * @param fcps the filtered connected points |
| 637 | * @return the filtered connect point associated to the given host; null |
| 638 | * otherwise |
| 639 | */ |
| 640 | private FilteredConnectPoint getHostPoint(Host host, |
| 641 | Set<FilteredConnectPoint> fcps) { |
| 642 | return fcps.stream() |
| 643 | .filter(fcp -> fcp.connectPoint().equals(host.location())) |
| 644 | .filter(fcp -> { |
| 645 | VlanIdCriterion vlanCriterion = |
| 646 | (VlanIdCriterion) fcp.trafficSelector(). |
| 647 | getCriterion(Criterion.Type.VLAN_VID); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 648 | return vlanCriterion == null || |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 649 | vlanCriterion.vlanId().equals(host.vlan()); |
| 650 | }) |
| 651 | .findFirst() |
| 652 | .orElse(null); |
| 653 | } |
| 654 | |
| 655 | /** |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 656 | * Computes a set of filtered connect points from a list of given interfaces. |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 657 | * |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 658 | * @param interfaces the interfaces to compute |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 659 | * @return the set of filtered connect points |
| 660 | */ |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 661 | private Set<FilteredConnectPoint> buildFCPoints(Collection<Interface> interfaces) { |
| 662 | // Build all filtered connected points in the VPLS |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 663 | return interfaces |
| 664 | .stream() |
| 665 | .map(intf -> { |
| 666 | TrafficSelector.Builder selectorBuilder = |
| 667 | DefaultTrafficSelector.builder(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 668 | if (!intf.vlan().equals(VlanId.NONE)) { |
| 669 | selectorBuilder.matchVlanId(intf.vlan()); |
| 670 | } |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 671 | return new FilteredConnectPoint(intf.connectPoint(), |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 672 | selectorBuilder.build()); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 673 | }) |
| 674 | .collect(Collectors.toSet()); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Builds an intent Key either for a single-point to multi-point or |
| 679 | * multi-point to single-point intent, based on a prefix that defines |
| 680 | * the intent type, the connection point representing the source or the |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 681 | * destination and the VLAN Id representing the VPLS. |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 682 | * |
| 683 | * @param prefix the key prefix |
| 684 | * @param cPoint the ingress/egress connect point |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 685 | * @param vplsName the VPLS name |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 686 | * @param hostMac the ingress/egress MAC address |
| 687 | * @return the key to identify the intent |
| 688 | */ |
| 689 | private Key buildKey(String prefix, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 690 | ConnectPoint cPoint, |
| 691 | String vplsName, |
| 692 | MacAddress hostMac) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 693 | String keyString = vplsName + |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 694 | DASH + |
| 695 | prefix + |
| 696 | DASH + |
| 697 | cPoint.deviceId() + |
| 698 | DASH + |
| 699 | cPoint.port() + |
| 700 | DASH + |
| 701 | hostMac; |
| 702 | |
| 703 | return Key.of(keyString, APPID); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Returns the device Id of the ith device. |
| 708 | * |
| 709 | * @param i the device to get the Id of |
| 710 | * @return the device Id |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 711 | */ |
| 712 | private static DeviceId getDeviceId(int i) { |
| 713 | return DeviceId.deviceId("" + i); |
| 714 | } |
| 715 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 716 | private static MacAddress getMac(int n) { |
| 717 | return MacAddress.valueOf(String.format("00:00:00:00:00:%s", n)); |
| 718 | } |
| 719 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 720 | private static HostLocation getLocation(int i) { |
| 721 | return new HostLocation(new ConnectPoint(getDeviceId(i), P1), 123L); |
| 722 | } |
| 723 | |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 724 | private static HostLocation getLocation(int d, int p) { |
| 725 | return new HostLocation(new ConnectPoint(getDeviceId(d), |
| 726 | PortNumber.portNumber(p)), 123L); |
| 727 | } |
| 728 | |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 729 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 730 | * Represents a fake IntentService class that allows to store and retrieve |
| 731 | * intents without implementing the IntentService logic. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 732 | */ |
| 733 | private class TestIntentService extends IntentServiceAdapter { |
| 734 | |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 735 | private Map<Key, Intent> intents; |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 736 | |
| 737 | public TestIntentService() { |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 738 | intents = Maps.newHashMap(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | @Override |
| 742 | public void submit(Intent intent) { |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 743 | intents.put(intent.key(), intent); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | @Override |
| 747 | public long getIntentCount() { |
| 748 | return intents.size(); |
| 749 | } |
| 750 | |
| 751 | @Override |
| 752 | public Iterable<Intent> getIntents() { |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 753 | return intents.values(); |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | @Override |
| 757 | public Intent getIntent(Key intentKey) { |
Luca Prete | c21c6e6 | 2016-09-22 14:52:21 -0700 | [diff] [blame] | 758 | for (Intent intent : intents.values()) { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 759 | if (intent.key().equals(intentKey)) { |
| 760 | return intent; |
| 761 | } |
| 762 | } |
| 763 | return null; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Represents a fake HostService class which allows to add hosts manually |
| 769 | * in each test, when needed. |
| 770 | */ |
| 771 | private class TestHostService extends HostServiceAdapter { |
| 772 | |
| 773 | private Set<Host> hosts; |
| 774 | |
| 775 | public TestHostService(Set<Host> hosts) { |
| 776 | this.hosts = hosts; |
| 777 | } |
| 778 | |
| 779 | @Override |
| 780 | public void addListener(HostListener listener) { |
| 781 | VplsTest.this.hostListener = listener; |
| 782 | } |
| 783 | |
| 784 | @Override |
| 785 | public Set<Host> getConnectedHosts(ConnectPoint connectPoint) { |
| 786 | return hosts.stream() |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 787 | .filter(h -> h.location().equals(connectPoint)) |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 788 | .collect(Collectors.toSet()); |
| 789 | } |
| 790 | |
| 791 | } |
| 792 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 793 | /** |
| 794 | * Represents a fake IdGenerator class for intents. |
| 795 | */ |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 796 | private static class TestIdGenerator implements IdGenerator { |
| 797 | |
| 798 | private final AtomicLong id = new AtomicLong(0); |
| 799 | |
| 800 | @Override |
| 801 | public long getNewId() { |
| 802 | return id.getAndIncrement(); |
| 803 | } |
| 804 | |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * Test IntentSynchronizer that passes all intents straight through to the |
| 809 | * intent service. |
| 810 | */ |
| 811 | private class TestIntentSynchronizer implements IntentSynchronizationService, |
| 812 | IntentSynchronizationAdminService { |
| 813 | |
| 814 | private final IntentService intentService; |
| 815 | |
| 816 | /** |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 817 | * Creates a new intent test synchronizer. |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 818 | * |
| 819 | * @param intentService intent service |
| 820 | */ |
| 821 | public TestIntentSynchronizer(IntentService intentService) { |
| 822 | this.intentService = intentService; |
| 823 | } |
| 824 | |
| 825 | @Override |
| 826 | public void submit(Intent intent) { |
| 827 | intentService.submit(intent); |
| 828 | } |
| 829 | |
| 830 | @Override |
| 831 | public void withdraw(Intent intent) { |
| 832 | intentService.withdraw(intent); |
| 833 | } |
| 834 | |
| 835 | @Override |
| 836 | public void modifyPrimary(boolean isPrimary) { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | @Override |
| 840 | public void removeIntents() { |
Luca Prete | 2705d66 | 2016-04-29 15:30:23 -0700 | [diff] [blame] | 841 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 842 | |
Luca Prete | 2705d66 | 2016-04-29 15:30:23 -0700 | [diff] [blame] | 843 | @Override |
| 844 | public void removeIntentsByAppId(ApplicationId applicationId) { |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 848 | /** |
| 849 | * Represents a fake VplsConfigService class which is needed for testing. |
| 850 | */ |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 851 | private class TestVplsConfigService extends VplsConfigServiceAdapter { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 852 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 853 | private final SetMultimap<String, Interface> ifacesByVplsName; |
| 854 | private final Map<String, EncapsulationType> encapsByVplsName; |
| 855 | |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 856 | private Set<String> vplsAffectByApi = new HashSet<>(); |
| 857 | |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 858 | TestVplsConfigService(SetMultimap<String, Interface> ifacesByVplsName, |
| 859 | Map<String, EncapsulationType> encapsByVplsName) { |
| 860 | this.ifacesByVplsName = ifacesByVplsName; |
| 861 | this.encapsByVplsName = encapsByVplsName; |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 865 | public void addVpls(String vplsName, Set<String> ifaceNames, String encap) { |
| 866 | if (!ifacesByVplsName.containsKey(vplsName)) { |
| 867 | ifaceNames.forEach(ifaceName -> { |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 868 | AVAILABLE_INTERFACES.forEach(iface -> { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 869 | if (iface.name().equals(ifaceName)) { |
| 870 | ifacesByVplsName.put(vplsName, iface); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 871 | } |
| 872 | }); |
| 873 | }); |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 874 | } |
| 875 | if (!ifacesByVplsName.containsKey(vplsName)) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 876 | encapsByVplsName.put(vplsName, valueOf(encap)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
| 880 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 881 | public void removeVpls(String vplsName) { |
| 882 | if (ifacesByVplsName.containsKey(vplsName)) { |
| 883 | ifacesByVplsName.removeAll(vplsName); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
| 887 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 888 | public void addIface(String vplsName, String iface) { |
| 889 | if (!ifacesByVplsName.containsKey(vplsName)) { |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 890 | AVAILABLE_INTERFACES.forEach(intf -> { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 891 | if (intf.name().equals(iface)) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 892 | ifacesByVplsName.put(vplsName, intf); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 893 | } |
| 894 | }); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 899 | public void setEncap(String vplsName, String encap) { |
| 900 | encapsByVplsName.put(vplsName, EncapsulationType.enumFromString(encap)); |
| 901 | } |
| 902 | |
| 903 | @Override |
| 904 | public void removeIface(String iface) { |
| 905 | SetMultimap<String, Interface> search = HashMultimap.create(ifacesByVplsName); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 906 | search.entries().forEach(e -> { |
| 907 | if (e.getValue().name().equals(iface)) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 908 | ifacesByVplsName.remove(e.getKey(), iface); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 909 | } |
| 910 | }); |
| 911 | } |
| 912 | |
| 913 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 914 | public void cleanVplsConfig() { |
| 915 | ifacesByVplsName.clear(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 919 | public EncapsulationType encap(String vplsName) { |
| 920 | EncapsulationType encap = null; |
| 921 | if (encapsByVplsName.containsKey(vplsName)) { |
| 922 | encap = encapsByVplsName.get(vplsName); |
| 923 | } |
| 924 | return encap; |
| 925 | } |
| 926 | |
| 927 | @Override |
| 928 | public Set<String> vplsAffectedByApi() { |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 929 | Set<String> vplsNames = ImmutableSet.copyOf(vplsAffectByApi); |
| 930 | |
| 931 | vplsAffectByApi.clear(); |
| 932 | |
| 933 | return vplsNames; |
| 934 | } |
| 935 | |
| 936 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 937 | public Set<Interface> allIfaces() { |
| 938 | return ifacesByVplsName.values() |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 939 | .stream() |
| 940 | .collect(Collectors.toSet()); |
| 941 | } |
| 942 | |
| 943 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 944 | public Set<Interface> ifaces(String name) { |
| 945 | return ifacesByVplsName.get(name) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 946 | .stream() |
| 947 | .collect(Collectors.toSet()); |
| 948 | } |
| 949 | |
| 950 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 951 | public Set<String> vplsNames() { |
| 952 | return ifacesByVplsName.keySet(); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 956 | public Set<String> vplsNamesOld() { |
| 957 | return ifacesByVplsName.keySet(); |
| 958 | } |
| 959 | |
| 960 | public SetMultimap<String, Interface> ifacesByVplsName() { |
| 961 | return ImmutableSetMultimap.copyOf(ifacesByVplsName); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | @Override |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 965 | public SetMultimap<String, Interface> ifacesByVplsName(VlanId vlan, |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 966 | ConnectPoint connectPoint) { |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 967 | String vplsName = |
| 968 | ifacesByVplsName.entries().stream() |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 969 | .filter(e -> e.getValue().connectPoint().equals(connectPoint)) |
| 970 | .filter(e -> e.getValue().vlan().equals(vlan)) |
Carolina Fernandez | b1cef5c | 2016-11-22 19:18:40 +0100 | [diff] [blame] | 971 | .map(Map.Entry::getKey) |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 972 | .findFirst() |
| 973 | .orElse(null); |
| 974 | SetMultimap<String, Interface> result = HashMultimap.create(); |
Luca Prete | 092e895 | 2016-10-26 16:25:56 +0200 | [diff] [blame] | 975 | if (vplsName != null && ifacesByVplsName.containsKey(vplsName)) { |
| 976 | ifacesByVplsName.get(vplsName) |
| 977 | .forEach(intf -> result.put(vplsName, intf)); |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 978 | return result; |
| 979 | } |
| 980 | return null; |
| 981 | } |
nosignal | 5fd282e | 2016-09-16 16:11:40 -0700 | [diff] [blame] | 982 | } |
Luca Prete | 9c2ee07 | 2016-02-16 11:00:44 -0800 | [diff] [blame] | 983 | } |