blob: cd28f9ebf12f4e01e409a40b2cafd5d864c3ac9e [file] [log] [blame]
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.pce.pceservice;
17
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;
23
24import java.util.Iterator;
25import java.util.List;
26import java.util.LinkedList;
27
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053031import org.onlab.packet.IpAddress;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053034import org.onosproject.core.DefaultGroupId;
35import org.onosproject.incubator.net.tunnel.Tunnel;
36import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
37import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
38import org.onosproject.incubator.net.tunnel.TunnelName;
39import org.onosproject.incubator.net.tunnel.TunnelId;
40import org.onosproject.incubator.net.tunnel.DefaultTunnel;
41import org.onosproject.incubator.net.resource.label.LabelResourceId;
42import org.onosproject.incubator.net.resource.label.LabelResourceService;
43import org.onosproject.net.ConnectPoint;
44import org.onosproject.net.DefaultAnnotations;
45import org.onosproject.net.DefaultPath;
46import org.onosproject.net.DeviceId;
47import org.onosproject.net.PortNumber;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053048import org.onosproject.net.flowobjective.FlowObjectiveService;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053049import org.onosproject.net.Path;
50import org.onosproject.pce.pcestore.api.LspLocalLabelInfo;
51import org.onosproject.pce.pcestore.api.PceStore;
52import org.onosproject.pce.pcestore.PceccTunnelInfo;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053053import org.onosproject.net.provider.ProviderId;
54import org.onosproject.pce.util.LabelResourceAdapter;
55import org.onosproject.pce.util.PceStoreAdapter;
56import org.onosproject.net.DefaultLink;
57import org.onosproject.net.Link;
58
59/**
60 * Unit tests for BasicPceccHandler class.
61 */
62public class BasicPceccHandlerTest {
63
64 public static final long LOCAL_LABEL_SPACE_MIN = 5122;
65 public static final long LOCAL_LABEL_SPACE_MAX = 9217;
66
67 private BasicPceccHandler pceccHandler;
68 protected LabelResourceService labelRsrcService;
69 protected PceStore pceStore;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053070 private FlowObjectiveService flowObjectiveService;
71 private CoreService coreService;
72 private ApplicationId appId;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053073 private TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
74 private TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
75 private DefaultGroupId groupId = new DefaultGroupId(92034);
76 private TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
77 private TunnelId tunnelId = TunnelId.valueOf("41654654");
78 private ProviderId producerName = new ProviderId("producer1", "13");
79 private Path path;
80 private Tunnel tunnel;
81 private PceccTunnelInfo pceccTunnelInfo;
82 private DeviceId deviceId1;
83 private DeviceId deviceId2;
84 private DeviceId deviceId3;
85 private DeviceId deviceId4;
86 private DeviceId deviceId5;
87 private PortNumber port1;
88 private PortNumber port2;
89 private PortNumber port3;
90 private PortNumber port4;
91 private PortNumber port5;
92
93 @Before
94 public void setUp() throws Exception {
95 pceccHandler = BasicPceccHandler.getInstance();
96 labelRsrcService = new LabelResourceAdapter();
97 pceStore = new PceStoreAdapter();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053098 flowObjectiveService = new PceManagerTest.MockFlowObjService();
99 coreService = new PceManagerTest.MockCoreService();
100 appId = coreService.registerApplication("org.onosproject.pce");
101 pceccHandler.initialize(labelRsrcService, flowObjectiveService, appId, pceStore);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530102
103 // Cretae tunnel test
104 // Link
105 ProviderId providerId = new ProviderId("of", "foo");
106 deviceId1 = DeviceId.deviceId("of:A");
107 deviceId2 = DeviceId.deviceId("of:B");
108 deviceId3 = DeviceId.deviceId("of:C");
109 deviceId4 = DeviceId.deviceId("of:D");
110 deviceId5 = DeviceId.deviceId("of:E");
111 port1 = PortNumber.portNumber(1);
112 port2 = PortNumber.portNumber(2);
113 port3 = PortNumber.portNumber(3);
114 port4 = PortNumber.portNumber(4);
115 port5 = PortNumber.portNumber(5);
116 List<Link> linkList = new LinkedList<>();
117
118 Link l1 = DefaultLink.builder()
119 .providerId(providerId)
120 .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
121 .src(new ConnectPoint(deviceId1, port1))
122 .dst(new ConnectPoint(deviceId2, port2))
123 .type(DIRECT)
124 .state(Link.State.ACTIVE)
125 .build();
126 linkList.add(l1);
127 Link l2 = DefaultLink.builder()
128 .providerId(providerId)
129 .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
130 .src(new ConnectPoint(deviceId2, port2))
131 .dst(new ConnectPoint(deviceId3, port3))
132 .type(DIRECT)
133 .state(Link.State.ACTIVE)
134 .build();
135 linkList.add(l2);
136 Link l3 = DefaultLink.builder()
137 .providerId(providerId)
138 .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
139 .src(new ConnectPoint(deviceId3, port3))
140 .dst(new ConnectPoint(deviceId4, port4))
141 .type(DIRECT)
142 .state(Link.State.ACTIVE)
143 .build();
144 linkList.add(l3);
145 Link l4 = DefaultLink.builder()
146 .providerId(providerId)
147 .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
148 .src(new ConnectPoint(deviceId4, port4))
149 .dst(new ConnectPoint(deviceId5, port5))
150 .type(DIRECT)
151 .state(Link.State.ACTIVE)
152 .build();
153 linkList.add(l4);
154
155 // Path
156 path = new DefaultPath(providerId, linkList, 10);
157
158 // Tunnel
159 tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
160 Tunnel.State.ACTIVE, groupId, tunnelId,
161 tunnelName, path);
162 }
163
164 @After
165 public void tearDown() throws Exception {
Priyanka B413fbe82016-05-26 11:44:45 +0530166 PceManagerTest.flowsDownloaded = 0;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530167 }
168
169 /**
170 * Checks the operation of getInstance() method.
171 */
172 @Test
173 public void testGetInstance() {
174 assertThat(pceccHandler, is(notNullValue()));
175 }
176
177 /**
178 * Checks the operation of allocateLabel() method.
179 */
180 @Test
181 public void testAllocateLabel() {
182 List<LspLocalLabelInfo> lspLocalLabelInfoList;
183 Iterator<LspLocalLabelInfo> iterator;
184 LspLocalLabelInfo lspLocalLabelInfo;
185 DeviceId deviceId;
186 LabelResourceId inLabelId;
187 LabelResourceId outLabelId;
188 PortNumber inPort;
189 PortNumber outPort;
190
191 // check allocation result
192 assertThat(pceccHandler.allocateLabel(tunnel), is(true));
193
194 // Check list of devices with IN and OUT labels whether stored properly in store
195 pceccTunnelInfo = pceStore.getTunnelInfo(tunnel.tunnelId());
196 lspLocalLabelInfoList = pceccTunnelInfo.lspLocalLabelInfoList();
197 iterator = lspLocalLabelInfoList.iterator();
198
199 // Retrieve values and check device5
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530200 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530201 deviceId = lspLocalLabelInfo.deviceId();
202 inLabelId = lspLocalLabelInfo.inLabelId();
203 outLabelId = lspLocalLabelInfo.outLabelId();
204 inPort = lspLocalLabelInfo.inPort();
205 outPort = lspLocalLabelInfo.outPort();
206
207 assertThat(deviceId, is(deviceId5));
208 assertThat(inLabelId, is(notNullValue()));
209 assertThat(outLabelId, is(nullValue()));
210 assertThat(inPort, is(port5));
211 assertThat(outPort, is(nullValue()));
212
213 // Next element check
214 // Retrieve values and check device4
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530215 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530216 deviceId = lspLocalLabelInfo.deviceId();
217 inLabelId = lspLocalLabelInfo.inLabelId();
218 outLabelId = lspLocalLabelInfo.outLabelId();
219 inPort = lspLocalLabelInfo.inPort();
220 outPort = lspLocalLabelInfo.outPort();
221
222 assertThat(deviceId, is(deviceId4));
223 assertThat(inLabelId, is(notNullValue()));
224 assertThat(outLabelId, is(notNullValue()));
225 assertThat(inPort, is(port4));
226 assertThat(outPort, is(port5));
227
228 // Next element check
229 // Retrieve values and check device3
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530230 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530231 deviceId = lspLocalLabelInfo.deviceId();
232 inLabelId = lspLocalLabelInfo.inLabelId();
233 outLabelId = lspLocalLabelInfo.outLabelId();
234 inPort = lspLocalLabelInfo.inPort();
235 outPort = lspLocalLabelInfo.outPort();
236
237 assertThat(deviceId, is(deviceId3));
238 assertThat(inLabelId, is(notNullValue()));
239 assertThat(outLabelId, is(notNullValue()));
240 assertThat(inPort, is(port3));
241 assertThat(outPort, is(port4));
242
243 // Next element check
244 // Retrieve values and check device2
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530245 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530246 deviceId = lspLocalLabelInfo.deviceId();
247 inLabelId = lspLocalLabelInfo.inLabelId();
248 outLabelId = lspLocalLabelInfo.outLabelId();
249 inPort = lspLocalLabelInfo.inPort();
250 outPort = lspLocalLabelInfo.outPort();
251
252 assertThat(deviceId, is(deviceId2));
253 assertThat(inLabelId, is(notNullValue()));
254 assertThat(outLabelId, is(notNullValue()));
255 assertThat(inPort, is(port2));
256 assertThat(outPort, is(port3));
257
258 // Next element check
259 // Retrieve values and check device1
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530260 lspLocalLabelInfo = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530261 deviceId = lspLocalLabelInfo.deviceId();
262 inLabelId = lspLocalLabelInfo.inLabelId();
263 outLabelId = lspLocalLabelInfo.outLabelId();
264 inPort = lspLocalLabelInfo.inPort();
265 outPort = lspLocalLabelInfo.outPort();
266
267 assertThat(deviceId, is(deviceId1));
268 assertThat(inLabelId, is(nullValue()));
269 assertThat(outLabelId, is(notNullValue()));
270 assertThat(inPort, is(nullValue()));
271 assertThat(outPort, is(port2));
272 }
273
274 /**
275 * Checks the operation of releaseLabel() method.
276 */
277 @Test
278 public void testReleaseLabel() {
279 // Release tunnels
280 assertThat(pceccHandler.allocateLabel(tunnel), is(true));
281 pceccHandler.releaseLabel(tunnel);
282
283 // Retrieve from store. Store should not contain this tunnel info.
284 pceccTunnelInfo = pceStore.getTunnelInfo(tunnel.tunnelId());
285 assertThat(pceccTunnelInfo, is(nullValue()));
286 }
287}