blob: 201b701409c9ef601e22d93fdc69bb81ff83a3a4 [file] [log] [blame]
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +05303 *
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 */
Avantika-Huawei9e848e82016-09-01 12:12:42 +053016package org.onosproject.pcelabelstore.label;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053017
18import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.is;
20import static org.hamcrest.Matchers.notNullValue;
21import static org.hamcrest.Matchers.nullValue;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053022import static org.onosproject.net.Link.Type.DIRECT;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053023import java.util.Iterator;
24import java.util.List;
25import java.util.LinkedList;
26
27import org.junit.After;
28import org.junit.Before;
29import org.junit.Test;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053030import org.onlab.packet.IpAddress;
Yi Tsengfa394de2017-02-01 11:26:40 -080031import org.onosproject.core.GroupId;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053032import org.onosproject.incubator.net.tunnel.Tunnel;
33import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
34import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
35import org.onosproject.incubator.net.tunnel.TunnelName;
36import org.onosproject.incubator.net.tunnel.TunnelId;
37import org.onosproject.incubator.net.tunnel.DefaultTunnel;
38import org.onosproject.incubator.net.resource.label.LabelResourceId;
39import org.onosproject.incubator.net.resource.label.LabelResourceService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053040import org.onosproject.net.AnnotationKeys;
41import org.onosproject.net.Annotations;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053042import org.onosproject.net.ConnectPoint;
43import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053044import org.onosproject.net.DefaultDevice;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053045import org.onosproject.net.DefaultPath;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053046import org.onosproject.net.Device;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053047import org.onosproject.net.DeviceId;
48import org.onosproject.net.PortNumber;
49import org.onosproject.net.Path;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053050import org.onosproject.net.provider.ProviderId;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053051import org.onosproject.pcelabelstore.api.LspLocalLabelInfo;
52import org.onosproject.pcelabelstore.api.PceLabelStore;
53import org.onosproject.pcelabelstore.util.LabelResourceAdapter;
54import org.onosproject.pcelabelstore.util.MockDeviceService;
55import org.onosproject.pcelabelstore.util.PceLabelStoreAdapter;
harikrushna-Huawei6ecfc772017-04-10 18:22:00 +053056import org.onosproject.pcep.server.impl.BasicPceccHandler;
57import org.onosproject.pcep.server.impl.PcepClientControllerImpl;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053058import org.onosproject.net.DefaultLink;
59import org.onosproject.net.Link;
60
61/**
62 * Unit tests for BasicPceccHandler class.
63 */
64public class BasicPceccHandlerTest {
65
66 public static final long LOCAL_LABEL_SPACE_MIN = 5122;
67 public static final long LOCAL_LABEL_SPACE_MAX = 9217;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053068 private static final String L3 = "L3";
69 private static final String LSRID = "lsrId";
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053070
71 private BasicPceccHandler pceccHandler;
72 protected LabelResourceService labelRsrcService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053073 protected MockDeviceService deviceService;
74 protected PceLabelStore pceStore;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053075 private TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
76 private TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
Yi Tsengfa394de2017-02-01 11:26:40 -080077 private GroupId groupId = new GroupId(92034);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053078 private TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
79 private TunnelId tunnelId = TunnelId.valueOf("41654654");
80 private ProviderId producerName = new ProviderId("producer1", "13");
81 private Path path;
82 private Tunnel tunnel;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053083 List<LspLocalLabelInfo> lspLocalLabelInfoList;
84 private Device deviceD1, deviceD2, deviceD3, deviceD4, deviceD5;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053085 private DeviceId deviceId1;
86 private DeviceId deviceId2;
87 private DeviceId deviceId3;
88 private DeviceId deviceId4;
89 private DeviceId deviceId5;
90 private PortNumber port1;
91 private PortNumber port2;
92 private PortNumber port3;
93 private PortNumber port4;
94 private PortNumber port5;
95
96 @Before
97 public void setUp() throws Exception {
98 pceccHandler = BasicPceccHandler.getInstance();
99 labelRsrcService = new LabelResourceAdapter();
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530100 pceStore = new PceLabelStoreAdapter();
101 deviceService = new MockDeviceService();
102 pceccHandler.initialize(labelRsrcService,
103 deviceService,
104 pceStore,
105 new PcepClientControllerImpl());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530106
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530107 // Create tunnel test
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530108 // Link
109 ProviderId providerId = new ProviderId("of", "foo");
110 deviceId1 = DeviceId.deviceId("of:A");
111 deviceId2 = DeviceId.deviceId("of:B");
112 deviceId3 = DeviceId.deviceId("of:C");
113 deviceId4 = DeviceId.deviceId("of:D");
114 deviceId5 = DeviceId.deviceId("of:E");
115 port1 = PortNumber.portNumber(1);
116 port2 = PortNumber.portNumber(2);
117 port3 = PortNumber.portNumber(3);
118 port4 = PortNumber.portNumber(4);
119 port5 = PortNumber.portNumber(5);
120 List<Link> linkList = new LinkedList<>();
121
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530122 // Making L3 devices
123 DefaultAnnotations.Builder builderDev1 = DefaultAnnotations.builder();
124 builderDev1.set(AnnotationKeys.TYPE, L3);
125 builderDev1.set(LSRID, "1.1.1.1");
126 deviceD1 = new MockDevice(deviceId1, builderDev1.build());
127 deviceService.addDevice(deviceD1);
128
129 // Making L3 devices
130 DefaultAnnotations.Builder builderDev2 = DefaultAnnotations.builder();
131 builderDev2.set(AnnotationKeys.TYPE, L3);
132 builderDev2.set(LSRID, "2.2.2.2");
133 deviceD2 = new MockDevice(deviceId2, builderDev2.build());
134 deviceService.addDevice(deviceD2);
135
136 // Making L3 devices
137 DefaultAnnotations.Builder builderDev3 = DefaultAnnotations.builder();
138 builderDev3.set(AnnotationKeys.TYPE, L3);
139 builderDev3.set(LSRID, "3.3.3.3");
140 deviceD3 = new MockDevice(deviceId3, builderDev3.build());
141 deviceService.addDevice(deviceD3);
142
143 // Making L3 devices
144 DefaultAnnotations.Builder builderDev4 = DefaultAnnotations.builder();
145 builderDev4.set(AnnotationKeys.TYPE, L3);
146 builderDev4.set(LSRID, "4.4.4.4");
147 deviceD4 = new MockDevice(deviceId4, builderDev4.build());
148 deviceService.addDevice(deviceD4);
149
150 // Making L3 devices
151 DefaultAnnotations.Builder builderDev5 = DefaultAnnotations.builder();
152 builderDev5.set(AnnotationKeys.TYPE, L3);
153 builderDev5.set(LSRID, "5.5.5.5");
154 deviceD5 = new MockDevice(deviceId5, builderDev5.build());
155 deviceService.addDevice(deviceD5);
156
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530157 Link l1 = DefaultLink.builder()
158 .providerId(providerId)
159 .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
160 .src(new ConnectPoint(deviceId1, port1))
161 .dst(new ConnectPoint(deviceId2, port2))
162 .type(DIRECT)
163 .state(Link.State.ACTIVE)
164 .build();
165 linkList.add(l1);
166 Link l2 = DefaultLink.builder()
167 .providerId(providerId)
168 .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
169 .src(new ConnectPoint(deviceId2, port2))
170 .dst(new ConnectPoint(deviceId3, port3))
171 .type(DIRECT)
172 .state(Link.State.ACTIVE)
173 .build();
174 linkList.add(l2);
175 Link l3 = DefaultLink.builder()
176 .providerId(providerId)
177 .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
178 .src(new ConnectPoint(deviceId3, port3))
179 .dst(new ConnectPoint(deviceId4, port4))
180 .type(DIRECT)
181 .state(Link.State.ACTIVE)
182 .build();
183 linkList.add(l3);
184 Link l4 = DefaultLink.builder()
185 .providerId(providerId)
186 .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
187 .src(new ConnectPoint(deviceId4, port4))
188 .dst(new ConnectPoint(deviceId5, port5))
189 .type(DIRECT)
190 .state(Link.State.ACTIVE)
191 .build();
192 linkList.add(l4);
193
194 // Path
195 path = new DefaultPath(providerId, linkList, 10);
196
197 // Tunnel
198 tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
199 Tunnel.State.ACTIVE, groupId, tunnelId,
200 tunnelName, path);
201 }
202
203 @After
204 public void tearDown() throws Exception {
205 }
206
207 /**
208 * Checks the operation of getInstance() method.
209 */
210 @Test
211 public void testGetInstance() {
212 assertThat(pceccHandler, is(notNullValue()));
213 }
214
215 /**
216 * Checks the operation of allocateLabel() method.
217 */
218 @Test
219 public void testAllocateLabel() {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530220 Iterator<LspLocalLabelInfo> iterator;
221 LspLocalLabelInfo lspLocalLabelInfo;
222 DeviceId deviceId;
223 LabelResourceId inLabelId;
224 LabelResourceId outLabelId;
225 PortNumber inPort;
226 PortNumber outPort;
227
228 // check allocation result
229 assertThat(pceccHandler.allocateLabel(tunnel), is(true));
230
231 // Check list of devices with IN and OUT labels whether stored properly in store
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530232 lspLocalLabelInfoList = pceStore.getTunnelInfo(tunnel.tunnelId());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530233 iterator = lspLocalLabelInfoList.iterator();
234
235 // Retrieve values and check device5
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530236 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530237 deviceId = lspLocalLabelInfo.deviceId();
238 inLabelId = lspLocalLabelInfo.inLabelId();
239 outLabelId = lspLocalLabelInfo.outLabelId();
240 inPort = lspLocalLabelInfo.inPort();
241 outPort = lspLocalLabelInfo.outPort();
242
243 assertThat(deviceId, is(deviceId5));
244 assertThat(inLabelId, is(notNullValue()));
245 assertThat(outLabelId, is(nullValue()));
246 assertThat(inPort, is(port5));
247 assertThat(outPort, is(nullValue()));
248
249 // Next element check
250 // Retrieve values and check device4
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530251 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530252 deviceId = lspLocalLabelInfo.deviceId();
253 inLabelId = lspLocalLabelInfo.inLabelId();
254 outLabelId = lspLocalLabelInfo.outLabelId();
255 inPort = lspLocalLabelInfo.inPort();
256 outPort = lspLocalLabelInfo.outPort();
257
258 assertThat(deviceId, is(deviceId4));
259 assertThat(inLabelId, is(notNullValue()));
260 assertThat(outLabelId, is(notNullValue()));
261 assertThat(inPort, is(port4));
262 assertThat(outPort, is(port5));
263
264 // Next element check
265 // Retrieve values and check device3
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530266 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530267 deviceId = lspLocalLabelInfo.deviceId();
268 inLabelId = lspLocalLabelInfo.inLabelId();
269 outLabelId = lspLocalLabelInfo.outLabelId();
270 inPort = lspLocalLabelInfo.inPort();
271 outPort = lspLocalLabelInfo.outPort();
272
273 assertThat(deviceId, is(deviceId3));
274 assertThat(inLabelId, is(notNullValue()));
275 assertThat(outLabelId, is(notNullValue()));
276 assertThat(inPort, is(port3));
277 assertThat(outPort, is(port4));
278
279 // Next element check
280 // Retrieve values and check device2
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530281 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530282 deviceId = lspLocalLabelInfo.deviceId();
283 inLabelId = lspLocalLabelInfo.inLabelId();
284 outLabelId = lspLocalLabelInfo.outLabelId();
285 inPort = lspLocalLabelInfo.inPort();
286 outPort = lspLocalLabelInfo.outPort();
287
288 assertThat(deviceId, is(deviceId2));
289 assertThat(inLabelId, is(notNullValue()));
290 assertThat(outLabelId, is(notNullValue()));
291 assertThat(inPort, is(port2));
292 assertThat(outPort, is(port3));
293
294 // Next element check
295 // Retrieve values and check device1
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530296 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530297 deviceId = lspLocalLabelInfo.deviceId();
298 inLabelId = lspLocalLabelInfo.inLabelId();
299 outLabelId = lspLocalLabelInfo.outLabelId();
300 inPort = lspLocalLabelInfo.inPort();
301 outPort = lspLocalLabelInfo.outPort();
302
303 assertThat(deviceId, is(deviceId1));
304 assertThat(inLabelId, is(nullValue()));
305 assertThat(outLabelId, is(notNullValue()));
306 assertThat(inPort, is(nullValue()));
307 assertThat(outPort, is(port2));
308 }
309
310 /**
311 * Checks the operation of releaseLabel() method.
312 */
313 @Test
314 public void testReleaseLabel() {
315 // Release tunnels
316 assertThat(pceccHandler.allocateLabel(tunnel), is(true));
317 pceccHandler.releaseLabel(tunnel);
318
319 // Retrieve from store. Store should not contain this tunnel info.
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530320 lspLocalLabelInfoList = pceStore.getTunnelInfo(tunnel.tunnelId());
321 assertThat(lspLocalLabelInfoList, is(nullValue()));
322 }
323
324 private class MockDevice extends DefaultDevice {
325 MockDevice(DeviceId id, Annotations annotations) {
326 super(null, id, null, null, null, null, null, null, annotations);
327 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530328 }
329}