Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 1 | /* |
| 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 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; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 47 | import org.onlab.osgi.ServiceDirectory; |
| 48 | import org.onlab.osgi.TestServiceDirectory; |
| 49 | import org.onlab.packet.IpAddress; |
| 50 | import org.onlab.rest.BaseResource; |
| 51 | import org.onosproject.codec.CodecService; |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 52 | import org.onosproject.core.GroupId; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 53 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 54 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 55 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 56 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 57 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 58 | import org.onosproject.incubator.net.tunnel.TunnelName; |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 59 | import org.onosproject.incubator.net.tunnel.TunnelService; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 60 | import org.onosproject.net.ConnectPoint; |
| 61 | import org.onosproject.net.DefaultAnnotations; |
| 62 | import org.onosproject.net.DefaultLink; |
| 63 | import org.onosproject.net.DefaultPath; |
| 64 | import org.onosproject.net.DeviceId; |
| 65 | import org.onosproject.net.Link; |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 66 | import org.onosproject.pce.pceservice.ExplicitPathInfo; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 67 | import org.onosproject.pce.pceservice.api.PceService; |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 68 | import org.onosproject.pce.pceservice.PcepAnnotationKeys; |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 69 | import org.onosproject.pce.pcestore.api.PceStore; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 70 | import org.onosproject.net.Path; |
| 71 | import org.onosproject.net.PortNumber; |
| 72 | import org.onosproject.net.provider.ProviderId; |
| 73 | |
| 74 | /** |
| 75 | * Unit tests for pce path REST APIs. |
| 76 | */ |
| 77 | public class PcePathResourceTest extends PceResourceTest { |
| 78 | private final PceService pceService = createMock(PceService.class); |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 79 | private final PceStore pceStore = createMock(PceStore.class); |
Priyanka B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 80 | private final TunnelService tunnelService = createMock(TunnelService.class); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 81 | private final TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423)); |
| 82 | private final TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421)); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 83 | private final GroupId groupId = new GroupId(92034); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 84 | 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 B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 89 | private List<ExplicitPathInfo> explicitPathInfoList; |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 90 | 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 B | f97e13d | 2016-07-28 10:57:15 +0530 | [diff] [blame] | 109 | .add(TunnelService.class, tunnelService) |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 110 | .add(PceStore.class, pceStore) |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 111 | .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 S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 169 | // Annotations |
| 170 | DefaultAnnotations.Builder builderAnn = DefaultAnnotations.builder(); |
Priyanka B | b977f56 | 2016-07-22 13:02:03 +0530 | [diff] [blame] | 171 | builderAnn.set(PcepAnnotationKeys.LSP_SIG_TYPE, "WITH_SIGNALLING"); |
| 172 | builderAnn.set(PcepAnnotationKeys.COST_TYPE, "COST"); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 173 | builderAnn.set(PcepAnnotationKeys.BANDWIDTH, "200"); |
| 174 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 175 | // Tunnel |
| 176 | tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN, |
| 177 | Tunnel.State.ACTIVE, groupId, tunnelId, |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 178 | tunnelName, path, builderAnn.build()); |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 179 | |
| 180 | explicitPathInfoList = Lists.newLinkedList(); |
| 181 | ExplicitPathInfo obj = new ExplicitPathInfo(ExplicitPathInfo.Type.LOOSE, deviceId2); |
| 182 | explicitPathInfoList.add(obj); |
| 183 | |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 184 | } |
| 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 S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 212 | expect(pceService.queryPath(anyObject())) |
| 213 | .andReturn(tunnel) |
| 214 | .anyTimes(); |
Priyanka B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 215 | |
| 216 | expect(pceService.explicitPathInfoList(tunnel.tunnelName().value())) |
| 217 | .andReturn(explicitPathInfoList) |
| 218 | .anyTimes(); |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 219 | replay(pceService); |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 220 | |
Mahesh Poojary S | 3353620 | 2016-05-30 07:22:36 +0530 | [diff] [blame] | 221 | 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 S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 225 | } |
| 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 B | bae0eeb1 | 2016-11-30 11:59:48 +0530 | [diff] [blame] | 251 | expect(pceService.setupPath(anyObject(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject())) |
Mahesh Poojary S | 8e1670e | 2016-05-19 22:15:34 +0530 | [diff] [blame] | 252 | .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 | } |