blob: 88cc6657f5080007348f27f31a1906789a948bcd [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 Moon187bf532017-01-19 10:57:40 +090022import org.opencord.cordvtn.api.core.ServiceNetworkAdminService;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070023import org.opencord.cordvtn.api.net.PortId;
24import org.opencord.cordvtn.api.net.ServicePort;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070025import 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;
Hyunsun Moon187bf532017-01-19 10:57:40 +090047import static javax.ws.rs.core.Response.*;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070048
49/**
50 * Query and manage service ports.
51 */
52@Path("servicePorts")
53public class ServicePortWebResource extends AbstractWebResource {
54 protected final Logger log = LoggerFactory.getLogger(getClass());
55
56 private static final String MESSAGE = "Received service port ";
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070057 private static final String SERVICE_PORT = "ServicePort";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070058 private static final String SERVICE_PORTS = "ServicePorts";
59
Hyunsun Moon187bf532017-01-19 10:57:40 +090060 private final ServiceNetworkAdminService adminService =
61 DefaultServiceDirectory.getService(ServiceNetworkAdminService.class);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070062
63 @Context
64 private UriInfo uriInfo;
65
66 /**
67 * Creates a service port from the JSON input stream.
68 *
69 * @param input service port JSON stream
70 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
71 * is invalid or duplicated port with different properties exists
72 */
73 @POST
74 @Consumes(MediaType.APPLICATION_JSON)
75 @Produces(MediaType.APPLICATION_JSON)
76 public Response createServicePort(InputStream input) {
77 try {
78 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
79 log.trace(MESSAGE + "CREATE " + mapper().writeValueAsString(jsonTree));
80
81 ObjectNode portJson = (ObjectNode) jsonTree.get(SERVICE_PORT);
82 if (portJson == null) {
83 throw new IllegalArgumentException();
84 }
85
86 final ServicePort sport = codec(ServicePort.class).decode(portJson, this);
Hyunsun Moon5c143952016-10-19 18:34:46 -070087 adminService.createServicePort(sport);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070088
89 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
90 .path(SERVICE_PORTS)
91 .path(sport.id().id());
92
93 return created(locationBuilder.build()).build();
94 } catch (IOException e) {
95 throw new IllegalArgumentException(e);
96 }
97 }
98
99 /**
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700100 * Updates the service port with the given identifier.
101 *
102 * @param id port identifier
103 * @param input service port JSON stream
104 * @return 200 OK with a service port, 400 BAD_REQUEST if the requested
105 * port does not exist
106 */
107 @PUT
108 @Path("{id}")
109 @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON)
111 public Response updateServicePort(@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 sportJson = (ObjectNode) jsonTree.get(SERVICE_PORT);
117 if (sportJson == null) {
118 throw new IllegalArgumentException();
119 }
120
121 final ServicePort sport = codec(ServicePort.class).decode(sportJson, this);
Hyunsun Moon5c143952016-10-19 18:34:46 -0700122 adminService.updateServicePort(sport);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700123
124 ObjectNode result = this.mapper().createObjectNode();
125 result.set(SERVICE_PORT, codec(ServicePort.class).encode(sport, this));
126 return ok(result).build();
127 } catch (IOException e) {
128 throw new IllegalArgumentException(e);
129 }
130 }
131
132 /**
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700133 * Returns all service ports.
134 *
135 * @return 200 OK with set of service ports
136 */
137 @GET
138 @Consumes(MediaType.APPLICATION_JSON)
139 @Produces(MediaType.APPLICATION_JSON)
140 public Response getServicePorts() {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700141 log.trace(MESSAGE + "GET");
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700142
Hyunsun Moon5c143952016-10-19 18:34:46 -0700143 Set<ServicePort> sports = adminService.servicePorts();
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700144 return ok(encodeArray(ServicePort.class, SERVICE_PORTS, sports)).build();
145 }
146
147 /**
148 * Returns the service port with the specified identifier.
149 *
150 * @param id port identifier
151 * @return 200 OK with a service port, 404 NOT_FOUND if the requested
152 * port does not exist
153 */
154 @GET
155 @Path("{id}")
156 @Consumes(MediaType.APPLICATION_JSON)
157 @Produces(MediaType.APPLICATION_JSON)
158 public Response getServicePort(@PathParam("id") String id) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700159 log.trace(MESSAGE + "GET " + id);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700160
Hyunsun Moon5c143952016-10-19 18:34:46 -0700161 ServicePort sport = adminService.servicePort(PortId.of(id));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700162 if (sport == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700163 log.trace("Returned NOT_FOUND");
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700164 return status(NOT_FOUND).build();
165 }
166
167 ObjectNode result = this.mapper().createObjectNode();
168 result.set(SERVICE_PORT, codec(ServicePort.class).encode(sport, this));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700169 log.trace("Returned OK {}", result);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700170 return ok(result).build();
171 }
172
173 /**
174 * Removes the service port.
175 *
176 * @param id port 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 deleteServicePort(@PathParam("id") String id) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700184 log.trace(MESSAGE + "DELETE " + id);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700185
Hyunsun Moon5c143952016-10-19 18:34:46 -0700186 adminService.removeServicePort(PortId.of(id));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700187 return noContent().build();
188 }
189}