blob: 94c30e72ded82e2e1645e7278b481a0e15150e47 [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 org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
sanghoshin94872a12015-10-16 18:04:34 +090019import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon44aac662017-02-18 02:07:01 +090020import org.openstack4j.openstack.networking.domain.NeutronPort;
sanghoc853a722016-07-04 21:10:42 +090021import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
sanghoshin94872a12015-10-16 18:04:34 +090023
24import javax.ws.rs.Consumes;
25import javax.ws.rs.DELETE;
26import javax.ws.rs.POST;
27import javax.ws.rs.PUT;
28import javax.ws.rs.Path;
sanghoshin65723ae2015-11-17 22:07:21 +090029import javax.ws.rs.PathParam;
sanghoshin94872a12015-10-16 18:04:34 +090030import javax.ws.rs.Produces;
Hyunsun Moon44aac662017-02-18 02:07:01 +090031import javax.ws.rs.core.Context;
sanghoshin94872a12015-10-16 18:04:34 +090032import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
Hyunsun Moon44aac662017-02-18 02:07:01 +090034import javax.ws.rs.core.UriBuilder;
35import javax.ws.rs.core.UriInfo;
sanghoshin94872a12015-10-16 18:04:34 +090036import java.io.InputStream;
37
Hyunsun Moon44aac662017-02-18 02:07:01 +090038import static javax.ws.rs.core.Response.created;
39import static javax.ws.rs.core.Response.noContent;
40import static javax.ws.rs.core.Response.status;
Jian Lidea0fdb2018-04-02 19:02:48 +090041import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity;
sanghoc853a722016-07-04 21:10:42 +090042
sanghoshinf25d2e02015-11-11 23:07:17 +090043/**
44 * Handles Rest API call from Neutron ML2 plugin.
45 */
sanghoshin94872a12015-10-16 18:04:34 +090046@Path("ports")
47public class OpenstackPortWebResource extends AbstractWebResource {
Hyunsun Moon44aac662017-02-18 02:07:01 +090048 protected final Logger log = LoggerFactory.getLogger(getClass());
sanghoc853a722016-07-04 21:10:42 +090049
Hyunsun Moon44aac662017-02-18 02:07:01 +090050 private static final String MESSAGE = "Received ports %s request";
51 private static final String PORTS = "ports";
sanghoshin94872a12015-10-16 18:04:34 +090052
Hyunsun Moon44aac662017-02-18 02:07:01 +090053 private final OpenstackNetworkAdminService adminService =
Jian Li5fe34472018-02-21 10:24:31 +090054 get(OpenstackNetworkAdminService.class);
Hyunsun Moon44aac662017-02-18 02:07:01 +090055
56 @Context
57 private UriInfo uriInfo;
58
59 /**
60 * Creates a port from the JSON input stream.
61 *
62 * @param input port JSON input stream
63 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
64 * is invalid or duplicated port already exists
Jian Li2077f662018-02-12 14:01:35 +090065 * @onos.rsModel NeutronPort
Hyunsun Moon44aac662017-02-18 02:07:01 +090066 */
sanghoshin94872a12015-10-16 18:04:34 +090067 @POST
68 @Consumes(MediaType.APPLICATION_JSON)
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response createPorts(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090071 log.trace(String.format(MESSAGE, "CREATE"));
72
Jian Li091d8d22018-02-20 10:42:06 +090073 final NeutronPort port = (NeutronPort)
74 jsonToModelEntity(input, NeutronPort.class);
75
Hyunsun Moon44aac662017-02-18 02:07:01 +090076 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
Jian Li2077f662018-02-12 14:01:35 +090091 * @onos.rsModel NeutronPort
Hyunsun Moon44aac662017-02-18 02:07:01 +090092 */
sanghoshin94872a12015-10-16 18:04:34 +090093 @PUT
94 @Path("{id}")
95 @Consumes(MediaType.APPLICATION_JSON)
96 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +090097 public Response updatePort(@PathParam("id") String id, InputStream input) {
98 log.trace(String.format(MESSAGE, "UPDATE " + id));
sanghoc853a722016-07-04 21:10:42 +090099
Jian Li091d8d22018-02-20 10:42:06 +0900100 final NeutronPort port = (NeutronPort)
101 jsonToModelEntity(input, NeutronPort.class);
102
Hyunsun Moon44aac662017-02-18 02:07:01 +0900103 adminService.updatePort(port);
104
105 return status(Response.Status.OK).build();
106 }
107
108 /**
109 * Removes the port with the given id.
110 *
111 * @param id port identifier
112 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist
113 */
114 @DELETE
115 @Path("{id}")
116 @Consumes(MediaType.APPLICATION_JSON)
117 @Produces(MediaType.APPLICATION_JSON)
118 public Response deletePorts(@PathParam("id") String id) {
119 log.trace(String.format(MESSAGE, "DELETE " + id));
120
121 adminService.removePort(id);
122 return noContent().build();
123 }
sanghoshin94872a12015-10-16 18:04:34 +0900124}