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 | 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 ports. |
| 54 | */ |
| 55 | @Path("servicePorts") |
| 56 | public class ServicePortWebResource extends AbstractWebResource { |
| 57 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 58 | |
| 59 | private static final String MESSAGE = "Received service port "; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 60 | private static final String SERVICE_PORT = "ServicePort"; |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 61 | private static final String SERVICE_PORTS = "ServicePorts"; |
| 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 port from the JSON input stream. |
| 71 | * |
| 72 | * @param input service port JSON stream |
| 73 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 74 | * is invalid or duplicated port with different properties exists |
| 75 | */ |
| 76 | @POST |
| 77 | @Consumes(MediaType.APPLICATION_JSON) |
| 78 | @Produces(MediaType.APPLICATION_JSON) |
| 79 | public Response createServicePort(InputStream input) { |
| 80 | try { |
| 81 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 82 | log.trace(MESSAGE + "CREATE " + mapper().writeValueAsString(jsonTree)); |
| 83 | |
| 84 | ObjectNode portJson = (ObjectNode) jsonTree.get(SERVICE_PORT); |
| 85 | if (portJson == null) { |
| 86 | throw new IllegalArgumentException(); |
| 87 | } |
| 88 | |
| 89 | final ServicePort sport = codec(ServicePort.class).decode(portJson, this); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 90 | adminService.createVtnPort(sport); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 91 | |
| 92 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 93 | .path(SERVICE_PORTS) |
| 94 | .path(sport.id().id()); |
| 95 | |
| 96 | return created(locationBuilder.build()).build(); |
| 97 | } catch (IOException e) { |
| 98 | throw new IllegalArgumentException(e); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 103 | * Updates the service port with the given identifier. |
| 104 | * |
| 105 | * @param id port identifier |
| 106 | * @param input service port JSON stream |
| 107 | * @return 200 OK with a service port, 400 BAD_REQUEST if the requested |
| 108 | * port does not exist |
| 109 | */ |
| 110 | @PUT |
| 111 | @Path("{id}") |
| 112 | @Consumes(MediaType.APPLICATION_JSON) |
| 113 | @Produces(MediaType.APPLICATION_JSON) |
| 114 | public Response updateServicePort(@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 sportJson = (ObjectNode) jsonTree.get(SERVICE_PORT); |
| 120 | if (sportJson == null) { |
| 121 | throw new IllegalArgumentException(); |
| 122 | } |
| 123 | |
| 124 | final ServicePort sport = codec(ServicePort.class).decode(sportJson, this); |
| 125 | adminService.updateVtnPort(sport); |
| 126 | |
| 127 | ObjectNode result = this.mapper().createObjectNode(); |
| 128 | result.set(SERVICE_PORT, codec(ServicePort.class).encode(sport, this)); |
| 129 | return ok(result).build(); |
| 130 | } catch (IOException e) { |
| 131 | throw new IllegalArgumentException(e); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 136 | * Returns all service ports. |
| 137 | * |
| 138 | * @return 200 OK with set of service ports |
| 139 | */ |
| 140 | @GET |
| 141 | @Consumes(MediaType.APPLICATION_JSON) |
| 142 | @Produces(MediaType.APPLICATION_JSON) |
| 143 | public Response getServicePorts() { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 144 | log.trace(MESSAGE + "GET"); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 145 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 146 | List<ServicePort> sports = new ArrayList<>(adminService.getVtnPorts()); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 147 | return ok(encodeArray(ServicePort.class, SERVICE_PORTS, sports)).build(); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Returns the service port with the specified identifier. |
| 152 | * |
| 153 | * @param id port identifier |
| 154 | * @return 200 OK with a service port, 404 NOT_FOUND if the requested |
| 155 | * port does not exist |
| 156 | */ |
| 157 | @GET |
| 158 | @Path("{id}") |
| 159 | @Consumes(MediaType.APPLICATION_JSON) |
| 160 | @Produces(MediaType.APPLICATION_JSON) |
| 161 | public Response getServicePort(@PathParam("id") String id) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 162 | log.trace(MESSAGE + "GET " + id); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 163 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 164 | ServicePort sport = adminService.getVtnPort(PortId.of(id)); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 165 | if (sport == 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_PORT, codec(ServicePort.class).encode(sport, 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 port. |
| 178 | * |
| 179 | * @param id port 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 deleteServicePort(@PathParam("id") String id) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 187 | log.trace(MESSAGE + "DELETE " + id); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 188 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame^] | 189 | adminService.removeVtnPort(PortId.of(id)); |
Hyunsun Moon | e5a1fc3 | 2016-09-02 16:01:01 -0700 | [diff] [blame] | 190 | return noContent().build(); |
| 191 | } |
| 192 | } |