Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 20 | import org.onosproject.openstacknetworking.OpenstackRouter; |
| 21 | import org.onosproject.openstacknetworking.OpenstackRouterInterface; |
| 22 | import org.onosproject.openstacknetworking.OpenstackRoutingService; |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 23 | import org.onosproject.rest.AbstractWebResource; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | import javax.ws.rs.Consumes; |
| 28 | import javax.ws.rs.DELETE; |
| 29 | import javax.ws.rs.POST; |
| 30 | import javax.ws.rs.PUT; |
| 31 | import javax.ws.rs.Path; |
| 32 | import javax.ws.rs.PathParam; |
| 33 | import javax.ws.rs.Produces; |
| 34 | import javax.ws.rs.core.MediaType; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | import java.io.InputStream; |
| 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | |
| 39 | /** |
| 40 | * Handles REST API call of Neturon L3 plugin. |
| 41 | */ |
| 42 | |
| 43 | @Path("routers") |
| 44 | public class OpensatckRouterWebResource extends AbstractWebResource { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 46 | |
| 47 | private static final OpenstackRouterInterfaceCodec ROUTER_INTERFACE_CODEC |
| 48 | = new OpenstackRouterInterfaceCodec(); |
| 49 | private static final OpenstackRouterCodec ROUTER_CODEC |
| 50 | = new OpenstackRouterCodec(); |
| 51 | |
| 52 | @POST |
| 53 | @Consumes(MediaType.APPLICATION_JSON) |
| 54 | @Produces(MediaType.APPLICATION_JSON) |
| 55 | public Response createRouter(InputStream input) { |
| 56 | checkNotNull(input); |
| 57 | try { |
| 58 | ObjectMapper mapper = new ObjectMapper(); |
| 59 | ObjectNode routerNode = (ObjectNode) mapper.readTree(input); |
| 60 | |
| 61 | OpenstackRouter openstackRouter |
| 62 | = ROUTER_CODEC.decode(routerNode, this); |
| 63 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 64 | OpenstackRoutingService routingService |
| 65 | = getService(OpenstackRoutingService.class); |
| 66 | routingService.createRouter(openstackRouter); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 67 | |
| 68 | log.debug("REST API CREATE router is called {}", input.toString()); |
| 69 | return Response.status(Response.Status.OK).build(); |
| 70 | } catch (Exception e) { |
| 71 | log.error("Create Router failed because of exception {}", |
| 72 | e.toString()); |
| 73 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 74 | .build(); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @PUT |
| 79 | @Path("{id}") |
| 80 | @Consumes(MediaType.APPLICATION_JSON) |
| 81 | @Produces(MediaType.APPLICATION_JSON) |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 82 | public Response updateRouter(InputStream input) { |
| 83 | checkNotNull(input); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 84 | try { |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 85 | ObjectMapper mapper = new ObjectMapper(); |
| 86 | ObjectNode routerNode = (ObjectNode) mapper.readTree(input); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 87 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 88 | OpenstackRouter openstackRouter |
| 89 | = ROUTER_CODEC.decode(routerNode, this); |
| 90 | |
| 91 | OpenstackRoutingService routingService |
| 92 | = getService(OpenstackRoutingService.class); |
| 93 | routingService.updateRouter(openstackRouter); |
| 94 | |
| 95 | log.debug("REST API UPDATE router is called from router {}", input.toString()); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 96 | return Response.status(Response.Status.OK).build(); |
| 97 | } catch (Exception e) { |
| 98 | log.error("Updates Router failed because of exception {}", |
| 99 | e.toString()); |
| 100 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 101 | .build(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @PUT |
| 106 | @Path("{id}/add_router_interface") |
| 107 | @Consumes(MediaType.APPLICATION_JSON) |
| 108 | @Produces(MediaType.APPLICATION_JSON) |
| 109 | public Response addRouterInterface(InputStream input) { |
| 110 | checkNotNull(input); |
| 111 | try { |
| 112 | ObjectMapper mapper = new ObjectMapper(); |
| 113 | ObjectNode routerIfNode = (ObjectNode) mapper.readTree(input); |
| 114 | |
| 115 | OpenstackRouterInterface openstackRouterInterface |
| 116 | = ROUTER_INTERFACE_CODEC.decode(routerIfNode, this); |
| 117 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 118 | OpenstackRoutingService routingService |
| 119 | = getService(OpenstackRoutingService.class); |
| 120 | routingService.updateRouterInterface(openstackRouterInterface); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 121 | |
| 122 | log.debug("REST API AddRouterInterface is called from router {} portId: {}, subnetId: {}, tenantId: {}", |
| 123 | openstackRouterInterface.id(), openstackRouterInterface.portId(), |
| 124 | openstackRouterInterface.subnetId(), openstackRouterInterface.tenantId()); |
| 125 | |
| 126 | return Response.status(Response.Status.OK).build(); |
| 127 | } catch (Exception e) { |
| 128 | log.error("AddRouterInterface failed because of exception {}", |
| 129 | e.toString()); |
| 130 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 131 | .build(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | @DELETE |
| 136 | @Path("{id}") |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 137 | @Produces(MediaType.APPLICATION_JSON) |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 138 | public Response deleteRouter(@PathParam("id") String id) { |
| 139 | checkNotNull(id); |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 140 | OpenstackRoutingService routingService |
| 141 | = getService(OpenstackRoutingService.class); |
| 142 | routingService.deleteRouter(id); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 143 | |
| 144 | log.debug("REST API DELETE routers is called {}", id); |
| 145 | return Response.status(Response.Status.OK).build(); |
| 146 | } |
| 147 | |
| 148 | @PUT |
| 149 | @Path("{id}/remove_router_interface") |
| 150 | @Consumes(MediaType.APPLICATION_JSON) |
| 151 | @Produces(MediaType.APPLICATION_JSON) |
| 152 | public Response removeRouterInterface(@PathParam("id") String id, InputStream input) { |
| 153 | checkNotNull(id); |
| 154 | checkNotNull(input); |
| 155 | try { |
| 156 | ObjectMapper mapper = new ObjectMapper(); |
| 157 | ObjectNode routerIfNode = (ObjectNode) mapper.readTree(input); |
| 158 | |
| 159 | OpenstackRouterInterface openstackRouterInterface |
| 160 | = ROUTER_INTERFACE_CODEC.decode(routerIfNode, this); |
| 161 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame^] | 162 | OpenstackRoutingService routingService |
| 163 | = getService(OpenstackRoutingService.class); |
| 164 | routingService.removeRouterInterface(openstackRouterInterface); |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 165 | |
| 166 | log.debug("REST API RemoveRouterInterface is called from router {} portId: {}, subnetId: {}," + |
| 167 | "tenantId: {}", openstackRouterInterface.id(), openstackRouterInterface.portId(), |
| 168 | openstackRouterInterface.subnetId(), openstackRouterInterface.tenantId()); |
| 169 | |
| 170 | return Response.status(Response.Status.OK).build(); |
| 171 | } catch (Exception e) { |
| 172 | log.error("RemoveRouterInterface failed because of exception {}", |
| 173 | e.toString()); |
| 174 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 175 | .build(); |
| 176 | } |
| 177 | } |
| 178 | } |