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 | |
| 22 | import com.fasterxml.jackson.databind.JsonNode; |
| 23 | import org.onlab.osgi.DefaultServiceDirectory; |
| 24 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 25 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 26 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 27 | import org.openstack4j.openstack.networking.domain.NeutronSubnet; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import javax.ws.rs.Consumes; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 32 | import javax.ws.rs.DELETE; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 33 | import javax.ws.rs.POST; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 34 | import javax.ws.rs.PUT; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 35 | import javax.ws.rs.Path; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 36 | import javax.ws.rs.PathParam; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 37 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 38 | import javax.ws.rs.core.Context; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 39 | import javax.ws.rs.core.MediaType; |
| 40 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 41 | import javax.ws.rs.core.UriBuilder; |
| 42 | import javax.ws.rs.core.UriInfo; |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 43 | import java.io.InputStream; |
| 44 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 45 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 46 | import static javax.ws.rs.core.Response.created; |
| 47 | import static javax.ws.rs.core.Response.noContent; |
| 48 | import static javax.ws.rs.core.Response.status; |
| 49 | |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 50 | @Path("subnets") |
| 51 | public class OpenstackSubnetWebResource extends AbstractWebResource { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 52 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 53 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 54 | private static final String MESSAGE = "Received subnets %s request"; |
| 55 | private static final String SUBNETS = "subnets"; |
| 56 | |
| 57 | private final OpenstackNetworkAdminService adminService = |
| 58 | DefaultServiceDirectory.getService(OpenstackNetworkAdminService.class); |
| 59 | |
| 60 | @Context |
| 61 | private UriInfo uriInfo; |
| 62 | |
| 63 | /** |
| 64 | * Creates a subnet from the JSON input stream. |
| 65 | * |
| 66 | * @param input subnet JSON input stream |
| 67 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 68 | * is invalid or duplicated subnet already exists |
| 69 | */ |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 70 | @POST |
| 71 | @Consumes(MediaType.APPLICATION_JSON) |
| 72 | @Produces(MediaType.APPLICATION_JSON) |
| 73 | public Response createSubnet(InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 74 | log.trace(String.format(MESSAGE, "CREATE")); |
| 75 | |
| 76 | final NeutronSubnet subnet = readSubnet(input); |
| 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 |
| 93 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 94 | @PUT |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 95 | @Path("{id}") |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 96 | @Produces(MediaType.APPLICATION_JSON) |
| 97 | @Consumes(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 98 | public Response updateSubnet(@PathParam("id") String id, InputStream input) { |
| 99 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
| 100 | |
| 101 | final NeutronSubnet subnet = readSubnet(input); |
| 102 | adminService.updateSubnet(subnet); |
| 103 | |
| 104 | return status(Response.Status.OK).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 105 | } |
| 106 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 107 | /** |
| 108 | * Removes the subnet. |
| 109 | * |
| 110 | * @param id subnet identifier |
| 111 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the subnet does not exist |
| 112 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 113 | @DELETE |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 114 | @Path("{id}") |
| 115 | @Consumes(MediaType.APPLICATION_JSON) |
jskim | 7d88174 | 2016-09-13 09:39:02 +0900 | [diff] [blame] | 116 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 117 | public Response deleteSubnet(@PathParam("id") String id) { |
| 118 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 119 | |
| 120 | adminService.removeSubnet(id); |
| 121 | return noContent().build(); |
| 122 | } |
| 123 | |
| 124 | private NeutronSubnet readSubnet(InputStream input) { |
| 125 | try { |
| 126 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 127 | log.trace(mapper().writeValueAsString(jsonTree)); |
| 128 | return ObjectMapperSingleton.getContext(NeutronSubnet.class) |
| 129 | .readerFor(NeutronSubnet.class) |
| 130 | .readValue(jsonTree); |
| 131 | } catch (Exception e) { |
| 132 | throw new IllegalArgumentException(); |
| 133 | } |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 134 | } |
daniel | bb83ebc | 2015-10-29 15:13:06 +0900 | [diff] [blame] | 135 | } |