blob: 3fe4b8cb81a5982e8f99039f53b16d05b5609cf1 [file] [log] [blame]
sanghoshin94872a12015-10-16 18:04:34 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sanghoshin94872a12015-10-16 18:04:34 +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;
sanghoshin94872a12015-10-16 18:04:34 +090017
Hyunsun Moon44aac662017-02-18 02:07:01 +090018import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
sanghoshin94872a12015-10-16 18:04:34 +090019import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon44aac662017-02-18 02:07:01 +090020import org.openstack4j.openstack.networking.domain.NeutronNetwork;
sanghoshin94872a12015-10-16 18:04:34 +090021import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import javax.ws.rs.Consumes;
sanghoshinf25d2e02015-11-11 23:07:17 +090025import javax.ws.rs.DELETE;
sanghoshin94872a12015-10-16 18:04:34 +090026import javax.ws.rs.POST;
sanghoshinf25d2e02015-11-11 23:07:17 +090027import javax.ws.rs.PUT;
sanghoshin94872a12015-10-16 18:04:34 +090028import javax.ws.rs.Path;
Hyunsun Moon44aac662017-02-18 02:07:01 +090029import javax.ws.rs.PathParam;
sanghoshin94872a12015-10-16 18:04:34 +090030import javax.ws.rs.Produces;
Hyunsun Moon44aac662017-02-18 02:07:01 +090031import javax.ws.rs.core.Context;
sanghoshin94872a12015-10-16 18:04:34 +090032import javax.ws.rs.core.MediaType;
33import javax.ws.rs.core.Response;
Hyunsun Moon44aac662017-02-18 02:07:01 +090034import javax.ws.rs.core.UriBuilder;
35import javax.ws.rs.core.UriInfo;
sanghoshin94872a12015-10-16 18:04:34 +090036import java.io.InputStream;
37
Hyunsun Moon44aac662017-02-18 02:07:01 +090038import static javax.ws.rs.core.Response.created;
39import static javax.ws.rs.core.Response.noContent;
40import static javax.ws.rs.core.Response.status;
Jian Lidea0fdb2018-04-02 19:02:48 +090041import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.jsonToModelEntity;
Hyunsun Moon44aac662017-02-18 02:07:01 +090042
sanghoshinf25d2e02015-11-11 23:07:17 +090043/**
44 * Handles REST API call of Neutron ML2 plugin.
45 */
sanghoshin94872a12015-10-16 18:04:34 +090046@Path("networks")
47public class OpenstackNetworkWebResource extends AbstractWebResource {
Hyunsun Moon44aac662017-02-18 02:07:01 +090048 protected final Logger log = LoggerFactory.getLogger(getClass());
sanghoshin94872a12015-10-16 18:04:34 +090049
Hyunsun Moon44aac662017-02-18 02:07:01 +090050 private static final String MESSAGE = "Received networks %s request";
51 private static final String NETWORKS = "networks";
sanghoshin94872a12015-10-16 18:04:34 +090052
Hyunsun Moon44aac662017-02-18 02:07:01 +090053 private final OpenstackNetworkAdminService adminService =
Jian Li5fe34472018-02-21 10:24:31 +090054 get(OpenstackNetworkAdminService.class);
Hyunsun Moon44aac662017-02-18 02:07:01 +090055
56 @Context
57 private UriInfo uriInfo;
58
59 /**
60 * Creates a network from the JSON input stream.
61 *
62 * @param input network JSON input stream
63 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
64 * is invalid or duplicated network already exists
Jian Li2077f662018-02-12 14:01:35 +090065 * @onos.rsModel NeutronNetwork
Hyunsun Moon44aac662017-02-18 02:07:01 +090066 */
sanghoshin94872a12015-10-16 18:04:34 +090067 @POST
68 @Consumes(MediaType.APPLICATION_JSON)
69 @Produces(MediaType.APPLICATION_JSON)
70 public Response createNetwork(InputStream input) {
Hyunsun Moon44aac662017-02-18 02:07:01 +090071 log.trace(String.format(MESSAGE, "CREATE"));
72
Jian Li091d8d22018-02-20 10:42:06 +090073 final NeutronNetwork net = (NeutronNetwork)
74 jsonToModelEntity(input, NeutronNetwork.class);
75
Hyunsun Moon44aac662017-02-18 02:07:01 +090076 adminService.createNetwork(net);
77
78 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
79 .path(NETWORKS)
80 .path(net.getId());
81
82 return created(locationBuilder.build()).build();
sanghoshinf25d2e02015-11-11 23:07:17 +090083 }
sanghoshin94872a12015-10-16 18:04:34 +090084
Hyunsun Moon44aac662017-02-18 02:07:01 +090085 /**
86 * Updates the network with the specified identifier.
87 *
88 * @param id network identifier
89 * @param input network JSON input stream
90 * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
91 * network does not exist
Jian Li2077f662018-02-12 14:01:35 +090092 * @onos.rsModel NeutronNetwork
Hyunsun Moon44aac662017-02-18 02:07:01 +090093 */
sanghoshinf25d2e02015-11-11 23:07:17 +090094 @PUT
95 @Path("{id}")
96 @Consumes(MediaType.APPLICATION_JSON)
97 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +090098 public Response updateNetwork(@PathParam("id") String id, InputStream input) {
99 log.trace(String.format(MESSAGE, "UPDATE " + id));
100
Jian Li091d8d22018-02-20 10:42:06 +0900101 final NeutronNetwork net = (NeutronNetwork)
102 jsonToModelEntity(input, NeutronNetwork.class);
103
Hyunsun Moon44aac662017-02-18 02:07:01 +0900104 adminService.updateNetwork(net);
105
106 return status(Response.Status.OK).build();
sanghoshinf25d2e02015-11-11 23:07:17 +0900107 }
sanghoshin94872a12015-10-16 18:04:34 +0900108
Hyunsun Moon44aac662017-02-18 02:07:01 +0900109 /**
110 * Removes the service network.
111 *
112 * @param id network identifier
113 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist
114 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900115 @DELETE
116 @Path("{id}")
117 @Consumes(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900118 @Produces(MediaType.APPLICATION_JSON)
119 public Response deleteNetwork(@PathParam("id") String id) {
120 log.trace(String.format(MESSAGE, "DELETE " + id));
121
122 adminService.removeNetwork(id);
123 return noContent().build();
124 }
sanghoshin94872a12015-10-16 18:04:34 +0900125}