blob: 72d814fd78d95d07229e22cd624b46805af5d628 [file] [log] [blame]
Hyunsun Moonb6febbe2016-02-12 15:59:53 -08001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2016-present Open Networking Foundation
Hyunsun Moonb6febbe2016-02-12 15:59:53 -08003 *
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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.cordvtn.rest;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080017
Hyunsun Moon01556a52016-02-12 12:48:47 -080018import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070019import org.onlab.osgi.DefaultServiceDirectory;
Hyunsun Moon187bf532017-01-19 10:57:40 +090020import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080022import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon187bf532017-01-19 10:57:40 +090023import org.opencord.cordvtn.api.core.ServiceNetworkAdminService;
24import org.opencord.cordvtn.api.net.NetworkId;
25import org.opencord.cordvtn.api.net.PortId;
26import org.opencord.cordvtn.api.net.ServicePort;
27import org.opencord.cordvtn.impl.DefaultServicePort;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070028import org.openstack4j.core.transport.ObjectMapperSingleton;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070029import org.openstack4j.openstack.networking.domain.NeutronPort;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import javax.ws.rs.Consumes;
34import javax.ws.rs.DELETE;
35import javax.ws.rs.POST;
36import javax.ws.rs.PUT;
37import javax.ws.rs.Path;
38import javax.ws.rs.PathParam;
39import javax.ws.rs.Produces;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070040import javax.ws.rs.core.Context;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080041import javax.ws.rs.core.MediaType;
42import javax.ws.rs.core.Response;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070043import javax.ws.rs.core.UriBuilder;
44import javax.ws.rs.core.UriInfo;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080045import java.io.InputStream;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070046
47import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
Hyunsun Moon187bf532017-01-19 10:57:40 +090048import static javax.ws.rs.core.Response.Status.OK;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070049import static javax.ws.rs.core.Response.created;
50import static javax.ws.rs.core.Response.noContent;
51import static javax.ws.rs.core.Response.status;
Hyunsun Moon01556a52016-02-12 12:48:47 -080052
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080053
54/**
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070055 * Neutron ML2 mechanism driver implementation for the port resource.
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080056 */
57@Path("ports")
58public class NeutronMl2PortsWebResource extends AbstractWebResource {
59 protected final Logger log = LoggerFactory.getLogger(getClass());
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080060
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070061 private static final String MESSAGE = "Received ports %s request";
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070062 private static final String PORTS = "ports";
Hyunsun Moon5510e342017-02-23 19:41:00 +090063 private static final String PORT_NAME_PREFIX = "tap";
Hyunsun Moon01556a52016-02-12 12:48:47 -080064
Hyunsun Moon187bf532017-01-19 10:57:40 +090065 private final ServiceNetworkAdminService adminService =
66 DefaultServiceDirectory.getService(ServiceNetworkAdminService.class);
Hyunsun Moon01556a52016-02-12 12:48:47 -080067
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070068 @Context
69 private UriInfo uriInfo;
70
71 /**
72 * Creates a port from the JSON input stream.
73 *
74 * @param input port JSON input stream
75 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
76 * is invalid or duplicated port already exists
77 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080078 @POST
79 @Consumes(MediaType.APPLICATION_JSON)
80 @Produces(MediaType.APPLICATION_JSON)
81 public Response createPorts(InputStream input) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070082 log.trace(String.format(MESSAGE, "CREATE"));
83
Hyunsun Moon187bf532017-01-19 10:57:40 +090084 final ServicePort sport = readPort(input);
85 adminService.createServicePort(sport);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070086 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
87 .path(PORTS)
Hyunsun Moon187bf532017-01-19 10:57:40 +090088 .path(sport.id().id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070089
90 return created(locationBuilder.build()).build();
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080091 }
92
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070093 /**
94 * Updates the port with the specified identifier.
95 *
96 * @param id port identifier
97 * @param input port JSON input stream
98 * @return 200 OK with the updated port, 400 BAD_REQUEST if the requested
99 * port does not exist
100 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800101 @PUT
102 @Path("{id}")
103 @Consumes(MediaType.APPLICATION_JSON)
104 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700105 public Response updatePort(@PathParam("id") String id, InputStream input) {
106 log.trace(String.format(MESSAGE, "UPDATE " + id));
Hyunsun Moon01556a52016-02-12 12:48:47 -0800107
Hyunsun Moon5510e342017-02-23 19:41:00 +0900108 final ServicePort sport = readPort(input);
109 adminService.updateServicePort(sport);
Hyunsun Moon01556a52016-02-12 12:48:47 -0800110
Hyunsun Moon187bf532017-01-19 10:57:40 +0900111 return status(OK).build();
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700112 }
113
114 /**
115 * Removes the port with the given id.
116 *
117 * @param id port identifier
118 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the port does not exist
119 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800120 @DELETE
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700121 @Path("{id}")
122 @Consumes(MediaType.APPLICATION_JSON)
123 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800124 public Response deletePorts(@PathParam("id") String id) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700125 log.trace(String.format(MESSAGE, "DELETE " + id));
126
Hyunsun Moon187bf532017-01-19 10:57:40 +0900127 adminService.removeServicePort(PortId.of(id));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700128 return noContent().build();
129 }
130
Hyunsun Moon187bf532017-01-19 10:57:40 +0900131 private ServicePort readPort(InputStream input) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700132 try {
133 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
134 log.trace(mapper().writeValueAsString(jsonTree));
Hyunsun Moon187bf532017-01-19 10:57:40 +0900135 NeutronPort osPort = ObjectMapperSingleton.getContext(NeutronPort.class)
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700136 .readerFor(NeutronPort.class)
137 .readValue(jsonTree);
Hyunsun Moon187bf532017-01-19 10:57:40 +0900138
139 ServicePort.Builder sportBuilder = DefaultServicePort.builder()
140 .id(PortId.of(osPort.getId()))
Hyunsun Moon5510e342017-02-23 19:41:00 +0900141 .name(PORT_NAME_PREFIX + osPort.getId().substring(0, 11))
Hyunsun Moon187bf532017-01-19 10:57:40 +0900142 .networkId(NetworkId.of(osPort.getNetworkId()));
143
Hyunsun Moon187bf532017-01-19 10:57:40 +0900144 if (osPort.getMacAddress() != null) {
145 sportBuilder.mac(MacAddress.valueOf(osPort.getMacAddress()));
146 }
147 if (!osPort.getFixedIps().isEmpty()) {
148 sportBuilder.ip(IpAddress.valueOf(
149 osPort.getFixedIps().iterator().next().getIpAddress()));
150 }
151
152 return sportBuilder.build();
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700153 } catch (Exception e) {
154 throw new IllegalArgumentException();
155 }
156 }
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800157}