blob: 4e2375392244bb96b89280c414b066ef61d1e15a [file] [log] [blame]
sanghoshin94872a12015-10-16 18:04:34 +09001/*
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 */
16package org.onosproject.openstackswitching.web;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.openstackswitching.OpenstackPort;
21import org.onosproject.openstackswitching.OpenstackSwitchingService;
22import org.onosproject.rest.AbstractWebResource;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import javax.ws.rs.Consumes;
27import javax.ws.rs.DELETE;
28import javax.ws.rs.POST;
29import javax.ws.rs.PUT;
30import javax.ws.rs.Path;
sanghoshin65723ae2015-11-17 22:07:21 +090031import javax.ws.rs.PathParam;
sanghoshin94872a12015-10-16 18:04:34 +090032import javax.ws.rs.Produces;
33import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
35import java.io.InputStream;
36
sanghoshinf25d2e02015-11-11 23:07:17 +090037/**
38 * Handles Rest API call from Neutron ML2 plugin.
39 */
sanghoshin94872a12015-10-16 18:04:34 +090040@Path("ports")
41public 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);
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}")
sanghoshin94872a12015-10-16 18:04:34 +090073 @DELETE
sanghoshin65723ae2015-11-17 22:07:21 +090074 public Response deletePorts(@PathParam("portUUID") String id) {
sangho3623cb62016-01-15 22:06:38 +090075 OpenstackSwitchingService switchingService =
76 getService(OpenstackSwitchingService.class);
77 switchingService.deletePort(id);
sanghoshinf25d2e02015-11-11 23:07:17 +090078 return Response.status(Response.Status.OK).build();
sanghoshin94872a12015-10-16 18:04:34 +090079 }
80
81 @PUT
82 @Path("{id}")
83 @Consumes(MediaType.APPLICATION_JSON)
84 @Produces(MediaType.APPLICATION_JSON)
85 public Response updatePorts(InputStream input) {
sanghoshinf25d2e02015-11-11 23:07:17 +090086 return Response.status(Response.Status.OK).build();
sanghoshin94872a12015-10-16 18:04:34 +090087 }
88}