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.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 19 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 20 | import org.openstack4j.openstack.networking.domain.NeutronNetwork; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import javax.ws.rs.Consumes; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 25 | import javax.ws.rs.DELETE; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 26 | import javax.ws.rs.POST; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 27 | import javax.ws.rs.PUT; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 28 | import javax.ws.rs.Path; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 29 | import javax.ws.rs.PathParam; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 30 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 31 | import javax.ws.rs.core.Context; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 34 | import javax.ws.rs.core.UriBuilder; |
| 35 | import javax.ws.rs.core.UriInfo; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 36 | import java.io.InputStream; |
| 37 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 38 | import static javax.ws.rs.core.Response.created; |
| 39 | import static javax.ws.rs.core.Response.noContent; |
| 40 | import static javax.ws.rs.core.Response.status; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 41 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 42 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 43 | /** |
| 44 | * Handles REST API call of Neutron ML2 plugin. |
| 45 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 46 | @Path("networks") |
| 47 | public class OpenstackNetworkWebResource extends AbstractWebResource { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 48 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 49 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 50 | private static final String MESSAGE = "Received networks %s request"; |
| 51 | private static final String NETWORKS = "networks"; |
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 final OpenstackNetworkAdminService adminService = |
Jian Li | 5fe3447 | 2018-02-21 10:24:31 +0900 | [diff] [blame] | 54 | get(OpenstackNetworkAdminService.class); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 55 | |
| 56 | @Context |
| 57 | private UriInfo uriInfo; |
| 58 | |
| 59 | /** |
| 60 | * Creates a network from the JSON input stream. |
| 61 | * |
| 62 | * @param input network JSON input stream |
| 63 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 64 | * is invalid or duplicated network already exists |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 65 | * @onos.rsModel NeutronNetwork |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 66 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 67 | @POST |
| 68 | @Consumes(MediaType.APPLICATION_JSON) |
| 69 | @Produces(MediaType.APPLICATION_JSON) |
| 70 | public Response createNetwork(InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 71 | log.trace(String.format(MESSAGE, "CREATE")); |
| 72 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 73 | final NeutronNetwork net = (NeutronNetwork) |
| 74 | jsonToModelEntity(input, NeutronNetwork.class); |
| 75 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 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 |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 92 | * @onos.rsModel NeutronNetwork |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 93 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +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 updateNetwork(@PathParam("id") String id, InputStream input) { |
| 99 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
| 100 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 101 | final NeutronNetwork net = (NeutronNetwork) |
| 102 | jsonToModelEntity(input, NeutronNetwork.class); |
| 103 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 104 | adminService.updateNetwork(net); |
| 105 | |
| 106 | return status(Response.Status.OK).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 107 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 108 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 109 | /** |
| 110 | * Removes the service network. |
| 111 | * |
| 112 | * @param id network identifier |
| 113 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist |
| 114 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 115 | @DELETE |
| 116 | @Path("{id}") |
| 117 | @Consumes(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 118 | @Produces(MediaType.APPLICATION_JSON) |
| 119 | public Response deleteNetwork(@PathParam("id") String id) { |
| 120 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 121 | |
| 122 | adminService.removeNetwork(id); |
| 123 | return noContent().build(); |
| 124 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 125 | } |