blob: fb6192ca5100ffdc17aa388daebecf8894806ee9 [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
Jian Lid3472bf2018-02-12 15:22:04 +090069 * @onos.rsModel NeutronSubnet
Hyunsun Moon44aac662017-02-18 02:07:01 +090070 */
danielbb83ebc2015-10-29 15:13:06 +090071 @POST
72 @Consumes(MediaType.APPLICATION_JSON)
73 @Produces(MediaType.APPLICATION_JSON)
74 public Response createSubnet(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090075 log.trace(String.format(MESSAGE, "CREATE"));
76
77 final NeutronSubnet subnet = readSubnet(input);
78 adminService.createSubnet(subnet);
79 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
80 .path(SUBNETS)
81 .path(subnet.getId());
82
83 // TODO fix networking-onos to send Network UPDATE when subnet created
84 return created(locationBuilder.build()).build();
danielbb83ebc2015-10-29 15:13:06 +090085 }
86
Hyunsun Moon44aac662017-02-18 02:07:01 +090087 /**
88 * Updates the subnet with the specified identifier.
89 *
90 * @param id subnet identifier
91 * @param input subnet JSON input stream
92 * @return 200 OK with the updated subnet, 400 BAD_REQUEST if the requested
93 * subnet does not exist
Jian Lid3472bf2018-02-12 15:22:04 +090094 * @onos.rsModel NeutronSubnet
Hyunsun Moon44aac662017-02-18 02:07:01 +090095 */
sanghoshinf25d2e02015-11-11 23:07:17 +090096 @PUT
Hyunsun Moon44aac662017-02-18 02:07:01 +090097 @Path("{id}")
sanghoshinf25d2e02015-11-11 23:07:17 +090098 @Produces(MediaType.APPLICATION_JSON)
99 @Consumes(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900100 public Response updateSubnet(@PathParam("id") String id, InputStream input) {
101 log.trace(String.format(MESSAGE, "UPDATE " + id));
102
103 final NeutronSubnet subnet = readSubnet(input);
104 adminService.updateSubnet(subnet);
105
106 return status(Response.Status.OK).build();
sanghoshinf25d2e02015-11-11 23:07:17 +0900107 }
108
Hyunsun Moon44aac662017-02-18 02:07:01 +0900109 /**
110 * Removes the subnet.
111 *
112 * @param id subnet identifier
113 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the subnet does not exist
114 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900115 @DELETE
Hyunsun Moon44aac662017-02-18 02:07:01 +0900116 @Path("{id}")
117 @Consumes(MediaType.APPLICATION_JSON)
jskim7d881742016-09-13 09:39:02 +0900118 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900119 public Response deleteSubnet(@PathParam("id") String id) {
120 log.trace(String.format(MESSAGE, "DELETE " + id));
121
122 adminService.removeSubnet(id);
123 return noContent().build();
124 }
125
126 private NeutronSubnet readSubnet(InputStream input) {
127 try {
128 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
129 log.trace(mapper().writeValueAsString(jsonTree));
130 return ObjectMapperSingleton.getContext(NeutronSubnet.class)
131 .readerFor(NeutronSubnet.class)
132 .readValue(jsonTree);
133 } catch (Exception e) {
134 throw new IllegalArgumentException();
135 }
sanghoshinf25d2e02015-11-11 23:07:17 +0900136 }
danielbb83ebc2015-10-29 15:13:06 +0900137}