blob: de5731115de23e1a56d04fa26fe697a77b18590d [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.onlab.osgi.DefaultServiceDirectory;
19import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
sanghoshin94872a12015-10-16 18:04:34 +090020import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon44aac662017-02-18 02:07:01 +090021import org.openstack4j.openstack.networking.domain.NeutronNetwork;
sanghoshin94872a12015-10-16 18:04:34 +090022import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import javax.ws.rs.Consumes;
sanghoshinf25d2e02015-11-11 23:07:17 +090026import javax.ws.rs.DELETE;
sanghoshin94872a12015-10-16 18:04:34 +090027import javax.ws.rs.POST;
sanghoshinf25d2e02015-11-11 23:07:17 +090028import javax.ws.rs.PUT;
sanghoshin94872a12015-10-16 18:04:34 +090029import javax.ws.rs.Path;
Hyunsun Moon44aac662017-02-18 02:07:01 +090030import javax.ws.rs.PathParam;
sanghoshin94872a12015-10-16 18:04:34 +090031import javax.ws.rs.Produces;
Hyunsun Moon44aac662017-02-18 02:07:01 +090032import javax.ws.rs.core.Context;
sanghoshin94872a12015-10-16 18:04:34 +090033import javax.ws.rs.core.MediaType;
34import javax.ws.rs.core.Response;
Hyunsun Moon44aac662017-02-18 02:07:01 +090035import javax.ws.rs.core.UriBuilder;
36import javax.ws.rs.core.UriInfo;
sanghoshin94872a12015-10-16 18:04:34 +090037import java.io.InputStream;
38
Hyunsun Moon44aac662017-02-18 02:07:01 +090039import static javax.ws.rs.core.Response.created;
40import static javax.ws.rs.core.Response.noContent;
41import static javax.ws.rs.core.Response.status;
Jian Li091d8d22018-02-20 10:42:06 +090042import static org.onosproject.openstacknetworking.util.OpenstackUtil.jsonToModelEntity;
Hyunsun Moon44aac662017-02-18 02:07:01 +090043
sanghoshinf25d2e02015-11-11 23:07:17 +090044/**
45 * Handles REST API call of Neutron ML2 plugin.
46 */
sanghoshin94872a12015-10-16 18:04:34 +090047@Path("networks")
48public class OpenstackNetworkWebResource extends AbstractWebResource {
Hyunsun Moon44aac662017-02-18 02:07:01 +090049 protected final Logger log = LoggerFactory.getLogger(getClass());
sanghoshin94872a12015-10-16 18:04:34 +090050
Hyunsun Moon44aac662017-02-18 02:07:01 +090051 private static final String MESSAGE = "Received networks %s request";
52 private static final String NETWORKS = "networks";
sanghoshin94872a12015-10-16 18:04:34 +090053
Hyunsun Moon44aac662017-02-18 02:07:01 +090054 private final OpenstackNetworkAdminService adminService =
55 DefaultServiceDirectory.getService(OpenstackNetworkAdminService.class);
56
57 @Context
58 private UriInfo uriInfo;
59
60 /**
61 * Creates a network from the JSON input stream.
62 *
63 * @param input network JSON input stream
64 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
65 * is invalid or duplicated network already exists
Jian Li2077f662018-02-12 14:01:35 +090066 * @onos.rsModel NeutronNetwork
Hyunsun Moon44aac662017-02-18 02:07:01 +090067 */
sanghoshin94872a12015-10-16 18:04:34 +090068 @POST
69 @Consumes(MediaType.APPLICATION_JSON)
70 @Produces(MediaType.APPLICATION_JSON)
71 public Response createNetwork(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 NeutronNetwork net = (NeutronNetwork)
75 jsonToModelEntity(input, NeutronNetwork.class);
76
Hyunsun Moon44aac662017-02-18 02:07:01 +090077 adminService.createNetwork(net);
78
79 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
80 .path(NETWORKS)
81 .path(net.getId());
82
83 return created(locationBuilder.build()).build();
sanghoshinf25d2e02015-11-11 23:07:17 +090084 }
sanghoshin94872a12015-10-16 18:04:34 +090085
Hyunsun Moon44aac662017-02-18 02:07:01 +090086 /**
87 * Updates the network with the specified identifier.
88 *
89 * @param id network identifier
90 * @param input network JSON input stream
91 * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
92 * network does not exist
Jian Li2077f662018-02-12 14:01:35 +090093 * @onos.rsModel NeutronNetwork
Hyunsun Moon44aac662017-02-18 02:07:01 +090094 */
sanghoshinf25d2e02015-11-11 23:07:17 +090095 @PUT
96 @Path("{id}")
97 @Consumes(MediaType.APPLICATION_JSON)
98 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +090099 public Response updateNetwork(@PathParam("id") String id, InputStream input) {
100 log.trace(String.format(MESSAGE, "UPDATE " + id));
101
Jian Li091d8d22018-02-20 10:42:06 +0900102 final NeutronNetwork net = (NeutronNetwork)
103 jsonToModelEntity(input, NeutronNetwork.class);
104
Hyunsun Moon44aac662017-02-18 02:07:01 +0900105 adminService.updateNetwork(net);
106
107 return status(Response.Status.OK).build();
sanghoshinf25d2e02015-11-11 23:07:17 +0900108 }
sanghoshin94872a12015-10-16 18:04:34 +0900109
Hyunsun Moon44aac662017-02-18 02:07:01 +0900110 /**
111 * Removes the service network.
112 *
113 * @param id network identifier
114 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist
115 */
sanghoshinf25d2e02015-11-11 23:07:17 +0900116 @DELETE
117 @Path("{id}")
118 @Consumes(MediaType.APPLICATION_JSON)
Hyunsun Moon44aac662017-02-18 02:07:01 +0900119 @Produces(MediaType.APPLICATION_JSON)
120 public Response deleteNetwork(@PathParam("id") String id) {
121 log.trace(String.format(MESSAGE, "DELETE " + id));
122
123 adminService.removeNetwork(id);
124 return noContent().build();
125 }
sanghoshin94872a12015-10-16 18:04:34 +0900126}