blob: 7b3a92c28d2b99ec8e1eac337cccfba11eb11e68 [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;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080030import org.onlab.graph.ScalarWeight;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053031import org.onlab.packet.IpAddress;
Yi Tsengfa394de2017-02-01 11:26:40 -080032import org.onosproject.core.GroupId;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053033import org.onosproject.incubator.net.tunnel.Tunnel;
34import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
35import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
36import org.onosproject.incubator.net.tunnel.TunnelName;
37import org.onosproject.incubator.net.tunnel.TunnelId;
38import org.onosproject.incubator.net.tunnel.DefaultTunnel;
39import org.onosproject.incubator.net.resource.label.LabelResourceId;
40import org.onosproject.incubator.net.resource.label.LabelResourceService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053041import org.onosproject.net.AnnotationKeys;
42import org.onosproject.net.Annotations;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053043import org.onosproject.net.ConnectPoint;
44import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053045import org.onosproject.net.DefaultDevice;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053046import org.onosproject.net.DefaultPath;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053047import org.onosproject.net.Device;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053048import org.onosproject.net.DeviceId;
49import org.onosproject.net.PortNumber;
50import org.onosproject.net.Path;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053051import org.onosproject.net.provider.ProviderId;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053052import org.onosproject.pcelabelstore.api.LspLocalLabelInfo;
53import org.onosproject.pcelabelstore.api.PceLabelStore;
54import org.onosproject.pcelabelstore.util.LabelResourceAdapter;
55import org.onosproject.pcelabelstore.util.MockDeviceService;
56import org.onosproject.pcelabelstore.util.PceLabelStoreAdapter;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053057import org.onosproject.pcep.server.impl.BasicPceccHandler;
58import org.onosproject.pcep.server.impl.PcepClientControllerImpl;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053059import org.onosproject.net.DefaultLink;
60import org.onosproject.net.Link;
61
62/**
63 * Unit tests for BasicPceccHandler class.
64 */
65public class BasicPceccHandlerTest {
66
67 public static final long LOCAL_LABEL_SPACE_MIN = 5122;
68 public static final long LOCAL_LABEL_SPACE_MAX = 9217;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053069 private static final String L3 = "L3";
70 private static final String LSRID = "lsrId";
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053071
72 private BasicPceccHandler pceccHandler;
73 protected LabelResourceService labelRsrcService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053074 protected MockDeviceService deviceService;
75 protected PceLabelStore pceStore;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053076 private TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
77 private TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
Yi Tsengfa394de2017-02-01 11:26:40 -080078 private GroupId groupId = new GroupId(92034);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053079 private TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
80 private TunnelId tunnelId = TunnelId.valueOf("41654654");
81 private ProviderId producerName = new ProviderId("producer1", "13");
82 private Path path;
83 private Tunnel tunnel;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053084 List<LspLocalLabelInfo> lspLocalLabelInfoList;
85 private Device deviceD1, deviceD2, deviceD3, deviceD4, deviceD5;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053086 private DeviceId deviceId1;
87 private DeviceId deviceId2;
88 private DeviceId deviceId3;
89 private DeviceId deviceId4;
90 private DeviceId deviceId5;
91 private PortNumber port1;
92 private PortNumber port2;
93 private PortNumber port3;
94 private PortNumber port4;
95 private PortNumber port5;
96
97 @Before
98 public void setUp() throws Exception {
99 pceccHandler = BasicPceccHandler.getInstance();
100 labelRsrcService = new LabelResourceAdapter();
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530101 pceStore = new PceLabelStoreAdapter();
102 deviceService = new MockDeviceService();
103 pceccHandler.initialize(labelRsrcService,
104 deviceService,
105 pceStore,
106 new PcepClientControllerImpl());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530107
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530108 // Create tunnel test
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530109 // Link
110 ProviderId providerId = new ProviderId("of", "foo");
111 deviceId1 = DeviceId.deviceId("of:A");
112 deviceId2 = DeviceId.deviceId("of:B");
113 deviceId3 = DeviceId.deviceId("of:C");
114 deviceId4 = DeviceId.deviceId("of:D");
115 deviceId5 = DeviceId.deviceId("of:E");
116 port1 = PortNumber.portNumber(1);
117 port2 = PortNumber.portNumber(2);
118 port3 = PortNumber.portNumber(3);
119 port4 = PortNumber.portNumber(4);
120 port5 = PortNumber.portNumber(5);
121 List<Link> linkList = new LinkedList<>();
122
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530123 // Making L3 devices
124 DefaultAnnotations.Builder builderDev1 = DefaultAnnotations.builder();
125 builderDev1.set(AnnotationKeys.TYPE, L3);
126 builderDev1.set(LSRID, "1.1.1.1");
127 deviceD1 = new MockDevice(deviceId1, builderDev1.build());
128 deviceService.addDevice(deviceD1);
129
130 // Making L3 devices
131 DefaultAnnotations.Builder builderDev2 = DefaultAnnotations.builder();
132 builderDev2.set(AnnotationKeys.TYPE, L3);
133 builderDev2.set(LSRID, "2.2.2.2");
134 deviceD2 = new MockDevice(deviceId2, builderDev2.build());
135 deviceService.addDevice(deviceD2);
136
137 // Making L3 devices
138 DefaultAnnotations.Builder builderDev3 = DefaultAnnotations.builder();
139 builderDev3.set(AnnotationKeys.TYPE, L3);
140 builderDev3.set(LSRID, "3.3.3.3");
141 deviceD3 = new MockDevice(deviceId3, builderDev3.build());
142 deviceService.addDevice(deviceD3);
143
144 // Making L3 devices
145 DefaultAnnotations.Builder builderDev4 = DefaultAnnotations.builder();
146 builderDev4.set(AnnotationKeys.TYPE, L3);
147 builderDev4.set(LSRID, "4.4.4.4");
148 deviceD4 = new MockDevice(deviceId4, builderDev4.build());
149 deviceService.addDevice(deviceD4);
150
151 // Making L3 devices
152 DefaultAnnotations.Builder builderDev5 = DefaultAnnotations.builder();
153 builderDev5.set(AnnotationKeys.TYPE, L3);
154 builderDev5.set(LSRID, "5.5.5.5");
155 deviceD5 = new MockDevice(deviceId5, builderDev5.build());
156 deviceService.addDevice(deviceD5);
157
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530158 Link l1 = DefaultLink.builder()
159 .providerId(providerId)
160 .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
161 .src(new ConnectPoint(deviceId1, port1))
162 .dst(new ConnectPoint(deviceId2, port2))
163 .type(DIRECT)
164 .state(Link.State.ACTIVE)
165 .build();
166 linkList.add(l1);
167 Link l2 = DefaultLink.builder()
168 .providerId(providerId)
169 .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
170 .src(new ConnectPoint(deviceId2, port2))
171 .dst(new ConnectPoint(deviceId3, port3))
172 .type(DIRECT)
173 .state(Link.State.ACTIVE)
174 .build();
175 linkList.add(l2);
176 Link l3 = DefaultLink.builder()
177 .providerId(providerId)
178 .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
179 .src(new ConnectPoint(deviceId3, port3))
180 .dst(new ConnectPoint(deviceId4, port4))
181 .type(DIRECT)
182 .state(Link.State.ACTIVE)
183 .build();
184 linkList.add(l3);
185 Link l4 = DefaultLink.builder()
186 .providerId(providerId)
187 .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
188 .src(new ConnectPoint(deviceId4, port4))
189 .dst(new ConnectPoint(deviceId5, port5))
190 .type(DIRECT)
191 .state(Link.State.ACTIVE)
192 .build();
193 linkList.add(l4);
194
195 // Path
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800196 path = new DefaultPath(providerId, linkList, ScalarWeight.toWeight(10));
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530197
198 // Tunnel
199 tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
200 Tunnel.State.ACTIVE, groupId, tunnelId,
201 tunnelName, path);
202 }
203
204 @After
205 public void tearDown() throws Exception {
206 }
207
208 /**
209 * Checks the operation of getInstance() method.
210 */
211 @Test
212 public void testGetInstance() {
213 assertThat(pceccHandler, is(notNullValue()));
214 }
215
216 /**
217 * Checks the operation of allocateLabel() method.
218 */
219 @Test
220 public void testAllocateLabel() {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530221 Iterator<LspLocalLabelInfo> iterator;
222 LspLocalLabelInfo lspLocalLabelInfo;
223 DeviceId deviceId;
224 LabelResourceId inLabelId;
225 LabelResourceId outLabelId;
226 PortNumber inPort;
227 PortNumber outPort;
228
229 // check allocation result
230 assertThat(pceccHandler.allocateLabel(tunnel), is(true));
231
232 // Check list of devices with IN and OUT labels whether stored properly in store
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530233 lspLocalLabelInfoList = pceStore.getTunnelInfo(tunnel.tunnelId());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530234 iterator = lspLocalLabelInfoList.iterator();
235
236 // Retrieve values and check device5
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530237 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530238 deviceId = lspLocalLabelInfo.deviceId();
239 inLabelId = lspLocalLabelInfo.inLabelId();
240 outLabelId = lspLocalLabelInfo.outLabelId();
241 inPort = lspLocalLabelInfo.inPort();
242 outPort = lspLocalLabelInfo.outPort();
243
244 assertThat(deviceId, is(deviceId5));
245 assertThat(inLabelId, is(notNullValue()));
246 assertThat(outLabelId, is(nullValue()));
247 assertThat(inPort, is(port5));
248 assertThat(outPort, is(nullValue()));
249
250 // Next element check
251 // Retrieve values and check device4
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530252 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530253 deviceId = lspLocalLabelInfo.deviceId();
254 inLabelId = lspLocalLabelInfo.inLabelId();
255 outLabelId = lspLocalLabelInfo.outLabelId();
256 inPort = lspLocalLabelInfo.inPort();
257 outPort = lspLocalLabelInfo.outPort();
258
259 assertThat(deviceId, is(deviceId4));
260 assertThat(inLabelId, is(notNullValue()));
261 assertThat(outLabelId, is(notNullValue()));
262 assertThat(inPort, is(port4));
263 assertThat(outPort, is(port5));
264
265 // Next element check
266 // Retrieve values and check device3
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530267 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530268 deviceId = lspLocalLabelInfo.deviceId();
269 inLabelId = lspLocalLabelInfo.inLabelId();
270 outLabelId = lspLocalLabelInfo.outLabelId();
271 inPort = lspLocalLabelInfo.inPort();
272 outPort = lspLocalLabelInfo.outPort();
273
274 assertThat(deviceId, is(deviceId3));
275 assertThat(inLabelId, is(notNullValue()));
276 assertThat(outLabelId, is(notNullValue()));
277 assertThat(inPort, is(port3));
278 assertThat(outPort, is(port4));
279
280 // Next element check
281 // Retrieve values and check device2
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530282 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530283 deviceId = lspLocalLabelInfo.deviceId();
284 inLabelId = lspLocalLabelInfo.inLabelId();
285 outLabelId = lspLocalLabelInfo.outLabelId();
286 inPort = lspLocalLabelInfo.inPort();
287 outPort = lspLocalLabelInfo.outPort();
288
289 assertThat(deviceId, is(deviceId2));
290 assertThat(inLabelId, is(notNullValue()));
291 assertThat(outLabelId, is(notNullValue()));
292 assertThat(inPort, is(port2));
293 assertThat(outPort, is(port3));
294
295 // Next element check
296 // Retrieve values and check device1
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530297 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530298 deviceId = lspLocalLabelInfo.deviceId();
299 inLabelId = lspLocalLabelInfo.inLabelId();
300 outLabelId = lspLocalLabelInfo.outLabelId();
301 inPort = lspLocalLabelInfo.inPort();
302 outPort = lspLocalLabelInfo.outPort();
303
304 assertThat(deviceId, is(deviceId1));
305 assertThat(inLabelId, is(nullValue()));
306 assertThat(outLabelId, is(notNullValue()));
307 assertThat(inPort, is(nullValue()));
308 assertThat(outPort, is(port2));
309 }
310
311 /**
312 * Checks the operation of releaseLabel() method.
313 */
314 @Test
315 public void testReleaseLabel() {
316 // Release tunnels
317 assertThat(pceccHandler.allocateLabel(tunnel), is(true));
318 pceccHandler.releaseLabel(tunnel);
319
320 // Retrieve from store. Store should not contain this tunnel info.
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530321 lspLocalLabelInfoList = pceStore.getTunnelInfo(tunnel.tunnelId());
322 assertThat(lspLocalLabelInfoList, is(nullValue()));
323 }
324
325 private class MockDevice extends DefaultDevice {
326 MockDevice(DeviceId id, Annotations annotations) {
327 super(null, id, null, null, null, null, null, null, annotations);
328 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530329 }
330}