sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.openstackswitching.OpenstackPort; |
| 21 | import org.onosproject.openstackswitching.OpenstackSwitchingService; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import javax.ws.rs.Consumes; |
| 27 | import javax.ws.rs.DELETE; |
| 28 | import javax.ws.rs.POST; |
| 29 | import javax.ws.rs.PUT; |
| 30 | import javax.ws.rs.Path; |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +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; |
| 33 | import javax.ws.rs.core.MediaType; |
| 34 | import javax.ws.rs.core.Response; |
| 35 | import java.io.InputStream; |
| 36 | |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 37 | /** |
| 38 | * Handles Rest API call from Neutron ML2 plugin. |
| 39 | */ |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 40 | @Path("ports") |
| 41 | public class OpenstackPortWebResource extends AbstractWebResource { |
| 42 | |
| 43 | protected static final Logger log = LoggerFactory |
| 44 | .getLogger(OpenstackPortWebResource.class); |
| 45 | |
| 46 | private static final OpenstackPortCodec PORT_CODEC = new OpenstackPortCodec(); |
| 47 | |
| 48 | @POST |
| 49 | @Consumes(MediaType.APPLICATION_JSON) |
| 50 | @Produces(MediaType.APPLICATION_JSON) |
| 51 | public Response createPorts(InputStream input) { |
| 52 | try { |
| 53 | ObjectMapper mapper = new ObjectMapper(); |
| 54 | ObjectNode portNode = (ObjectNode) mapper.readTree(input); |
| 55 | |
| 56 | OpenstackPort openstackPort = PORT_CODEC.decode(portNode, this); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 57 | OpenstackSwitchingService switchingService = |
| 58 | getService(OpenstackSwitchingService.class); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 59 | switchingService.createPorts(openstackPort); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 60 | |
sanghoshin | 46297d2 | 2015-11-03 17:51:24 +0900 | [diff] [blame] | 61 | log.debug("REST API ports is called with {}", portNode.toString()); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 62 | return Response.status(Response.Status.OK).build(); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 63 | |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 64 | } catch (Exception e) { |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 65 | log.error("Creates Port failed because of exception {}", |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 66 | e.toString()); |
| 67 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 68 | .build(); |
| 69 | } |
| 70 | } |
| 71 | |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 72 | @Path("{portUUID}") |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 73 | @DELETE |
sanghoshin | 65723ae | 2015-11-17 22:07:21 +0900 | [diff] [blame] | 74 | public Response deletePorts(@PathParam("portUUID") String id) { |
sangho | 3623cb6 | 2016-01-15 22:06:38 +0900 | [diff] [blame^] | 75 | OpenstackSwitchingService switchingService = |
| 76 | getService(OpenstackSwitchingService.class); |
| 77 | switchingService.deletePort(id); |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 78 | return Response.status(Response.Status.OK).build(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @PUT |
| 82 | @Path("{id}") |
| 83 | @Consumes(MediaType.APPLICATION_JSON) |
| 84 | @Produces(MediaType.APPLICATION_JSON) |
| 85 | public Response updatePorts(InputStream input) { |
sanghoshin | f25d2e0 | 2015-11-11 23:07:17 +0900 | [diff] [blame] | 86 | return Response.status(Response.Status.OK).build(); |
sanghoshin | 94872a1 | 2015-10-16 18:04:34 +0900 | [diff] [blame] | 87 | } |
| 88 | } |