Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +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 Park | 3a06c52 | 2016-01-28 20:51:12 +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 Park | 3a06c52 | 2016-01-28 20:51:12 +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.OpenstackRouterAdminService; |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +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.NeutronRouter; |
| 24 | import org.openstack4j.openstack.networking.domain.NeutronRouterInterface; |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import javax.ws.rs.Consumes; |
| 29 | import javax.ws.rs.DELETE; |
| 30 | import javax.ws.rs.POST; |
| 31 | import javax.ws.rs.PUT; |
| 32 | import javax.ws.rs.Path; |
| 33 | import javax.ws.rs.PathParam; |
| 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 Park | 3a06c52 | 2016-01-28 20:51:12 +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 Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 40 | import java.io.InputStream; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 41 | |
| 42 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 43 | import static javax.ws.rs.core.Response.created; |
| 44 | import static javax.ws.rs.core.Response.noContent; |
| 45 | import static javax.ws.rs.core.Response.status; |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Handles REST API call of Neturon L3 plugin. |
| 49 | */ |
| 50 | |
| 51 | @Path("routers") |
| 52 | public class OpensatckRouterWebResource extends AbstractWebResource { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 53 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 54 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 55 | private static final String MESSAGE_ROUTER = "Received router %s request"; |
| 56 | private static final String MESSAGE_ROUTER_IFACE = "Received router interface %s request"; |
| 57 | private static final String ROUTERS = "routers"; |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 58 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 59 | private final OpenstackRouterAdminService adminService = |
| 60 | DefaultServiceDirectory.getService(OpenstackRouterAdminService.class); |
| 61 | |
| 62 | @Context |
| 63 | private UriInfo uriInfo; |
| 64 | |
| 65 | /** |
| 66 | * Creates a router from the JSON input stream. |
| 67 | * |
| 68 | * @param input router JSON input stream |
| 69 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 70 | * is invalid or duplicated router already exists |
| 71 | */ |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 72 | @POST |
| 73 | @Consumes(MediaType.APPLICATION_JSON) |
| 74 | @Produces(MediaType.APPLICATION_JSON) |
| 75 | public Response createRouter(InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 76 | log.trace(String.format(MESSAGE_ROUTER, "CREATE")); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 77 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 78 | final NeutronRouter osRouter = readRouter(input); |
| 79 | adminService.createRouter(osRouter); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 80 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 81 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 82 | .path(ROUTERS) |
| 83 | .path(osRouter.getId()); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 84 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 85 | return created(locationBuilder.build()).build(); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 86 | } |
| 87 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 88 | /** |
| 89 | * Updates the router with the specified identifier. |
| 90 | * |
| 91 | * @param id router identifier |
| 92 | * @param input router JSON input stream |
| 93 | * @return 200 OK with the updated router, 400 BAD_REQUEST if the requested |
| 94 | * router does not exist |
| 95 | */ |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 96 | @PUT |
| 97 | @Path("{id}") |
| 98 | @Consumes(MediaType.APPLICATION_JSON) |
| 99 | @Produces(MediaType.APPLICATION_JSON) |
sangho | efbc038 | 2016-04-06 13:35:38 +0900 | [diff] [blame] | 100 | public Response updateRouter(@PathParam("id") String id, InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 101 | log.trace(String.format(MESSAGE_ROUTER, "UPDATE " + id)); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 102 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 103 | final NeutronRouter osRouter = readRouter(input); |
| 104 | osRouter.setId(id); |
| 105 | adminService.updateRouter(osRouter); |
sangho | efbc038 | 2016-04-06 13:35:38 +0900 | [diff] [blame] | 106 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 107 | return status(Response.Status.OK).build(); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 110 | /** |
| 111 | * Updates the router with the specified router interface. |
| 112 | * |
| 113 | * @param id router identifier |
| 114 | * @param input router interface JSON input stream |
| 115 | * @return 200 OK with the updated router interface, 400 BAD_REQUEST if the |
| 116 | * requested router does not exist |
| 117 | */ |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 118 | @PUT |
| 119 | @Path("{id}/add_router_interface") |
| 120 | @Consumes(MediaType.APPLICATION_JSON) |
| 121 | @Produces(MediaType.APPLICATION_JSON) |
sangho | efbc038 | 2016-04-06 13:35:38 +0900 | [diff] [blame] | 122 | public Response addRouterInterface(@PathParam("id") String id, InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 123 | log.trace(String.format(MESSAGE_ROUTER_IFACE, "UPDATE " + id)); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 124 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 125 | final NeutronRouterInterface osRouterIface = readRouterInterface(input); |
| 126 | adminService.addRouterInterface(osRouterIface); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 127 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 128 | return status(Response.Status.OK).build(); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 129 | } |
| 130 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 131 | /** |
| 132 | * Updates the router with the specified router interface. |
| 133 | * |
| 134 | * @param id router identifier |
| 135 | * @param input router interface JSON input stream |
| 136 | * @return 200 OK with the updated router interface, 400 BAD_REQUEST if the |
| 137 | * requested router does not exist |
| 138 | */ |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 139 | @PUT |
| 140 | @Path("{id}/remove_router_interface") |
| 141 | @Consumes(MediaType.APPLICATION_JSON) |
| 142 | @Produces(MediaType.APPLICATION_JSON) |
| 143 | public Response removeRouterInterface(@PathParam("id") String id, InputStream input) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 144 | log.trace(String.format(MESSAGE_ROUTER_IFACE, "DELETE " + id)); |
| 145 | |
| 146 | final NeutronRouterInterface osRouterIface = readRouterInterface(input); |
| 147 | adminService.removeRouterInterface(osRouterIface.getPortId()); |
| 148 | |
| 149 | return status(Response.Status.OK).build(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Removes the router. |
| 154 | * |
| 155 | * @param id router identifier |
| 156 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the router does not exist |
| 157 | */ |
| 158 | @DELETE |
| 159 | @Path("{id}") |
| 160 | @Consumes(MediaType.APPLICATION_JSON) |
| 161 | public Response deleteRouter(@PathParam("id") String id) { |
| 162 | log.trace(String.format(MESSAGE_ROUTER, "DELETE " + id)); |
| 163 | |
| 164 | adminService.removeRouter(id); |
| 165 | return noContent().build(); |
| 166 | } |
| 167 | |
| 168 | private NeutronRouter readRouter(InputStream input) { |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 169 | try { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 170 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 171 | log.trace(mapper().writeValueAsString(jsonTree)); |
| 172 | return ObjectMapperSingleton.getContext(NeutronRouter.class) |
| 173 | .readerFor(NeutronRouter.class) |
| 174 | .readValue(jsonTree); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 175 | } catch (Exception e) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 176 | throw new IllegalArgumentException(); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | private NeutronRouterInterface readRouterInterface(InputStream input) { |
| 181 | try { |
| 182 | JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input); |
| 183 | log.trace(mapper().writeValueAsString(jsonTree)); |
| 184 | return ObjectMapperSingleton.getContext(NeutronRouterInterface.class) |
| 185 | .readerFor(NeutronRouterInterface.class) |
| 186 | .readValue(jsonTree); |
| 187 | } catch (Exception e) { |
| 188 | throw new IllegalArgumentException(); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |