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; |
| 31 | import javax.ws.rs.Produces; |
| 32 | import javax.ws.rs.core.MediaType; |
| 33 | import javax.ws.rs.core.Response; |
| 34 | import java.io.InputStream; |
| 35 | |
| 36 | @Path("ports") |
| 37 | public class OpenstackPortWebResource extends AbstractWebResource { |
| 38 | |
| 39 | protected static final Logger log = LoggerFactory |
| 40 | .getLogger(OpenstackPortWebResource.class); |
| 41 | |
| 42 | private static final OpenstackPortCodec PORT_CODEC = new OpenstackPortCodec(); |
| 43 | |
| 44 | @POST |
| 45 | @Consumes(MediaType.APPLICATION_JSON) |
| 46 | @Produces(MediaType.APPLICATION_JSON) |
| 47 | public Response createPorts(InputStream input) { |
| 48 | try { |
| 49 | ObjectMapper mapper = new ObjectMapper(); |
| 50 | ObjectNode portNode = (ObjectNode) mapper.readTree(input); |
| 51 | |
| 52 | OpenstackPort openstackPort = PORT_CODEC.decode(portNode, this); |
| 53 | |
| 54 | OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class); |
| 55 | switchingService.createPorts(openstackPort); |
| 56 | log.info("REST API ports is called with {}", portNode.toString()); |
| 57 | return Response.status(Response.Status.OK).build(); |
| 58 | } catch (Exception e) { |
| 59 | log.error("Creates VirtualPort failed because of exception {}", |
| 60 | e.toString()); |
| 61 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 62 | .build(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | @DELETE |
| 67 | @Consumes(MediaType.APPLICATION_JSON) |
| 68 | @Produces(MediaType.APPLICATION_JSON) |
| 69 | public Response deletesPorts(InputStream input) { |
| 70 | try { |
| 71 | ObjectMapper mapper = new ObjectMapper(); |
| 72 | ObjectNode portNode = (ObjectNode) mapper.readTree(input); |
| 73 | |
| 74 | OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class); |
| 75 | switchingService.deletePorts(); |
| 76 | log.info("REST API ports is called with {}", portNode.toString()); |
| 77 | return Response.status(Response.Status.OK).build(); |
| 78 | } catch (Exception e) { |
| 79 | log.error("Delete VirtualPort failed because of exception {}", |
| 80 | e.toString()); |
| 81 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 82 | .build(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @PUT |
| 87 | @Path("{id}") |
| 88 | @Consumes(MediaType.APPLICATION_JSON) |
| 89 | @Produces(MediaType.APPLICATION_JSON) |
| 90 | public Response updatePorts(InputStream input) { |
| 91 | try { |
| 92 | ObjectMapper mapper = new ObjectMapper(); |
| 93 | ObjectNode portNode = (ObjectNode) mapper.readTree(input); |
| 94 | |
| 95 | OpenstackSwitchingService switchingService = get(OpenstackSwitchingService.class); |
| 96 | switchingService.updatePorts(); |
| 97 | log.info("REST API ports is called with {}", portNode.toString()); |
| 98 | return Response.status(Response.Status.OK).build(); |
| 99 | } catch (Exception e) { |
| 100 | log.error("Update VirtualPort failed because of exception {}", |
| 101 | e.toString()); |
| 102 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString()) |
| 103 | .build(); |
| 104 | } |
| 105 | } |
| 106 | } |