blob: 140739d8a77d823ddb9e8f3b225ed4537c3c69af [file] [log] [blame]
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +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 */
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;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053047import org.onlab.osgi.ServiceDirectory;
48import org.onlab.osgi.TestServiceDirectory;
49import org.onlab.packet.IpAddress;
50import org.onlab.rest.BaseResource;
51import org.onosproject.codec.CodecService;
Yi Tsengfa394de2017-02-01 11:26:40 -080052import org.onosproject.core.GroupId;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053053import org.onosproject.incubator.net.tunnel.DefaultTunnel;
54import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
55import org.onosproject.incubator.net.tunnel.Tunnel;
56import org.onosproject.incubator.net.tunnel.TunnelId;
57import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
58import org.onosproject.incubator.net.tunnel.TunnelName;
Priyanka Bf97e13d2016-07-28 10:57:15 +053059import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053060import org.onosproject.net.ConnectPoint;
61import org.onosproject.net.DefaultAnnotations;
62import org.onosproject.net.DefaultLink;
63import org.onosproject.net.DefaultPath;
64import org.onosproject.net.DeviceId;
65import org.onosproject.net.Link;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053066import org.onosproject.pce.pceservice.ExplicitPathInfo;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053067import org.onosproject.pce.pceservice.api.PceService;
Mahesh Poojary S33536202016-05-30 07:22:36 +053068import org.onosproject.pce.pceservice.PcepAnnotationKeys;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053069import org.onosproject.pce.pcestore.api.PceStore;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053070import org.onosproject.net.Path;
71import org.onosproject.net.PortNumber;
72import org.onosproject.net.provider.ProviderId;
73
74/**
75 * Unit tests for pce path REST APIs.
76 */
77public class PcePathResourceTest extends PceResourceTest {
78 private final PceService pceService = createMock(PceService.class);
Priyanka Bbae0eeb12016-11-30 11:59:48 +053079 private final PceStore pceStore = createMock(PceStore.class);
Priyanka Bf97e13d2016-07-28 10:57:15 +053080 private final TunnelService tunnelService = createMock(TunnelService.class);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053081 private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
82 private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
Yi Tsengfa394de2017-02-01 11:26:40 -080083 private final GroupId groupId = new GroupId(92034);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053084 private final TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
85 private final TunnelId tunnelId = TunnelId.valueOf("41654654");
86 private final ProviderId producerName = new ProviderId("producer1", "13");
87 private Path path;
88 private Tunnel tunnel;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053089 private List<ExplicitPathInfo> explicitPathInfoList;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053090 private DeviceId deviceId1;
91 private DeviceId deviceId2;
92 private DeviceId deviceId3;
93 private DeviceId deviceId4;
94 private DeviceId deviceId5;
95 private PortNumber port1;
96 private PortNumber port2;
97 private PortNumber port3;
98 private PortNumber port4;
99 private PortNumber port5;
100
101 /**
102 * Sets up the global values for all the tests.
103 */
104 @Before
105 public void setUpTest() {
106 // Mock environment setup
107 MockPceCodecContext context = new MockPceCodecContext();
108 ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService)
Priyanka Bf97e13d2016-07-28 10:57:15 +0530109 .add(TunnelService.class, tunnelService)
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530110 .add(PceStore.class, pceStore)
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530111 .add(CodecService.class, context.codecManager());
112 BaseResource.setServiceDirectory(testDirectory);
113
114 // Tunnel creation
115 // Links
116 ProviderId providerId = new ProviderId("of", "foo");
117 deviceId1 = DeviceId.deviceId("of:A");
118 deviceId2 = DeviceId.deviceId("of:B");
119 deviceId3 = DeviceId.deviceId("of:C");
120 deviceId4 = DeviceId.deviceId("of:D");
121 deviceId5 = DeviceId.deviceId("of:E");
122 port1 = PortNumber.portNumber(1);
123 port2 = PortNumber.portNumber(2);
124 port3 = PortNumber.portNumber(3);
125 port4 = PortNumber.portNumber(4);
126 port5 = PortNumber.portNumber(5);
127 List<Link> linkList = new LinkedList<>();
128
129 Link l1 = DefaultLink.builder()
130 .providerId(providerId)
131 .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
132 .src(new ConnectPoint(deviceId1, port1))
133 .dst(new ConnectPoint(deviceId2, port2))
134 .type(DIRECT)
135 .state(Link.State.ACTIVE)
136 .build();
137 linkList.add(l1);
138 Link l2 = DefaultLink.builder()
139 .providerId(providerId)
140 .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
141 .src(new ConnectPoint(deviceId2, port2))
142 .dst(new ConnectPoint(deviceId3, port3))
143 .type(DIRECT)
144 .state(Link.State.ACTIVE)
145 .build();
146 linkList.add(l2);
147 Link l3 = DefaultLink.builder()
148 .providerId(providerId)
149 .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
150 .src(new ConnectPoint(deviceId3, port3))
151 .dst(new ConnectPoint(deviceId4, port4))
152 .type(DIRECT)
153 .state(Link.State.ACTIVE)
154 .build();
155 linkList.add(l3);
156 Link l4 = DefaultLink.builder()
157 .providerId(providerId)
158 .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
159 .src(new ConnectPoint(deviceId4, port4))
160 .dst(new ConnectPoint(deviceId5, port5))
161 .type(DIRECT)
162 .state(Link.State.ACTIVE)
163 .build();
164 linkList.add(l4);
165
166 // Path
167 path = new DefaultPath(providerId, linkList, 10);
168
Mahesh Poojary S33536202016-05-30 07:22:36 +0530169 // Annotations
170 DefaultAnnotations.Builder builderAnn = DefaultAnnotations.builder();
Priyanka Bb977f562016-07-22 13:02:03 +0530171 builderAnn.set(PcepAnnotationKeys.LSP_SIG_TYPE, "WITH_SIGNALLING");
172 builderAnn.set(PcepAnnotationKeys.COST_TYPE, "COST");
Mahesh Poojary S33536202016-05-30 07:22:36 +0530173 builderAnn.set(PcepAnnotationKeys.BANDWIDTH, "200");
174
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530175 // Tunnel
176 tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
177 Tunnel.State.ACTIVE, groupId, tunnelId,
Mahesh Poojary S33536202016-05-30 07:22:36 +0530178 tunnelName, path, builderAnn.build());
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530179
180 explicitPathInfoList = Lists.newLinkedList();
181 ExplicitPathInfo obj = new ExplicitPathInfo(ExplicitPathInfo.Type.LOOSE, deviceId2);
182 explicitPathInfoList.add(obj);
183
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530184 }
185
186 /**
187 * Cleans up.
188 */
189 @After
190 public void tearDownTest() {
191 }
192
193 /**
194 * Tests the result of the rest api GET when there are no pce paths.
195 */
196 @Test
197 public void testPcePathsEmpty() {
198 expect(pceService.queryAllPath())
199 .andReturn(null)
200 .anyTimes();
201 replay(pceService);
202 WebTarget wt = target();
203 String response = wt.path("path").request().get(String.class);
204 assertThat(response, is("{\"paths\":[]}"));
205 }
206
207 /**
208 * Tests the result of a rest api GET for pce path id.
209 */
210 @Test
211 public void testGetTunnelId() {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530212 expect(pceService.queryPath(anyObject()))
213 .andReturn(tunnel)
214 .anyTimes();
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530215
216 expect(pceService.explicitPathInfoList(tunnel.tunnelName().value()))
217 .andReturn(explicitPathInfoList)
218 .anyTimes();
Mahesh Poojary S33536202016-05-30 07:22:36 +0530219 replay(pceService);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530220
Mahesh Poojary S33536202016-05-30 07:22:36 +0530221 WebTarget wt = target();
222 String response = wt.path("path/1").request().get(String.class);
223 JsonObject result = Json.parse(response).asObject();
224 assertThat(result, notNullValue());
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530225 }
226
227 /**
228 * Tests that a fetch of a non-existent pce path object throws an exception.
229 */
230 @Test
231 public void testBadGet() {
232 expect(pceService.queryPath(anyObject()))
233 .andReturn(null)
234 .anyTimes();
235 replay(pceService);
236
237 WebTarget wt = target();
238 try {
239 wt.path("path/1").request().get(String.class);
240 fail("Fetch of non-existent pce path did not throw an exception");
241 } catch (NotFoundException ex) {
242 assertThat(ex.getMessage(), containsString("HTTP 404 Not Found"));
243 }
244 }
245
246 /**
247 * Tests creating a pce path with POST.
248 */
249 @Test
250 public void testPost() {
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530251 expect(pceService.setupPath(anyObject(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject()))
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530252 .andReturn(true)
253 .anyTimes();
254 replay(pceService);
255
256 WebTarget wt = target();
257 InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
258
259 Response response = wt.path("path")
260 .request(MediaType.APPLICATION_JSON_TYPE)
261 .post(Entity.json(jsonStream));
262 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
263 }
264
265 /**
266 * Tests creating a pce path with PUT.
267 */
268 @Test
269 public void testPut() {
270 expect(pceService.updatePath(anyObject(), anyObject()))
271 .andReturn(true)
272 .anyTimes();
273 replay(pceService);
274
275 WebTarget wt = target();
276 InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
277
278 Response response = wt.path("path/1")
279 .request(MediaType.APPLICATION_JSON_TYPE)
280 .put(Entity.json(jsonStream));
281 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
282 }
283
284 /**
285 * Tests deleting a pce path.
286 */
287 @Test
288 public void testDelete() {
289 expect(pceService.releasePath(anyObject()))
290 .andReturn(true)
291 .anyTimes();
292 replay(pceService);
293
294 WebTarget wt = target();
295
296 String location = "path/1";
297
298 Response deleteResponse = wt.path(location)
299 .request(MediaType.APPLICATION_JSON_TYPE)
300 .delete();
301 assertThat(deleteResponse.getStatus(), is(HttpURLConnection.HTTP_OK));
302 }
303}