blob: 79bc6764a65c4d746d149c9f1fcd787c84dbd92b [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
Hyunsun Moon44aac662017-02-18 02:07:01 +090022import org.onlab.osgi.DefaultServiceDirectory;
23import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
danielbb83ebc2015-10-29 15:13:06 +090024import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon44aac662017-02-18 02:07:01 +090025import org.openstack4j.openstack.networking.domain.NeutronSubnet;
danielbb83ebc2015-10-29 15:13:06 +090026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import javax.ws.rs.Consumes;
sanghoshinf25d2e02015-11-11 23:07:17 +090030import javax.ws.rs.DELETE;
danielbb83ebc2015-10-29 15:13:06 +090031import javax.ws.rs.POST;
sanghoshinf25d2e02015-11-11 23:07:17 +090032import javax.ws.rs.PUT;
danielbb83ebc2015-10-29 15:13:06 +090033import javax.ws.rs.Path;
sanghoshinf25d2e02015-11-11 23:07:17 +090034import javax.ws.rs.PathParam;
danielbb83ebc2015-10-29 15:13:06 +090035import javax.ws.rs.Produces;
Hyunsun Moon44aac662017-02-18 02:07:01 +090036import javax.ws.rs.core.Context;
danielbb83ebc2015-10-29 15:13:06 +090037import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
Hyunsun Moon44aac662017-02-18 02:07:01 +090039import javax.ws.rs.core.UriBuilder;
40import javax.ws.rs.core.UriInfo;
danielbb83ebc2015-10-29 15:13:06 +090041import java.io.InputStream;
42
Hyunsun Moon44aac662017-02-18 02:07:01 +090043import static javax.ws.rs.core.Response.created;
44import static javax.ws.rs.core.Response.noContent;
45import static javax.ws.rs.core.Response.status;
Jian Li091d8d22018-02-20 10:42:06 +090046import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
Hyunsun Moon44aac662017-02-18 02:07:01 +090047
danielbb83ebc2015-10-29 15:13:06 +090048@Path("subnets")
49public class OpenstackSubnetWebResource extends AbstractWebResource {
Hyunsun Moon44aac662017-02-18 02:07:01 +090050 protected final Logger log = LoggerFactory.getLogger(getClass());
danielbb83ebc2015-10-29 15:13:06 +090051
Hyunsun Moon44aac662017-02-18 02:07:01 +090052 private static final String MESSAGE = "Received subnets %s request";
53 private static final String SUBNETS = "subnets";
54
55 private final OpenstackNetworkAdminService adminService =
56 DefaultServiceDirectory.getService(OpenstackNetworkAdminService.class);
57
58 @Context
59 private UriInfo uriInfo;
60
61 /**
62 * Creates a subnet from the JSON input stream.
63 *
64 * @param input subnet JSON input stream
65 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
66 * is invalid or duplicated subnet already exists
Jian Lid3472bf2018-02-12 15:22:04 +090067 * @onos.rsModel NeutronSubnet
Hyunsun Moon44aac662017-02-18 02:07:01 +090068 */
danielbb83ebc2015-10-29 15:13:06 +090069 @POST
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
72 public Response createSubnet(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090073 log.trace(String.format(MESSAGE, "CREATE"));
74
Jian Li091d8d22018-02-20 10:42:06 +090075 final NeutronSubnet subnet = (NeutronSubnet)
76 jsonToModelEntity(input, NeutronSubnet.class);
77
Hyunsun Moon44aac662017-02-18 02:07:01 +090078 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
Jian Li091d8d22018-02-20 10:42:06 +0900103 final NeutronSubnet subnet = (NeutronSubnet)
104 jsonToModelEntity(input, NeutronSubnet.class);
105
Hyunsun Moon44aac662017-02-18 02:07:01 +0900106 adminService.updateSubnet(subnet);
107
108 return status(Response.Status.OK).build();
sanghoshinf25d2e02015-11-11 23:07:17 +0900109 }
110
Hyunsun Moon44aac662017-02-18 02:07:01 +0900111 /**
112 * Removes the subnet.
113 *
114 * @param id subnet identifier
115 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the subnet does not exist
116 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900117 @DELETE
Hyunsun Moon44aac662017-02-18 02:07:01 +0900118 @Path("{id}")
119 @Consumes(MediaType.APPLICATION_JSON)
jskim7d881742016-09-13 09:39:02 +0900120 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900121 public Response deleteSubnet(@PathParam("id") String id) {
122 log.trace(String.format(MESSAGE, "DELETE " + id));
123
124 adminService.removeSubnet(id);
125 return noContent().build();
126 }
danielbb83ebc2015-10-29 15:13:06 +0900127}