blob: 218100e5cb5a8b110905c303f39c2fa0afc2fcd5 [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
Jian Li5ecfd1a2018-12-10 11:41:03 +090061 private final OpenstackNetworkAdminService
62 adminService = get(OpenstackNetworkAdminService.class);
Daniel Park7e8c4d82018-08-13 23:47:49 +090063 private final OpenstackNodeService nodeService = get(OpenstackNodeService.class);
Hyunsun Moon44aac662017-02-18 02:07:01 +090064
65 @Context
66 private UriInfo uriInfo;
67
68 /**
69 * Creates a port from the JSON input stream.
70 *
71 * @param input port JSON input stream
72 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
73 * is invalid or duplicated port already exists
Jian Li2077f662018-02-12 14:01:35 +090074 * @onos.rsModel NeutronPort
Hyunsun Moon44aac662017-02-18 02:07:01 +090075 */
sanghoshin94872a12015-10-16 18:04:34 +090076 @POST
77 @Consumes(MediaType.APPLICATION_JSON)
78 @Produces(MediaType.APPLICATION_JSON)
79 public Response createPorts(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090080 log.trace(String.format(MESSAGE, "CREATE"));
81
Jian Li091d8d22018-02-20 10:42:06 +090082 final NeutronPort port = (NeutronPort)
83 jsonToModelEntity(input, NeutronPort.class);
84
Hyunsun Moon44aac662017-02-18 02:07:01 +090085 adminService.createPort(port);
86 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
87 .path(PORTS)
88 .path(port.getId());
89
90 return created(locationBuilder.build()).build();
sanghoshin94872a12015-10-16 18:04:34 +090091 }
92
Hyunsun Moon44aac662017-02-18 02:07:01 +090093 /**
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
Jian Li2077f662018-02-12 14:01:35 +0900100 * @onos.rsModel NeutronPort
Hyunsun Moon44aac662017-02-18 02:07:01 +0900101 */
sanghoshin94872a12015-10-16 18:04:34 +0900102 @PUT
103 @Path("{id}")
104 @Consumes(MediaType.APPLICATION_JSON)
105 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900106 public Response updatePort(@PathParam("id") String id, InputStream input) {
107 log.trace(String.format(MESSAGE, "UPDATE " + id));
sanghoc853a722016-07-04 21:10:42 +0900108
Jian Li091d8d22018-02-20 10:42:06 +0900109 final NeutronPort port = (NeutronPort)
110 jsonToModelEntity(input, NeutronPort.class);
111
Hyunsun Moon44aac662017-02-18 02:07:01 +0900112 adminService.updatePort(port);
Daniel Park7e8c4d82018-08-13 23:47:49 +0900113 ObjectMapper mapper = new ObjectMapper();
114 ObjectNode jsonNode = mapper.createObjectNode();
Hyunsun Moon44aac662017-02-18 02:07:01 +0900115
Daniel Park7e8c4d82018-08-13 23:47:49 +0900116 OpenstackNode node = nodeService.node(port.getHostId());
117 if (node == null) {
118 return status(Response.Status.OK).build();
Daniel Parkd02d7bd2018-08-23 23:04:31 +0900119 } else if (node.datapathType().equals(DpdkConfig.DatapathType.NETDEV)) {
Daniel Park7e8c4d82018-08-13 23:47:49 +0900120 log.debug("UpdatePort for port {} called in netdev device {} " +
121 "so sends vif type as a payload of the response",
122 port.getId(), node.hostname());
123 jsonNode.put(VIF_TYPE, VHOSTUSER);
124
125 if (node.socketDir() != null) {
126 jsonNode.put(SOCKET_DIR, node.socketDir());
127 }
128 return status(Response.Status.OK).entity(jsonNode.toString()).build();
129 } else {
130 return status(Response.Status.OK).build();
131 }
Hyunsun Moon44aac662017-02-18 02:07:01 +0900132 }
133
134 /**
135 * Removes the port with the given id.
136 *
137 * @param id port identifier
138 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist
139 */
140 @DELETE
141 @Path("{id}")
142 @Consumes(MediaType.APPLICATION_JSON)
143 @Produces(MediaType.APPLICATION_JSON)
144 public Response deletePorts(@PathParam("id") String id) {
145 log.trace(String.format(MESSAGE, "DELETE " + id));
146
147 adminService.removePort(id);
148 return noContent().build();
149 }
sanghoshin94872a12015-10-16 18:04:34 +0900150}