blob: 4300fb035b1c8b8706f1d4bbb2fd66bc389c531c [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 Mooneaf75e62016-09-27 16:40:23 -070043import java.util.ArrayList;
44import java.util.List;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070045
46import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
47import static javax.ws.rs.core.Response.Status.NOT_FOUND;
48import static javax.ws.rs.core.Response.created;
49import static javax.ws.rs.core.Response.noContent;
50import static javax.ws.rs.core.Response.status;
51
52/**
53 * Query and manage service ports.
54 */
55@Path("servicePorts")
56public class ServicePortWebResource extends AbstractWebResource {
57 protected final Logger log = LoggerFactory.getLogger(getClass());
58
59 private static final String MESSAGE = "Received service port ";
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070060 private static final String SERVICE_PORT = "ServicePort";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070061 private static final String SERVICE_PORTS = "ServicePorts";
62
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070063 private final CordVtnAdminService adminService =
64 DefaultServiceDirectory.getService(CordVtnAdminService.class);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070065
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 Mooneaf75e62016-09-27 16:40:23 -070090 adminService.createVtnPort(sport);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070091
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 Mooneaf75e62016-09-27 16:40:23 -0700103 * 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 Moone5a1fc32016-09-02 16:01:01 -0700136 * 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 Mooneaf75e62016-09-27 16:40:23 -0700144 log.trace(MESSAGE + "GET");
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700145
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700146 List<ServicePort> sports = new ArrayList<>(adminService.getVtnPorts());
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700147 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 Mooneaf75e62016-09-27 16:40:23 -0700162 log.trace(MESSAGE + "GET " + id);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700163
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700164 ServicePort sport = adminService.getVtnPort(PortId.of(id));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700165 if (sport == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700166 log.trace("Returned NOT_FOUND");
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700167 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 Mooneaf75e62016-09-27 16:40:23 -0700172 log.trace("Returned OK {}", result);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700173 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 Mooneaf75e62016-09-27 16:40:23 -0700187 log.trace(MESSAGE + "DELETE " + id);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700188
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700189 adminService.removeVtnPort(PortId.of(id));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700190 return noContent().build();
191 }
192}