blob: 51f5c2db4eb550d7197a81d863d830273bec3204 [file] [log] [blame]
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +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 */
Priyanka Bb977f562016-07-22 13:02:03 +053016package org.onosproject.pcerest;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053017
18import static org.easymock.EasyMock.anyObject;
19import static org.easymock.EasyMock.createMock;
20import static org.easymock.EasyMock.expect;
21import static org.easymock.EasyMock.replay;
22import static org.hamcrest.Matchers.containsString;
23import static org.hamcrest.Matchers.is;
Mahesh Poojary S33536202016-05-30 07:22:36 +053024import static org.hamcrest.Matchers.notNullValue;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053025import static org.junit.Assert.assertThat;
26import static org.junit.Assert.fail;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053027import static org.onosproject.net.Link.Type.DIRECT;
28
Mahesh Poojary S33536202016-05-30 07:22:36 +053029import com.eclipsesource.json.Json;
30import com.eclipsesource.json.JsonObject;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053031
32import java.io.InputStream;
33import java.net.HttpURLConnection;
34import java.util.LinkedList;
35import java.util.List;
Priyanka Bf97e13d2016-07-28 10:57:15 +053036
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053037import javax.ws.rs.client.Entity;
38import javax.ws.rs.client.WebTarget;
39import javax.ws.rs.core.MediaType;
40import javax.ws.rs.core.Response;
41import javax.ws.rs.NotFoundException;
42
Priyanka Bbae0eeb12016-11-30 11:59:48 +053043import com.google.common.collect.Lists;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053044import org.junit.After;
45import org.junit.Before;
46import org.junit.Test;
Ray Milkey094a1352018-01-22 14:03:54 -080047import org.onlab.junit.TestUtils;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053048import org.onlab.osgi.ServiceDirectory;
49import org.onlab.osgi.TestServiceDirectory;
50import org.onlab.packet.IpAddress;
51import org.onlab.rest.BaseResource;
52import org.onosproject.codec.CodecService;
Satish K2eb5d842017-04-04 16:28:37 +053053// import org.onosproject.core.DefaultGroupId;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053054import org.onosproject.incubator.net.tunnel.DefaultTunnel;
55import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
56import org.onosproject.incubator.net.tunnel.Tunnel;
57import org.onosproject.incubator.net.tunnel.TunnelId;
58import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
59import org.onosproject.incubator.net.tunnel.TunnelName;
Priyanka Bf97e13d2016-07-28 10:57:15 +053060import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053061import org.onosproject.net.ConnectPoint;
62import org.onosproject.net.DefaultAnnotations;
63import org.onosproject.net.DefaultLink;
64import org.onosproject.net.DefaultPath;
65import org.onosproject.net.DeviceId;
66import org.onosproject.net.Link;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053067import org.onosproject.pce.pceservice.ExplicitPathInfo;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053068import org.onosproject.pce.pceservice.api.PceService;
Mahesh Poojary S33536202016-05-30 07:22:36 +053069import org.onosproject.pce.pceservice.PcepAnnotationKeys;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053070import org.onosproject.pce.pcestore.api.PceStore;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053071import org.onosproject.net.Path;
72import org.onosproject.net.PortNumber;
73import org.onosproject.net.provider.ProviderId;
74
75/**
76 * Unit tests for pce path REST APIs.
77 */
78public class PcePathResourceTest extends PceResourceTest {
79 private final PceService pceService = createMock(PceService.class);
Priyanka Bbae0eeb12016-11-30 11:59:48 +053080 private final PceStore pceStore = createMock(PceStore.class);
Priyanka Bf97e13d2016-07-28 10:57:15 +053081 private final TunnelService tunnelService = createMock(TunnelService.class);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053082 private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
83 private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
Satish K2eb5d842017-04-04 16:28:37 +053084 // private final DefaultGroupId groupId = new DefaultGroupId(92034);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053085 private final TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
86 private final TunnelId tunnelId = TunnelId.valueOf("41654654");
87 private final ProviderId producerName = new ProviderId("producer1", "13");
88 private Path path;
89 private Tunnel tunnel;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053090 private List<ExplicitPathInfo> explicitPathInfoList;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053091 private DeviceId deviceId1;
92 private DeviceId deviceId2;
93 private DeviceId deviceId3;
94 private DeviceId deviceId4;
95 private DeviceId deviceId5;
96 private PortNumber port1;
97 private PortNumber port2;
98 private PortNumber port3;
99 private PortNumber port4;
100 private PortNumber port5;
101
102 /**
103 * Sets up the global values for all the tests.
104 */
105 @Before
106 public void setUpTest() {
107 // Mock environment setup
108 MockPceCodecContext context = new MockPceCodecContext();
109 ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService)
Priyanka Bf97e13d2016-07-28 10:57:15 +0530110 .add(TunnelService.class, tunnelService)
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530111 .add(PceStore.class, pceStore)
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530112 .add(CodecService.class, context.codecManager());
Ray Milkey094a1352018-01-22 14:03:54 -0800113 TestUtils.setField(BaseResource.class, "services", testDirectory);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530114
115 // Tunnel creation
116 // Links
117 ProviderId providerId = new ProviderId("of", "foo");
118 deviceId1 = DeviceId.deviceId("of:A");
119 deviceId2 = DeviceId.deviceId("of:B");
120 deviceId3 = DeviceId.deviceId("of:C");
121 deviceId4 = DeviceId.deviceId("of:D");
122 deviceId5 = DeviceId.deviceId("of:E");
123 port1 = PortNumber.portNumber(1);
124 port2 = PortNumber.portNumber(2);
125 port3 = PortNumber.portNumber(3);
126 port4 = PortNumber.portNumber(4);
127 port5 = PortNumber.portNumber(5);
128 List<Link> linkList = new LinkedList<>();
129
130 Link l1 = DefaultLink.builder()
131 .providerId(providerId)
132 .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
133 .src(new ConnectPoint(deviceId1, port1))
134 .dst(new ConnectPoint(deviceId2, port2))
135 .type(DIRECT)
136 .state(Link.State.ACTIVE)
137 .build();
138 linkList.add(l1);
139 Link l2 = DefaultLink.builder()
140 .providerId(providerId)
141 .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
142 .src(new ConnectPoint(deviceId2, port2))
143 .dst(new ConnectPoint(deviceId3, port3))
144 .type(DIRECT)
145 .state(Link.State.ACTIVE)
146 .build();
147 linkList.add(l2);
148 Link l3 = DefaultLink.builder()
149 .providerId(providerId)
150 .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
151 .src(new ConnectPoint(deviceId3, port3))
152 .dst(new ConnectPoint(deviceId4, port4))
153 .type(DIRECT)
154 .state(Link.State.ACTIVE)
155 .build();
156 linkList.add(l3);
157 Link l4 = DefaultLink.builder()
158 .providerId(providerId)
159 .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
160 .src(new ConnectPoint(deviceId4, port4))
161 .dst(new ConnectPoint(deviceId5, port5))
162 .type(DIRECT)
163 .state(Link.State.ACTIVE)
164 .build();
165 linkList.add(l4);
166
167 // Path
168 path = new DefaultPath(providerId, linkList, 10);
169
Mahesh Poojary S33536202016-05-30 07:22:36 +0530170 // Annotations
171 DefaultAnnotations.Builder builderAnn = DefaultAnnotations.builder();
Priyanka Bb977f562016-07-22 13:02:03 +0530172 builderAnn.set(PcepAnnotationKeys.LSP_SIG_TYPE, "WITH_SIGNALLING");
173 builderAnn.set(PcepAnnotationKeys.COST_TYPE, "COST");
Mahesh Poojary S33536202016-05-30 07:22:36 +0530174 builderAnn.set(PcepAnnotationKeys.BANDWIDTH, "200");
175
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530176 // Tunnel
177 tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
Satish K2eb5d842017-04-04 16:28:37 +0530178 Tunnel.State.ACTIVE, null, tunnelId,
Mahesh Poojary S33536202016-05-30 07:22:36 +0530179 tunnelName, path, builderAnn.build());
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530180
181 explicitPathInfoList = Lists.newLinkedList();
182 ExplicitPathInfo obj = new ExplicitPathInfo(ExplicitPathInfo.Type.LOOSE, deviceId2);
183 explicitPathInfoList.add(obj);
184
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530185 }
186
187 /**
188 * Cleans up.
189 */
190 @After
191 public void tearDownTest() {
192 }
193
194 /**
195 * Tests the result of the rest api GET when there are no pce paths.
196 */
197 @Test
198 public void testPcePathsEmpty() {
199 expect(pceService.queryAllPath())
200 .andReturn(null)
201 .anyTimes();
202 replay(pceService);
203 WebTarget wt = target();
204 String response = wt.path("path").request().get(String.class);
205 assertThat(response, is("{\"paths\":[]}"));
206 }
207
208 /**
209 * Tests the result of a rest api GET for pce path id.
210 */
211 @Test
212 public void testGetTunnelId() {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530213 expect(pceService.queryPath(anyObject()))
214 .andReturn(tunnel)
215 .anyTimes();
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530216
217 expect(pceService.explicitPathInfoList(tunnel.tunnelName().value()))
218 .andReturn(explicitPathInfoList)
219 .anyTimes();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530220 replay(pceService);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530221
Mahesh Poojary S33536202016-05-30 07:22:36 +0530222 WebTarget wt = target();
223 String response = wt.path("path/1").request().get(String.class);
224 JsonObject result = Json.parse(response).asObject();
225 assertThat(result, notNullValue());
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530226 }
227
228 /**
229 * Tests that a fetch of a non-existent pce path object throws an exception.
230 */
231 @Test
232 public void testBadGet() {
233 expect(pceService.queryPath(anyObject()))
234 .andReturn(null)
235 .anyTimes();
236 replay(pceService);
237
238 WebTarget wt = target();
239 try {
240 wt.path("path/1").request().get(String.class);
241 fail("Fetch of non-existent pce path did not throw an exception");
242 } catch (NotFoundException ex) {
243 assertThat(ex.getMessage(), containsString("HTTP 404 Not Found"));
244 }
245 }
246
247 /**
248 * Tests creating a pce path with POST.
249 */
250 @Test
251 public void testPost() {
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530252 expect(pceService.setupPath(anyObject(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject()))
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530253 .andReturn(true)
254 .anyTimes();
255 replay(pceService);
256
257 WebTarget wt = target();
258 InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
259
260 Response response = wt.path("path")
261 .request(MediaType.APPLICATION_JSON_TYPE)
262 .post(Entity.json(jsonStream));
263 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
264 }
265
266 /**
267 * Tests creating a pce path with PUT.
268 */
269 @Test
270 public void testPut() {
271 expect(pceService.updatePath(anyObject(), anyObject()))
272 .andReturn(true)
273 .anyTimes();
274 replay(pceService);
275
276 WebTarget wt = target();
277 InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
278
279 Response response = wt.path("path/1")
280 .request(MediaType.APPLICATION_JSON_TYPE)
281 .put(Entity.json(jsonStream));
282 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
283 }
284
285 /**
286 * Tests deleting a pce path.
287 */
288 @Test
289 public void testDelete() {
Satish Kba1c9122017-04-05 15:27:23 +0530290 expect(pceService.releasePath(TunnelId.valueOf("1")))
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530291 .andReturn(true)
292 .anyTimes();
293 replay(pceService);
294
295 WebTarget wt = target();
296
297 String location = "path/1";
298
299 Response deleteResponse = wt.path(location)
300 .request(MediaType.APPLICATION_JSON_TYPE)
301 .delete();
302 assertThat(deleteResponse.getStatus(), is(HttpURLConnection.HTTP_OK));
303 }
304}