blob: ef2068ceaa4583266e24a1ee866b7300f9d539b1 [file] [log] [blame]
Hyunsun Moonb6febbe2016-02-12 15:59:53 -08001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2016-present Open Networking Foundation
Hyunsun Moonb6febbe2016-02-12 15:59:53 -08003 *
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 */
alshabibb4d31712016-06-01 18:51:03 -070016package org.opencord.cordvtn.rest;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080017
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070018import com.fasterxml.jackson.databind.JsonNode;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070019import org.onlab.osgi.DefaultServiceDirectory;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080020import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon187bf532017-01-19 10:57:40 +090021import org.opencord.cordvtn.api.core.ServiceNetworkAdminService;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070022import org.opencord.cordvtn.api.net.NetworkId;
Hyunsun Moon187bf532017-01-19 10:57:40 +090023import org.opencord.cordvtn.api.net.SegmentId;
24import org.opencord.cordvtn.api.net.ServiceNetwork;
25import org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType;
26import org.opencord.cordvtn.impl.DefaultServiceNetwork;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070027import org.openstack4j.core.transport.ObjectMapperSingleton;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070028import org.openstack4j.openstack.networking.domain.NeutronNetwork;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import javax.ws.rs.Consumes;
33import javax.ws.rs.DELETE;
34import javax.ws.rs.POST;
35import javax.ws.rs.PUT;
36import javax.ws.rs.Path;
37import javax.ws.rs.PathParam;
38import javax.ws.rs.Produces;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070039import javax.ws.rs.core.Context;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080040import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070042import javax.ws.rs.core.UriBuilder;
43import javax.ws.rs.core.UriInfo;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080044import java.io.InputStream;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070045
46import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
Hyunsun Moon187bf532017-01-19 10:57:40 +090047import static javax.ws.rs.core.Response.Status.OK;
48import static javax.ws.rs.core.Response.created;
49import static javax.ws.rs.core.Response.noContent;
50import static javax.ws.rs.core.Response.status;
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080051
52/**
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070053 * Neutron ML2 mechanism driver implementation for the network resource.
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080054 */
55@Path("networks")
56public class NeutronMl2NetworksWebResource extends AbstractWebResource {
57 protected final Logger log = LoggerFactory.getLogger(getClass());
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080058
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070059 private static final String MESSAGE = "Received networks %s request";
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070060 private static final String NETWORKS = "networks";
61
Hyunsun Moon187bf532017-01-19 10:57:40 +090062 private final ServiceNetworkAdminService adminService =
63 DefaultServiceDirectory.getService(ServiceNetworkAdminService.class);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070064
65 @Context
66 private UriInfo uriInfo;
67
68 /**
69 * Creates a network from the JSON input stream.
70 *
71 * @param input network JSON input stream
72 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
73 * is invalid or duplicated network already exists
74 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080075 @POST
76 @Consumes(MediaType.APPLICATION_JSON)
77 @Produces(MediaType.APPLICATION_JSON)
78 public Response createNetwork(InputStream input) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070079 log.trace(String.format(MESSAGE, "CREATE"));
80
Hyunsun Moon187bf532017-01-19 10:57:40 +090081 final ServiceNetwork snet = readNetwork(input);
82 adminService.createServiceNetwork(snet);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070083
84 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
85 .path(NETWORKS)
Hyunsun Moon187bf532017-01-19 10:57:40 +090086 .path(snet.id().id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070087
88 return created(locationBuilder.build()).build();
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080089 }
90
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070091 /**
92 * Updates the network with the specified identifier.
93 *
94 * @param id network identifier
95 * @param input network JSON input stream
96 * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
97 * network does not exist
98 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -080099 @PUT
100 @Path("{id}")
101 @Consumes(MediaType.APPLICATION_JSON)
102 @Produces(MediaType.APPLICATION_JSON)
103 public Response updateNetwork(@PathParam("id") String id, InputStream input) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700104 log.trace(String.format(MESSAGE, "UPDATE " + id));
105
Hyunsun Moon187bf532017-01-19 10:57:40 +0900106 final ServiceNetwork snet = readNetwork(input);
107 adminService.updateServiceNetwork(snet);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700108
Hyunsun Moon187bf532017-01-19 10:57:40 +0900109 return status(OK).build();
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700110 }
111
112 /**
113 * Removes the service network.
114 *
115 * @param id network identifier
116 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist
117 */
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800118 @DELETE
119 @Path("{id}")
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700120 @Consumes(MediaType.APPLICATION_JSON)
121 @Produces(MediaType.APPLICATION_JSON)
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800122 public Response deleteNetwork(@PathParam("id") String id) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700123 log.trace(String.format(MESSAGE, "DELETE " + id));
124
Hyunsun Moon187bf532017-01-19 10:57:40 +0900125 adminService.removeServiceNetwork(NetworkId.of(id));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700126 return noContent().build();
127 }
128
Hyunsun Moon187bf532017-01-19 10:57:40 +0900129 private ServiceNetwork readNetwork(InputStream input) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700130 try {
131 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
132 log.trace(mapper().writeValueAsString(jsonTree));
Hyunsun Moon187bf532017-01-19 10:57:40 +0900133 NeutronNetwork osNet = ObjectMapperSingleton.getContext(NeutronNetwork.class)
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700134 .readerFor(NeutronNetwork.class)
135 .readValue(jsonTree);
Hyunsun Moon187bf532017-01-19 10:57:40 +0900136
137 return DefaultServiceNetwork.builder()
138 .id(NetworkId.of(osNet.getId()))
139 .name(osNet.getName())
140 .type(NetworkType.PRIVATE)
141 .segmentId(SegmentId.of(Long.valueOf(osNet.getProviderSegID())))
142 .build();
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700143 } catch (Exception e) {
144 throw new IllegalArgumentException();
145 }
146 }
Hyunsun Moonb6febbe2016-02-12 15:59:53 -0800147}