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 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 18 | import org.onlab.osgi.DefaultServiceDirectory; |
| 19 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 20 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 21 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
sangho | c853a72 | 2016-07-04 21:10:42 +0900 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 24 | |
| 25 | import javax.ws.rs.Consumes; |
| 26 | import javax.ws.rs.DELETE; |
| 27 | import javax.ws.rs.POST; |
| 28 | import javax.ws.rs.PUT; |
| 29 | import javax.ws.rs.Path; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 30 | import javax.ws.rs.PathParam; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 31 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 32 | import javax.ws.rs.core.Context; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 35 | import javax.ws.rs.core.UriBuilder; |
| 36 | import javax.ws.rs.core.UriInfo; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 37 | import java.io.InputStream; |
| 38 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 39 | import static javax.ws.rs.core.Response.created; |
| 40 | import static javax.ws.rs.core.Response.noContent; |
| 41 | import static javax.ws.rs.core.Response.status; |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 42 | import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity; |
sangho | c853a72 | 2016-07-04 21:10:42 +0900 | [diff] [blame] | 43 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 44 | /** |
| 45 | * Handles Rest API call from Neutron ML2 plugin. |
| 46 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 47 | @Path("ports") |
| 48 | public class OpenstackPortWebResource extends AbstractWebResource { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 49 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
sangho | c853a72 | 2016-07-04 21:10:42 +0900 | [diff] [blame] | 50 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 51 | private static final String MESSAGE = "Received ports %s request"; |
| 52 | private static final String PORTS = "ports"; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 53 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 54 | private final OpenstackNetworkAdminService adminService = |
| 55 | DefaultServiceDirectory.getService(OpenstackNetworkAdminService.class); |
| 56 | |
| 57 | @Context |
| 58 | private UriInfo uriInfo; |
| 59 | |
| 60 | /** |
| 61 | * Creates a port from the JSON input stream. |
| 62 | * |
| 63 | * @param input port JSON input stream |
| 64 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 65 | * is invalid or duplicated port already exists |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 66 | * @onos.rsModel NeutronPort |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 67 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 68 | @POST |
| 69 | @Consumes(MediaType.APPLICATION_JSON) |
| 70 | @Produces(MediaType.APPLICATION_JSON) |
| 71 | public Response createPorts(InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 72 | log.trace(String.format(MESSAGE, "CREATE")); |
| 73 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 74 | final NeutronPort port = (NeutronPort) |
| 75 | jsonToModelEntity(input, NeutronPort.class); |
| 76 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 77 | adminService.createPort(port); |
| 78 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 79 | .path(PORTS) |
| 80 | .path(port.getId()); |
| 81 | |
| 82 | return created(locationBuilder.build()).build(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 83 | } |
| 84 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 85 | /** |
| 86 | * Updates the port with the specified identifier. |
| 87 | * |
| 88 | * @param id port identifier |
| 89 | * @param input port JSON input stream |
| 90 | * @return 200 OK with the updated port, 400 BAD_REQUEST if the requested |
| 91 | * port does not exist |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 92 | * @onos.rsModel NeutronPort |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 93 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 94 | @PUT |
| 95 | @Path("{id}") |
| 96 | @Consumes(MediaType.APPLICATION_JSON) |
| 97 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 98 | public Response updatePort(@PathParam("id") String id, InputStream input) { |
| 99 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
sangho | c853a72 | 2016-07-04 21:10:42 +0900 | [diff] [blame] | 100 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 101 | final NeutronPort port = (NeutronPort) |
| 102 | jsonToModelEntity(input, NeutronPort.class); |
| 103 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 104 | adminService.updatePort(port); |
| 105 | |
| 106 | return status(Response.Status.OK).build(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Removes the port with the given id. |
| 111 | * |
| 112 | * @param id port identifier |
| 113 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist |
| 114 | */ |
| 115 | @DELETE |
| 116 | @Path("{id}") |
| 117 | @Consumes(MediaType.APPLICATION_JSON) |
| 118 | @Produces(MediaType.APPLICATION_JSON) |
| 119 | public Response deletePorts(@PathParam("id") String id) { |
| 120 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 121 | |
| 122 | adminService.removePort(id); |
| 123 | return noContent().build(); |
| 124 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 125 | } |