blob: 67df9a247af7f259c7ac37d5819be6d5495687ce [file] [log] [blame]
Hyunsun Moone5a1fc32016-09-02 16:01:01 -07001/*
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 */
16package org.opencord.cordvtn.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.osgi.DefaultServiceDirectory;
21import org.onosproject.rest.AbstractWebResource;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070022import org.opencord.cordvtn.api.CordVtnAdminService;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070023import org.opencord.cordvtn.api.PortId;
24import org.opencord.cordvtn.api.ServicePort;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.DELETE;
30import javax.ws.rs.GET;
31import javax.ws.rs.POST;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070032import javax.ws.rs.PUT;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070033import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.Context;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import javax.ws.rs.core.UriBuilder;
40import javax.ws.rs.core.UriInfo;
41import java.io.IOException;
42import java.io.InputStream;
Hyunsun Moon5c143952016-10-19 18:34:46 -070043import java.util.Set;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070044
45import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
46import static javax.ws.rs.core.Response.Status.NOT_FOUND;
47import static javax.ws.rs.core.Response.created;
48import static javax.ws.rs.core.Response.noContent;
49import static javax.ws.rs.core.Response.status;
50
51/**
52 * Query and manage service ports.
53 */
54@Path("servicePorts")
55public class ServicePortWebResource extends AbstractWebResource {
56 protected final Logger log = LoggerFactory.getLogger(getClass());
57
58 private static final String MESSAGE = "Received service port ";
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070059 private static final String SERVICE_PORT = "ServicePort";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070060 private static final String SERVICE_PORTS = "ServicePorts";
61
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070062 private final CordVtnAdminService adminService =
63 DefaultServiceDirectory.getService(CordVtnAdminService.class);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070064
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 Moon5c143952016-10-19 18:34:46 -070089 adminService.createServicePort(sport);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070090
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 Mooneaf75e62016-09-27 16:40:23 -0700102 * 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 Moon5c143952016-10-19 18:34:46 -0700124 adminService.updateServicePort(sport);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700125
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 Moone5a1fc32016-09-02 16:01:01 -0700135 * 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 Mooneaf75e62016-09-27 16:40:23 -0700143 log.trace(MESSAGE + "GET");
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700144
Hyunsun Moon5c143952016-10-19 18:34:46 -0700145 Set<ServicePort> sports = adminService.servicePorts();
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700146 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 Mooneaf75e62016-09-27 16:40:23 -0700161 log.trace(MESSAGE + "GET " + id);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700162
Hyunsun Moon5c143952016-10-19 18:34:46 -0700163 ServicePort sport = adminService.servicePort(PortId.of(id));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700164 if (sport == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700165 log.trace("Returned NOT_FOUND");
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700166 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 Mooneaf75e62016-09-27 16:40:23 -0700171 log.trace("Returned OK {}", result);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700172 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 Mooneaf75e62016-09-27 16:40:23 -0700186 log.trace(MESSAGE + "DELETE " + id);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700187
Hyunsun Moon5c143952016-10-19 18:34:46 -0700188 adminService.removeServicePort(PortId.of(id));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700189 return noContent().build();
190 }
191}