blob: 862fb14c759e4d0c6db1fd17bd427d580ae1f297 [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;
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053067import org.onosproject.net.Path;
68import org.onosproject.net.PortNumber;
69import org.onosproject.net.provider.ProviderId;
70
71/**
72 * Unit tests for pce path REST APIs.
73 */
74public class PcePathResourceTest extends PceResourceTest {
75 private final PceService pceService = createMock(PceService.class);
Priyanka Bf97e13d2016-07-28 10:57:15 +053076 private final TunnelService tunnelService = createMock(TunnelService.class);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +053077 private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423));
78 private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421));
79 private final DefaultGroupId groupId = new DefaultGroupId(92034);
80 private final TunnelName tunnelName = TunnelName.tunnelName("TunnelName");
81 private final TunnelId tunnelId = TunnelId.valueOf("41654654");
82 private final ProviderId producerName = new ProviderId("producer1", "13");
83 private Path path;
84 private Tunnel tunnel;
85 private DeviceId deviceId1;
86 private DeviceId deviceId2;
87 private DeviceId deviceId3;
88 private DeviceId deviceId4;
89 private DeviceId deviceId5;
90 private PortNumber port1;
91 private PortNumber port2;
92 private PortNumber port3;
93 private PortNumber port4;
94 private PortNumber port5;
95
96 /**
97 * Sets up the global values for all the tests.
98 */
99 @Before
100 public void setUpTest() {
101 // Mock environment setup
102 MockPceCodecContext context = new MockPceCodecContext();
103 ServiceDirectory testDirectory = new TestServiceDirectory().add(PceService.class, pceService)
Priyanka Bf97e13d2016-07-28 10:57:15 +0530104 .add(TunnelService.class, tunnelService)
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530105 .add(CodecService.class, context.codecManager());
106 BaseResource.setServiceDirectory(testDirectory);
107
108 // Tunnel creation
109 // Links
110 ProviderId providerId = new ProviderId("of", "foo");
111 deviceId1 = DeviceId.deviceId("of:A");
112 deviceId2 = DeviceId.deviceId("of:B");
113 deviceId3 = DeviceId.deviceId("of:C");
114 deviceId4 = DeviceId.deviceId("of:D");
115 deviceId5 = DeviceId.deviceId("of:E");
116 port1 = PortNumber.portNumber(1);
117 port2 = PortNumber.portNumber(2);
118 port3 = PortNumber.portNumber(3);
119 port4 = PortNumber.portNumber(4);
120 port5 = PortNumber.portNumber(5);
121 List<Link> linkList = new LinkedList<>();
122
123 Link l1 = DefaultLink.builder()
124 .providerId(providerId)
125 .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build())
126 .src(new ConnectPoint(deviceId1, port1))
127 .dst(new ConnectPoint(deviceId2, port2))
128 .type(DIRECT)
129 .state(Link.State.ACTIVE)
130 .build();
131 linkList.add(l1);
132 Link l2 = DefaultLink.builder()
133 .providerId(providerId)
134 .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build())
135 .src(new ConnectPoint(deviceId2, port2))
136 .dst(new ConnectPoint(deviceId3, port3))
137 .type(DIRECT)
138 .state(Link.State.ACTIVE)
139 .build();
140 linkList.add(l2);
141 Link l3 = DefaultLink.builder()
142 .providerId(providerId)
143 .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build())
144 .src(new ConnectPoint(deviceId3, port3))
145 .dst(new ConnectPoint(deviceId4, port4))
146 .type(DIRECT)
147 .state(Link.State.ACTIVE)
148 .build();
149 linkList.add(l3);
150 Link l4 = DefaultLink.builder()
151 .providerId(providerId)
152 .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build())
153 .src(new ConnectPoint(deviceId4, port4))
154 .dst(new ConnectPoint(deviceId5, port5))
155 .type(DIRECT)
156 .state(Link.State.ACTIVE)
157 .build();
158 linkList.add(l4);
159
160 // Path
161 path = new DefaultPath(providerId, linkList, 10);
162
Mahesh Poojary S33536202016-05-30 07:22:36 +0530163 // Annotations
164 DefaultAnnotations.Builder builderAnn = DefaultAnnotations.builder();
Priyanka Bb977f562016-07-22 13:02:03 +0530165 builderAnn.set(PcepAnnotationKeys.LSP_SIG_TYPE, "WITH_SIGNALLING");
166 builderAnn.set(PcepAnnotationKeys.COST_TYPE, "COST");
Mahesh Poojary S33536202016-05-30 07:22:36 +0530167 builderAnn.set(PcepAnnotationKeys.BANDWIDTH, "200");
168
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530169 // Tunnel
170 tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN,
171 Tunnel.State.ACTIVE, groupId, tunnelId,
Mahesh Poojary S33536202016-05-30 07:22:36 +0530172 tunnelName, path, builderAnn.build());
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530173 }
174
175 /**
176 * Cleans up.
177 */
178 @After
179 public void tearDownTest() {
180 }
181
182 /**
183 * Tests the result of the rest api GET when there are no pce paths.
184 */
185 @Test
186 public void testPcePathsEmpty() {
187 expect(pceService.queryAllPath())
188 .andReturn(null)
189 .anyTimes();
190 replay(pceService);
191 WebTarget wt = target();
192 String response = wt.path("path").request().get(String.class);
193 assertThat(response, is("{\"paths\":[]}"));
194 }
195
196 /**
197 * Tests the result of a rest api GET for pce path id.
198 */
199 @Test
200 public void testGetTunnelId() {
Mahesh Poojary S33536202016-05-30 07:22:36 +0530201 expect(pceService.queryPath(anyObject()))
202 .andReturn(tunnel)
203 .anyTimes();
204 replay(pceService);
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530205
Mahesh Poojary S33536202016-05-30 07:22:36 +0530206 WebTarget wt = target();
207 String response = wt.path("path/1").request().get(String.class);
208 JsonObject result = Json.parse(response).asObject();
209 assertThat(result, notNullValue());
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530210 }
211
212 /**
213 * Tests that a fetch of a non-existent pce path object throws an exception.
214 */
215 @Test
216 public void testBadGet() {
217 expect(pceService.queryPath(anyObject()))
218 .andReturn(null)
219 .anyTimes();
220 replay(pceService);
221
222 WebTarget wt = target();
223 try {
224 wt.path("path/1").request().get(String.class);
225 fail("Fetch of non-existent pce path did not throw an exception");
226 } catch (NotFoundException ex) {
227 assertThat(ex.getMessage(), containsString("HTTP 404 Not Found"));
228 }
229 }
230
231 /**
232 * Tests creating a pce path with POST.
233 */
234 @Test
235 public void testPost() {
Priyankab-Huawei3946d732016-10-11 06:24:38 +0000236 expect(pceService.setupPath(anyObject(), anyObject(), anyObject(), anyObject(), anyObject()))
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530237 .andReturn(true)
238 .anyTimes();
239 replay(pceService);
240
241 WebTarget wt = target();
242 InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
243
244 Response response = wt.path("path")
245 .request(MediaType.APPLICATION_JSON_TYPE)
246 .post(Entity.json(jsonStream));
247 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
248 }
249
250 /**
251 * Tests creating a pce path with PUT.
252 */
253 @Test
254 public void testPut() {
255 expect(pceService.updatePath(anyObject(), anyObject()))
256 .andReturn(true)
257 .anyTimes();
258 replay(pceService);
259
260 WebTarget wt = target();
261 InputStream jsonStream = PcePathResourceTest.class.getResourceAsStream("post-PcePath.json");
262
263 Response response = wt.path("path/1")
264 .request(MediaType.APPLICATION_JSON_TYPE)
265 .put(Entity.json(jsonStream));
266 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
267 }
268
269 /**
270 * Tests deleting a pce path.
271 */
272 @Test
273 public void testDelete() {
274 expect(pceService.releasePath(anyObject()))
275 .andReturn(true)
276 .anyTimes();
277 replay(pceService);
278
279 WebTarget wt = target();
280
281 String location = "path/1";
282
283 Response deleteResponse = wt.path(location)
284 .request(MediaType.APPLICATION_JSON_TYPE)
285 .delete();
286 assertThat(deleteResponse.getStatus(), is(HttpURLConnection.HTTP_OK));
287 }
288}