blob: ce4fe52f5d13b6a2dc7adb372a65143c915d575e [file] [log] [blame]
sanghoshin94872a12015-10-16 18:04:34 +09001/*
Daniel Park3a06c522016-01-28 20:51:12 +09002 * Copyright 2015-2016 Open Networking Laboratory
sanghoshin94872a12015-10-16 18:04:34 +09003 *
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 */
sangho0c2a3da2016-02-16 13:39:07 +090016package org.onosproject.openstacknetworking.web;
sanghoshin94872a12015-10-16 18:04:34 +090017
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
sangho93447f12016-02-24 00:33:22 +090020import org.onosproject.openstackinterface.OpenstackPort;
21import org.onosproject.openstackinterface.web.OpenstackPortCodec;
sangho0c2a3da2016-02-16 13:39:07 +090022import org.onosproject.openstacknetworking.OpenstackSwitchingService;
sanghoshin94872a12015-10-16 18:04:34 +090023import org.onosproject.rest.AbstractWebResource;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
26
27import javax.ws.rs.Consumes;
28import javax.ws.rs.DELETE;
29import javax.ws.rs.POST;
30import javax.ws.rs.PUT;
31import javax.ws.rs.Path;
sanghoshin65723ae2015-11-17 22:07:21 +090032import javax.ws.rs.PathParam;
sanghoshin94872a12015-10-16 18:04:34 +090033import javax.ws.rs.Produces;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.Response;
36import java.io.InputStream;
37
sanghoshinf25d2e02015-11-11 23:07:17 +090038/**
39 * Handles Rest API call from Neutron ML2 plugin.
40 */
sanghoshin94872a12015-10-16 18:04:34 +090041@Path("ports")
42public class OpenstackPortWebResource extends AbstractWebResource {
43
sangho0c2a3da2016-02-16 13:39:07 +090044 private final Logger log = LoggerFactory.getLogger(getClass());
sanghoshin94872a12015-10-16 18:04:34 +090045
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);
sanghoshinf25d2e02015-11-11 23:07:17 +090057 OpenstackSwitchingService switchingService =
58 getService(OpenstackSwitchingService.class);
sanghoshin94872a12015-10-16 18:04:34 +090059 switchingService.createPorts(openstackPort);
sanghoshinf25d2e02015-11-11 23:07:17 +090060
sanghoshin46297d22015-11-03 17:51:24 +090061 log.debug("REST API ports is called with {}", portNode.toString());
sanghoshin94872a12015-10-16 18:04:34 +090062 return Response.status(Response.Status.OK).build();
sanghoshinf25d2e02015-11-11 23:07:17 +090063
sanghoshin94872a12015-10-16 18:04:34 +090064 } catch (Exception e) {
sanghoshinf25d2e02015-11-11 23:07:17 +090065 log.error("Creates Port failed because of exception {}",
sanghoshin94872a12015-10-16 18:04:34 +090066 e.toString());
67 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
68 .build();
69 }
70 }
71
sanghoshin65723ae2015-11-17 22:07:21 +090072 @Path("{portUUID}")
sangho0c2a3da2016-02-16 13:39:07 +090073 @Produces(MediaType.APPLICATION_JSON)
sanghoshin94872a12015-10-16 18:04:34 +090074 @DELETE
sanghoshin65723ae2015-11-17 22:07:21 +090075 public Response deletePorts(@PathParam("portUUID") String id) {
sangho3623cb62016-01-15 22:06:38 +090076 OpenstackSwitchingService switchingService =
77 getService(OpenstackSwitchingService.class);
sangho0c2a3da2016-02-16 13:39:07 +090078 switchingService.removePort(id);
sanghoshinf25d2e02015-11-11 23:07:17 +090079 return Response.status(Response.Status.OK).build();
sanghoshin94872a12015-10-16 18:04:34 +090080 }
81
82 @PUT
83 @Path("{id}")
84 @Consumes(MediaType.APPLICATION_JSON)
85 @Produces(MediaType.APPLICATION_JSON)
86 public Response updatePorts(InputStream input) {
sangho90088532016-02-25 18:06:12 +090087 try {
88 ObjectMapper mapper = new ObjectMapper();
89 ObjectNode portNode = (ObjectNode) mapper.readTree(input);
90
91 OpenstackPort openstackPort = PORT_CODEC.decode(portNode, this);
92 OpenstackSwitchingService switchingService =
93 getService(OpenstackSwitchingService.class);
94 switchingService.updatePort(openstackPort);
95
96 log.debug("REST API update port is called with {}", portNode.toString());
97 return Response.status(Response.Status.OK).build();
98
99 } catch (Exception e) {
100 log.error("Update Port failed because of exception {}",
101 e.toString());
102 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
103 .build();
104 }
sanghoshin94872a12015-10-16 18:04:34 +0900105 }
106}