blob: 32417bc8f4c56b38e6f6a201dd5e67ae9c8e8398 [file] [log] [blame]
Hyunsun Moonb6febbe2016-02-12 15:59:53 -08001/*
Brian O'Connor8e57fd52016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Hyunsun Moonb6febbe2016-02-12 15:59:53 -08003 *
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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.cordvtn.rest;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080017
Hyunsun Moon01556a52016-02-12 12:48:47 -080018import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070019import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070021import org.onlab.osgi.DefaultServiceDirectory;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070022import org.opencord.cordvtn.api.CordVtnAdminService;
23import org.opencord.cordvtn.api.PortId;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080024import org.onosproject.rest.AbstractWebResource;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070025import org.openstack4j.core.transport.ObjectMapperSingleton;
26import org.openstack4j.model.network.Port;
27import org.openstack4j.openstack.networking.domain.NeutronPort;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import javax.ws.rs.Consumes;
32import javax.ws.rs.DELETE;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070033import javax.ws.rs.GET;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080034import javax.ws.rs.POST;
35import javax.ws.rs.PUT;
36import javax.ws.rs.Path;
37import javax.ws.rs.PathParam;
38import javax.ws.rs.Produces;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070039import javax.ws.rs.core.Context;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080040import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070042import javax.ws.rs.core.UriBuilder;
43import javax.ws.rs.core.UriInfo;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080044import java.io.InputStream;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070045import java.util.Set;
46
47import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
48import static javax.ws.rs.core.Response.Status.NOT_FOUND;
49import static javax.ws.rs.core.Response.created;
50import static javax.ws.rs.core.Response.noContent;
51import static javax.ws.rs.core.Response.status;
Hyunsun Moon01556a52016-02-12 12:48:47 -080052
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080053
54/**
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070055 * Neutron ML2 mechanism driver implementation for the port resource.
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080056 */
57@Path("ports")
58public class NeutronMl2PortsWebResource extends AbstractWebResource {
59 protected final Logger log = LoggerFactory.getLogger(getClass());
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080060
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070061 private static final String MESSAGE = "Received ports %s request";
62 private static final String PORT = "port";
63 private static final String PORTS = "ports";
Hyunsun Moon01556a52016-02-12 12:48:47 -080064
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070065 private final CordVtnAdminService adminService =
66 DefaultServiceDirectory.getService(CordVtnAdminService.class);
Hyunsun Moon01556a52016-02-12 12:48:47 -080067
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070068 @Context
69 private UriInfo uriInfo;
70
71 /**
72 * Creates a port from the JSON input stream.
73 *
74 * @param input port JSON input stream
75 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
76 * is invalid or duplicated port already exists
77 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080078 @POST
79 @Consumes(MediaType.APPLICATION_JSON)
80 @Produces(MediaType.APPLICATION_JSON)
81 public Response createPorts(InputStream input) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070082 log.trace(String.format(MESSAGE, "CREATE"));
83
84 final NeutronPort port = readPort(input);
85 adminService.createPort(port);
86 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
87 .path(PORTS)
88 .path(port.getId());
89
90 return created(locationBuilder.build()).build();
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080091 }
92
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070093 /**
94 * Updates the port with the specified identifier.
95 *
96 * @param id port identifier
97 * @param input port JSON input stream
98 * @return 200 OK with the updated port, 400 BAD_REQUEST if the requested
99 * port does not exist
100 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800101 @PUT
102 @Path("{id}")
103 @Consumes(MediaType.APPLICATION_JSON)
104 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700105 public Response updatePort(@PathParam("id") String id, InputStream input) {
106 log.trace(String.format(MESSAGE, "UPDATE " + id));
Hyunsun Moon01556a52016-02-12 12:48:47 -0800107
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700108 final NeutronPort port = readPort(input);
109 adminService.updatePort(port);
Hyunsun Moon01556a52016-02-12 12:48:47 -0800110
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700111 ObjectNode result = this.mapper().createObjectNode();
112 return ok(result.set(PORT, writePort(port))).build();
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800113 }
114
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700115 /**
116 * Returns all ports.
117 *
118 * @return 200 OK with set of ports
119 */
120 @GET
121 @Consumes(MediaType.APPLICATION_JSON)
122 @Produces(MediaType.APPLICATION_JSON)
123 public Response getPorts() {
124 log.trace(String.format(MESSAGE, "GET"));
125
126 Set<Port> ports = adminService.getPorts();
127 ArrayNode arrayNodes = mapper().createArrayNode();
128 ports.stream().forEach(port -> {
129 arrayNodes.add(writePort(port));
130 });
131
132 ObjectNode result = this.mapper().createObjectNode();
133 return ok(result.set(PORTS, arrayNodes)).build();
134 }
135
136 /**
137 * Returns the port with the given id.
138 *
139 * @param id port id
140 * @return 200 OK with the port, 404 NOT_FOUND if the port does not exist
141 */
142 @GET
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800143 @Path("{id}")
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700144 @Consumes(MediaType.APPLICATION_JSON)
145 @Produces(MediaType.APPLICATION_JSON)
146 public Response getPort(@PathParam("id") String id) {
147 log.trace(String.format(MESSAGE, "GET " + id));
148
149 Port port = adminService.getPort(PortId.of(id));
150 if (port == null) {
151 return status(NOT_FOUND).build();
152 }
153
154 ObjectNode result = this.mapper().createObjectNode();
155 return ok(result.set(PORT, writePort(port))).build();
156 }
157
158 /**
159 * Removes the port with the given id.
160 *
161 * @param id port identifier
162 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist
163 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800164 @DELETE
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700165 @Path("{id}")
166 @Consumes(MediaType.APPLICATION_JSON)
167 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800168 public Response deletePorts(@PathParam("id") String id) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700169 log.trace(String.format(MESSAGE, "DELETE " + id));
170
171 adminService.removePort(PortId.of(id));
172 return noContent().build();
173 }
174
175 private NeutronPort readPort(InputStream input) {
176 try {
177 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
178 log.trace(mapper().writeValueAsString(jsonTree));
179 return ObjectMapperSingleton.getContext(NeutronPort.class)
180 .readerFor(NeutronPort.class)
181 .readValue(jsonTree);
182 } catch (Exception e) {
183 throw new IllegalArgumentException();
184 }
185 }
186
187 private ObjectNode writePort(Port port) {
188 try {
189 String strPort = ObjectMapperSingleton.getContext(NeutronPort.class)
190 .writerFor(NeutronPort.class)
191 .writeValueAsString(port);
192 log.trace(strPort);
193 return (ObjectNode) mapper().readTree(strPort.getBytes());
194 } catch (Exception e) {
195 throw new IllegalStateException();
196 }
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800197 }
198}