Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 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 B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 16 | package org.onosproject.pcerest; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 17 | |
| 18 | import static org.easymock.EasyMock.anyObject; |
| 19 | import static org.easymock.EasyMock.createMock; |
| 20 | import static org.easymock.EasyMock.expect; |
| 21 | import static org.easymock.EasyMock.replay; |
| 22 | import static org.hamcrest.Matchers.containsString; |
| 23 | import static org.hamcrest.Matchers.is; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 24 | import static org.hamcrest.Matchers.notNullValue; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 25 | import static org.junit.Assert.assertThat; |
| 26 | import static org.junit.Assert.fail; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 27 | import static org.onosproject.net.Link.Type.DIRECT; |
| 28 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 29 | import com.eclipsesource.json.Json; |
| 30 | import com.eclipsesource.json.JsonObject; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 31 | |
| 32 | import java.io.InputStream; |
| 33 | import java.net.HttpURLConnection; |
| 34 | import java.util.LinkedList; |
| 35 | import java.util.List; |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 36 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 37 | import javax.ws.rs.client.Entity; |
| 38 | import javax.ws.rs.client.WebTarget; |
| 39 | import javax.ws.rs.core.MediaType; |
| 40 | import javax.ws.rs.core.Response; |
| 41 | import javax.ws.rs.NotFoundException; |
| 42 | |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 43 | import com.google.common.collect.Lists; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 44 | import org.junit.After; |
| 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame^] | 47 | import org.onlab.junit.TestUtils; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 48 | import org.onlab.osgi.ServiceDirectory; |
| 49 | import org.onlab.osgi.TestServiceDirectory; |
| 50 | import org.onlab.packet.IpAddress; |
| 51 | import org.onlab.rest.BaseResource; |
| 52 | import org.onosproject.codec.CodecService; |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 53 | // import org.onosproject.core.DefaultGroupId; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 54 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 55 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 56 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 57 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 58 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 59 | import org.onosproject.incubator.net.tunnel.TunnelName; |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 60 | import org.onosproject.incubator.net.tunnel.TunnelService; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 61 | import org.onosproject.net.ConnectPoint; |
| 62 | import org.onosproject.net.DefaultAnnotations; |
| 63 | import org.onosproject.net.DefaultLink; |
| 64 | import org.onosproject.net.DefaultPath; |
| 65 | import org.onosproject.net.DeviceId; |
| 66 | import org.onosproject.net.Link; |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 67 | import org.onosproject.pce.pceservice.ExplicitPathInfo; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 68 | import org.onosproject.pce.pceservice.api.PceService; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 69 | import org.onosproject.pce.pceservice.PcepAnnotationKeys; |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 70 | import org.onosproject.pce.pcestore.api.PceStore; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 71 | import org.onosproject.net.Path; |
| 72 | import org.onosproject.net.PortNumber; |
| 73 | import org.onosproject.net.provider.ProviderId; |
| 74 | |
| 75 | /** |
| 76 | * Unit tests for pce path REST APIs. |
| 77 | */ |
| 78 | public class PcePathResourceTest extends PceResourceTest { |
| 79 | private final PceService pceService = createMock(PceService.class); |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 80 | private final PceStore pceStore = createMock(PceStore.class); |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 81 | private final TunnelService tunnelService = createMock(TunnelService.class); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 82 | private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423)); |
| 83 | private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421)); |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 84 | // private final DefaultGroupId groupId = new DefaultGroupId(92034); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 85 | 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 B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 90 | private List<ExplicitPathInfo> explicitPathInfoList; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 91 | 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 B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 110 | .add(TunnelService.class, tunnelService) |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 111 | .add(PceStore.class, pceStore) |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 112 | .add(CodecService.class, context.codecManager()); |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame^] | 113 | TestUtils.setField(BaseResource.class, "services", testDirectory); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 114 | |
| 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 S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 170 | // Annotations |
| 171 | DefaultAnnotations.Builder builderAnn = DefaultAnnotations.builder(); |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 172 | builderAnn.set(PcepAnnotationKeys.LSP_SIG_TYPE, "WITH_SIGNALLING"); |
| 173 | builderAnn.set(PcepAnnotationKeys.COST_TYPE, "COST"); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 174 | builderAnn.set(PcepAnnotationKeys.BANDWIDTH, "200"); |
| 175 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 176 | // Tunnel |
| 177 | tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN, |
Satish K | 2eb5d84 | 2017-04-04 16:28:37 +0530 | [diff] [blame] | 178 | Tunnel.State.ACTIVE, null, tunnelId, |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 179 | tunnelName, path, builderAnn.build()); |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 180 | |
| 181 | explicitPathInfoList = Lists.newLinkedList(); |
| 182 | ExplicitPathInfo obj = new ExplicitPathInfo(ExplicitPathInfo.Type.LOOSE, deviceId2); |
| 183 | explicitPathInfoList.add(obj); |
| 184 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 185 | } |
| 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 S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 213 | expect(pceService.queryPath(anyObject())) |
| 214 | .andReturn(tunnel) |
| 215 | .anyTimes(); |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 216 | |
| 217 | expect(pceService.explicitPathInfoList(tunnel.tunnelName().value())) |
| 218 | .andReturn(explicitPathInfoList) |
| 219 | .anyTimes(); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 220 | replay(pceService); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 221 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 222 | 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 S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 226 | } |
| 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 B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 252 | expect(pceService.setupPath(anyObject(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject())) |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 253 | .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 K | ba1c912 | 2017-04-05 15:27:23 +0530 | [diff] [blame] | 290 | expect(pceService.releasePath(TunnelId.valueOf("1"))) |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 291 | .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 | } |