blob: a1c5e7c922bcbf808bb3470eba0a0bd44374839a [file] [log] [blame]
sanghoshin94872a12015-10-16 18:04:34 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sanghoshin94872a12015-10-16 18:04:34 +09003 *
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 */
sangho0c2a3da2016-02-16 13:39:07 +090016package org.onosproject.openstacknetworking.web;
sanghoshin94872a12015-10-16 18:04:34 +090017
Hyunsun Moon44aac662017-02-18 02:07:01 +090018import com.fasterxml.jackson.databind.JsonNode;
19import org.onlab.osgi.DefaultServiceDirectory;
20import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
sanghoshin94872a12015-10-16 18:04:34 +090021import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon44aac662017-02-18 02:07:01 +090022import org.openstack4j.core.transport.ObjectMapperSingleton;
23import org.openstack4j.openstack.networking.domain.NeutronPort;
sanghoc853a722016-07-04 21:10:42 +090024import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
sanghoshin94872a12015-10-16 18:04:34 +090026
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.POST;
30import javax.ws.rs.PUT;
31import javax.ws.rs.Path;
sanghoshin65723ae2015-11-17 22:07:21 +090032import javax.ws.rs.PathParam;
sanghoshin94872a12015-10-16 18:04:34 +090033import javax.ws.rs.Produces;
Hyunsun Moon44aac662017-02-18 02:07:01 +090034import javax.ws.rs.core.Context;
sanghoshin94872a12015-10-16 18:04:34 +090035import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
Hyunsun Moon44aac662017-02-18 02:07:01 +090037import javax.ws.rs.core.UriBuilder;
38import javax.ws.rs.core.UriInfo;
sanghoshin94872a12015-10-16 18:04:34 +090039import java.io.InputStream;
40
Hyunsun Moon44aac662017-02-18 02:07:01 +090041import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
42import static javax.ws.rs.core.Response.created;
43import static javax.ws.rs.core.Response.noContent;
44import static javax.ws.rs.core.Response.status;
sanghoc853a722016-07-04 21:10:42 +090045
sanghoshinf25d2e02015-11-11 23:07:17 +090046/**
47 * Handles Rest API call from Neutron ML2 plugin.
48 */
sanghoshin94872a12015-10-16 18:04:34 +090049@Path("ports")
50public class OpenstackPortWebResource extends AbstractWebResource {
Hyunsun Moon44aac662017-02-18 02:07:01 +090051 protected final Logger log = LoggerFactory.getLogger(getClass());
sanghoc853a722016-07-04 21:10:42 +090052
Hyunsun Moon44aac662017-02-18 02:07:01 +090053 private static final String MESSAGE = "Received ports %s request";
54 private static final String PORTS = "ports";
sanghoshin94872a12015-10-16 18:04:34 +090055
Hyunsun Moon44aac662017-02-18 02:07:01 +090056 private final OpenstackNetworkAdminService adminService =
57 DefaultServiceDirectory.getService(OpenstackNetworkAdminService.class);
58
59 @Context
60 private UriInfo uriInfo;
61
62 /**
63 * Creates a port from the JSON input stream.
64 *
65 * @param input port JSON input stream
66 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
67 * is invalid or duplicated port already exists
68 */
sanghoshin94872a12015-10-16 18:04:34 +090069 @POST
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
72 public Response createPorts(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090073 log.trace(String.format(MESSAGE, "CREATE"));
74
75 final NeutronPort port = readPort(input);
76 adminService.createPort(port);
77 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
78 .path(PORTS)
79 .path(port.getId());
80
81 return created(locationBuilder.build()).build();
sanghoshin94872a12015-10-16 18:04:34 +090082 }
83
Hyunsun Moon44aac662017-02-18 02:07:01 +090084 /**
85 * Updates the port with the specified identifier.
86 *
87 * @param id port identifier
88 * @param input port JSON input stream
89 * @return 200 OK with the updated port, 400 BAD_REQUEST if the requested
90 * port does not exist
91 */
sanghoshin94872a12015-10-16 18:04:34 +090092 @PUT
93 @Path("{id}")
94 @Consumes(MediaType.APPLICATION_JSON)
95 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +090096 public Response updatePort(@PathParam("id") String id, InputStream input) {
97 log.trace(String.format(MESSAGE, "UPDATE " + id));
sanghoc853a722016-07-04 21:10:42 +090098
Hyunsun Moon44aac662017-02-18 02:07:01 +090099 final NeutronPort port = readPort(input);
100 adminService.updatePort(port);
101
102 return status(Response.Status.OK).build();
103 }
104
105 /**
106 * Removes the port with the given id.
107 *
108 * @param id port identifier
109 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist
110 */
111 @DELETE
112 @Path("{id}")
113 @Consumes(MediaType.APPLICATION_JSON)
114 @Produces(MediaType.APPLICATION_JSON)
115 public Response deletePorts(@PathParam("id") String id) {
116 log.trace(String.format(MESSAGE, "DELETE " + id));
117
118 adminService.removePort(id);
119 return noContent().build();
120 }
121
122 private NeutronPort readPort(InputStream input) {
sanghoc853a722016-07-04 21:10:42 +0900123 try {
Hyunsun Moon44aac662017-02-18 02:07:01 +0900124 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
125 log.trace(mapper().writeValueAsString(jsonTree));
126 return ObjectMapperSingleton.getContext(NeutronPort.class)
127 .readerFor(NeutronPort.class)
128 .readValue(jsonTree);
129 } catch (Exception e) {
130 throw new IllegalArgumentException();
sanghoc853a722016-07-04 21:10:42 +0900131 }
sanghoshin94872a12015-10-16 18:04:34 +0900132 }
133}