blob: a3e23689b4ca132c0fa35e373b79d16599ea84f8 [file] [log] [blame]
danielbb83ebc2015-10-29 15:13:06 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
danielbb83ebc2015-10-29 15:13:06 +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;
danielbb83ebc2015-10-29 15:13:06 +090017
sanghoshinf25d2e02015-11-11 23:07:17 +090018/**
19 * Handles Rest API call from Neutron ML2 plugin.
20 */
Hyunsun Moon44aac662017-02-18 02:07:01 +090021
22import com.fasterxml.jackson.databind.JsonNode;
23import org.onlab.osgi.DefaultServiceDirectory;
24import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
danielbb83ebc2015-10-29 15:13:06 +090025import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon44aac662017-02-18 02:07:01 +090026import org.openstack4j.core.transport.ObjectMapperSingleton;
27import org.openstack4j.openstack.networking.domain.NeutronSubnet;
danielbb83ebc2015-10-29 15:13:06 +090028import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import javax.ws.rs.Consumes;
sanghoshinf25d2e02015-11-11 23:07:17 +090032import javax.ws.rs.DELETE;
danielbb83ebc2015-10-29 15:13:06 +090033import javax.ws.rs.POST;
sanghoshinf25d2e02015-11-11 23:07:17 +090034import javax.ws.rs.PUT;
danielbb83ebc2015-10-29 15:13:06 +090035import javax.ws.rs.Path;
sanghoshinf25d2e02015-11-11 23:07:17 +090036import javax.ws.rs.PathParam;
danielbb83ebc2015-10-29 15:13:06 +090037import javax.ws.rs.Produces;
Hyunsun Moon44aac662017-02-18 02:07:01 +090038import javax.ws.rs.core.Context;
danielbb83ebc2015-10-29 15:13:06 +090039import javax.ws.rs.core.MediaType;
40import javax.ws.rs.core.Response;
Hyunsun Moon44aac662017-02-18 02:07:01 +090041import javax.ws.rs.core.UriBuilder;
42import javax.ws.rs.core.UriInfo;
danielbb83ebc2015-10-29 15:13:06 +090043import java.io.InputStream;
44
Hyunsun Moon44aac662017-02-18 02:07:01 +090045import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
46import static javax.ws.rs.core.Response.created;
47import static javax.ws.rs.core.Response.noContent;
48import static javax.ws.rs.core.Response.status;
49
danielbb83ebc2015-10-29 15:13:06 +090050@Path("subnets")
51public class OpenstackSubnetWebResource extends AbstractWebResource {
Hyunsun Moon44aac662017-02-18 02:07:01 +090052 protected final Logger log = LoggerFactory.getLogger(getClass());
danielbb83ebc2015-10-29 15:13:06 +090053
Hyunsun Moon44aac662017-02-18 02:07:01 +090054 private static final String MESSAGE = "Received subnets %s request";
55 private static final String SUBNETS = "subnets";
56
57 private final OpenstackNetworkAdminService adminService =
58 DefaultServiceDirectory.getService(OpenstackNetworkAdminService.class);
59
60 @Context
61 private UriInfo uriInfo;
62
63 /**
64 * Creates a subnet from the JSON input stream.
65 *
66 * @param input subnet JSON input stream
67 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
68 * is invalid or duplicated subnet already exists
69 */
danielbb83ebc2015-10-29 15:13:06 +090070 @POST
71 @Consumes(MediaType.APPLICATION_JSON)
72 @Produces(MediaType.APPLICATION_JSON)
73 public Response createSubnet(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090074 log.trace(String.format(MESSAGE, "CREATE"));
75
76 final NeutronSubnet subnet = readSubnet(input);
77 adminService.createSubnet(subnet);
78 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
79 .path(SUBNETS)
80 .path(subnet.getId());
81
82 // TODO fix networking-onos to send Network UPDATE when subnet created
83 return created(locationBuilder.build()).build();
danielbb83ebc2015-10-29 15:13:06 +090084 }
85
Hyunsun Moon44aac662017-02-18 02:07:01 +090086 /**
87 * Updates the subnet with the specified identifier.
88 *
89 * @param id subnet identifier
90 * @param input subnet JSON input stream
91 * @return 200 OK with the updated subnet, 400 BAD_REQUEST if the requested
92 * subnet does not exist
93 */
sanghoshinf25d2e02015-11-11 23:07:17 +090094 @PUT
Hyunsun Moon44aac662017-02-18 02:07:01 +090095 @Path("{id}")
sanghoshinf25d2e02015-11-11 23:07:17 +090096 @Produces(MediaType.APPLICATION_JSON)
97 @Consumes(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +090098 public Response updateSubnet(@PathParam("id") String id, InputStream input) {
99 log.trace(String.format(MESSAGE, "UPDATE " + id));
100
101 final NeutronSubnet subnet = readSubnet(input);
102 adminService.updateSubnet(subnet);
103
104 return status(Response.Status.OK).build();
sanghoshinf25d2e02015-11-11 23:07:17 +0900105 }
106
Hyunsun Moon44aac662017-02-18 02:07:01 +0900107 /**
108 * Removes the subnet.
109 *
110 * @param id subnet identifier
111 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the subnet does not exist
112 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900113 @DELETE
Hyunsun Moon44aac662017-02-18 02:07:01 +0900114 @Path("{id}")
115 @Consumes(MediaType.APPLICATION_JSON)
jskim7d881742016-09-13 09:39:02 +0900116 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900117 public Response deleteSubnet(@PathParam("id") String id) {
118 log.trace(String.format(MESSAGE, "DELETE " + id));
119
120 adminService.removeSubnet(id);
121 return noContent().build();
122 }
123
124 private NeutronSubnet readSubnet(InputStream input) {
125 try {
126 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
127 log.trace(mapper().writeValueAsString(jsonTree));
128 return ObjectMapperSingleton.getContext(NeutronSubnet.class)
129 .readerFor(NeutronSubnet.class)
130 .readValue(jsonTree);
131 } catch (Exception e) {
132 throw new IllegalArgumentException();
133 }
sanghoshinf25d2e02015-11-11 23:07:17 +0900134 }
danielbb83ebc2015-10-29 15:13:06 +0900135}