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 com.fasterxml.jackson.databind.JsonNode; |
| 19 | import org.onlab.osgi.DefaultServiceDirectory; |
| 20 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 21 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 22 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 23 | import org.openstack4j.openstack.networking.domain.NeutronNetwork; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 28 | import javax.ws.rs.DELETE; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 29 | import javax.ws.rs.POST; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 30 | import javax.ws.rs.PUT; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 31 | import javax.ws.rs.Path; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 32 | import javax.ws.rs.PathParam; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 33 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 34 | import javax.ws.rs.core.Context; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 35 | import javax.ws.rs.core.MediaType; |
| 36 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 37 | import javax.ws.rs.core.UriBuilder; |
| 38 | import javax.ws.rs.core.UriInfo; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 39 | import java.io.InputStream; |
| 40 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 41 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 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; |
| 45 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 46 | /** |
| 47 | * Handles REST API call of Neutron ML2 plugin. |
| 48 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 49 | @Path("networks") |
| 50 | public class OpenstackNetworkWebResource extends AbstractWebResource { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 51 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 52 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 53 | private static final String MESSAGE = "Received networks %s request"; |
| 54 | private static final String NETWORKS = "networks"; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 55 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 56 | private final OpenstackNetworkAdminService adminService = |
| 57 | DefaultServiceDirectory.getService(OpenstackNetworkAdminService.class); |
| 58 | |
| 59 | @Context |
| 60 | private UriInfo uriInfo; |
| 61 | |
| 62 | /** |
| 63 | * Creates a network from the JSON input stream. |
| 64 | * |
| 65 | * @param input network JSON input stream |
| 66 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 67 | * is invalid or duplicated network already exists |
| 68 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 69 | @POST |
| 70 | @Consumes(MediaType.APPLICATION_JSON) |
| 71 | @Produces(MediaType.APPLICATION_JSON) |
| 72 | public Response createNetwork(InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 73 | log.trace(String.format(MESSAGE, "CREATE")); |
| 74 | |
| 75 | final NeutronNetwork net = readNetwork(input); |
| 76 | adminService.createNetwork(net); |
| 77 | |
| 78 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 79 | .path(NETWORKS) |
| 80 | .path(net.getId()); |
| 81 | |
| 82 | return created(locationBuilder.build()).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 83 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 84 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 85 | /** |
| 86 | * Updates the network with the specified identifier. |
| 87 | * |
| 88 | * @param id network identifier |
| 89 | * @param input network JSON input stream |
| 90 | * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested |
| 91 | * network does not exist |
| 92 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 93 | @PUT |
| 94 | @Path("{id}") |
| 95 | @Consumes(MediaType.APPLICATION_JSON) |
| 96 | @Produces(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 97 | public Response updateNetwork(@PathParam("id") String id, InputStream input) { |
| 98 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
| 99 | |
| 100 | final NeutronNetwork net = readNetwork(input); |
| 101 | adminService.updateNetwork(net); |
| 102 | |
| 103 | return status(Response.Status.OK).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 104 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 105 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 106 | /** |
| 107 | * Removes the service network. |
| 108 | * |
| 109 | * @param id network identifier |
| 110 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist |
| 111 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 112 | @DELETE |
| 113 | @Path("{id}") |
| 114 | @Consumes(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 115 | @Produces(MediaType.APPLICATION_JSON) |
| 116 | public Response deleteNetwork(@PathParam("id") String id) { |
| 117 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 118 | |
| 119 | adminService.removeNetwork(id); |
| 120 | return noContent().build(); |
| 121 | } |
| 122 | |
| 123 | private NeutronNetwork readNetwork(InputStream input) { |
| 124 | try { |
| 125 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 126 | log.trace(mapper().writeValueAsString(jsonTree)); |
| 127 | return ObjectMapperSingleton.getContext(NeutronNetwork.class) |
| 128 | .readerFor(NeutronNetwork.class) |
| 129 | .readValue(jsonTree); |
| 130 | } catch (Exception e) { |
| 131 | throw new IllegalArgumentException(); |
| 132 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 133 | } |
| 134 | } |