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 | */ |
| 16 | package org.onosproject.openstackswitching.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.onosproject.openstackrouting.OpenstackRouter; |
| 21 | import org.onosproject.openstackrouting.OpenstackRouterInterface; |
| 22 | import org.onosproject.openstackswitching.OpenstackSwitchingService; |
| 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 { |
| 45 | protected static final Logger log = LoggerFactory |
| 46 | .getLogger(OpenstackNetworkWebResource.class); |
| 47 | |
| 48 | private static final OpenstackRouterInterfaceCodec ROUTER_INTERFACE_CODEC |
| 49 | = new OpenstackRouterInterfaceCodec(); |
| 50 | private static final OpenstackRouterCodec ROUTER_CODEC |
| 51 | = new OpenstackRouterCodec(); |
| 52 | |
| 53 | @POST |
| 54 | @Consumes(MediaType.APPLICATION_JSON) |
| 55 | @Produces(MediaType.APPLICATION_JSON) |
| 56 | public Response createRouter(InputStream input) { |
| 57 | checkNotNull(input); |
| 58 | try { |
| 59 | ObjectMapper mapper = new ObjectMapper(); |
| 60 | ObjectNode routerNode = (ObjectNode) mapper.readTree(input); |
| 61 | |
| 62 | OpenstackRouter openstackRouter |
| 63 | = ROUTER_CODEC.decode(routerNode, this); |
| 64 | |
| 65 | OpenstackSwitchingService switchingService |
| 66 | = getService(OpenstackSwitchingService.class); |
| 67 | switchingService.createRouter(openstackRouter); |
| 68 | |
| 69 | log.debug("REST API CREATE router is called {}", input.toString()); |
| 70 | return Response.status(Response.Status.OK).build(); |
| 71 | } catch (Exception e) { |
| 72 | log.error("Create Router failed because of exception {}", |
| 73 | e.toString()); |
| 74 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 75 | .build(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @PUT |
| 80 | @Path("{id}") |
| 81 | @Consumes(MediaType.APPLICATION_JSON) |
| 82 | @Produces(MediaType.APPLICATION_JSON) |
| 83 | public Response updateRouter(@PathParam("id") String id) { |
| 84 | checkNotNull(id); |
| 85 | try { |
| 86 | OpenstackSwitchingService switchingService |
| 87 | = getService(OpenstackSwitchingService.class); |
| 88 | switchingService.updateRouter(id); |
| 89 | |
| 90 | log.debug("REST API UPDATE router is called from router {}", id); |
| 91 | return Response.status(Response.Status.OK).build(); |
| 92 | } catch (Exception e) { |
| 93 | log.error("Updates Router failed because of exception {}", |
| 94 | e.toString()); |
| 95 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 96 | .build(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @PUT |
| 101 | @Path("{id}/add_router_interface") |
| 102 | @Consumes(MediaType.APPLICATION_JSON) |
| 103 | @Produces(MediaType.APPLICATION_JSON) |
| 104 | public Response addRouterInterface(InputStream input) { |
| 105 | checkNotNull(input); |
| 106 | try { |
| 107 | ObjectMapper mapper = new ObjectMapper(); |
| 108 | ObjectNode routerIfNode = (ObjectNode) mapper.readTree(input); |
| 109 | |
| 110 | OpenstackRouterInterface openstackRouterInterface |
| 111 | = ROUTER_INTERFACE_CODEC.decode(routerIfNode, this); |
| 112 | |
| 113 | OpenstackSwitchingService switchingService |
| 114 | = getService(OpenstackSwitchingService.class); |
| 115 | switchingService.updateRouterInterface(openstackRouterInterface); |
| 116 | |
| 117 | log.debug("REST API AddRouterInterface is called from router {} portId: {}, subnetId: {}, tenantId: {}", |
| 118 | openstackRouterInterface.id(), openstackRouterInterface.portId(), |
| 119 | openstackRouterInterface.subnetId(), openstackRouterInterface.tenantId()); |
| 120 | |
| 121 | return Response.status(Response.Status.OK).build(); |
| 122 | } catch (Exception e) { |
| 123 | log.error("AddRouterInterface failed because of exception {}", |
| 124 | e.toString()); |
| 125 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 126 | .build(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @DELETE |
| 131 | @Path("{id}") |
| 132 | public Response deleteRouter(@PathParam("id") String id) { |
| 133 | checkNotNull(id); |
| 134 | OpenstackSwitchingService switchingService = |
| 135 | getService(OpenstackSwitchingService.class); |
| 136 | switchingService.deleteRouter(id); |
| 137 | |
| 138 | log.debug("REST API DELETE routers is called {}", id); |
| 139 | return Response.status(Response.Status.OK).build(); |
| 140 | } |
| 141 | |
| 142 | @PUT |
| 143 | @Path("{id}/remove_router_interface") |
| 144 | @Consumes(MediaType.APPLICATION_JSON) |
| 145 | @Produces(MediaType.APPLICATION_JSON) |
| 146 | public Response removeRouterInterface(@PathParam("id") String id, InputStream input) { |
| 147 | checkNotNull(id); |
| 148 | checkNotNull(input); |
| 149 | try { |
| 150 | ObjectMapper mapper = new ObjectMapper(); |
| 151 | ObjectNode routerIfNode = (ObjectNode) mapper.readTree(input); |
| 152 | |
| 153 | OpenstackRouterInterface openstackRouterInterface |
| 154 | = ROUTER_INTERFACE_CODEC.decode(routerIfNode, this); |
| 155 | |
| 156 | OpenstackSwitchingService switchingService |
| 157 | = getService(OpenstackSwitchingService.class); |
| 158 | switchingService.removeRouterInterface(openstackRouterInterface); |
| 159 | |
| 160 | log.debug("REST API RemoveRouterInterface is called from router {} portId: {}, subnetId: {}," + |
| 161 | "tenantId: {}", openstackRouterInterface.id(), openstackRouterInterface.portId(), |
| 162 | openstackRouterInterface.subnetId(), openstackRouterInterface.tenantId()); |
| 163 | |
| 164 | return Response.status(Response.Status.OK).build(); |
| 165 | } catch (Exception e) { |
| 166 | log.error("RemoveRouterInterface failed because of exception {}", |
| 167 | e.toString()); |
| 168 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 169 | .build(); |
| 170 | } |
| 171 | } |
| 172 | } |