blob: 856c1fdcd468d5553e286fc13e55c08914c83a84 [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
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053018import org.junit.After;
19import org.junit.Before;
20import org.junit.Test;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080021import org.onlab.graph.ScalarWeight;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053022import org.onlab.packet.IpAddress;
Yi Tsengfa394de2017-02-01 11:26:40 -080023import org.onosproject.core.GroupId;
Ray Milkeyd1c34da2018-06-22 18:10:53 -070024import org.onosproject.incubator.net.resource.label.LabelResourceAdapter;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053025import org.onosproject.incubator.net.resource.label.LabelResourceId;
26import org.onosproject.incubator.net.resource.label.LabelResourceService;
Ray Milkeyd1c34da2018-06-22 18:10:53 -070027import org.onosproject.incubator.net.tunnel.DefaultTunnel;
28import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
29import org.onosproject.incubator.net.tunnel.Tunnel;
30import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
31import org.onosproject.incubator.net.tunnel.TunnelId;
32import org.onosproject.incubator.net.tunnel.TunnelName;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053033import org.onosproject.net.AnnotationKeys;
34import org.onosproject.net.Annotations;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053035import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053037import org.onosproject.net.DefaultDevice;
Ray Milkeyd1c34da2018-06-22 18:10:53 -070038import org.onosproject.net.DefaultLink;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053039import org.onosproject.net.DefaultPath;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053040import org.onosproject.net.Device;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053041import org.onosproject.net.DeviceId;
Ray Milkeyd1c34da2018-06-22 18:10:53 -070042import org.onosproject.net.Link;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053043import org.onosproject.net.Path;
Ray Milkeyd1c34da2018-06-22 18:10:53 -070044import org.onosproject.net.PortNumber;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053045import org.onosproject.net.provider.ProviderId;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053046import org.onosproject.pcelabelstore.api.LspLocalLabelInfo;
47import org.onosproject.pcelabelstore.api.PceLabelStore;
Ray Milkeyd1c34da2018-06-22 18:10:53 -070048import org.onosproject.pcelabelstore.api.PceLabelStoreAdapter;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053049import org.onosproject.pcelabelstore.util.MockDeviceService;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053050import org.onosproject.pcep.server.impl.BasicPceccHandler;
51import org.onosproject.pcep.server.impl.PcepClientControllerImpl;
Ray Milkeyd1c34da2018-06-22 18:10:53 -070052
53import java.util.Iterator;
54import java.util.LinkedList;
55import java.util.List;
56
57import static org.hamcrest.MatcherAssert.assertThat;
58import static org.hamcrest.Matchers.is;
59import static org.hamcrest.Matchers.notNullValue;
60import static org.hamcrest.Matchers.nullValue;
61import static org.onosproject.net.Link.Type.DIRECT;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053062
63/**
64 * Unit tests for BasicPceccHandler class.
65 */
66public class BasicPceccHandlerTest {
67
68 public static final long LOCAL_LABEL_SPACE_MIN = 5122;
69 public static final long LOCAL_LABEL_SPACE_MAX = 9217;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053070 private static final String L3 = "L3";
71 private static final String LSRID = "lsrId";
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053072
73 private BasicPceccHandler pceccHandler;
74 protected LabelResourceService labelRsrcService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053075 protected MockDeviceService deviceService;
76 protected PceLabelStore pceStore;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053077 private TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
78 private TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
Yi Tsengfa394de2017-02-01 11:26:40 -080079 private GroupId groupId = new GroupId(92034);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053080 private TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
81 private TunnelId tunnelId = TunnelId.valueOf("41654654");
82 private ProviderId producerName = new ProviderId("producer1", "13");
83 private Path path;
84 private Tunnel tunnel;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053085 List<LspLocalLabelInfo> lspLocalLabelInfoList;
86 private Device deviceD1, deviceD2, deviceD3, deviceD4, deviceD5;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053087 private DeviceId deviceId1;
88 private DeviceId deviceId2;
89 private DeviceId deviceId3;
90 private DeviceId deviceId4;
91 private DeviceId deviceId5;
92 private PortNumber port1;
93 private PortNumber port2;
94 private PortNumber port3;
95 private PortNumber port4;
96 private PortNumber port5;
97
98 @Before
99 public void setUp() throws Exception {
100 pceccHandler = BasicPceccHandler.getInstance();
101 labelRsrcService = new LabelResourceAdapter();
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530102 pceStore = new PceLabelStoreAdapter();
103 deviceService = new MockDeviceService();
104 pceccHandler.initialize(labelRsrcService,
105 deviceService,
106 pceStore,
107 new PcepClientControllerImpl());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530108
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530109 // Create tunnel test
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530110 // Link
111 ProviderId providerId = new ProviderId("of", "foo");
112 deviceId1 = DeviceId.deviceId("of:A");
113 deviceId2 = DeviceId.deviceId("of:B");
114 deviceId3 = DeviceId.deviceId("of:C");
115 deviceId4 = DeviceId.deviceId("of:D");
116 deviceId5 = DeviceId.deviceId("of:E");
117 port1 = PortNumber.portNumber(1);
118 port2 = PortNumber.portNumber(2);
119 port3 = PortNumber.portNumber(3);
120 port4 = PortNumber.portNumber(4);
121 port5 = PortNumber.portNumber(5);
122 List<Link> linkList = new LinkedList<>();
123
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530124 // Making L3 devices
125 DefaultAnnotations.Builder builderDev1 = DefaultAnnotations.builder();
126 builderDev1.set(AnnotationKeys.TYPE, L3);
127 builderDev1.set(LSRID, "1.1.1.1");
128 deviceD1 = new MockDevice(deviceId1, builderDev1.build());
129 deviceService.addDevice(deviceD1);
130
131 // Making L3 devices
132 DefaultAnnotations.Builder builderDev2 = DefaultAnnotations.builder();
133 builderDev2.set(AnnotationKeys.TYPE, L3);
134 builderDev2.set(LSRID, "2.2.2.2");
135 deviceD2 = new MockDevice(deviceId2, builderDev2.build());
136 deviceService.addDevice(deviceD2);
137
138 // Making L3 devices
139 DefaultAnnotations.Builder builderDev3 = DefaultAnnotations.builder();
140 builderDev3.set(AnnotationKeys.TYPE, L3);
141 builderDev3.set(LSRID, "3.3.3.3");
142 deviceD3 = new MockDevice(deviceId3, builderDev3.build());
143 deviceService.addDevice(deviceD3);
144
145 // Making L3 devices
146 DefaultAnnotations.Builder builderDev4 = DefaultAnnotations.builder();
147 builderDev4.set(AnnotationKeys.TYPE, L3);
148 builderDev4.set(LSRID, "4.4.4.4");
149 deviceD4 = new MockDevice(deviceId4, builderDev4.build());
150 deviceService.addDevice(deviceD4);
151
152 // Making L3 devices
153 DefaultAnnotations.Builder builderDev5 = DefaultAnnotations.builder();
154 builderDev5.set(AnnotationKeys.TYPE, L3);
155 builderDev5.set(LSRID, "5.5.5.5");
156 deviceD5 = new MockDevice(deviceId5, builderDev5.build());
157 deviceService.addDevice(deviceD5);
158
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530159 Link l1 = DefaultLink.builder()
160 .providerId(providerId)
161 .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
162 .src(new ConnectPoint(deviceId1, port1))
163 .dst(new ConnectPoint(deviceId2, port2))
164 .type(DIRECT)
165 .state(Link.State.ACTIVE)
166 .build();
167 linkList.add(l1);
168 Link l2 = DefaultLink.builder()
169 .providerId(providerId)
170 .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
171 .src(new ConnectPoint(deviceId2, port2))
172 .dst(new ConnectPoint(deviceId3, port3))
173 .type(DIRECT)
174 .state(Link.State.ACTIVE)
175 .build();
176 linkList.add(l2);
177 Link l3 = DefaultLink.builder()
178 .providerId(providerId)
179 .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
180 .src(new ConnectPoint(deviceId3, port3))
181 .dst(new ConnectPoint(deviceId4, port4))
182 .type(DIRECT)
183 .state(Link.State.ACTIVE)
184 .build();
185 linkList.add(l3);
186 Link l4 = DefaultLink.builder()
187 .providerId(providerId)
188 .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
189 .src(new ConnectPoint(deviceId4, port4))
190 .dst(new ConnectPoint(deviceId5, port5))
191 .type(DIRECT)
192 .state(Link.State.ACTIVE)
193 .build();
194 linkList.add(l4);
195
196 // Path
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800197 path = new DefaultPath(providerId, linkList, ScalarWeight.toWeight(10));
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530198
199 // Tunnel
200 tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
201 Tunnel.State.ACTIVE, groupId, tunnelId,
202 tunnelName, path);
203 }
204
205 @After
206 public void tearDown() throws Exception {
207 }
208
209 /**
210 * Checks the operation of getInstance() method.
211 */
212 @Test
213 public void testGetInstance() {
214 assertThat(pceccHandler, is(notNullValue()));
215 }
216
217 /**
218 * Checks the operation of allocateLabel() method.
219 */
220 @Test
221 public void testAllocateLabel() {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530222 Iterator<LspLocalLabelInfo> iterator;
223 LspLocalLabelInfo lspLocalLabelInfo;
224 DeviceId deviceId;
225 LabelResourceId inLabelId;
226 LabelResourceId outLabelId;
227 PortNumber inPort;
228 PortNumber outPort;
229
230 // check allocation result
231 assertThat(pceccHandler.allocateLabel(tunnel), is(true));
232
233 // Check list of devices with IN and OUT labels whether stored properly in store
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530234 lspLocalLabelInfoList = pceStore.getTunnelInfo(tunnel.tunnelId());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530235 iterator = lspLocalLabelInfoList.iterator();
236
237 // Retrieve values and check device5
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530238 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530239 deviceId = lspLocalLabelInfo.deviceId();
240 inLabelId = lspLocalLabelInfo.inLabelId();
241 outLabelId = lspLocalLabelInfo.outLabelId();
242 inPort = lspLocalLabelInfo.inPort();
243 outPort = lspLocalLabelInfo.outPort();
244
245 assertThat(deviceId, is(deviceId5));
246 assertThat(inLabelId, is(notNullValue()));
247 assertThat(outLabelId, is(nullValue()));
248 assertThat(inPort, is(port5));
249 assertThat(outPort, is(nullValue()));
250
251 // Next element check
252 // Retrieve values and check device4
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530253 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530254 deviceId = lspLocalLabelInfo.deviceId();
255 inLabelId = lspLocalLabelInfo.inLabelId();
256 outLabelId = lspLocalLabelInfo.outLabelId();
257 inPort = lspLocalLabelInfo.inPort();
258 outPort = lspLocalLabelInfo.outPort();
259
260 assertThat(deviceId, is(deviceId4));
261 assertThat(inLabelId, is(notNullValue()));
262 assertThat(outLabelId, is(notNullValue()));
263 assertThat(inPort, is(port4));
264 assertThat(outPort, is(port5));
265
266 // Next element check
267 // Retrieve values and check device3
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530268 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530269 deviceId = lspLocalLabelInfo.deviceId();
270 inLabelId = lspLocalLabelInfo.inLabelId();
271 outLabelId = lspLocalLabelInfo.outLabelId();
272 inPort = lspLocalLabelInfo.inPort();
273 outPort = lspLocalLabelInfo.outPort();
274
275 assertThat(deviceId, is(deviceId3));
276 assertThat(inLabelId, is(notNullValue()));
277 assertThat(outLabelId, is(notNullValue()));
278 assertThat(inPort, is(port3));
279 assertThat(outPort, is(port4));
280
281 // Next element check
282 // Retrieve values and check device2
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530283 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530284 deviceId = lspLocalLabelInfo.deviceId();
285 inLabelId = lspLocalLabelInfo.inLabelId();
286 outLabelId = lspLocalLabelInfo.outLabelId();
287 inPort = lspLocalLabelInfo.inPort();
288 outPort = lspLocalLabelInfo.outPort();
289
290 assertThat(deviceId, is(deviceId2));
291 assertThat(inLabelId, is(notNullValue()));
292 assertThat(outLabelId, is(notNullValue()));
293 assertThat(inPort, is(port2));
294 assertThat(outPort, is(port3));
295
296 // Next element check
297 // Retrieve values and check device1
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530298 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530299 deviceId = lspLocalLabelInfo.deviceId();
300 inLabelId = lspLocalLabelInfo.inLabelId();
301 outLabelId = lspLocalLabelInfo.outLabelId();
302 inPort = lspLocalLabelInfo.inPort();
303 outPort = lspLocalLabelInfo.outPort();
304
305 assertThat(deviceId, is(deviceId1));
306 assertThat(inLabelId, is(nullValue()));
307 assertThat(outLabelId, is(notNullValue()));
308 assertThat(inPort, is(nullValue()));
309 assertThat(outPort, is(port2));
310 }
311
312 /**
313 * Checks the operation of releaseLabel() method.
314 */
315 @Test
316 public void testReleaseLabel() {
317 // Release tunnels
318 assertThat(pceccHandler.allocateLabel(tunnel), is(true));
319 pceccHandler.releaseLabel(tunnel);
320
321 // Retrieve from store. Store should not contain this tunnel info.
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530322 lspLocalLabelInfoList = pceStore.getTunnelInfo(tunnel.tunnelId());
323 assertThat(lspLocalLabelInfoList, is(nullValue()));
324 }
325
326 private class MockDevice extends DefaultDevice {
327 MockDevice(DeviceId id, Annotations annotations) {
328 super(null, id, null, null, null, null, null, null, annotations);
329 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530330 }
331}