blob: 590dc5693fe2cf8c267672b0f902872bac940d92 [file] [log] [blame]
Jian Li49109b52019-01-22 00:17:28 +09001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
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.onosproject.k8snode.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.Sets;
Jian Li3defa842019-02-12 00:31:35 +090022import org.onosproject.k8snode.api.K8sApiConfig;
23import org.onosproject.k8snode.api.K8sApiConfigAdminService;
Jian Li49109b52019-01-22 00:17:28 +090024import org.onosproject.k8snode.api.K8sNode;
25import org.onosproject.k8snode.api.K8sNodeAdminService;
26import org.onosproject.rest.AbstractWebResource;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
30import javax.ws.rs.Consumes;
Jian Li3defa842019-02-12 00:31:35 +090031import javax.ws.rs.DELETE;
Jian Li49109b52019-01-22 00:17:28 +090032import javax.ws.rs.POST;
33import javax.ws.rs.PUT;
34import javax.ws.rs.Path;
35import javax.ws.rs.PathParam;
36import javax.ws.rs.Produces;
37import javax.ws.rs.core.Context;
38import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
40import javax.ws.rs.core.UriBuilder;
41import javax.ws.rs.core.UriInfo;
42import java.io.InputStream;
43import java.util.Set;
44
45import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
46import static javax.ws.rs.core.Response.created;
47import static org.onlab.util.Tools.nullIsIllegal;
48import static org.onlab.util.Tools.readTreeFromStream;
Jian Li3defa842019-02-12 00:31:35 +090049import static org.onosproject.k8snode.util.K8sNodeUtil.endpoint;
Jian Li49109b52019-01-22 00:17:28 +090050
51/**
52 * Handles REST API call of kubernetes node config.
53 */
54
55@Path("configure")
56public class K8sNodeWebResource extends AbstractWebResource {
57
58 private final Logger log = LoggerFactory.getLogger(getClass());
59
60 private static final String MESSAGE_NODE = "Received node %s request";
61 private static final String NODES = "nodes";
Jian Li3defa842019-02-12 00:31:35 +090062 private static final String API_CONFIGS = "apiConfigs";
Jian Li49109b52019-01-22 00:17:28 +090063 private static final String CREATE = "CREATE";
64 private static final String UPDATE = "UPDATE";
65 private static final String NODE_ID = "NODE_ID";
Jian Li3defa842019-02-12 00:31:35 +090066 private static final String REMOVE = "REMOVE";
Jian Li49109b52019-01-22 00:17:28 +090067
68 private static final String HOST_NAME = "hostname";
Jian Li3defa842019-02-12 00:31:35 +090069 private static final String ENDPOINT = "endpoint";
Jian Li49109b52019-01-22 00:17:28 +090070 private static final String ERROR_MESSAGE = " cannot be null";
71
Jian Li3defa842019-02-12 00:31:35 +090072 private final K8sNodeAdminService nodeAdminService = get(K8sNodeAdminService.class);
73 private final K8sApiConfigAdminService configAdminService = get(K8sApiConfigAdminService.class);
Jian Li49109b52019-01-22 00:17:28 +090074
75 @Context
76 private UriInfo uriInfo;
77
78 /**
79 * Creates a set of kubernetes nodes' config from the JSON input stream.
80 *
81 * @param input kubernetes nodes JSON input stream
82 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
83 * is malformed
84 * @onos.rsModel K8sNode
85 */
86 @POST
Jian Li3defa842019-02-12 00:31:35 +090087 @Path("node")
Jian Li49109b52019-01-22 00:17:28 +090088 @Consumes(MediaType.APPLICATION_JSON)
89 @Produces(MediaType.APPLICATION_JSON)
90 public Response createNodes(InputStream input) {
91 log.trace(String.format(MESSAGE_NODE, CREATE));
92
93 readNodeConfiguration(input).forEach(node -> {
Jian Li3defa842019-02-12 00:31:35 +090094 K8sNode existing = nodeAdminService.node(node.hostname());
Jian Li49109b52019-01-22 00:17:28 +090095 if (existing == null) {
Jian Li3defa842019-02-12 00:31:35 +090096 nodeAdminService.createNode(node);
Jian Li49109b52019-01-22 00:17:28 +090097 }
98 });
99
100 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
101 .path(NODES)
102 .path(NODE_ID);
103
104 return created(locationBuilder.build()).build();
105 }
106
107 /**
108 * Updates a set of kubernetes nodes' config from the JSON input stream.
109 *
110 * @param input kubernetes nodes JSON input stream
111 * @return 200 OK with the updated kubernetes node's config, 400 BAD_REQUEST
112 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
113 * @onos.rsModel K8sNode
114 */
115 @PUT
Jian Li3defa842019-02-12 00:31:35 +0900116 @Path("node")
Jian Li49109b52019-01-22 00:17:28 +0900117 @Consumes(MediaType.APPLICATION_JSON)
118 @Produces(MediaType.APPLICATION_JSON)
119 public Response updateNodes(InputStream input) {
120 log.trace(String.format(MESSAGE_NODE, UPDATE));
121
122 Set<K8sNode> nodes = readNodeConfiguration(input);
123 for (K8sNode node: nodes) {
Jian Li3defa842019-02-12 00:31:35 +0900124 K8sNode existing = nodeAdminService.node(node.hostname());
Jian Li49109b52019-01-22 00:17:28 +0900125 if (existing == null) {
126 log.warn("There is no node configuration to update : {}", node.hostname());
127 return Response.notModified().build();
128 } else if (!existing.equals(node)) {
Jian Li3defa842019-02-12 00:31:35 +0900129 nodeAdminService.updateNode(node);
Jian Li49109b52019-01-22 00:17:28 +0900130 }
131 }
132
133 return Response.ok().build();
134 }
135
136 /**
137 * Removes a set of kubernetes nodes' config from the JSON input stream.
138 *
139 * @param hostname host name contained in kubernetes nodes configuration
140 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
141 * 304 NOT_MODIFIED without the updated config
Jian Li49109b52019-01-22 00:17:28 +0900142 */
Jian Li3defa842019-02-12 00:31:35 +0900143 @DELETE
144 @Path("node/{hostname}")
Jian Li49109b52019-01-22 00:17:28 +0900145 @Consumes(MediaType.APPLICATION_JSON)
146 @Produces(MediaType.APPLICATION_JSON)
Jian Li49109b52019-01-22 00:17:28 +0900147 public Response deleteNodes(@PathParam("hostname") String hostname) {
Jian Li3defa842019-02-12 00:31:35 +0900148 log.trace(String.format(MESSAGE_NODE, REMOVE));
Jian Li49109b52019-01-22 00:17:28 +0900149
150 K8sNode existing =
Jian Li3defa842019-02-12 00:31:35 +0900151 nodeAdminService.node(nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
Jian Li49109b52019-01-22 00:17:28 +0900152
153 if (existing == null) {
154 log.warn("There is no node configuration to delete : {}", hostname);
155 return Response.notModified().build();
156 } else {
Jian Li3defa842019-02-12 00:31:35 +0900157 nodeAdminService.removeNode(hostname);
Jian Li49109b52019-01-22 00:17:28 +0900158 }
159
160 return Response.noContent().build();
161 }
162
163 private Set<K8sNode> readNodeConfiguration(InputStream input) {
164 Set<K8sNode> nodeSet = Sets.newHashSet();
165 try {
166 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
167 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
168 nodes.forEach(node -> {
169 try {
170 ObjectNode objectNode = node.deepCopy();
171 K8sNode k8sNode =
172 codec(K8sNode.class).decode(objectNode, this);
173
174 nodeSet.add(k8sNode);
175 } catch (Exception e) {
176 log.error("Exception occurred due to {}", e);
177 throw new IllegalArgumentException();
178 }
179 });
180 } catch (Exception e) {
181 throw new IllegalArgumentException(e);
182 }
183
184 return nodeSet;
185 }
Jian Li3defa842019-02-12 00:31:35 +0900186
187 /**
188 * Creates a set of kubernetes API config from the JSON input stream.
189 *
190 * @param input kubernetes API configs JSON input stream
191 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
192 * is malformed
193 * @onos.rsModel K8sApiConfig
194 */
195 @POST
196 @Path("api")
197 @Consumes(MediaType.APPLICATION_JSON)
198 @Produces(MediaType.APPLICATION_JSON)
199 public Response createApiConfigs(InputStream input) {
200 log.trace(String.format(MESSAGE_NODE, CREATE));
201
202 readApiConfigConfiguration(input).forEach(config -> {
203 K8sApiConfig existing = configAdminService.apiConfig(endpoint(config));
204 if (existing == null) {
205 configAdminService.createApiConfig(config);
206 }
207 });
208
209 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
210 .path(API_CONFIGS);
211
212 return created(locationBuilder.build()).build();
213 }
214
215 /**
216 * Updates a set of kubernetes API config from the JSON input stream.
217 *
218 * @param input kubernetes API configs JSON input stream
219 * @return 200 OK with the updated kubernetes API config, 400 BAD_REQUEST
220 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
221 * @onos.rsModel K8sApiConfig
222 */
223 @PUT
224 @Path("api")
225 @Consumes(MediaType.APPLICATION_JSON)
226 @Produces(MediaType.APPLICATION_JSON)
227 public Response updateApiConfigs(InputStream input) {
228 log.trace(String.format(MESSAGE_NODE, UPDATE));
229
230 Set<K8sApiConfig> configs = readApiConfigConfiguration(input);
231 for (K8sApiConfig config: configs) {
232 K8sApiConfig existing = configAdminService.apiConfig(endpoint(config));
233 if (existing == null) {
234 log.warn("There is no API configuration to update : {}", endpoint(config));
235 return Response.notModified().build();
236 } else if (!existing.equals(config)) {
237 configAdminService.updateApiConfig(config);
238 }
239 }
240
241 return Response.ok().build();
242 }
243
244 /**
245 * Removes a kubernetes API config.
246 *
247 * @param endpoint kubernetes API endpoint
248 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed
249 */
250 @DELETE
251 @Path("api/{endpoint : .+}")
252 @Consumes(MediaType.APPLICATION_JSON)
253 @Produces(MediaType.APPLICATION_JSON)
254 public Response deleteApiConfig(@PathParam("endpoint") String endpoint) {
255 log.trace(String.format(MESSAGE_NODE, REMOVE));
256
257 K8sApiConfig existing =
258 configAdminService.apiConfig(nullIsIllegal(endpoint, ENDPOINT + ERROR_MESSAGE));
259
260 if (existing == null) {
261 log.warn("There is no API configuration to delete : {}", endpoint);
262 return Response.notModified().build();
263 } else {
264 configAdminService.removeApiConfig(endpoint);
265 }
266
267 return Response.noContent().build();
268 }
269
270 private Set<K8sApiConfig> readApiConfigConfiguration(InputStream input) {
271 Set<K8sApiConfig> configSet = Sets.newHashSet();
272 try {
273 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
274 ArrayNode configs = (ArrayNode) jsonTree.path(API_CONFIGS);
275 configs.forEach(config -> {
276 try {
277 ObjectNode objectNode = config.deepCopy();
278 K8sApiConfig k8sApiConfig =
279 codec(K8sApiConfig.class).decode(objectNode, this);
280
281 configSet.add(k8sApiConfig);
282 } catch (Exception e) {
283 log.error("Exception occurred due to {}", e);
284 throw new IllegalArgumentException();
285 }
286 });
287 } catch (Exception e) {
288 throw new IllegalArgumentException(e);
289 }
290
291 return configSet;
292 }
Jian Li49109b52019-01-22 00:17:28 +0900293}