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