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