blob: bd10f6fec1242153af91a5970ac6256010e72bfe [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
Daniel Park7e8c4d82018-08-13 23:47:49 +090018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Hyunsun Moon44aac662017-02-18 02:07:01 +090020import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
Daniel Parkd02d7bd2018-08-23 23:04:31 +090021import org.onosproject.openstacknode.api.DpdkConfig;
Daniel Park7e8c4d82018-08-13 23:47:49 +090022import org.onosproject.openstacknode.api.OpenstackNode;
23import org.onosproject.openstacknode.api.OpenstackNodeService;
sanghoshin94872a12015-10-16 18:04:34 +090024import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon44aac662017-02-18 02:07:01 +090025import org.openstack4j.openstack.networking.domain.NeutronPort;
sanghoc853a722016-07-04 21:10:42 +090026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
sanghoshin94872a12015-10-16 18:04:34 +090028
29import javax.ws.rs.Consumes;
30import javax.ws.rs.DELETE;
31import javax.ws.rs.POST;
32import javax.ws.rs.PUT;
33import javax.ws.rs.Path;
sanghoshin65723ae2015-11-17 22:07:21 +090034import javax.ws.rs.PathParam;
sanghoshin94872a12015-10-16 18:04:34 +090035import javax.ws.rs.Produces;
Hyunsun Moon44aac662017-02-18 02:07:01 +090036import javax.ws.rs.core.Context;
sanghoshin94872a12015-10-16 18:04:34 +090037import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
Hyunsun Moon44aac662017-02-18 02:07:01 +090039import javax.ws.rs.core.UriBuilder;
40import javax.ws.rs.core.UriInfo;
sanghoshin94872a12015-10-16 18:04:34 +090041import java.io.InputStream;
42
Hyunsun Moon44aac662017-02-18 02:07:01 +090043import static javax.ws.rs.core.Response.created;
44import static javax.ws.rs.core.Response.noContent;
45import static javax.ws.rs.core.Response.status;
Jian Lidea0fdb2018-04-02 19:02:48 +090046import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity;
sanghoc853a722016-07-04 21:10:42 +090047
sanghoshinf25d2e02015-11-11 23:07:17 +090048/**
49 * Handles Rest API call from Neutron ML2 plugin.
50 */
sanghoshin94872a12015-10-16 18:04:34 +090051@Path("ports")
52public class OpenstackPortWebResource extends AbstractWebResource {
Hyunsun Moon44aac662017-02-18 02:07:01 +090053 protected final Logger log = LoggerFactory.getLogger(getClass());
sanghoc853a722016-07-04 21:10:42 +090054
Hyunsun Moon44aac662017-02-18 02:07:01 +090055 private static final String MESSAGE = "Received ports %s request";
56 private static final String PORTS = "ports";
Daniel Park7e8c4d82018-08-13 23:47:49 +090057 private static final String VIF_TYPE = "vif_type";
58 private static final String VHOSTUSER = "vhostuser";
59 private static final String SOCKET_DIR = "socket_dir";
sanghoshin94872a12015-10-16 18:04:34 +090060
Daniel Park7e8c4d82018-08-13 23:47:49 +090061 private final OpenstackNetworkAdminService adminService = get(OpenstackNetworkAdminService.class);
62 private final OpenstackNodeService nodeService = get(OpenstackNodeService.class);
Hyunsun Moon44aac662017-02-18 02:07:01 +090063
64 @Context
65 private UriInfo uriInfo;
66
67 /**
68 * Creates a port from the JSON input stream.
69 *
70 * @param input port JSON input stream
71 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
72 * is invalid or duplicated port already exists
Jian Li2077f662018-02-12 14:01:35 +090073 * @onos.rsModel NeutronPort
Hyunsun Moon44aac662017-02-18 02:07:01 +090074 */
sanghoshin94872a12015-10-16 18:04:34 +090075 @POST
76 @Consumes(MediaType.APPLICATION_JSON)
77 @Produces(MediaType.APPLICATION_JSON)
78 public Response createPorts(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090079 log.trace(String.format(MESSAGE, "CREATE"));
80
Jian Li091d8d22018-02-20 10:42:06 +090081 final NeutronPort port = (NeutronPort)
82 jsonToModelEntity(input, NeutronPort.class);
83
Hyunsun Moon44aac662017-02-18 02:07:01 +090084 adminService.createPort(port);
85 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
86 .path(PORTS)
87 .path(port.getId());
88
89 return created(locationBuilder.build()).build();
sanghoshin94872a12015-10-16 18:04:34 +090090 }
91
Hyunsun Moon44aac662017-02-18 02:07:01 +090092 /**
93 * Updates the port with the specified identifier.
94 *
95 * @param id port identifier
96 * @param input port JSON input stream
97 * @return 200 OK with the updated port, 400 BAD_REQUEST if the requested
98 * port does not exist
Jian Li2077f662018-02-12 14:01:35 +090099 * @onos.rsModel NeutronPort
Hyunsun Moon44aac662017-02-18 02:07:01 +0900100 */
sanghoshin94872a12015-10-16 18:04:34 +0900101 @PUT
102 @Path("{id}")
103 @Consumes(MediaType.APPLICATION_JSON)
104 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900105 public Response updatePort(@PathParam("id") String id, InputStream input) {
106 log.trace(String.format(MESSAGE, "UPDATE " + id));
sanghoc853a722016-07-04 21:10:42 +0900107
Jian Li091d8d22018-02-20 10:42:06 +0900108 final NeutronPort port = (NeutronPort)
109 jsonToModelEntity(input, NeutronPort.class);
110
Hyunsun Moon44aac662017-02-18 02:07:01 +0900111 adminService.updatePort(port);
Daniel Park7e8c4d82018-08-13 23:47:49 +0900112 ObjectMapper mapper = new ObjectMapper();
113 ObjectNode jsonNode = mapper.createObjectNode();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900114
Daniel Park7e8c4d82018-08-13 23:47:49 +0900115 OpenstackNode node = nodeService.node(port.getHostId());
116 if (node == null) {
117 return status(Response.Status.OK).build();
Daniel Parkd02d7bd2018-08-23 23:04:31 +0900118 } else if (node.datapathType().equals(DpdkConfig.DatapathType.NETDEV)) {
Daniel Park7e8c4d82018-08-13 23:47:49 +0900119 log.debug("UpdatePort for port {} called in netdev device {} " +
120 "so sends vif type as a payload of the response",
121 port.getId(), node.hostname());
122 jsonNode.put(VIF_TYPE, VHOSTUSER);
123
124 if (node.socketDir() != null) {
125 jsonNode.put(SOCKET_DIR, node.socketDir());
126 }
127 return status(Response.Status.OK).entity(jsonNode.toString()).build();
128 } else {
129 return status(Response.Status.OK).build();
130 }
Hyunsun Moon44aac662017-02-18 02:07:01 +0900131 }
132
133 /**
134 * Removes the port with the given id.
135 *
136 * @param id port identifier
137 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist
138 */
139 @DELETE
140 @Path("{id}")
141 @Consumes(MediaType.APPLICATION_JSON)
142 @Produces(MediaType.APPLICATION_JSON)
143 public Response deletePorts(@PathParam("id") String id) {
144 log.trace(String.format(MESSAGE, "DELETE " + id));
145
146 adminService.removePort(id);
147 return noContent().build();
148 }
sanghoshin94872a12015-10-16 18:04:34 +0900149}