blob: 6c1acaac38487cab8ba6921f6a3d89340bc4064a [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
43import org.junit.After;
44import org.junit.Before;
45import org.junit.Test;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053046import org.onlab.osgi.ServiceDirectory;
47import org.onlab.osgi.TestServiceDirectory;
48import org.onlab.packet.IpAddress;
49import org.onlab.rest.BaseResource;
50import org.onosproject.codec.CodecService;
51import org.onosproject.core.DefaultGroupId;
52import org.onosproject.incubator.net.tunnel.DefaultTunnel;
53import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
54import org.onosproject.incubator.net.tunnel.Tunnel;
55import org.onosproject.incubator.net.tunnel.TunnelId;
56import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
57import org.onosproject.incubator.net.tunnel.TunnelName;
Priyanka Bf97e13d2016-07-28 10:57:15 +053058import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053059import org.onosproject.net.ConnectPoint;
60import org.onosproject.net.DefaultAnnotations;
61import org.onosproject.net.DefaultLink;
62import org.onosproject.net.DefaultPath;
63import org.onosproject.net.DeviceId;
64import org.onosproject.net.Link;
65import org.onosproject.pce.pceservice.api.PceService;
Mahesh Poojary S33536202016-05-30 07:22:36 +053066import org.onosproject.pce.pceservice.PcepAnnotationKeys;
Priyanka Ba32f6da2016-09-02 16:10:21 +053067import org.onosproject.pce.pcestore.api.PceStore;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053068import org.onosproject.net.Path;
69import org.onosproject.net.PortNumber;
70import org.onosproject.net.provider.ProviderId;
71
72/**
73 * Unit tests for pce path REST APIs.
74 */
75public class PcePathResourceTest extends PceResourceTest {
76 private final PceService pceService = createMock(PceService.class);
Priyanka Ba32f6da2016-09-02 16:10:21 +053077 private final PceStore pceStore = createMock(PceStore.class);
Priyanka Bf97e13d2016-07-28 10:57:15 +053078 private final TunnelService tunnelService = createMock(TunnelService.class);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053079 private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
80 private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
81 private final DefaultGroupId groupId = new DefaultGroupId(92034);
82 private final TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
83 private final TunnelId tunnelId = TunnelId.valueOf("41654654");
84 private final ProviderId producerName = new ProviderId("producer1", "13");
85 private Path path;
86 private Tunnel tunnel;
87 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 /**
99 * Sets up the global values for all the tests.
100 */
101 @Before
102 public void setUpTest() {
103 // Mock environment setup
104 MockPceCodecContext context = new MockPceCodecContext();
105 ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService)
Priyanka Bf97e13d2016-07-28 10:57:15 +0530106 .add(TunnelService.class, tunnelService)
Priyanka Ba32f6da2016-09-02 16:10:21 +0530107 .add(PceStore.class, pceStore)
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530108 .add(CodecService.class, context.codecManager());
109 BaseResource.setServiceDirectory(testDirectory);
110
111 // Tunnel creation
112 // Links
113 ProviderId providerId = new ProviderId("of", "foo");
114 deviceId1 = DeviceId.deviceId("of:A");
115 deviceId2 = DeviceId.deviceId("of:B");
116 deviceId3 = DeviceId.deviceId("of:C");
117 deviceId4 = DeviceId.deviceId("of:D");
118 deviceId5 = DeviceId.deviceId("of:E");
119 port1 = PortNumber.portNumber(1);
120 port2 = PortNumber.portNumber(2);
121 port3 = PortNumber.portNumber(3);
122 port4 = PortNumber.portNumber(4);
123 port5 = PortNumber.portNumber(5);
124 List<Link> linkList = new LinkedList<>();
125
126 Link l1 = DefaultLink.builder()
127 .providerId(providerId)
128 .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
129 .src(new ConnectPoint(deviceId1, port1))
130 .dst(new ConnectPoint(deviceId2, port2))
131 .type(DIRECT)
132 .state(Link.State.ACTIVE)
133 .build();
134 linkList.add(l1);
135 Link l2 = DefaultLink.builder()
136 .providerId(providerId)
137 .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
138 .src(new ConnectPoint(deviceId2, port2))
139 .dst(new ConnectPoint(deviceId3, port3))
140 .type(DIRECT)
141 .state(Link.State.ACTIVE)
142 .build();
143 linkList.add(l2);
144 Link l3 = DefaultLink.builder()
145 .providerId(providerId)
146 .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
147 .src(new ConnectPoint(deviceId3, port3))
148 .dst(new ConnectPoint(deviceId4, port4))
149 .type(DIRECT)
150 .state(Link.State.ACTIVE)
151 .build();
152 linkList.add(l3);
153 Link l4 = DefaultLink.builder()
154 .providerId(providerId)
155 .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
156 .src(new ConnectPoint(deviceId4, port4))
157 .dst(new ConnectPoint(deviceId5, port5))
158 .type(DIRECT)
159 .state(Link.State.ACTIVE)
160 .build();
161 linkList.add(l4);
162
163 // Path
164 path = new DefaultPath(providerId, linkList, 10);
165
Mahesh Poojary S33536202016-05-30 07:22:36 +0530166 // Annotations
167 DefaultAnnotations.Builder builderAnn = DefaultAnnotations.builder();
Priyanka Bb977f562016-07-22 13:02:03 +0530168 builderAnn.set(PcepAnnotationKeys.LSP_SIG_TYPE, "WITH_SIGNALLING");
169 builderAnn.set(PcepAnnotationKeys.COST_TYPE, "COST");
Mahesh Poojary S33536202016-05-30 07:22:36 +0530170 builderAnn.set(PcepAnnotationKeys.BANDWIDTH, "200");
171
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530172 // Tunnel
173 tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
174 Tunnel.State.ACTIVE, groupId, tunnelId,
Mahesh Poojary S33536202016-05-30 07:22:36 +0530175 tunnelName, path, builderAnn.build());
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530176 }
177
178 /**
179 * Cleans up.
180 */
181 @After
182 public void tearDownTest() {
183 }
184
185 /**
186 * Tests the result of the rest api GET when there are no pce paths.
187 */
188 @Test
189 public void testPcePathsEmpty() {
190 expect(pceService.queryAllPath())
191 .andReturn(null)
192 .anyTimes();
193 replay(pceService);
194 WebTarget wt = target();
195 String response = wt.path("path").request().get(String.class);
196 assertThat(response, is("{\"paths\":[]}"));
197 }
198
199 /**
200 * Tests the result of a rest api GET for pce path id.
201 */
202 @Test
203 public void testGetTunnelId() {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530204 expect(pceService.queryPath(anyObject()))
205 .andReturn(tunnel)
206 .anyTimes();
207 replay(pceService);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530208
Mahesh Poojary S33536202016-05-30 07:22:36 +0530209 WebTarget wt = target();
210 String response = wt.path("path/1").request().get(String.class);
211 JsonObject result = Json.parse(response).asObject();
212 assertThat(result, notNullValue());
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530213 }
214
215 /**
216 * Tests that a fetch of a non-existent pce path object throws an exception.
217 */
218 @Test
219 public void testBadGet() {
220 expect(pceService.queryPath(anyObject()))
221 .andReturn(null)
222 .anyTimes();
223 replay(pceService);
224
225 WebTarget wt = target();
226 try {
227 wt.path("path/1").request().get(String.class);
228 fail("Fetch of non-existent pce path did not throw an exception");
229 } catch (NotFoundException ex) {
230 assertThat(ex.getMessage(), containsString("HTTP 404 Not Found"));
231 }
232 }
233
234 /**
235 * Tests creating a pce path with POST.
236 */
237 @Test
238 public void testPost() {
Priyanka Ba32f6da2016-09-02 16:10:21 +0530239 expect(pceService.setupPath(anyObject(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject()))
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530240 .andReturn(true)
241 .anyTimes();
242 replay(pceService);
243
244 WebTarget wt = target();
245 InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
246
247 Response response = wt.path("path")
248 .request(MediaType.APPLICATION_JSON_TYPE)
249 .post(Entity.json(jsonStream));
250 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
251 }
252
253 /**
254 * Tests creating a pce path with PUT.
255 */
256 @Test
257 public void testPut() {
258 expect(pceService.updatePath(anyObject(), anyObject()))
259 .andReturn(true)
260 .anyTimes();
261 replay(pceService);
262
263 WebTarget wt = target();
264 InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
265
266 Response response = wt.path("path/1")
267 .request(MediaType.APPLICATION_JSON_TYPE)
268 .put(Entity.json(jsonStream));
269 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
270 }
271
272 /**
273 * Tests deleting a pce path.
274 */
275 @Test
276 public void testDelete() {
277 expect(pceService.releasePath(anyObject()))
278 .andReturn(true)
279 .anyTimes();
280 replay(pceService);
281
282 WebTarget wt = target();
283
284 String location = "path/1";
285
286 Response deleteResponse = wt.path(location)
287 .request(MediaType.APPLICATION_JSON_TYPE)
288 .delete();
289 assertThat(deleteResponse.getStatus(), is(HttpURLConnection.HTTP_OK));
290 }
291}