daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
daniel | bb83ebc | 2015-10-29 15:13:06 +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; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 17 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 18 | /** |
| 19 | * Handles Rest API call from Neutron ML2 plugin. |
| 20 | */ |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 21 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 22 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 23 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 24 | import org.openstack4j.openstack.networking.domain.NeutronSubnet; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import javax.ws.rs.Consumes; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 29 | import javax.ws.rs.DELETE; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 30 | import javax.ws.rs.POST; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 31 | import javax.ws.rs.PUT; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 32 | import javax.ws.rs.Path; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 33 | import javax.ws.rs.PathParam; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 34 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 35 | import javax.ws.rs.core.Context; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 38 | import javax.ws.rs.core.UriBuilder; |
| 39 | import javax.ws.rs.core.UriInfo; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 40 | import java.io.InputStream; |
| 41 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 42 | import static javax.ws.rs.core.Response.created; |
| 43 | import static javax.ws.rs.core.Response.noContent; |
| 44 | import static javax.ws.rs.core.Response.status; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 45 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 46 | |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 47 | @Path("subnets") |
| 48 | public class OpenstackSubnetWebResource extends AbstractWebResource { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 49 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 50 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 51 | private static final String MESSAGE = "Received subnets %s request"; |
| 52 | private static final String SUBNETS = "subnets"; |
| 53 | |
| 54 | private final OpenstackNetworkAdminService adminService = |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 55 | get(OpenstackNetworkAdminService.class); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 56 | |
| 57 | @Context |
| 58 | private UriInfo uriInfo; |
| 59 | |
| 60 | /** |
| 61 | * Creates a subnet from the JSON input stream. |
| 62 | * |
| 63 | * @param input subnet JSON input stream |
| 64 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 65 | * is invalid or duplicated subnet already exists |
Jian Li | d3472bf | 2018-02-12 15:22:04 +0900 | [diff] [blame] | 66 | * @onos.rsModel NeutronSubnet |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 67 | */ |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 68 | @POST |
| 69 | @Consumes(MediaType.APPLICATION_JSON) |
| 70 | @Produces(MediaType.APPLICATION_JSON) |
| 71 | public Response createSubnet(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 NeutronSubnet subnet = (NeutronSubnet) |
| 75 | jsonToModelEntity(input, NeutronSubnet.class); |
| 76 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 77 | adminService.createSubnet(subnet); |
| 78 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 79 | .path(SUBNETS) |
| 80 | .path(subnet.getId()); |
| 81 | |
| 82 | // TODO fix networking-onos to send Network UPDATE when subnet created |
| 83 | return created(locationBuilder.build()).build(); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 84 | } |
| 85 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 86 | /** |
| 87 | * Updates the subnet with the specified identifier. |
| 88 | * |
| 89 | * @param id subnet identifier |
| 90 | * @param input subnet JSON input stream |
| 91 | * @return 200 OK with the updated subnet, 400 BAD_REQUEST if the requested |
| 92 | * subnet does not exist |
Jian Li | d3472bf | 2018-02-12 15:22:04 +0900 | [diff] [blame] | 93 | * @onos.rsModel NeutronSubnet |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 94 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 95 | @PUT |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 96 | @Path("{id}") |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 97 | @Produces(MediaType.APPLICATION_JSON) |
| 98 | @Consumes(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 99 | public Response updateSubnet(@PathParam("id") String id, InputStream input) { |
| 100 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
| 101 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 102 | final NeutronSubnet subnet = (NeutronSubnet) |
| 103 | jsonToModelEntity(input, NeutronSubnet.class); |
| 104 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 105 | adminService.updateSubnet(subnet); |
| 106 | |
| 107 | return status(Response.Status.OK).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 110 | /** |
| 111 | * Removes the subnet. |
| 112 | * |
| 113 | * @param id subnet identifier |
| 114 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the subnet does not exist |
| 115 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 116 | @DELETE |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 117 | @Path("{id}") |
| 118 | @Consumes(MediaType.APPLICATION_JSON) |
jskim | 7d88174 | 2016-09-13 09:39:02 +0900 | [diff] [blame] | 119 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 120 | public Response deleteSubnet(@PathParam("id") String id) { |
| 121 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 122 | |
| 123 | adminService.removeSubnet(id); |
| 124 | return noContent().build(); |
| 125 | } |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 126 | } |