blob: c3e21b4c6fb9f65abd18eadea92a37b15de4397b [file] [log] [blame]
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301package org.onosproject.pce.pceservice;
2
3import static org.hamcrest.MatcherAssert.assertThat;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +05304import static org.hamcrest.Matchers.notNullValue;
5import static org.hamcrest.Matchers.nullValue;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05306import static org.hamcrest.core.Is.is;
7import static org.onlab.graph.GraphPathSearch.ALL_PATHS;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +05308import static org.onosproject.incubator.net.tunnel.Tunnel.State.ESTABLISHED;
9import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053010import static org.onosproject.net.Link.State.ACTIVE;
11import static org.onosproject.net.Link.Type.DIRECT;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053012import static org.onosproject.net.MastershipRole.MASTER;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053013import static org.onosproject.net.resource.Resources.continuous;
14import static org.onosproject.pce.pceservice.LspType.SR_WITHOUT_SIGNALLING;
15import static org.onosproject.pce.pceservice.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
16import static org.onosproject.pce.pceservice.LspType.WITH_SIGNALLING;
17import static org.onosproject.pce.pceservice.PathComputationTest.D1;
18import static org.onosproject.pce.pceservice.PathComputationTest.D2;
19import static org.onosproject.pce.pceservice.PathComputationTest.D3;
20import static org.onosproject.pce.pceservice.PathComputationTest.D4;
Priyanka B3f92c5a2016-05-27 10:14:16 +053021import static org.onosproject.pce.pceservice.PathComputationTest.DEVICE1;
22import static org.onosproject.pce.pceservice.PathComputationTest.DEVICE2;
23import static org.onosproject.pce.pceservice.PathComputationTest.DEVICE3;
24import static org.onosproject.pce.pceservice.PathComputationTest.DEVICE4;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053025import static org.onosproject.pce.pceservice.PceManager.PCEP_PORT;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053026import static org.onosproject.pce.pceservice.PcepAnnotationKeys.LOCAL_LSP_ID;
27import static org.onosproject.pce.pceservice.PcepAnnotationKeys.PLSP_ID;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053028import static org.onosproject.pce.pceservice.constraint.CostConstraint.Type.COST;
29import static org.onosproject.pce.pceservice.constraint.CostConstraint.Type.TE_COST;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053030
31import java.net.URISyntaxException;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053032import java.util.Collection;
33import java.util.Collections;
34import java.util.HashMap;
35import java.util.HashSet;
36import java.util.LinkedList;
37import java.util.List;
38import java.util.Set;
39import java.util.concurrent.atomic.AtomicLong;
40
41import org.junit.After;
42import org.junit.Before;
43import org.junit.Test;
44import org.onlab.graph.GraphPathSearch;
Priyanka B3f92c5a2016-05-27 10:14:16 +053045import org.onlab.junit.TestUtils;
46import org.onlab.junit.TestUtils.TestUtilsException;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053047import org.onlab.packet.Ethernet;
48import org.onlab.packet.IPv4;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053049import org.onlab.packet.TCP;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053050import org.onlab.util.Bandwidth;
51import org.onosproject.common.DefaultTopologyGraph;
52import org.onosproject.core.ApplicationId;
53import org.onosproject.core.CoreServiceAdapter;
54import org.onosproject.core.DefaultApplicationId;
55import org.onosproject.core.IdGenerator;
Priyanka B3f92c5a2016-05-27 10:14:16 +053056import org.onosproject.event.Event;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053057import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053058import org.onosproject.incubator.net.resource.label.LabelResourceId;
59import org.onosproject.incubator.net.resource.label.LabelResourceService;
60import org.onosproject.incubator.net.tunnel.DefaultTunnel;
61import org.onosproject.incubator.net.tunnel.Tunnel;
62import org.onosproject.incubator.net.tunnel.Tunnel.State;
63import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
64import org.onosproject.incubator.net.tunnel.TunnelEvent;
65import org.onosproject.incubator.net.tunnel.TunnelId;
66import org.onosproject.incubator.net.tunnel.TunnelListener;
Priyanka B3f92c5a2016-05-27 10:14:16 +053067import org.onosproject.mastership.MastershipServiceAdapter;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053068import org.onosproject.net.AnnotationKeys;
69import org.onosproject.net.Annotations;
70import org.onosproject.net.ConnectPoint;
71import org.onosproject.net.DefaultAnnotations;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053072import org.onosproject.net.DefaultAnnotations.Builder;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053073import org.onosproject.net.DefaultDevice;
74import org.onosproject.net.DefaultLink;
75import org.onosproject.net.Device;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053076import org.onosproject.net.DeviceId;
77import org.onosproject.net.ElementId;
78import org.onosproject.net.Link;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053079import org.onosproject.net.MastershipRole;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053080import org.onosproject.net.Path;
81import org.onosproject.net.PortNumber;
82import org.onosproject.net.SparseAnnotations;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053083import org.onosproject.net.device.DeviceEvent;
84import org.onosproject.net.device.DeviceListener;
85import org.onosproject.net.flowobjective.ForwardingObjective;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053086import org.onosproject.net.intent.Constraint;
87import org.onosproject.net.intent.IntentId;
88import org.onosproject.net.intent.constraint.BandwidthConstraint;
Priyanka B3f92c5a2016-05-27 10:14:16 +053089import org.onosproject.net.link.LinkEvent;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053090import org.onosproject.net.link.LinkListener;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053091import org.onosproject.net.packet.DefaultInboundPacket;
92import org.onosproject.net.packet.DefaultPacketContext;
93import org.onosproject.net.packet.InboundPacket;
94import org.onosproject.net.packet.OutboundPacket;
95import org.onosproject.net.packet.PacketProcessor;
96import org.onosproject.net.packet.PacketService;
97import org.onosproject.net.packet.PacketServiceAdapter;
98import org.onosproject.net.provider.ProviderId;
99import org.onosproject.net.resource.Resource;
100import org.onosproject.net.topology.DefaultTopologyEdge;
101import org.onosproject.net.topology.DefaultTopologyVertex;
102import org.onosproject.net.topology.LinkWeight;
103import org.onosproject.net.topology.PathServiceAdapter;
104import org.onosproject.net.topology.Topology;
105import org.onosproject.net.topology.TopologyEdge;
Priyanka B3f92c5a2016-05-27 10:14:16 +0530106import org.onosproject.net.topology.TopologyEvent;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530107import org.onosproject.net.topology.TopologyGraph;
Priyanka B3f92c5a2016-05-27 10:14:16 +0530108import org.onosproject.net.topology.TopologyListener;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530109import org.onosproject.net.topology.TopologyServiceAdapter;
110import org.onosproject.net.topology.TopologyVertex;
Avantika-Huawei032a9872016-05-27 22:57:38 +0530111import org.onosproject.pce.pceservice.PathComputationTest.MockNetConfigRegistryAdapter;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530112import org.onosproject.pce.pceservice.PathComputationTest.MockPathResourceService;
113import org.onosproject.pce.pceservice.constraint.CostConstraint;
114import org.onosproject.pce.pcestore.api.PceStore;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530115import org.onosproject.pce.util.FlowObjServiceAdapter;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530116import org.onosproject.pce.util.LabelResourceAdapter;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530117import org.onosproject.pce.util.MockDeviceService;
118import org.onosproject.pce.util.MockLinkService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530119import org.onosproject.pce.util.PceStoreAdapter;
120import org.onosproject.pce.util.TunnelServiceAdapter;
Avantika-Huawei032a9872016-05-27 22:57:38 +0530121import org.onosproject.pcep.api.DeviceCapability;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530122import org.onosproject.store.service.TestStorageService;
Priyanka B3f92c5a2016-05-27 10:14:16 +0530123
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530124import com.google.common.collect.ImmutableSet;
125
126/**
127 * Tests the functions of PceManager.
128 */
129public class PceManagerTest {
130
131 private PathComputationTest pathCompTest = new PathComputationTest();
132 private MockPathResourceService resourceService = pathCompTest.new MockPathResourceService();
133 private MockTopologyService topologyService = new MockTopologyService();
Priyanka B3f92c5a2016-05-27 10:14:16 +0530134 private MockMastershipService mastershipService = new MockMastershipService();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530135 private MockPathService pathService = new MockPathService();
136 private PceManager pceManager = new PceManager();
137 private MockCoreService coreService = new MockCoreService();
138 private MockTunnelServiceAdapter tunnelService = new MockTunnelServiceAdapter();
139 private TestStorageService storageService = new TestStorageService();
140 private PacketService packetService = new MockPacketService();
141 private MockDeviceService deviceService = new MockDeviceService();
Avantika-Huawei032a9872016-05-27 22:57:38 +0530142 private MockNetConfigRegistryAdapter netConfigRegistry = new PathComputationTest.MockNetConfigRegistryAdapter();
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530143 private MockLinkService linkService = new MockLinkService();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530144 private MockFlowObjService flowObjectiveService = new MockFlowObjService();
145 private PceStore pceStore = new PceStoreAdapter();
146 private LabelResourceService labelResourceService = new LabelResourceAdapter();
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530147 private LabelResourceAdminService labelRsrcAdminService = new LabelResourceAdapter();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530148
149 public static ProviderId providerId = new ProviderId("pce", "foo");
150 private static final String L3 = "L3";
151 private static final String LSRID = "lsrId";
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530152 private static final String PCECC_CAPABILITY = "pceccCapability";
153 private static final String SR_CAPABILITY = "srCapability";
154 private static final String LABEL_STACK_CAPABILITY = "labelStackCapability";
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530155
156 private TopologyGraph graph = null;
157 private Device deviceD1, deviceD2, deviceD3, deviceD4;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530158 private Device pcepDeviceD1, pcepDeviceD2, pcepDeviceD3, pcepDeviceD4;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530159 private Link link1, link2, link3, link4;
Priyanka B413fbe82016-05-26 11:44:45 +0530160 protected static int flowsDownloaded;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530161 private TunnelListener tunnelListener;
Priyanka B3f92c5a2016-05-27 10:14:16 +0530162 private TopologyListener listener;
163 private Topology topology;
164 private Set<TopologyEdge> edges;
165 private Set<TopologyVertex> vertexes;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530166
167 @Before
Priyanka B3f92c5a2016-05-27 10:14:16 +0530168 public void startUp() throws TestUtilsException {
169 listener = TestUtils.getField(pceManager, "topologyListener");
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530170 pceManager.pathService = pathService;
171 pceManager.resourceService = resourceService;
Priyanka B3f92c5a2016-05-27 10:14:16 +0530172 pceManager.topologyService = topologyService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530173 pceManager.tunnelService = tunnelService;
174 pceManager.coreService = coreService;
175 pceManager.storageService = storageService;
176 pceManager.packetService = packetService;
177 pceManager.deviceService = deviceService;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530178 pceManager.linkService = linkService;
Avantika-Huawei032a9872016-05-27 22:57:38 +0530179 pceManager.netCfgService = netConfigRegistry;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530180 pceManager.labelRsrcAdminService = labelRsrcAdminService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530181 pceManager.labelRsrcService = labelResourceService;
182 pceManager.flowObjectiveService = flowObjectiveService;
183 pceManager.pceStore = pceStore;
Priyanka B3f92c5a2016-05-27 10:14:16 +0530184 pceManager.mastershipService = mastershipService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530185 pceManager.activate();
186 }
187
Priyanka B3f92c5a2016-05-27 10:14:16 +0530188 private class MockMastershipService extends MastershipServiceAdapter {
189 @Override
190 public MastershipRole getLocalRole(DeviceId deviceId) {
191 return MASTER;
192 }
193
194 @Override
195 public boolean isLocalMaster(DeviceId deviceId) {
196 return getLocalRole(deviceId) == MASTER;
197 }
198 }
199
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530200 private void build4RouterTopo(boolean setCost, boolean setPceccCap, boolean setSrCap,
201 boolean setLabelStackCap, int bandwidth) {
Priyanka B3f92c5a2016-05-27 10:14:16 +0530202 link1 = PathComputationTest.addLink(DEVICE1, 10, DEVICE2, 20, setCost, 50);
203 link2 = PathComputationTest.addLink(DEVICE2, 30, DEVICE4, 40, setCost, 20);
204 link3 = PathComputationTest.addLink(DEVICE1, 80, DEVICE3, 70, setCost, 100);
205 link4 = PathComputationTest.addLink(DEVICE3, 60, DEVICE4, 50, setCost, 80);
206
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530207 Set<TopologyVertex> vertexes = new HashSet<TopologyVertex>();
208 vertexes.add(D1);
209 vertexes.add(D2);
210 vertexes.add(D3);
211 vertexes.add(D4);
212
Priyanka B3f92c5a2016-05-27 10:14:16 +0530213 this.vertexes = vertexes;
214
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530215 Set<TopologyEdge> edges = new HashSet<TopologyEdge>();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530216 TopologyEdge edge1 = new DefaultTopologyEdge(D1, D2, link1);
217 edges.add(edge1);
218
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530219 TopologyEdge edge2 = new DefaultTopologyEdge(D2, D4, link2);
220 edges.add(edge2);
221
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530222 TopologyEdge edge3 = new DefaultTopologyEdge(D1, D3, link3);
223 edges.add(edge3);
224
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530225 TopologyEdge edge4 = new DefaultTopologyEdge(D3, D4, link4);
226 edges.add(edge4);
227
Priyanka B3f92c5a2016-05-27 10:14:16 +0530228 this.edges = edges;
229
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530230 graph = new DefaultTopologyGraph(vertexes, edges);
231
232 DefaultAnnotations.Builder builderDev1 = DefaultAnnotations.builder();
233 DefaultAnnotations.Builder builderDev2 = DefaultAnnotations.builder();
234 DefaultAnnotations.Builder builderDev3 = DefaultAnnotations.builder();
235 DefaultAnnotations.Builder builderDev4 = DefaultAnnotations.builder();
236
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530237 // Making L3 devices
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530238 builderDev1.set(AnnotationKeys.TYPE, L3);
239 builderDev1.set(LSRID, "1.1.1.1");
240
241 builderDev2.set(AnnotationKeys.TYPE, L3);
242 builderDev2.set(LSRID, "2.2.2.2");
243
244 builderDev3.set(AnnotationKeys.TYPE, L3);
245 builderDev3.set(LSRID, "3.3.3.3");
246
247 builderDev4.set(AnnotationKeys.TYPE, L3);
248 builderDev4.set(LSRID, "4.4.4.4");
249
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530250 deviceD1 = new MockDevice(D1.deviceId(), builderDev1.build());
251 deviceD2 = new MockDevice(D2.deviceId(), builderDev2.build());
252 deviceD3 = new MockDevice(D3.deviceId(), builderDev3.build());
253 deviceD4 = new MockDevice(D4.deviceId(), builderDev4.build());
254
255 deviceService.addDevice(deviceD1);
256 deviceService.addDevice(deviceD2);
257 deviceService.addDevice(deviceD3);
258 deviceService.addDevice(deviceD4);
259
Avantika-Huawei032a9872016-05-27 22:57:38 +0530260 DeviceCapability device1Cap = netConfigRegistry.addConfig(DeviceId.deviceId("1.1.1.1"), DeviceCapability.class);
261 device1Cap.setLabelStackCap(setLabelStackCap)
262 .setLocalLabelCap(setPceccCap)
263 .setSrCap(setSrCap)
264 .apply();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530265
Avantika-Huawei032a9872016-05-27 22:57:38 +0530266 DeviceCapability device2Cap = netConfigRegistry.addConfig(DeviceId.deviceId("2.2.2.2"), DeviceCapability.class);
267 device2Cap.setLabelStackCap(setLabelStackCap)
268 .setLocalLabelCap(setPceccCap)
269 .setSrCap(setSrCap)
270 .apply();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530271
Avantika-Huawei032a9872016-05-27 22:57:38 +0530272 DeviceCapability device3Cap = netConfigRegistry.addConfig(DeviceId.deviceId("3.3.3.3"), DeviceCapability.class);
273 device3Cap.setLabelStackCap(setLabelStackCap)
274 .setLocalLabelCap(setPceccCap)
275 .setSrCap(setSrCap)
276 .apply();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530277
Avantika-Huawei032a9872016-05-27 22:57:38 +0530278 DeviceCapability device4Cap = netConfigRegistry.addConfig(DeviceId.deviceId("4.4.4.4"), DeviceCapability.class);
279 device4Cap.setLabelStackCap(setLabelStackCap)
280 .setLocalLabelCap(setPceccCap)
281 .setSrCap(setSrCap)
282 .apply();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530283
284 if (bandwidth != 0) {
285 List<Resource> resources = new LinkedList<>();
286 resources.add(continuous(link1.src().deviceId(), link1.src().port(), Bandwidth.class).resource(bandwidth));
287 resources.add(continuous(link2.src().deviceId(), link2.src().port(), Bandwidth.class).resource(bandwidth));
288 resources.add(continuous(link3.src().deviceId(), link3.src().port(), Bandwidth.class).resource(bandwidth));
289 resources.add(continuous(link4.src().deviceId(), link4.src().port(), Bandwidth.class).resource(bandwidth));
290
291 resources.add(continuous(link1.dst().deviceId(), link1.dst().port(), Bandwidth.class).resource(bandwidth));
292 resources.add(continuous(link2.dst().deviceId(), link2.dst().port(), Bandwidth.class).resource(bandwidth));
293 resources.add(continuous(link3.dst().deviceId(), link3.dst().port(), Bandwidth.class).resource(bandwidth));
294 resources.add(continuous(link4.dst().deviceId(), link4.dst().port(), Bandwidth.class).resource(bandwidth));
295
296 resourceService.allocate(IntentId.valueOf(bandwidth), resources);
297 }
298 }
299
300 /**
301 * Tests path success with (IGP) cost constraint for signalled LSP.
302 */
303 @Test
304 public void setupPathTest1() {
305 build4RouterTopo(true, false, false, false, 0); // IGP cost is set here.
306 List<Constraint> constraints = new LinkedList<Constraint>();
307 CostConstraint costConstraint = new CostConstraint(COST);
308 constraints.add(costConstraint);
309
310 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
311 assertThat(result, is(true));
312 }
313
314 /**
315 * Tests path failure with (IGP) cost constraint for signalled LSP.
316 */
317 @Test
318 public void setupPathTest2() {
319 build4RouterTopo(false, false, false, false, 0); // TE cost is set here, not IGP.
320 List<Constraint> constraints = new LinkedList<Constraint>();
321 CostConstraint costConstraint = new CostConstraint(COST);
322 constraints.add(costConstraint);
323
324 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
325 assertThat(result, is(false));
326 }
327
328 /**
329 * Tests path success with TE-cost constraint for signalled LSP.
330 */
331 @Test
332 public void setupPathTest3() {
333 build4RouterTopo(false, false, false, false, 0); // TE cost is set here.
334
335 List<Constraint> constraints = new LinkedList<Constraint>();
336 CostConstraint costConstraint = new CostConstraint(TE_COST);
337 constraints.add(costConstraint);
338
339 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
340 assertThat(result, is(true));
341 }
342
343 /**
344 * Tests path failure with TE-cost constraint for signalled LSP.
345 */
346 @Test
347 public void setupPathTest4() {
348 build4RouterTopo(true, false, false, false, 0); // IGP cost is set here, not TE.
349
350 List<Constraint> constraints = new LinkedList<Constraint>();
351 CostConstraint costConstraint = new CostConstraint(TE_COST);
352 constraints.add(costConstraint);
353
354 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
355 assertThat(result, is(false));
356 }
357
358 /**
359 * Tests path success with (IGP) cost constraint for non-SR non-signalled LSP.
360 */
361 @Test
362 public void setupPathTest5() {
363 build4RouterTopo(true, true, false, false, 0);
364
365 List<Constraint> constraints = new LinkedList<Constraint>();
366 CostConstraint costConstraint = new CostConstraint(COST);
367 constraints.add(costConstraint);
368
369 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints,
370 WITHOUT_SIGNALLING_AND_WITHOUT_SR);
371 assertThat(result, is(true));
372 }
373
374 /**
375 * Tests path success with TE-cost constraint for non-SR non-sgnalled LSP.
376 */
377 @Test
378 public void setupPathTest6() {
379 build4RouterTopo(false, true, false, false, 0);
380
381 List<Constraint> constraints = new LinkedList<Constraint>();
382 CostConstraint costConstraint = new CostConstraint(TE_COST);
383 constraints.add(costConstraint);
384
385 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints,
386 WITHOUT_SIGNALLING_AND_WITHOUT_SR);
387 assertThat(result, is(true));
388 }
389
390 /**
391 * Tests path failure with TE-cost constraint for non-SR non-signalled LSP(CR). Label capability not registered.
392 */
393 @Test
394 public void setupPathTest7() {
395 build4RouterTopo(true, false, false, false, 0);
396
397 List<Constraint> constraints = new LinkedList<Constraint>();
398 CostConstraint costConstraint = new CostConstraint(TE_COST);
399 constraints.add(costConstraint);
400
401 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints,
402 WITHOUT_SIGNALLING_AND_WITHOUT_SR);
403 assertThat(result, is(false));
404 }
405
406 /**
407 * Tests path failure as bandwidth is requested but is not registered.
408 */
409 @Test
410 public void setupPathTest8() {
411 build4RouterTopo(true, false, false, false, 0);
412 List<Constraint> constraints = new LinkedList<Constraint>();
413 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(10.0));
414 CostConstraint costConstraint = new CostConstraint(TE_COST);
415
416 constraints.add(costConstraint);
417 constraints.add(bwConstraint);
418
419 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
420 assertThat(result, is(false));
421 }
422
423 /**
424 * Tests path failure as bandwidth requested is more than registered.
425 */
426 @Test
427 public void setupPathTest9() {
428 build4RouterTopo(false, false, false, false, 5);
429 List<Constraint> constraints = new LinkedList<Constraint>();
430 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(10.0));
431 CostConstraint costConstraint = new CostConstraint(TE_COST);
432
433 constraints.add(costConstraint);
434 constraints.add(bwConstraint);
435
436 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
437 assertThat(result, is(false));
438 }
439
440 /**
441 * Tests path setup failure(without signalling). Label capability is not present.
442 */
443 @Test
444 public void setupPathTest10() {
445 build4RouterTopo(false, false, false, false, 0);
446 List<Constraint> constraints = new LinkedList<Constraint>();
447 CostConstraint costConstraint = new CostConstraint(TE_COST);
448 constraints.add(costConstraint);
449
450 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, SR_WITHOUT_SIGNALLING);
451 assertThat(result, is(false));
452 }
453
454 /**
455 * Tests path setup without failure for LSP with signalling and with bandwidth reservation.
456 */
457 @Test
458 public void setupPathTest11() {
459 build4RouterTopo(false, true, true, true, 15);
460 List<Constraint> constraints = new LinkedList<Constraint>();
461 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(10.0));
462 CostConstraint costConstraint = new CostConstraint(TE_COST);
463
464 constraints.add(costConstraint);
465 constraints.add(bwConstraint);
466
467 LabelResourceId node1Label = LabelResourceId.labelResourceId(5200);
468 LabelResourceId node2Label = LabelResourceId.labelResourceId(5201);
469
470 pceManager.pceStore.addGlobalNodeLabel(D1.deviceId(), node1Label);
471 pceManager.pceStore.addGlobalNodeLabel(D2.deviceId(), node2Label);
472
473 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, SR_WITHOUT_SIGNALLING);
474 assertThat(result, is(false));
475 }
476
477 /**
478 * Tests path setup without signalling and with bandwidth reservation.
479 */
480 @Test
481 public void setupPathTest12() {
482 build4RouterTopo(false, true, true, true, 15);
483 List<Constraint> constraints = new LinkedList<Constraint>();
484 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(10.0));
485 CostConstraint costConstraint = new CostConstraint(TE_COST);
486
487 constraints.add(costConstraint);
488 constraints.add(bwConstraint);
489
490 LabelResourceId node1Label = LabelResourceId.labelResourceId(5200);
491 LabelResourceId node2Label = LabelResourceId.labelResourceId(5201);
492
493 pceManager.pceStore.addGlobalNodeLabel(D1.deviceId(), node1Label);
494 pceManager.pceStore.addGlobalNodeLabel(D2.deviceId(), node2Label);
495
496 LabelResourceId link1Label = LabelResourceId.labelResourceId(5202);
497 pceManager.pceStore.addAdjLabel(link1, link1Label);
498
499 LabelResourceId link2Label = LabelResourceId.labelResourceId(5203);
500 pceManager.pceStore.addAdjLabel(link2, link2Label);
501
502 LabelResourceId link3Label = LabelResourceId.labelResourceId(5204);
503 pceManager.pceStore.addAdjLabel(link3, link3Label);
504
505 LabelResourceId link4Label = LabelResourceId.labelResourceId(5205);
506 pceManager.pceStore.addAdjLabel(link4, link4Label);
507
508 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, SR_WITHOUT_SIGNALLING);
509 assertThat(result, is(true));
510 }
511
512 /**
513 * Tests path setup without cost/bandwidth constraints.
514 */
515 @Test
516 public void setupPathTest13() {
517 build4RouterTopo(false, false, false, false, 0);
518
519 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", null, WITH_SIGNALLING);
520 assertThat(result, is(true));
521 }
522
523 /**
524 * Tests path update with increase in bandwidth.
525 */
526 @Test
527 public void updatePathTest1() {
528 build4RouterTopo(false, true, true, true, 100);
529
530 // Setup tunnel.
531 List<Constraint> constraints = new LinkedList<>();
532 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(60.0));
533 constraints.add(bwConstraint);
534 CostConstraint costConstraint = new CostConstraint(TE_COST);
535 constraints.add(costConstraint);
536
537 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
538 assertThat(result, is(true));
539
540 // Change constraint and update it.
541 constraints = new LinkedList<>();
542 bwConstraint = new BandwidthConstraint(Bandwidth.bps(50.0));
543 constraints.add(bwConstraint);
544 constraints.add(costConstraint);
545
546 Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
547 assertThat(tunnels.size(), is(1));
548
549 Tunnel tunnel = tunnels.iterator().next();
550
551 // Stimulate the effect of LSP ids from protocol msg.
552 tunnelService.updateTunnelWithLspIds(tunnel, "123", "1", State.ACTIVE);
553
554 result = pceManager.updatePath(tunnel.tunnelId(), constraints);
555 assertThat(result, is(true));
556
557 tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
558 assertThat(tunnels.size(), is(2));
559 }
560
561 /**
562 * Tests path update with decrease in bandwidth.
563 */
564 @Test
565 public void updatePathTest2() {
566 build4RouterTopo(false, true, true, true, 100);
567
568 // Setup tunnel.
569 List<Constraint> constraints = new LinkedList<Constraint>();
570 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(60.0));
571 constraints.add(bwConstraint);
572 CostConstraint costConstraint = new CostConstraint(TE_COST);
573 constraints.add(costConstraint);
574
575 LabelResourceId node1Label = LabelResourceId.labelResourceId(5200);
576 LabelResourceId node2Label = LabelResourceId.labelResourceId(5201);
577
578 pceManager.pceStore.addGlobalNodeLabel(D1.deviceId(), node1Label);
579 pceManager.pceStore.addGlobalNodeLabel(D2.deviceId(), node2Label);
580
581 LabelResourceId link1Label = LabelResourceId.labelResourceId(5202);
582 pceManager.pceStore.addAdjLabel(link1, link1Label);
583
584 LabelResourceId link2Label = LabelResourceId.labelResourceId(5203);
585 pceManager.pceStore.addAdjLabel(link2, link2Label);
586
587 LabelResourceId link3Label = LabelResourceId.labelResourceId(5204);
588 pceManager.pceStore.addAdjLabel(link3, link3Label);
589
590 LabelResourceId link4Label = LabelResourceId.labelResourceId(5205);
591 pceManager.pceStore.addAdjLabel(link4, link4Label);
592
593 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, SR_WITHOUT_SIGNALLING);
594 assertThat(result, is(true));
595
596 // Change constraint and update it.
597 constraints.remove(bwConstraint);
598 bwConstraint = new BandwidthConstraint(Bandwidth.bps(70.0));
599 constraints.add(bwConstraint);
600
601 Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
602 assertThat(tunnels.size(), is(1));
603
604 for (Tunnel tunnel : tunnels) {
605 result = pceManager.updatePath(tunnel.tunnelId(), constraints);
606 assertThat(result, is(true));
607 }
608
609 tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
610 assertThat(tunnels.size(), is(2));
611 }
612
613 /**
614 * Tests path update without cost/bandwidth constraints.
615 */
616 @Test
617 public void updatePathTest3() {
618 build4RouterTopo(false, true, true, true, 100);
619
620 // Setup tunnel.
621 boolean result = pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", null, WITH_SIGNALLING);
622 assertThat(result, is(true));
623
624 Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
625 assertThat(tunnels.size(), is(1));
626
627 for (Tunnel tunnel : tunnels) {
628 result = pceManager.updatePath(tunnel.tunnelId(), null);
629 assertThat(result, is(true));
630 }
631
632 Iterable<Tunnel> queryTunnelResult = pceManager.queryAllPath();
633 assertThat((int) queryTunnelResult.spliterator().getExactSizeIfKnown(), is(2));
634 }
635
636 /**
637 * Tests path release.
638 */
639 @Test
640 public void releasePathTest1() {
641 build4RouterTopo(false, false, false, false, 5);
642 List<Constraint> constraints = new LinkedList<Constraint>();
643 CostConstraint costConstraint = new CostConstraint(TE_COST);
Priyanka B4c3b4512016-07-22 11:41:49 +0530644 BandwidthConstraint bwConst = new BandwidthConstraint(Bandwidth.bps(3));
645 constraints.add(bwConst);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530646 constraints.add(costConstraint);
647
648 pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
649
650 Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
651 assertThat(tunnels.size(), is(1));
652 boolean result;
653 for (Tunnel tunnel : tunnels) {
654 result = pceManager.releasePath(tunnel.tunnelId());
655 assertThat(result, is(true));
656 }
657 tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
658 assertThat(tunnels.size(), is(0));
659 }
660
661 /**
662 * Tests path release failure.
663 */
664 @Test
665 public void releasePathTest2() {
666 build4RouterTopo(false, false, false, false, 5);
667 List<Constraint> constraints = new LinkedList<Constraint>();
668 CostConstraint costConstraint = new CostConstraint(TE_COST);
669 constraints.add(costConstraint);
670
671 pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T123", constraints, WITH_SIGNALLING);
672
673 Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
674 assertThat(tunnels.size(), is(1));
675
676 // Random tunnel id.
677 boolean result = pceManager.releasePath(TunnelId.valueOf("111"));
678 assertThat(result, is(false));
679
680 tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
681 assertThat(tunnels.size(), is(1));
682 }
683
684 /**
685 * Tests packet in to trigger label DB sync.
686 */
687 @Test
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530688 public void packetProcessingTest1() throws URISyntaxException {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530689
690 build4RouterTopo(false, true, true, true, 0); // This also initializes devices etc.
691
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530692 LabelResourceId node1Label = LabelResourceId.labelResourceId(5200);
693 LabelResourceId node2Label = LabelResourceId.labelResourceId(5201);
694
Avantika-Huawei28b53752016-06-23 17:04:49 +0530695 pceManager.pceStore.addLsrIdDevice(deviceD1.annotations().value(LSRID), deviceD1.id());
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530696 pceManager.pceStore.addGlobalNodeLabel(D1.deviceId(), node1Label);
697 pceManager.pceStore.addGlobalNodeLabel(D2.deviceId(), node2Label);
698
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530699 ConnectPoint src = new ConnectPoint(D1.deviceId(), PortNumber.portNumber(1));
700 ConnectPoint dst = new ConnectPoint(D2.deviceId(), PortNumber.portNumber(2));
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530701
702 Link link1 = DefaultLink.builder().src(src).dst(dst).state(ACTIVE).type(DIRECT)
703 .providerId(new ProviderId("eth", "1")).build();
704
705 LabelResourceId link1Label = LabelResourceId.labelResourceId(5204);
706 pceManager.pceStore.addAdjLabel(link1, link1Label);
707
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530708 TCP tcp = new TCP();
709 tcp.setDestinationPort(PCEP_PORT);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530710
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530711 IPv4 ipv4 = new IPv4();
712 ipv4.setProtocol(IPv4.PROTOCOL_TCP);
713 ipv4.setPayload(tcp);
714
715 Ethernet eth = new Ethernet();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530716 eth.setEtherType(Ethernet.TYPE_IPV4);
717 eth.setPayload(ipv4);
718
Avantika-Huawei28b53752016-06-23 17:04:49 +0530719 InboundPacket inPkt = new DefaultInboundPacket(new ConnectPoint(DeviceId.deviceId("1.1.1.1"),
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530720 PortNumber.portNumber(PCEP_PORT)),
721 eth, null);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530722
723 pktProcessor.process(new MockPcepPacketContext(inPkt, null));
724 assertThat(flowsDownloaded, is(4));
725 }
726
727 /**
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530728 * Tests faulty packet in to trigger label DB sync.
729 */
730 @Test
731 public void packetProcessingTest2() throws URISyntaxException {
732
733 build4RouterTopo(false, true, true, true, 0); // This also initializes devices etc.
734
735 LabelResourceId node1Label = LabelResourceId.labelResourceId(5200);
736 LabelResourceId node2Label = LabelResourceId.labelResourceId(5201);
737
738 pceManager.pceStore.addGlobalNodeLabel(D1.deviceId(), node1Label);
739 pceManager.pceStore.addGlobalNodeLabel(D2.deviceId(), node2Label);
740
741 ConnectPoint src = new ConnectPoint(D1.deviceId(), PortNumber.portNumber(1));
742 ConnectPoint dst = new ConnectPoint(D2.deviceId(), PortNumber.portNumber(2));
743
744 Link link1 = DefaultLink.builder().src(src).dst(dst).state(ACTIVE).type(DIRECT)
745 .providerId(new ProviderId("eth", "1")).build();
746
747 LabelResourceId link1Label = LabelResourceId.labelResourceId(5204);
748 pceManager.pceStore.addAdjLabel(link1, link1Label);
749
750 TCP tcp = new TCP(); // Not set the pcep port.
751 IPv4 ipv4 = new IPv4();
752 ipv4.setProtocol(IPv4.PROTOCOL_TCP);
753 ipv4.setPayload(tcp);
754
755 Ethernet eth = new Ethernet();
756 eth.setEtherType(Ethernet.TYPE_IPV4);
757 eth.setPayload(ipv4);
758
759 InboundPacket inPkt = new DefaultInboundPacket(new ConnectPoint(D1.deviceId(),
760 PortNumber.portNumber(PCEP_PORT)),
761 eth, null);
762
763 pktProcessor.process(new MockPcepPacketContext(inPkt, null));
764 assertThat(flowsDownloaded, is(0));
765 }
766
767 /**
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530768 * Tests tunnel events added and removed.
769 */
770 @Test
771 public void tunnelEventTest1() {
772 build4RouterTopo(false, true, true, true, 15);
773 List<Constraint> constraints = new LinkedList<Constraint>();
774 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(10.0));
775 CostConstraint costConstraint = new CostConstraint(TE_COST);
776
777 constraints.add(costConstraint);
778 constraints.add(bwConstraint);
779
780 LabelResourceId node1Label = LabelResourceId.labelResourceId(5200);
781 LabelResourceId node2Label = LabelResourceId.labelResourceId(5201);
782
783 pceManager.pceStore.addGlobalNodeLabel(D1.deviceId(), node1Label);
784 pceManager.pceStore.addGlobalNodeLabel(D2.deviceId(), node2Label);
785
786 LabelResourceId link1Label = LabelResourceId.labelResourceId(5202);
787 pceManager.pceStore.addAdjLabel(link1, link1Label);
788
789 LabelResourceId link2Label = LabelResourceId.labelResourceId(5203);
790 pceManager.pceStore.addAdjLabel(link2, link2Label);
791
792 LabelResourceId link3Label = LabelResourceId.labelResourceId(5204);
793 pceManager.pceStore.addAdjLabel(link3, link3Label);
794
795 LabelResourceId link4Label = LabelResourceId.labelResourceId(5205);
796 pceManager.pceStore.addAdjLabel(link4, link4Label);
797
798 pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T1", constraints, SR_WITHOUT_SIGNALLING);
799 assertThat(pceStore.getTunnelInfoCount(), is(1));
800
801 Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
802
803 for (Tunnel tunnel : tunnels) {
804 TunnelEvent event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED, tunnel);
805 tunnelListener.event(event);
806
807 pceManager.releasePath(tunnel.tunnelId());
808
809 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, tunnel);
810 tunnelListener.event(event);
811 }
812
813 assertThat(pceStore.getTunnelInfoCount(), is(0));
814 }
815
816 /**
817 * Tests label allocation/removal in CR case based on tunnel event.
818 */
819 @Test
820 public void tunnelEventTest2() {
821 build4RouterTopo(false, true, true, true, 15);
822 List<Constraint> constraints = new LinkedList<Constraint>();
823 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(10.0));
824 CostConstraint costConstraint = new CostConstraint(TE_COST);
825
826 constraints.add(costConstraint);
827 constraints.add(bwConstraint);
828
829 pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T2", constraints, WITHOUT_SIGNALLING_AND_WITHOUT_SR);
830 assertThat(pceStore.getTunnelInfoCount(), is(1));
831
832 TunnelEvent event;
833 Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
834 for (Tunnel tunnel : tunnels) {
835 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED, tunnel);
836 tunnelListener.event(event);
837
838 // Stimulate the effect of LSP ids from protocol msg.
839 tunnelService.updateTunnelWithLspIds(tunnel, "123", "1", ESTABLISHED);
840 }
841
842 tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
843 for (Tunnel tunnel : tunnels) {
844 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_UPDATED, tunnel);
845 tunnelListener.event(event);
846
847 pceManager.releasePath(tunnel.tunnelId());
848
849 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_REMOVED, tunnel);
850 tunnelListener.event(event);
851 }
852
853 assertThat(pceStore.getTunnelInfoCount(), is(0));
854 }
855
856 /**
857 * Tests handling UNSTABLE state based on tunnel event.
858 */
859 @Test
860 public void tunnelEventTest3() {
861 build4RouterTopo(false, true, true, true, 15);
862 List<Constraint> constraints = new LinkedList<Constraint>();
863 BandwidthConstraint bwConstraint = new BandwidthConstraint(Bandwidth.bps(10.0));
864 CostConstraint costConstraint = new CostConstraint(TE_COST);
865
866 constraints.add(costConstraint);
867 constraints.add(bwConstraint);
868
869 pceManager.setupPath(D1.deviceId(), D2.deviceId(), "T2", constraints, WITHOUT_SIGNALLING_AND_WITHOUT_SR);
870 assertThat(pceStore.getTunnelInfoCount(), is(1));
871 assertThat(pceStore.getFailedPathInfoCount(), is(0));
872
873 TunnelEvent event;
874 Collection<Tunnel> tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
875 for (Tunnel tunnel : tunnels) {
876 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_ADDED, tunnel);
877 tunnelListener.event(event);
878
879 // Stimulate the effect of LSP ids from protocol msg.
880 tunnelService.updateTunnelWithLspIds(tunnel, "123", "1", UNSTABLE);
881 }
882
883 tunnels = (Collection<Tunnel>) pceManager.queryAllPath();
884 for (Tunnel tunnel : tunnels) {
885 event = new TunnelEvent(TunnelEvent.Type.TUNNEL_UPDATED, tunnel);
886 tunnelListener.event(event);
887 }
888 assertThat(pceStore.getTunnelInfoCount(), is(1));
889 assertThat(pceStore.getFailedPathInfoCount(), is(1));
890 }
891
Priyanka B3f92c5a2016-05-27 10:14:16 +0530892 /**
893 * Tests resilency when L2 link is down.
894 */
895 @Test
896 public void resilencyTest1() {
897 build4RouterTopo(true, false, false, false, 10);
898
899
900 List<Constraint> constraints = new LinkedList<Constraint>();
901 CostConstraint costConstraint = new CostConstraint(COST);
902 constraints.add(costConstraint);
903 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
904 constraints.add(localBwConst);
905
906 //Setup the path , tunnel created
907 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
908 assertThat(result, is(true));
909 assertThat(pceStore.getTunnelInfoCount(), is(1));
910 assertThat(pceStore.getFailedPathInfoCount(), is(0));
911
912 List<Event> reasons = new LinkedList<>();
913 final LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link2);
914 reasons.add(linkEvent);
915 final TopologyEvent event = new TopologyEvent(
916 TopologyEvent.Type.TOPOLOGY_CHANGED,
917 topology,
918 reasons);
919
920 //Change Topology : remove link2
921 Set<TopologyEdge> tempEdges = new HashSet<>();
922 tempEdges.add(new DefaultTopologyEdge(D2, D4, link2));
923 topologyService.changeInTopology(getGraph(null, tempEdges));
924 listener.event(event);
925
926 List<Link> links = new LinkedList<>();
927 links.add(link3);
928 links.add(link4);
929
930 //Path is D1-D3-D4
931 assertThat(pathService.paths().iterator().next().links(), is(links));
932 assertThat(pathService.paths().iterator().next().cost(), is((double) 180));
933 }
934
935 /**
936 * Tests resilency when L2 and L4 link is down.
937 */
938 @Test
939 public void resilencyTest2() {
940 build4RouterTopo(true, false, false, false, 10);
941
942 List<Constraint> constraints = new LinkedList<Constraint>();
943 CostConstraint costConstraint = new CostConstraint(COST);
944 constraints.add(costConstraint);
945 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
946 constraints.add(localBwConst);
947
948 //Setup the path , tunnel created
949 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
950 assertThat(result, is(true));
951
952 List<Event> reasons = new LinkedList<>();
953 LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link2);
954 reasons.add(linkEvent);
955 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link4);
956 reasons.add(linkEvent);
957 final TopologyEvent event = new TopologyEvent(
958 TopologyEvent.Type.TOPOLOGY_CHANGED,
959 topology,
960 reasons);
961
962 //Change Topology : remove link2 and link4
963 Set<TopologyEdge> tempEdges = new HashSet<>();
964 tempEdges.add(new DefaultTopologyEdge(D2, D4, link2));
965 tempEdges.add(new DefaultTopologyEdge(D3, D4, link4));
966 topologyService.changeInTopology(getGraph(null, tempEdges));
967 listener.event(event);
968
969 //No Path
970 assertThat(pathService.paths().size(), is(0));
971 }
972
973 /**
974 * Tests resilency when D2 device is down.
975 */
976 @Test
977 public void resilencyTest3() {
978 build4RouterTopo(true, false, false, false, 10);
979
980 List<Constraint> constraints = new LinkedList<Constraint>();
981 CostConstraint costConstraint = new CostConstraint(COST);
982 constraints.add(costConstraint);
983 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
984 constraints.add(localBwConst);
985
986 //Setup the path , tunnel created
987 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
988 assertThat(result, is(true));
989
990 List<Event> reasons = new LinkedList<>();
991 LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link2);
992 reasons.add(linkEvent);
993 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link1);
994 reasons.add(linkEvent);
995 final TopologyEvent event = new TopologyEvent(
996 TopologyEvent.Type.TOPOLOGY_CHANGED,
997 topology,
998 reasons);
999
1000 //Change Topology : remove link2 and link1
1001 Set<TopologyEdge> tempEdges = new HashSet<>();
1002 tempEdges.add(new DefaultTopologyEdge(D2, D4, link2));
1003 tempEdges.add(new DefaultTopologyEdge(D1, D2, link1));
1004 topologyService.changeInTopology(getGraph(null, tempEdges));
1005 listener.event(event);
1006
1007 List<Link> links = new LinkedList<>();
1008 links.add(link3);
1009 links.add(link4);
1010
1011 //Path is D1-D3-D4
1012 assertThat(pathService.paths().iterator().next().links(), is(links));
1013 assertThat(pathService.paths().iterator().next().cost(), is((double) 180));
1014 }
1015
1016 /**
1017 * Tests resilency when ingress device is down.
1018 */
1019 @Test
1020 public void resilencyTest4() {
1021 build4RouterTopo(true, false, false, false, 10);
1022
1023 List<Constraint> constraints = new LinkedList<Constraint>();
1024 CostConstraint costConstraint = new CostConstraint(COST);
1025 constraints.add(costConstraint);
1026 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
1027 constraints.add(localBwConst);
1028
1029 //Setup the path , tunnel created
1030 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
1031 assertThat(result, is(true));
1032
1033 List<Event> reasons = new LinkedList<>();
1034 LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link3);
1035 reasons.add(linkEvent);
1036 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link1);
1037 reasons.add(linkEvent);
1038 final TopologyEvent event = new TopologyEvent(
1039 TopologyEvent.Type.TOPOLOGY_CHANGED,
1040 topology,
1041 reasons);
1042
1043 //Change Topology : remove link2 and link1
1044 Set<TopologyEdge> tempEdges = new HashSet<>();
1045 tempEdges.add(new DefaultTopologyEdge(D1, D3, link3));
1046 tempEdges.add(new DefaultTopologyEdge(D1, D2, link1));
1047 topologyService.changeInTopology(getGraph(null, tempEdges));
1048 listener.event(event);
1049
1050 //No path
1051 assertThat(pathService.paths().size(), is(0));
1052 }
1053
1054 /**
1055 * Tests resilency when D2 and D3 devices are down.
1056 */
1057 @Test
1058 public void resilencyTest5() {
1059 build4RouterTopo(true, false, false, false, 10);
1060
1061 List<Constraint> constraints = new LinkedList<Constraint>();
1062 CostConstraint costConstraint = new CostConstraint(COST);
1063 constraints.add(costConstraint);
1064 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
1065 constraints.add(localBwConst);
1066
1067 //Setup the path , tunnel created
1068 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
1069 assertThat(result, is(true));
1070
1071 List<Event> reasons = new LinkedList<>();
1072 LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link2);
1073 reasons.add(linkEvent);
1074 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link1);
1075 reasons.add(linkEvent);
1076 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link3);
1077 reasons.add(linkEvent);
1078 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link4);
1079 reasons.add(linkEvent);
1080
1081 final TopologyEvent event = new TopologyEvent(
1082 TopologyEvent.Type.TOPOLOGY_CHANGED,
1083 topology,
1084 reasons);
1085
1086 //Change Topology : remove device2, device3 and all links
1087 Set<TopologyEdge> tempEdges = new HashSet<>();
1088 tempEdges.add(new DefaultTopologyEdge(D1, D2, link1));
1089 tempEdges.add(new DefaultTopologyEdge(D2, D4, link2));
1090 tempEdges.add(new DefaultTopologyEdge(D1, D3, link3));
1091 tempEdges.add(new DefaultTopologyEdge(D3, D4, link4));
1092 Set<TopologyVertex> tempVertexes = new HashSet<>();
1093 tempVertexes.add(D2);
1094 tempVertexes.add(D3);
1095 topologyService.changeInTopology(getGraph(tempVertexes, tempEdges));
1096 listener.event(event);
1097
1098 //No path
1099 assertThat(pathService.paths().size(), is(0));
1100 }
1101
1102 /**
1103 * Tests resilency when egress device is down.
1104 */
1105 @Test
1106 public void resilencyTest6() {
1107 build4RouterTopo(true, false, false, false, 10);
1108
1109 List<Constraint> constraints = new LinkedList<Constraint>();
1110 CostConstraint costConstraint = new CostConstraint(COST);
1111 constraints.add(costConstraint);
1112 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
1113 constraints.add(localBwConst);
1114
1115 //Setup the path , tunnel created
1116 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
1117 assertThat(result, is(true));
1118
1119 List<Event> reasons = new LinkedList<>();
1120 LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link2);
1121 reasons.add(linkEvent);
1122 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link4);
1123 reasons.add(linkEvent);
1124
1125 final TopologyEvent event = new TopologyEvent(
1126 TopologyEvent.Type.TOPOLOGY_CHANGED,
1127 topology,
1128 reasons);
1129
1130 //Change Topology : remove device4 , link2 and link4
1131 Set<TopologyEdge> tempEdges = new HashSet<>();
1132 tempEdges.add(new DefaultTopologyEdge(D2, D4, link2));
1133 tempEdges.add(new DefaultTopologyEdge(D3, D4, link4));
1134 Set<TopologyVertex> tempVertexes = new HashSet<>();
1135 tempVertexes.add(D4);
1136 topologyService.changeInTopology(getGraph(tempVertexes, tempEdges));
1137 listener.event(event);
1138
1139 //No path
1140 assertThat(pathService.paths().size(), is(0));
1141 }
1142
1143 /**
1144 * Tests resilency when egress device is down.
1145 */
1146 @Test
1147 public void resilencyTest7() {
1148 build4RouterTopo(true, false, false, false, 10);
1149
1150 List<Constraint> constraints = new LinkedList<Constraint>();
1151 CostConstraint costConstraint = new CostConstraint(COST);
1152 constraints.add(costConstraint);
1153 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
1154 constraints.add(localBwConst);
1155
1156 //Setup the path , tunnel created
1157 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
1158 assertThat(result, is(true));
1159
1160 List<Event> reasons = new LinkedList<>();
1161 LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link2);
1162 reasons.add(linkEvent);
1163 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link4);
1164 reasons.add(linkEvent);
1165
1166 final TopologyEvent event = new TopologyEvent(
1167 TopologyEvent.Type.TOPOLOGY_CHANGED,
1168 topology,
1169 reasons);
1170
1171 //Change Topology : remove device4 , link2 and link4
1172 Set<TopologyEdge> tempEdges = new HashSet<>();
1173 tempEdges.add(new DefaultTopologyEdge(D2, D4, link2));
1174 tempEdges.add(new DefaultTopologyEdge(D3, D4, link4));
1175 Set<TopologyVertex> tempVertexes = new HashSet<>();
1176 tempVertexes.add(D4);
1177 topologyService.changeInTopology(getGraph(tempVertexes, tempEdges));
1178 listener.event(event);
1179
1180 //No path
1181 assertThat(pathService.paths().size(), is(0));
1182 }
1183
1184 /**
1185 * Tests resilency when D2 device is suspended.
1186 */
1187 @Test
1188 public void resilencyTest8() {
1189 build4RouterTopo(true, false, false, false, 10);
1190
1191 List<Constraint> constraints = new LinkedList<Constraint>();
1192 CostConstraint costConstraint = new CostConstraint(COST);
1193 constraints.add(costConstraint);
1194 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
1195 constraints.add(localBwConst);
1196
1197 //Setup the path , tunnel created
1198 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
1199 assertThat(result, is(true));
1200
1201 List<Event> reasons = new LinkedList<>();
1202 LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link1);
1203 reasons.add(linkEvent);
1204 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link2);
1205 reasons.add(linkEvent);
1206
1207 final TopologyEvent event = new TopologyEvent(
1208 TopologyEvent.Type.TOPOLOGY_CHANGED,
1209 topology,
1210 reasons);
1211
1212 //Change Topology : remove device2 , link1 and link2
1213 Set<TopologyEdge> tempEdges = new HashSet<>();
1214 tempEdges.add(new DefaultTopologyEdge(D1, D2, link1));
1215 tempEdges.add(new DefaultTopologyEdge(D2, D4, link2));
1216 Set<TopologyVertex> tempVertexes = new HashSet<>();
1217 tempVertexes.add(D2);
1218 topologyService.changeInTopology(getGraph(tempVertexes, tempEdges));
1219 listener.event(event);
1220
1221 List<Link> links = new LinkedList<>();
1222 links.add(link3);
1223 links.add(link4);
1224
1225 //Path is D1-D3-D4
1226 assertThat(pathService.paths().iterator().next().links(), is(links));
1227 assertThat(pathService.paths().iterator().next().cost(), is((double) 180));
1228 }
1229
1230 /**
1231 * Tests resilency when D2 device availability is changed.
1232 */
1233 @Test
1234 public void resilencyTest11() {
1235 build4RouterTopo(true, false, false, false, 10);
1236
1237 List<Constraint> constraints = new LinkedList<Constraint>();
1238 CostConstraint costConstraint = new CostConstraint(COST);
1239 constraints.add(costConstraint);
1240 BandwidthConstraint localBwConst = new BandwidthConstraint(Bandwidth.bps(10));
1241 constraints.add(localBwConst);
1242
1243 //Setup the path , tunnel created
1244 boolean result = pceManager.setupPath(D1.deviceId(), D4.deviceId(), "T123", constraints, WITH_SIGNALLING);
1245 assertThat(result, is(true));
1246
1247 List<Event> reasons = new LinkedList<>();
1248 LinkEvent linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link1);
1249 reasons.add(linkEvent);
1250 linkEvent = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link2);
1251 reasons.add(linkEvent);
1252
1253 final TopologyEvent event = new TopologyEvent(
1254 TopologyEvent.Type.TOPOLOGY_CHANGED,
1255 topology,
1256 reasons);
1257
1258 //Change Topology : remove device2 , link1 and link2
1259 Set<TopologyEdge> tempEdges = new HashSet<>();
1260 tempEdges.add(new DefaultTopologyEdge(D1, D2, link1));
1261 tempEdges.add(new DefaultTopologyEdge(D2, D4, link2));
1262 Set<TopologyVertex> tempVertexes = new HashSet<>();
1263 tempVertexes.add(D2);
1264 topologyService.changeInTopology(getGraph(tempVertexes, tempEdges));
1265 listener.event(event);
1266
1267 List<Link> links = new LinkedList<>();
1268 links.add(link3);
1269 links.add(link4);
1270
1271 //Path is D1-D3-D4
1272 assertThat(pathService.paths().iterator().next().links(), is(links));
1273 assertThat(pathService.paths().iterator().next().cost(), is((double) 180));
1274 }
1275
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +05301276 /*
1277 * Tests node label allocation/removal in SR-TE case based on device event.
1278 */
1279 @Test
1280 public void deviceEventTest() {
1281 // Make four router topology with SR-TE capabilities.
1282 build4RouterTopo(true, false, true, true, 0);
1283
1284 // Add new L3 device
1285 DefaultAnnotations.Builder builderDev5 = DefaultAnnotations.builder();
1286 builderDev5.set(AnnotationKeys.TYPE, L3);
1287 builderDev5.set(LSRID, "5.5.5.5");
1288
1289 Device dev5 = new MockDevice(DeviceId.deviceId("P005"), builderDev5.build());
1290 deviceService.addDevice(dev5);
1291
1292 // Add capability
1293 DeviceCapability device5Cap = netConfigRegistry.addConfig(DeviceId.deviceId("5.5.5.5"), DeviceCapability.class);
1294 device5Cap.setLabelStackCap(true)
1295 .setLocalLabelCap(false)
1296 .setSrCap(true)
1297 .apply();
1298
1299 // Get listener
1300 DeviceListener listener = deviceService.getListener();
1301
1302 // Generate Remove events
1303 deviceService.removeDevice(dev5);
1304 DeviceEvent event = new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, dev5);
1305 listener.event(event);
1306
1307 assertThat(pceStore.getGlobalNodeLabel(dev5.id()), is(nullValue()));
1308 }
1309
1310 /**
1311 * Tests adjacency label allocation/removal in SR-TE case based on link event.
1312 */
1313 @Test
1314 public void linkEventTest() {
1315 // Make four router topology with SR-TE capabilities.
1316 build4RouterTopo(true, false, true, true, 0);
1317
1318 // Get listener
1319 LinkListener listener = linkService.getListener();
1320
1321 // Adding link3
1322 linkService.addLink(link3);
1323
1324 // Generate events
1325 LinkEvent event = new LinkEvent(LinkEvent.Type.LINK_ADDED, link3);
1326 listener.event(event);
1327
1328 assertThat(pceStore.getAdjLabel(link3), is(notNullValue()));
1329
1330 // Adding link4
1331 linkService.addLink(link4);
1332
1333 event = new LinkEvent(LinkEvent.Type.LINK_ADDED, link4);
1334 listener.event(event);
1335
1336 assertThat(pceStore.getAdjLabel(link4), is(notNullValue()));
1337
1338 // Remove link3
1339 linkService.removeLink(link3);
1340
1341 // Generate events
1342 event = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link3);
1343 listener.event(event);
1344
1345 assertThat(pceStore.getAdjLabel(link3), is(nullValue()));
1346
1347 // Remove link4
1348 linkService.removeLink(link4);
1349
1350 event = new LinkEvent(LinkEvent.Type.LINK_REMOVED, link4);
1351 listener.event(event);
1352
1353 assertThat(pceStore.getAdjLabel(link4), is(nullValue()));
1354 }
1355
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301356 @After
1357 public void tearDown() {
1358 pceManager.deactivate();
1359 pceManager.pathService = null;
1360 pceManager.resourceService = null;
1361 pceManager.tunnelService = null;
1362 pceManager.coreService = null;
1363 pceManager.storageService = null;
1364 pceManager.packetService = null;
1365 pceManager.deviceService = null;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +05301366 pceManager.linkService = null;
1367 pceManager.netCfgService = null;
1368 pceManager.labelRsrcAdminService = null;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301369 pceManager.labelRsrcService = null;
1370 pceManager.flowObjectiveService = null;
1371 pceManager.pceStore = null;
Priyanka B3f92c5a2016-05-27 10:14:16 +05301372 pceManager.topologyService = null;
1373 pceManager.mastershipService = null;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301374 flowsDownloaded = 0;
1375 }
1376
1377 private class MockTopologyService extends TopologyServiceAdapter {
Priyanka B3f92c5a2016-05-27 10:14:16 +05301378 private void changeInTopology(TopologyGraph graphModified) {
1379 graph = graphModified;
1380 }
1381
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301382 @Override
1383 public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) {
1384 DefaultTopologyVertex srcV = new DefaultTopologyVertex(src);
1385 DefaultTopologyVertex dstV = new DefaultTopologyVertex(dst);
1386 Set<TopologyVertex> vertices = graph.getVertexes();
1387 if (!vertices.contains(srcV) || !vertices.contains(dstV)) {
1388 // src or dst not part of the current graph
1389 return ImmutableSet.of();
1390 }
1391
1392 GraphPathSearch.Result<TopologyVertex, TopologyEdge> result = PathComputationTest.graphSearch()
1393 .search(graph, srcV, dstV, weight, ALL_PATHS);
1394 ImmutableSet.Builder<Path> builder = ImmutableSet.builder();
1395 for (org.onlab.graph.Path<TopologyVertex, TopologyEdge> path : result.paths()) {
1396 builder.add(PathComputationTest.networkPath(path));
1397 }
1398 return builder.build();
1399 }
1400 }
1401
Priyanka B3f92c5a2016-05-27 10:14:16 +05301402 private TopologyGraph getGraph(Set<TopologyVertex> removedVertex, Set<TopologyEdge> removedEdges) {
1403 if (removedVertex != null) {
1404 vertexes.remove(removedVertex);
1405 removedVertex.forEach(v ->
1406 {
1407 vertexes.remove(v);
1408 });
1409 }
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301410
Priyanka B3f92c5a2016-05-27 10:14:16 +05301411 if (removedEdges != null) {
1412 removedEdges.forEach(e ->
1413 {
1414 edges.remove(e);
1415 });
1416 }
1417
1418 return new DefaultTopologyGraph(vertexes, edges);
1419 }
1420
1421 private class MockPathService extends PathServiceAdapter {
1422 Set<Path> computedPaths;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301423 @Override
1424 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
1425 // If either edge is null, bail with no paths.
1426 if (src == null || dst == null) {
1427 return ImmutableSet.of();
1428 }
1429
1430 // Otherwise get all paths between the source and destination edge
1431 // devices.
Priyanka B3f92c5a2016-05-27 10:14:16 +05301432 computedPaths = topologyService.getPaths(null, (DeviceId) src, (DeviceId) dst, weight);
1433 return computedPaths;
1434 }
1435
1436 private Set<Path> paths() {
1437 return computedPaths;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301438 }
1439 }
1440
1441 private class MockTunnelServiceAdapter extends TunnelServiceAdapter {
1442 private HashMap<TunnelId, Tunnel> tunnelIdAsKeyStore = new HashMap<TunnelId, Tunnel>();
1443 private int tunnelIdCounter = 0;
1444
1445 @Override
1446 public TunnelId setupTunnel(ApplicationId producerId, ElementId srcElementId, Tunnel tunnel, Path path) {
1447 TunnelId tunnelId = TunnelId.valueOf(String.valueOf(++tunnelIdCounter));
1448 Tunnel tunnelToInsert = new DefaultTunnel(tunnel.providerId(), tunnel.src(), tunnel.dst(), tunnel.type(),
1449 tunnel.state(), tunnel.groupId(), tunnelId, tunnel.tunnelName(),
1450 path, tunnel.annotations());
1451 tunnelIdAsKeyStore.put(tunnelId, tunnelToInsert);
1452 return tunnelId;
1453 }
1454
1455 @Override
1456 public void addListener(TunnelListener listener) {
1457 tunnelListener = listener;
1458 }
1459
1460 /**
1461 * Stimulates the effect of receiving PLSP id and LSP id from protocol PCRpt msg.
1462 */
1463 public TunnelId updateTunnelWithLspIds(Tunnel tunnel, String pLspId, String localLspId, State state) {
1464 TunnelId tunnelId = tunnel.tunnelId();
1465 Builder annotationBuilder = DefaultAnnotations.builder();
1466 annotationBuilder.putAll(tunnel.annotations());
1467
1468 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1469 if (tunnel.annotations().value(PLSP_ID) == null) {
1470 annotationBuilder.set(PLSP_ID, pLspId);
1471 }
1472
1473 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1474 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1475 annotationBuilder.set(LOCAL_LSP_ID, localLspId);
1476 }
1477 SparseAnnotations annotations = annotationBuilder.build();
1478 tunnelIdAsKeyStore.remove(tunnelId, tunnel);
1479
1480 Tunnel tunnelToInsert = new DefaultTunnel(tunnel.providerId(), tunnel.src(), tunnel.dst(), tunnel.type(),
1481 state, tunnel.groupId(), tunnelId, tunnel.tunnelName(),
1482 tunnel.path(), annotations);
1483
1484 tunnelIdAsKeyStore.put(tunnelId, tunnelToInsert);
1485
1486 return tunnelId;
1487 }
1488
1489 @Override
1490 public boolean downTunnel(ApplicationId producerId, TunnelId tunnelId) {
1491 for (TunnelId tunnelIdKey : tunnelIdAsKeyStore.keySet()) {
1492 if (tunnelIdKey.equals(tunnelId)) {
1493 tunnelIdAsKeyStore.remove(tunnelId);
1494 return true;
1495 }
1496 }
1497 return false;
1498 }
1499
1500 @Override
1501 public Tunnel queryTunnel(TunnelId tunnelId) {
1502 for (TunnelId tunnelIdKey : tunnelIdAsKeyStore.keySet()) {
1503 if (tunnelIdKey.equals(tunnelId)) {
1504 return tunnelIdAsKeyStore.get(tunnelId);
1505 }
1506 }
1507 return null;
1508 }
1509
1510 @Override
1511 public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) {
1512 Collection<Tunnel> result = new HashSet<Tunnel>();
1513 Tunnel tunnel = null;
1514 for (TunnelId tunnelId : tunnelIdAsKeyStore.keySet()) {
1515 tunnel = tunnelIdAsKeyStore.get(tunnelId);
1516
1517 if ((null != tunnel) && (src.equals(tunnel.src())) && (dst.equals(tunnel.dst()))) {
1518 result.add(tunnel);
1519 }
1520 }
1521
1522 return result.size() == 0 ? Collections.emptySet() : ImmutableSet.copyOf(result);
1523 }
1524
1525 @Override
1526 public Collection<Tunnel> queryTunnel(Tunnel.Type type) {
1527 Collection<Tunnel> result = new HashSet<Tunnel>();
1528
1529 for (TunnelId tunnelId : tunnelIdAsKeyStore.keySet()) {
1530 result.add(tunnelIdAsKeyStore.get(tunnelId));
1531 }
1532
1533 return result.size() == 0 ? Collections.emptySet() : ImmutableSet.copyOf(result);
1534 }
Priyanka B3f92c5a2016-05-27 10:14:16 +05301535
1536 @Override
1537 public Collection<Tunnel> queryAllTunnels() {
1538 Collection<Tunnel> result = new HashSet<Tunnel>();
1539
1540 for (TunnelId tunnelId : tunnelIdAsKeyStore.keySet()) {
1541 result.add(tunnelIdAsKeyStore.get(tunnelId));
1542 }
1543
1544 return result.size() == 0 ? Collections.emptySet() : ImmutableSet.copyOf(result);
1545 }
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +05301546
Priyanka B3f92c5a2016-05-27 10:14:16 +05301547 @Override
1548 public Iterable<Tunnel> getTunnels(DeviceId deviceId) {
1549 List<Tunnel> tunnelList = new LinkedList<>();
1550
1551 for (Tunnel t : tunnelIdAsKeyStore.values()) {
1552 for (Link l : t.path().links()) {
1553 if (l.src().deviceId().equals(deviceId) || l.dst().deviceId().equals(deviceId)) {
1554 tunnelList.add(t);
1555 break;
1556 }
1557 }
1558 }
1559 return tunnelList;
1560 }
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301561 }
1562
1563 public static class MockCoreService extends CoreServiceAdapter {
1564
1565 @Override
1566 public ApplicationId registerApplication(String name) {
1567 return new DefaultApplicationId(1, name);
1568 }
1569
1570 @Override
1571 public IdGenerator getIdGenerator(String topic) {
1572 return new IdGenerator() {
1573 private AtomicLong counter = new AtomicLong(0);
1574
1575 @Override
1576 public long getNewId() {
1577 return counter.getAndIncrement();
1578 }
1579 };
1580 }
1581 }
1582
1583 private class MockDevice extends DefaultDevice {
1584 MockDevice(DeviceId id, Annotations annotations) {
1585 super(null, id, null, null, null, null, null, null, annotations);
1586 }
1587 }
1588
Avantika-Huaweidbdf7722016-05-21 14:20:31 +05301589 private PacketProcessor pktProcessor = null;
1590
1591 private class MockPacketService extends PacketServiceAdapter {
1592 @Override
1593 public void addProcessor(PacketProcessor processor, int priority) {
1594 pktProcessor = processor;
1595 }
1596 }
1597
1598 // Minimal PacketContext to make core and applications happy.
1599 final class MockPcepPacketContext extends DefaultPacketContext {
1600 private MockPcepPacketContext(InboundPacket inPkt, OutboundPacket outPkt) {
1601 super(System.currentTimeMillis(), inPkt, outPkt, false);
1602 }
1603
1604 @Override
1605 public void send() {
1606 }
1607 }
1608
1609 public static class MockFlowObjService extends FlowObjServiceAdapter {
1610 @Override
1611 public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) {
1612 ++flowsDownloaded;
1613 }
1614 }
1615}