blob: e49ee18ebe565091469c19564216dd74945307b0 [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;
Kyuhwi Choiee9e3712016-02-22 22:49:36 +090022import org.onosproject.openstacknetworking.OpenstackPortInfo;
23import org.onosproject.openstacknetworking.OpenstackRoutingService;
sangho0c2a3da2016-02-16 13:39:07 +090024import org.onosproject.openstacknetworking.OpenstackSwitchingService;
sanghoshin94872a12015-10-16 18:04:34 +090025import org.onosproject.rest.AbstractWebResource;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import javax.ws.rs.Consumes;
30import javax.ws.rs.DELETE;
31import javax.ws.rs.POST;
32import javax.ws.rs.PUT;
33import javax.ws.rs.Path;
sanghoshin65723ae2015-11-17 22:07:21 +090034import javax.ws.rs.PathParam;
sanghoshin94872a12015-10-16 18:04:34 +090035import javax.ws.rs.Produces;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38import java.io.InputStream;
39
sanghoshinf25d2e02015-11-11 23:07:17 +090040/**
41 * Handles Rest API call from Neutron ML2 plugin.
42 */
sanghoshin94872a12015-10-16 18:04:34 +090043@Path("ports")
44public class OpenstackPortWebResource extends AbstractWebResource {
45
sangho0c2a3da2016-02-16 13:39:07 +090046 private final Logger log = LoggerFactory.getLogger(getClass());
Kyuhwi Choiee9e3712016-02-22 22:49:36 +090047 private static final String PORTNAME_PREFIX_VM = "tap";
sanghoshin94872a12015-10-16 18:04:34 +090048 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);
sanghoshinf25d2e02015-11-11 23:07:17 +090059 OpenstackSwitchingService switchingService =
60 getService(OpenstackSwitchingService.class);
sanghoshin94872a12015-10-16 18:04:34 +090061 switchingService.createPorts(openstackPort);
sanghoshinf25d2e02015-11-11 23:07:17 +090062
sanghoshin46297d22015-11-03 17:51:24 +090063 log.debug("REST API ports is called with {}", portNode.toString());
sanghoshin94872a12015-10-16 18:04:34 +090064 return Response.status(Response.Status.OK).build();
sanghoshinf25d2e02015-11-11 23:07:17 +090065
sanghoshin94872a12015-10-16 18:04:34 +090066 } catch (Exception e) {
sanghoshinf25d2e02015-11-11 23:07:17 +090067 log.error("Creates Port failed because of exception {}",
sanghoshin94872a12015-10-16 18:04:34 +090068 e.toString());
69 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.toString())
70 .build();
71 }
72 }
73
sanghoshin65723ae2015-11-17 22:07:21 +090074 @Path("{portUUID}")
sangho0c2a3da2016-02-16 13:39:07 +090075 @Produces(MediaType.APPLICATION_JSON)
sanghoshin94872a12015-10-16 18:04:34 +090076 @DELETE
sanghoshin65723ae2015-11-17 22:07:21 +090077 public Response deletePorts(@PathParam("portUUID") String id) {
sangho3623cb62016-01-15 22:06:38 +090078 OpenstackSwitchingService switchingService =
79 getService(OpenstackSwitchingService.class);
Kyuhwi Choiee9e3712016-02-22 22:49:36 +090080 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
sangho0c2a3da2016-02-16 13:39:07 +090086 switchingService.removePort(id);
Kyuhwi Choiee9e3712016-02-22 22:49:36 +090087
sanghoshinf25d2e02015-11-11 23:07:17 +090088 return Response.status(Response.Status.OK).build();
sanghoshin94872a12015-10-16 18:04:34 +090089 }
90
91 @PUT
92 @Path("{id}")
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces(MediaType.APPLICATION_JSON)
95 public Response updatePorts(InputStream input) {
sangho90088532016-02-25 18:06:12 +090096 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 }
sanghoshin94872a12015-10-16 18:04:34 +0900114 }
115}