blob: 0c46bfc1f9b5ad4425865f2387906c97610864c4 [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.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
danielbb83ebc2015-10-29 15:13:06 +090023import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon44aac662017-02-18 02:07:01 +090024import org.openstack4j.openstack.networking.domain.NeutronSubnet;
danielbb83ebc2015-10-29 15:13:06 +090025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import javax.ws.rs.Consumes;
sanghoshinf25d2e02015-11-11 23:07:17 +090029import javax.ws.rs.DELETE;
danielbb83ebc2015-10-29 15:13:06 +090030import javax.ws.rs.POST;
sanghoshinf25d2e02015-11-11 23:07:17 +090031import javax.ws.rs.PUT;
danielbb83ebc2015-10-29 15:13:06 +090032import javax.ws.rs.Path;
sanghoshinf25d2e02015-11-11 23:07:17 +090033import javax.ws.rs.PathParam;
danielbb83ebc2015-10-29 15:13:06 +090034import javax.ws.rs.Produces;
Hyunsun Moon44aac662017-02-18 02:07:01 +090035import javax.ws.rs.core.Context;
danielbb83ebc2015-10-29 15:13:06 +090036import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
Hyunsun Moon44aac662017-02-18 02:07:01 +090038import javax.ws.rs.core.UriBuilder;
39import javax.ws.rs.core.UriInfo;
danielbb83ebc2015-10-29 15:13:06 +090040import java.io.InputStream;
41
Hyunsun Moon44aac662017-02-18 02:07:01 +090042import static javax.ws.rs.core.Response.created;
43import static javax.ws.rs.core.Response.noContent;
44import static javax.ws.rs.core.Response.status;
Jian Li091d8d22018-02-20 10:42:06 +090045import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
Hyunsun Moon44aac662017-02-18 02:07:01 +090046
danielbb83ebc2015-10-29 15:13:06 +090047@Path("subnets")
48public class OpenstackSubnetWebResource extends AbstractWebResource {
Hyunsun Moon44aac662017-02-18 02:07:01 +090049 protected final Logger log = LoggerFactory.getLogger(getClass());
danielbb83ebc2015-10-29 15:13:06 +090050
Hyunsun Moon44aac662017-02-18 02:07:01 +090051 private static final String MESSAGE = "Received subnets %s request";
52 private static final String SUBNETS = "subnets";
53
54 private final OpenstackNetworkAdminService adminService =
Jian Li96abb152018-02-21 17:06:58 +090055 get(OpenstackNetworkAdminService.class);
Hyunsun Moon44aac662017-02-18 02:07:01 +090056
57 @Context
58 private UriInfo uriInfo;
59
60 /**
61 * Creates a subnet from the JSON input stream.
62 *
63 * @param input subnet JSON input stream
64 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
65 * is invalid or duplicated subnet already exists
Jian Lid3472bf2018-02-12 15:22:04 +090066 * @onos.rsModel NeutronSubnet
Hyunsun Moon44aac662017-02-18 02:07:01 +090067 */
danielbb83ebc2015-10-29 15:13:06 +090068 @POST
69 @Consumes(MediaType.APPLICATION_JSON)
70 @Produces(MediaType.APPLICATION_JSON)
71 public Response createSubnet(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090072 log.trace(String.format(MESSAGE, "CREATE"));
73
Jian Li091d8d22018-02-20 10:42:06 +090074 final NeutronSubnet subnet = (NeutronSubnet)
75 jsonToModelEntity(input, NeutronSubnet.class);
76
Hyunsun Moon44aac662017-02-18 02:07:01 +090077 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
Jian Lid3472bf2018-02-12 15:22:04 +090093 * @onos.rsModel NeutronSubnet
Hyunsun Moon44aac662017-02-18 02:07:01 +090094 */
sanghoshinf25d2e02015-11-11 23:07:17 +090095 @PUT
Hyunsun Moon44aac662017-02-18 02:07:01 +090096 @Path("{id}")
sanghoshinf25d2e02015-11-11 23:07:17 +090097 @Produces(MediaType.APPLICATION_JSON)
98 @Consumes(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +090099 public Response updateSubnet(@PathParam("id") String id, InputStream input) {
100 log.trace(String.format(MESSAGE, "UPDATE " + id));
101
Jian Li091d8d22018-02-20 10:42:06 +0900102 final NeutronSubnet subnet = (NeutronSubnet)
103 jsonToModelEntity(input, NeutronSubnet.class);
104
Hyunsun Moon44aac662017-02-18 02:07:01 +0900105 adminService.updateSubnet(subnet);
106
107 return status(Response.Status.OK).build();
sanghoshinf25d2e02015-11-11 23:07:17 +0900108 }
109
Hyunsun Moon44aac662017-02-18 02:07:01 +0900110 /**
111 * Removes the subnet.
112 *
113 * @param id subnet identifier
114 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the subnet does not exist
115 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900116 @DELETE
Hyunsun Moon44aac662017-02-18 02:07:01 +0900117 @Path("{id}")
118 @Consumes(MediaType.APPLICATION_JSON)
jskim7d881742016-09-13 09:39:02 +0900119 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900120 public Response deleteSubnet(@PathParam("id") String id) {
121 log.trace(String.format(MESSAGE, "DELETE " + id));
122
123 adminService.removeSubnet(id);
124 return noContent().build();
125 }
danielbb83ebc2015-10-29 15:13:06 +0900126}