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 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 18 | import org.apache.commons.io.IOUtils; |
| 19 | import org.onosproject.openstacknetworking.api.OpenstackHaService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 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.openstack.networking.domain.NeutronNetwork; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 27 | import javax.ws.rs.DELETE; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 28 | import javax.ws.rs.POST; |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 29 | import javax.ws.rs.PUT; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 30 | import javax.ws.rs.Path; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 31 | import javax.ws.rs.PathParam; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 32 | import javax.ws.rs.Produces; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 33 | import javax.ws.rs.core.Context; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 36 | import javax.ws.rs.core.UriBuilder; |
| 37 | import javax.ws.rs.core.UriInfo; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 38 | import java.io.IOException; |
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 javax.ws.rs.core.Response.created; |
| 42 | import static javax.ws.rs.core.Response.noContent; |
| 43 | import static javax.ws.rs.core.Response.status; |
Daniel Park | f8f4f20 | 2019-09-15 16:38:06 +0900 | [diff] [blame] | 44 | import static org.onosproject.openstacknetworking.api.Constants.DEFAULT_ACTIVE_IP_ADDRESS; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 45 | import static org.onosproject.openstacknetworking.api.Constants.REST_UTF8; |
Jian Li | dea0fdb | 2018-04-02 19:02:48 +0900 | [diff] [blame] | 46 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 47 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncDelete; |
| 48 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncPost; |
| 49 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.syncPut; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 50 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 51 | /** |
Jian Li | 68efab4 | 2019-03-31 19:43:18 +0900 | [diff] [blame] | 52 | * Handles network REST API call of Neutron ML2 plugin. |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 53 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 54 | @Path("networks") |
| 55 | public class OpenstackNetworkWebResource extends AbstractWebResource { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 56 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 57 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 58 | private static final String MESSAGE = "Received networks %s request"; |
| 59 | private static final String NETWORKS = "networks"; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 60 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 61 | private final OpenstackNetworkAdminService adminService = |
Jian Li | 5fe3447 | 2018-02-21 10:24:31 +0900 | [diff] [blame] | 62 | get(OpenstackNetworkAdminService.class); |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 63 | private final OpenstackHaService haService = get(OpenstackHaService.class); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 64 | |
| 65 | @Context |
| 66 | private UriInfo uriInfo; |
| 67 | |
| 68 | /** |
| 69 | * Creates a network from the JSON input stream. |
| 70 | * |
| 71 | * @param input network JSON input stream |
| 72 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 73 | * is invalid or duplicated network already exists |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 74 | * @throws IOException exception |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 75 | * @onos.rsModel NeutronNetwork |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 76 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 77 | @POST |
| 78 | @Consumes(MediaType.APPLICATION_JSON) |
| 79 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 80 | public Response createNetwork(InputStream input) throws IOException { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 81 | log.trace(String.format(MESSAGE, "CREATE")); |
| 82 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 83 | String inputStr = IOUtils.toString(input, REST_UTF8); |
| 84 | |
Daniel Park | f8f4f20 | 2019-09-15 16:38:06 +0900 | [diff] [blame] | 85 | if (!haService.isActive() |
| 86 | && !DEFAULT_ACTIVE_IP_ADDRESS.equals(haService.getActiveIp())) { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 87 | return syncPost(haService, NETWORKS, inputStr); |
| 88 | } |
| 89 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 90 | final NeutronNetwork net = (NeutronNetwork) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 91 | jsonToModelEntity(inputStr, NeutronNetwork.class); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 92 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 93 | adminService.createNetwork(net); |
| 94 | |
| 95 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 96 | .path(NETWORKS) |
| 97 | .path(net.getId()); |
| 98 | |
| 99 | return created(locationBuilder.build()).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 100 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 101 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 102 | /** |
| 103 | * Updates the network with the specified identifier. |
| 104 | * |
| 105 | * @param id network identifier |
| 106 | * @param input network JSON input stream |
| 107 | * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested |
| 108 | * network does not exist |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 109 | * @throws IOException exception |
Jian Li | 2077f66 | 2018-02-12 14:01:35 +0900 | [diff] [blame] | 110 | * @onos.rsModel NeutronNetwork |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 111 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 112 | @PUT |
| 113 | @Path("{id}") |
| 114 | @Consumes(MediaType.APPLICATION_JSON) |
| 115 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 116 | public Response updateNetwork(@PathParam("id") String id, InputStream input) throws IOException { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 117 | log.trace(String.format(MESSAGE, "UPDATE " + id)); |
| 118 | |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 119 | String inputStr = IOUtils.toString(input, REST_UTF8); |
| 120 | |
Daniel Park | f8f4f20 | 2019-09-15 16:38:06 +0900 | [diff] [blame] | 121 | if (!haService.isActive() |
| 122 | && !DEFAULT_ACTIVE_IP_ADDRESS.equals(haService.getActiveIp())) { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 123 | return syncPut(haService, NETWORKS, id, inputStr); |
| 124 | } |
| 125 | |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 126 | final NeutronNetwork net = (NeutronNetwork) |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 127 | jsonToModelEntity(inputStr, NeutronNetwork.class); |
Jian Li | 091d8d2 | 2018-02-20 10:42:06 +0900 | [diff] [blame] | 128 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 129 | adminService.updateNetwork(net); |
| 130 | |
| 131 | return status(Response.Status.OK).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 132 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 133 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 134 | /** |
| 135 | * Removes the service network. |
| 136 | * |
| 137 | * @param id network identifier |
| 138 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist |
| 139 | */ |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 140 | @DELETE |
| 141 | @Path("{id}") |
| 142 | @Consumes(MediaType.APPLICATION_JSON) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 143 | @Produces(MediaType.APPLICATION_JSON) |
| 144 | public Response deleteNetwork(@PathParam("id") String id) { |
| 145 | log.trace(String.format(MESSAGE, "DELETE " + id)); |
| 146 | |
Daniel Park | f8f4f20 | 2019-09-15 16:38:06 +0900 | [diff] [blame] | 147 | if (!haService.isActive() |
| 148 | && !DEFAULT_ACTIVE_IP_ADDRESS.equals(haService.getActiveIp())) { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 149 | return syncDelete(haService, NETWORKS, id); |
| 150 | } |
| 151 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 152 | adminService.removeNetwork(id); |
| 153 | return noContent().build(); |
| 154 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 155 | } |