sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 3 | * |
| 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 | */ |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 16 | package org.onosproject.openstacknetworking.web; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 17 | |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 20 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame] | 21 | import org.onosproject.openstacknode.api.DpdkConfig; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 22 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 23 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 24 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 25 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
sangho | c853a72 | 2016-07-04 21:10:42 +0900 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 28 | |
| 29 | import javax.ws.rs.Consumes; |
| 30 | import javax.ws.rs.DELETE; |
| 31 | import javax.ws.rs.POST; |
| 32 | import javax.ws.rs.PUT; |
| 33 | import javax.ws.rs.Path; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 34 | import javax.ws.rs.PathParam; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 35 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 36 | import javax.ws.rs.core.Context; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 39 | import javax.ws.rs.core.UriBuilder; |
| 40 | import javax.ws.rs.core.UriInfo; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 41 | import java.io.InputStream; |
| 42 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 43 | import static javax.ws.rs.core.Response.created; |
| 44 | import static javax.ws.rs.core.Response.noContent; |
| 45 | import static javax.ws.rs.core.Response.status; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 46 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
sangho | c853a72 | 2016-07-04 21:10:42 +0900 | [diff] [blame] | 47 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 48 | /** |
| 49 | * Handles Rest API call from Neutron ML2 plugin. |
| 50 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 51 | @Path("ports") |
| 52 | public class OpenstackPortWebResource extends AbstractWebResource { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 53 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
sangho | c853a72 | 2016-07-04 21:10:42 +0900 | [diff] [blame] | 54 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 55 | private static final String MESSAGE = "Received ports %s request"; |
| 56 | private static final String PORTS = "ports"; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 57 | private static final String VIF_TYPE = "vif_type"; |
| 58 | private static final String VHOSTUSER = "vhostuser"; |
| 59 | private static final String SOCKET_DIR = "socket_dir"; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 60 | |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 61 | private final OpenstackNetworkAdminService |
| 62 | adminService = get(OpenstackNetworkAdminService.class); |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 63 | private final OpenstackNodeService nodeService = get(OpenstackNodeService.class); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 64 | |
| 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 Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 74 | * @onos.rsModel NeutronPort |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 75 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 76 | @POST |
| 77 | @Consumes(MediaType.APPLICATION_JSON) |
| 78 | @Produces(MediaType.APPLICATION_JSON) |
| 79 | public Response createPorts(InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 80 | log.trace(String.format(MESSAGE, "CREATE")); |
| 81 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 82 | final NeutronPort port = (NeutronPort) |
| 83 | jsonToModelEntity(input, NeutronPort.class); |
| 84 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 85 | adminService.createPort(port); |
| 86 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 87 | .path(PORTS) |
| 88 | .path(port.getId()); |
| 89 | |
| 90 | return created(locationBuilder.build()).build(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 91 | } |
| 92 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 93 | /** |
| 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 Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 100 | * @onos.rsModel NeutronPort |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 101 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 102 | @PUT |
| 103 | @Path("{id}") |
| 104 | @Consumes(MediaType.APPLICATION_JSON) |
| 105 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 106 | public Response updatePort(@PathParam("id") String id, InputStream input) { |
| 107 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
sangho | c853a72 | 2016-07-04 21:10:42 +0900 | [diff] [blame] | 108 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 109 | final NeutronPort port = (NeutronPort) |
| 110 | jsonToModelEntity(input, NeutronPort.class); |
| 111 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 112 | adminService.updatePort(port); |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 113 | ObjectMapper mapper = new ObjectMapper(); |
| 114 | ObjectNode jsonNode = mapper.createObjectNode(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 115 | |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 116 | OpenstackNode node = nodeService.node(port.getHostId()); |
| 117 | if (node == null) { |
| 118 | return status(Response.Status.OK).build(); |
Daniel Park | d02d7bd | 2018-08-23 23:04:31 +0900 | [diff] [blame] | 119 | } else if (node.datapathType().equals(DpdkConfig.DatapathType.NETDEV)) { |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 120 | 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 Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 132 | } |
| 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 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 150 | } |