blob: 551d6af0977abd8547784a9299b07282aa9c1f44 [file] [log] [blame]
Hyunsun Moone5a1fc32016-09-02 16:01:01 -07001/*
Hyunsun Moon187bf532017-01-19 10:57:40 +09002 * Copyright 2017-present Open Networking Laboratory
Hyunsun Moone5a1fc32016-09-02 16:01:01 -07003 *
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 */
16package org.opencord.cordvtn.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onlab.osgi.DefaultServiceDirectory;
21import org.onosproject.rest.AbstractWebResource;
Hyunsun Moon187bf532017-01-19 10:57:40 +090022import org.opencord.cordvtn.api.core.ServiceNetworkAdminService;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070023import org.opencord.cordvtn.api.net.NetworkId;
24import org.opencord.cordvtn.api.net.ServiceNetwork;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070025import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
28import javax.ws.rs.Consumes;
29import javax.ws.rs.DELETE;
30import javax.ws.rs.GET;
31import javax.ws.rs.POST;
32import javax.ws.rs.PUT;
33import javax.ws.rs.Path;
34import javax.ws.rs.PathParam;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.Context;
37import javax.ws.rs.core.MediaType;
38import javax.ws.rs.core.Response;
39import javax.ws.rs.core.UriBuilder;
40import javax.ws.rs.core.UriInfo;
41import java.io.IOException;
42import java.io.InputStream;
Hyunsun Moon5c143952016-10-19 18:34:46 -070043import java.util.Set;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070044
45import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
46import static javax.ws.rs.core.Response.Status.NOT_FOUND;
Hyunsun Moon187bf532017-01-19 10:57:40 +090047import static javax.ws.rs.core.Response.*;
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070048
49/**
50 * Query and manage service networks.
51 */
52@Path("serviceNetworks")
53public class ServiceNetworkWebResource extends AbstractWebResource {
54 protected final Logger log = LoggerFactory.getLogger(getClass());
55
56 private static final String MESSAGE = "Received service network ";
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070057 private static final String SERVICE_NETWORK = "ServiceNetwork";
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070058 private static final String SERVICE_NETWORKS = "ServiceNetworks";
59
Hyunsun Moon187bf532017-01-19 10:57:40 +090060 private final ServiceNetworkAdminService adminService =
61 DefaultServiceDirectory.getService(ServiceNetworkAdminService.class);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070062
63 @Context
64 private UriInfo uriInfo;
65
66 /**
67 * Creates a service network from the JSON input stream.
68 *
69 * @param input service network JSON stream
70 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
71 * is invalid or duplicated network with different properties exists
72 */
73 @POST
74 @Consumes(MediaType.APPLICATION_JSON)
75 @Produces(MediaType.APPLICATION_JSON)
76 public Response createServiceNetwork(InputStream input) {
77 try {
78 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
79 log.trace(MESSAGE + "CREATE " + mapper().writeValueAsString(jsonTree));
80
81 ObjectNode snetJson = (ObjectNode) jsonTree.get(SERVICE_NETWORK);
82 if (snetJson == null) {
83 throw new IllegalArgumentException();
84 }
85
86 final ServiceNetwork snet = codec(ServiceNetwork.class).decode(snetJson, this);
Hyunsun Moon5c143952016-10-19 18:34:46 -070087 adminService.createServiceNetwork(snet);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -070088
89 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
90 .path(SERVICE_NETWORKS)
91 .path(snet.id().id());
92
93 return created(locationBuilder.build()).build();
94 } catch (IOException e) {
95 throw new IllegalArgumentException(e);
96 }
97 }
98
99 /**
100 * Updates the service network with the specified identifier.
101 *
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700102 * @param id network identifier
103 * @param input service network JSON stream
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700104 * @return 200 OK with a service network, 400 BAD_REQUEST if the requested
105 * network does not exist
106 */
107 @PUT
108 @Path("{id}")
109 @Consumes(MediaType.APPLICATION_JSON)
110 @Produces(MediaType.APPLICATION_JSON)
111 public Response updateServiceNetwork(@PathParam("id") String id, InputStream input) {
112 try {
113 JsonNode jsonTree = mapper().enable(INDENT_OUTPUT).readTree(input);
114 log.trace(MESSAGE + "UPDATE " + mapper().writeValueAsString(jsonTree));
115
116 ObjectNode snetJson = (ObjectNode) jsonTree.get(SERVICE_NETWORK);
117 if (snetJson == null) {
118 throw new IllegalArgumentException();
119 }
120
121 final ServiceNetwork snet = codec(ServiceNetwork.class).decode(snetJson, this);
Hyunsun Moon5c143952016-10-19 18:34:46 -0700122 adminService.updateServiceNetwork(snet);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700123
124 ObjectNode result = this.mapper().createObjectNode();
125 result.set(SERVICE_NETWORK, codec(ServiceNetwork.class).encode(snet, this));
126 return ok(result).build();
127 } catch (IOException e) {
128 throw new IllegalArgumentException(e);
129 }
130 }
131
132 /**
133 * Returns all service networks.
134 *
135 * @return 200 OK with set of service networks
136 */
137 @GET
138 @Consumes(MediaType.APPLICATION_JSON)
139 @Produces(MediaType.APPLICATION_JSON)
140 public Response getServiceNetworks() {
141 log.trace(MESSAGE + "GET");
142
Hyunsun Moon5c143952016-10-19 18:34:46 -0700143 Set<ServiceNetwork> snets = adminService.serviceNetworks();
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700144 return ok(encodeArray(ServiceNetwork.class, SERVICE_NETWORKS, snets)).build();
145 }
146
147 /**
148 * Returns the service network with the specified identifier.
149 *
150 * @param id network identifier
151 * @return 200 OK with a service network, 404 NOT_FOUND if the requested
152 * network does not exist
153 */
154 @GET
155 @Path("{id}")
156 @Consumes(MediaType.APPLICATION_JSON)
157 @Produces(MediaType.APPLICATION_JSON)
158 public Response getServiceNetwork(@PathParam("id") String id) {
159 log.trace(MESSAGE + "GET " + id);
160
Hyunsun Moon5c143952016-10-19 18:34:46 -0700161 ServiceNetwork snet = adminService.serviceNetwork(NetworkId.of(id));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700162 if (snet == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700163 log.trace("Returned NOT_FOUND");
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700164 return status(NOT_FOUND).build();
165 }
166
167 ObjectNode result = this.mapper().createObjectNode();
168 result.set(SERVICE_NETWORK, codec(ServiceNetwork.class).encode(snet, this));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700169 log.trace("Returned OK {}", result);
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700170 return ok(result).build();
171 }
172
173 /**
174 * Removes the service network.
175 *
176 * @param id network identifier
177 * @return 204 NO CONTENT, 400 BAD_REQUEST if the network does not exist
178 */
179 @DELETE
180 @Path("{id}")
181 @Consumes(MediaType.APPLICATION_JSON)
182 @Produces(MediaType.APPLICATION_JSON)
183 public Response deleteServiceNetwork(@PathParam("id") String id) {
184 log.trace(MESSAGE + "DELETE " + id);
185
Hyunsun Moon5c143952016-10-19 18:34:46 -0700186 adminService.removeServiceNetwork(NetworkId.of(id));
Hyunsun Moone5a1fc32016-09-02 16:01:01 -0700187 return noContent().build();
188 }
189}