Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 1 | /* |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Laboratory |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [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 | */ |
| 16 | package org.opencord.cordvtn.rest; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onlab.osgi.DefaultServiceDirectory; |
| 21 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 22 | import org.opencord.cordvtn.api.core.ServiceNetworkAdminService; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 23 | import org.opencord.cordvtn.api.net.NetworkId; |
| 24 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import javax.ws.rs.Consumes; |
| 29 | import javax.ws.rs.DELETE; |
| 30 | import javax.ws.rs.GET; |
| 31 | import javax.ws.rs.POST; |
| 32 | import javax.ws.rs.PUT; |
| 33 | import javax.ws.rs.Path; |
| 34 | import javax.ws.rs.PathParam; |
| 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.core.Context; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import javax.ws.rs.core.UriBuilder; |
| 40 | import javax.ws.rs.core.UriInfo; |
| 41 | import java.io.IOException; |
| 42 | import java.io.InputStream; |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 43 | import java.util.Set; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 44 | |
| 45 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 46 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 47 | import static javax.ws.rs.core.Response.*; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Query and manage service networks. |
| 51 | */ |
| 52 | @Path("serviceNetworks") |
| 53 | public class ServiceNetworkWebResource extends AbstractWebResource { |
| 54 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 55 | |
| 56 | private static final String MESSAGE = "Received service network "; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 57 | private static final String SERVICE_NETWORK = "ServiceNetwork"; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 58 | private static final String SERVICE_NETWORKS = "ServiceNetworks"; |
| 59 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 60 | private final ServiceNetworkAdminService adminService = |
| 61 | DefaultServiceDirectory.getService(ServiceNetworkAdminService.class); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 62 | |
| 63 | @Context |
| 64 | private UriInfo uriInfo; |
| 65 | |
| 66 | /** |
| 67 | * Creates a service network from the JSON input stream. |
| 68 | * |
| 69 | * @param input service network JSON stream |
| 70 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 71 | * is invalid or duplicated network with different properties exists |
| 72 | */ |
| 73 | @POST |
| 74 | @Consumes(MediaType.APPLICATION_JSON) |
| 75 | @Produces(MediaType.APPLICATION_JSON) |
| 76 | public Response createServiceNetwork(InputStream input) { |
| 77 | try { |
| 78 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 79 | log.trace(MESSAGE + "CREATE " + mapper().writeValueAsString(jsonTree)); |
| 80 | |
| 81 | ObjectNode snetJson = (ObjectNode) jsonTree.get(SERVICE_NETWORK); |
| 82 | if (snetJson == null) { |
| 83 | throw new IllegalArgumentException(); |
| 84 | } |
| 85 | |
| 86 | final ServiceNetwork snet = codec(ServiceNetwork.class).decode(snetJson, this); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 87 | adminService.createServiceNetwork(snet); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 88 | |
| 89 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 90 | .path(SERVICE_NETWORKS) |
| 91 | .path(snet.id().id()); |
| 92 | |
| 93 | return created(locationBuilder.build()).build(); |
| 94 | } catch (IOException e) { |
| 95 | throw new IllegalArgumentException(e); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Updates the service network with the specified identifier. |
| 101 | * |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 102 | * @param id network identifier |
| 103 | * @param input service network JSON stream |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 104 | * @return 200 OK with a service network, 400 BAD_REQUEST if the requested |
| 105 | * network does not exist |
| 106 | */ |
| 107 | @PUT |
| 108 | @Path("{id}") |
| 109 | @Consumes(MediaType.APPLICATION_JSON) |
| 110 | @Produces(MediaType.APPLICATION_JSON) |
| 111 | public Response updateServiceNetwork(@PathParam("id") String id, InputStream input) { |
| 112 | try { |
| 113 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 114 | log.trace(MESSAGE + "UPDATE " + mapper().writeValueAsString(jsonTree)); |
| 115 | |
| 116 | ObjectNode snetJson = (ObjectNode) jsonTree.get(SERVICE_NETWORK); |
| 117 | if (snetJson == null) { |
| 118 | throw new IllegalArgumentException(); |
| 119 | } |
| 120 | |
| 121 | final ServiceNetwork snet = codec(ServiceNetwork.class).decode(snetJson, this); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 122 | adminService.updateServiceNetwork(snet); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 123 | |
| 124 | ObjectNode result = this.mapper().createObjectNode(); |
| 125 | result.set(SERVICE_NETWORK, codec(ServiceNetwork.class).encode(snet, this)); |
| 126 | return ok(result).build(); |
| 127 | } catch (IOException e) { |
| 128 | throw new IllegalArgumentException(e); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Returns all service networks. |
| 134 | * |
| 135 | * @return 200 OK with set of service networks |
| 136 | */ |
| 137 | @GET |
| 138 | @Consumes(MediaType.APPLICATION_JSON) |
| 139 | @Produces(MediaType.APPLICATION_JSON) |
| 140 | public Response getServiceNetworks() { |
| 141 | log.trace(MESSAGE + "GET"); |
| 142 | |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 143 | Set<ServiceNetwork> snets = adminService.serviceNetworks(); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 144 | return ok(encodeArray(ServiceNetwork.class, SERVICE_NETWORKS, snets)).build(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns the service network with the specified identifier. |
| 149 | * |
| 150 | * @param id network identifier |
| 151 | * @return 200 OK with a service network, 404 NOT_FOUND if the requested |
| 152 | * network does not exist |
| 153 | */ |
| 154 | @GET |
| 155 | @Path("{id}") |
| 156 | @Consumes(MediaType.APPLICATION_JSON) |
| 157 | @Produces(MediaType.APPLICATION_JSON) |
| 158 | public Response getServiceNetwork(@PathParam("id") String id) { |
| 159 | log.trace(MESSAGE + "GET " + id); |
| 160 | |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 161 | ServiceNetwork snet = adminService.serviceNetwork(NetworkId.of(id)); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 162 | if (snet == null) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 163 | log.trace("Returned NOT_FOUND"); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 164 | return status(NOT_FOUND).build(); |
| 165 | } |
| 166 | |
| 167 | ObjectNode result = this.mapper().createObjectNode(); |
| 168 | result.set(SERVICE_NETWORK, codec(ServiceNetwork.class).encode(snet, this)); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 169 | log.trace("Returned OK {}", result); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 170 | return ok(result).build(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Removes the service network. |
| 175 | * |
| 176 | * @param id network identifier |
| 177 | * @return 204 NO CONTENT, 400 BAD_REQUEST if the network does not exist |
| 178 | */ |
| 179 | @DELETE |
| 180 | @Path("{id}") |
| 181 | @Consumes(MediaType.APPLICATION_JSON) |
| 182 | @Produces(MediaType.APPLICATION_JSON) |
| 183 | public Response deleteServiceNetwork(@PathParam("id") String id) { |
| 184 | log.trace(MESSAGE + "DELETE " + id); |
| 185 | |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 186 | adminService.removeServiceNetwork(NetworkId.of(id)); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 187 | return noContent().build(); |
| 188 | } |
| 189 | } |