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