sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 1 | /* |
Daniel Park | 3a06c52 | 2016-01-28 20:51:12 +0900 | [diff] [blame] | 2 | * Copyright 2015-2016 Open Networking Laboratory |
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 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
sangho | 93447f1 | 2016-02-24 00:33:22 +0900 | [diff] [blame] | 20 | import org.onosproject.openstackinterface.OpenstackPort; |
| 21 | import org.onosproject.openstackinterface.web.OpenstackPortCodec; |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 22 | import org.onosproject.openstacknetworking.OpenstackPortInfo; |
| 23 | import org.onosproject.openstacknetworking.OpenstackRoutingService; |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknetworking.OpenstackSwitchingService; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 25 | import org.onosproject.rest.AbstractWebResource; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import javax.ws.rs.Consumes; |
| 30 | import javax.ws.rs.DELETE; |
| 31 | import javax.ws.rs.POST; |
| 32 | import javax.ws.rs.PUT; |
| 33 | import javax.ws.rs.Path; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 34 | import javax.ws.rs.PathParam; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 35 | import javax.ws.rs.Produces; |
| 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
| 38 | import java.io.InputStream; |
| 39 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 40 | /** |
| 41 | * Handles Rest API call from Neutron ML2 plugin. |
| 42 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 43 | @Path("ports") |
| 44 | public class OpenstackPortWebResource extends AbstractWebResource { |
| 45 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 46 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 47 | private static final String PORTNAME_PREFIX_VM = "tap"; |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 48 | private static final OpenstackPortCodec PORT_CODEC = new OpenstackPortCodec(); |
| 49 | |
| 50 | @POST |
| 51 | @Consumes(MediaType.APPLICATION_JSON) |
| 52 | @Produces(MediaType.APPLICATION_JSON) |
| 53 | public Response createPorts(InputStream input) { |
| 54 | try { |
| 55 | ObjectMapper mapper = new ObjectMapper(); |
| 56 | ObjectNode portNode = (ObjectNode) mapper.readTree(input); |
| 57 | |
| 58 | OpenstackPort openstackPort = PORT_CODEC.decode(portNode, this); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 59 | OpenstackSwitchingService switchingService = |
| 60 | getService(OpenstackSwitchingService.class); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 61 | switchingService.createPorts(openstackPort); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 62 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 63 | log.debug("REST API ports is called with {}", portNode.toString()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 64 | return Response.status(Response.Status.OK).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 65 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 66 | } catch (Exception e) { |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 67 | log.error("Creates Port failed because of exception {}", |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 68 | e.toString()); |
| 69 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 70 | .build(); |
| 71 | } |
| 72 | } |
| 73 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 74 | @Path("{portUUID}") |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 75 | @Produces(MediaType.APPLICATION_JSON) |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 76 | @DELETE |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 77 | public Response deletePorts(@PathParam("portUUID") String id) { |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame] | 78 | OpenstackSwitchingService switchingService = |
| 79 | getService(OpenstackSwitchingService.class); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 80 | OpenstackPortInfo portInfo = switchingService.openstackPortInfo() |
| 81 | .get(PORTNAME_PREFIX_VM.concat(id.substring(0, 11))); |
| 82 | OpenstackRoutingService routingService = |
| 83 | getService(OpenstackRoutingService.class); |
| 84 | routingService.checkDisassociatedFloatingIp(id, portInfo); |
| 85 | |
sangho | 0c2a3da | 2016-02-16 13:39:07 +0900 | [diff] [blame] | 86 | switchingService.removePort(id); |
Kyuhwi Choi | ee9e371 | 2016-02-22 22:49:36 +0900 | [diff] [blame^] | 87 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 88 | return Response.status(Response.Status.OK).build(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @PUT |
| 92 | @Path("{id}") |
| 93 | @Consumes(MediaType.APPLICATION_JSON) |
| 94 | @Produces(MediaType.APPLICATION_JSON) |
| 95 | public Response updatePorts(InputStream input) { |
sangho | 9008853 | 2016-02-25 18:06:12 +0900 | [diff] [blame] | 96 | try { |
| 97 | ObjectMapper mapper = new ObjectMapper(); |
| 98 | ObjectNode portNode = (ObjectNode) mapper.readTree(input); |
| 99 | |
| 100 | OpenstackPort openstackPort = PORT_CODEC.decode(portNode, this); |
| 101 | OpenstackSwitchingService switchingService = |
| 102 | getService(OpenstackSwitchingService.class); |
| 103 | switchingService.updatePort(openstackPort); |
| 104 | |
| 105 | log.debug("REST API update port is called with {}", portNode.toString()); |
| 106 | return Response.status(Response.Status.OK).build(); |
| 107 | |
| 108 | } catch (Exception e) { |
| 109 | log.error("Update Port failed because of exception {}", |
| 110 | e.toString()); |
| 111 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 112 | .build(); |
| 113 | } |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 114 | } |
| 115 | } |