blob: c28bc94b0bfa7a287bc3ab77bcf0d425f22d7680 [file] [log] [blame]
Jian Li762bc222020-12-19 01:26:57 +09001/*
2 * Copyright 2020-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.kubevirtnode.web;
17
Jian Li7c4a8822020-12-21 11:25:12 +090018import 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;
22import org.onosproject.kubevirtnode.api.KubevirtNode;
23import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService;
24import org.onosproject.kubevirtnode.api.KubevirtNodeState;
Jian Li762bc222020-12-19 01:26:57 +090025import org.onosproject.rest.AbstractWebResource;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import javax.ws.rs.Consumes;
Jian Li7c4a8822020-12-21 11:25:12 +090030import javax.ws.rs.DELETE;
31import javax.ws.rs.GET;
Jian Li762bc222020-12-19 01:26:57 +090032import javax.ws.rs.POST;
Jian Li7c4a8822020-12-21 11:25:12 +090033import javax.ws.rs.PUT;
Jian Li762bc222020-12-19 01:26:57 +090034import javax.ws.rs.Path;
Jian Li7c4a8822020-12-21 11:25:12 +090035import javax.ws.rs.PathParam;
Jian Li762bc222020-12-19 01:26:57 +090036import javax.ws.rs.Produces;
Jian Li7c4a8822020-12-21 11:25:12 +090037import javax.ws.rs.core.Context;
Jian Li762bc222020-12-19 01:26:57 +090038import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
Jian Li7c4a8822020-12-21 11:25:12 +090040import javax.ws.rs.core.UriBuilder;
41import javax.ws.rs.core.UriInfo;
Jian Li762bc222020-12-19 01:26:57 +090042import java.io.InputStream;
Jian Li7c4a8822020-12-21 11:25:12 +090043import java.util.Set;
Jian Li762bc222020-12-19 01:26:57 +090044
Jian Li7c4a8822020-12-21 11:25:12 +090045import 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;
49
50/**
51 * Handles REST API call of KubeVirt node config.
52 */
Jian Lic4604302020-12-25 02:24:16 +090053@Path("node")
Jian Li762bc222020-12-19 01:26:57 +090054public class KubevirtNodeWebResource extends AbstractWebResource {
55
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
Jian Li7c4a8822020-12-21 11:25:12 +090058 private static final String MESSAGE_NODE = "Received node %s request";
59 private static final String NODES = "nodes";
60 private static final String CREATE = "CREATE";
61 private static final String UPDATE = "UPDATE";
62 private static final String NODE_ID = "NODE_ID";
63 private static final String REMOVE = "REMOVE";
64 private static final String QUERY = "QUERY";
65 private static final String INIT = "INIT";
66 private static final String NOT_EXIST = "Not exist";
67 private static final String STATE = "State";
68 private static final String RESULT = "Result";
69
70 private static final String HOST_NAME = "hostname";
71 private static final String ERROR_MESSAGE = " cannot be null";
72
Jian Li7c4a8822020-12-21 11:25:12 +090073 @Context
74 private UriInfo uriInfo;
75
Jian Li762bc222020-12-19 01:26:57 +090076 /**
77 * Creates a set of KubeVirt nodes' config from the JSON input stream.
78 *
79 * @param input KubeVirt nodes JSON input stream
80 * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
81 * is malformed
82 * @onos.rsModel KubevirtNode
83 */
84 @POST
Jian Li762bc222020-12-19 01:26:57 +090085 @Consumes(MediaType.APPLICATION_JSON)
86 @Produces(MediaType.APPLICATION_JSON)
Jian Li7c4a8822020-12-21 11:25:12 +090087 public Response createNodes(InputStream input) {
88 log.trace(String.format(MESSAGE_NODE, CREATE));
89
Jian Lif868cd52021-02-19 10:28:01 +090090 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
91
Jian Li7c4a8822020-12-21 11:25:12 +090092 readNodeConfiguration(input).forEach(node -> {
Jian Lif868cd52021-02-19 10:28:01 +090093 KubevirtNode existing = service.node(node.hostname());
Jian Li7c4a8822020-12-21 11:25:12 +090094 if (existing == null) {
Jian Lif868cd52021-02-19 10:28:01 +090095 service.createNode(node);
Jian Li7c4a8822020-12-21 11:25:12 +090096 }
97 });
98
99 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
100 .path(NODES)
101 .path(NODE_ID);
102
103 return created(locationBuilder.build()).build();
104 }
105
106 /**
107 * Updates a set of KubeVirt nodes' config from the JSON input stream.
108 *
109 * @param input KubeVirt nodes JSON input stream
110 * @return 200 OK with the updated KubeVirt node's config, 400 BAD_REQUEST
111 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
112 * @onos.rsModel KubevirtNode
113 */
114 @PUT
Jian Li7c4a8822020-12-21 11:25:12 +0900115 @Consumes(MediaType.APPLICATION_JSON)
116 @Produces(MediaType.APPLICATION_JSON)
117 public Response updateNodes(InputStream input) {
118 log.trace(String.format(MESSAGE_NODE, UPDATE));
119
Jian Lif868cd52021-02-19 10:28:01 +0900120 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
Jian Li7c4a8822020-12-21 11:25:12 +0900121 Set<KubevirtNode> nodes = readNodeConfiguration(input);
122 for (KubevirtNode node: nodes) {
Jian Lif868cd52021-02-19 10:28:01 +0900123 KubevirtNode existing = service.node(node.hostname());
Jian Li7c4a8822020-12-21 11:25:12 +0900124 if (existing == null) {
125 log.warn("There is no node configuration to update : {}", node.hostname());
126 return Response.notModified().build();
127 } else if (!existing.equals(node)) {
Jian Lif868cd52021-02-19 10:28:01 +0900128 service.updateNode(node);
Jian Li7c4a8822020-12-21 11:25:12 +0900129 }
130 }
131
Jian Li762bc222020-12-19 01:26:57 +0900132 return Response.ok().build();
133 }
Jian Li7c4a8822020-12-21 11:25:12 +0900134
135 /**
136 * Removes a set of KubeVirt nodes' config from the JSON input stream.
137 *
138 * @param hostname host name contained in KubeVirt nodes configuration
139 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
140 * 304 NOT_MODIFIED without the updated config
141 */
142 @DELETE
Jian Lic4604302020-12-25 02:24:16 +0900143 @Path("{hostname}")
Jian Li7c4a8822020-12-21 11:25:12 +0900144 @Consumes(MediaType.APPLICATION_JSON)
145 @Produces(MediaType.APPLICATION_JSON)
146 public Response deleteNode(@PathParam("hostname") String hostname) {
147 log.trace(String.format(MESSAGE_NODE, REMOVE));
148
Jian Lif868cd52021-02-19 10:28:01 +0900149 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
150 KubevirtNode existing = service.node(
Jian Li7c4a8822020-12-21 11:25:12 +0900151 nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE));
152
153 if (existing == null) {
154 log.warn("There is no node configuration to delete : {}", hostname);
155 return Response.notModified().build();
156 } else {
Jian Lif868cd52021-02-19 10:28:01 +0900157 service.removeNode(hostname);
Jian Li7c4a8822020-12-21 11:25:12 +0900158 }
159
160 return Response.noContent().build();
161 }
162
163 /**
164 * Obtains the state of the KubeVirt node.
165 *
166 * @param hostname hostname of the KubeVirt
167 * @return the state of the KubeVirt node in Json
168 */
169 @GET
170 @Produces(MediaType.APPLICATION_JSON)
171 @Path("state/{hostname}")
172 public Response stateOfNode(@PathParam("hostname") String hostname) {
173 log.trace(String.format(MESSAGE_NODE, QUERY));
174
Jian Lif868cd52021-02-19 10:28:01 +0900175 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
176 KubevirtNode node = service.node(hostname);
Jian Li7c4a8822020-12-21 11:25:12 +0900177 String nodeState = node != null ? node.state().toString() : NOT_EXIST;
178
179 return ok(mapper().createObjectNode().put(STATE, nodeState)).build();
180 }
181
182 /**
183 * Initializes KubeVirt node.
184 *
185 * @param hostname hostname of KubeVirt node
186 * @return 200 OK with init result, 404 not found, 500 server error
187 */
188 @GET
189 @Produces(MediaType.APPLICATION_JSON)
Jian Lic4604302020-12-25 02:24:16 +0900190 @Path("init/{hostname}")
Jian Li7c4a8822020-12-21 11:25:12 +0900191 public Response initNode(@PathParam("hostname") String hostname) {
192 log.trace(String.format(MESSAGE_NODE, QUERY));
193
Jian Lif868cd52021-02-19 10:28:01 +0900194 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
195 KubevirtNode node = service.node(hostname);
Jian Li7c4a8822020-12-21 11:25:12 +0900196 if (node == null) {
197 log.error("Given node {} does not exist", hostname);
198 return Response.serverError().build();
199 }
200 KubevirtNode updated = node.updateState(KubevirtNodeState.INIT);
Jian Lif868cd52021-02-19 10:28:01 +0900201 service.updateNode(updated);
Jian Li7c4a8822020-12-21 11:25:12 +0900202 return ok(mapper().createObjectNode()).build();
203 }
204
205 /**
206 * Initializes all KubeVirt nodes.
207 *
208 * @return 200 OK with init result, 500 server error
209 */
210 @GET
211 @Produces(MediaType.APPLICATION_JSON)
212 @Path("init/all")
213 public Response initAllNodes() {
214 log.trace(String.format(MESSAGE_NODE, QUERY));
215
Jian Lif868cd52021-02-19 10:28:01 +0900216 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
217
218 service.nodes()
Jian Li7c4a8822020-12-21 11:25:12 +0900219 .forEach(n -> {
220 KubevirtNode updated = n.updateState(KubevirtNodeState.INIT);
Jian Lif868cd52021-02-19 10:28:01 +0900221 service.updateNode(updated);
Jian Li7c4a8822020-12-21 11:25:12 +0900222 });
223
224 return ok(mapper().createObjectNode()).build();
225 }
226
227 /**
228 * Initializes KubeVirt nodes which are in the stats other than COMPLETE.
229 *
230 * @return 200 OK with init result, 500 server error
231 */
232 @GET
233 @Produces(MediaType.APPLICATION_JSON)
234 @Path("init/incomplete")
235 public Response initIncompleteNodes() {
236 log.trace(String.format(MESSAGE_NODE, QUERY));
237
Jian Lif868cd52021-02-19 10:28:01 +0900238 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
239 service.nodes().stream()
Jian Li7c4a8822020-12-21 11:25:12 +0900240 .filter(n -> n.state() != KubevirtNodeState.COMPLETE)
241 .forEach(n -> {
242 KubevirtNode updated = n.updateState(KubevirtNodeState.INIT);
Jian Lif868cd52021-02-19 10:28:01 +0900243 service.updateNode(updated);
Jian Li7c4a8822020-12-21 11:25:12 +0900244 });
245
246 return ok(mapper().createObjectNode()).build();
247 }
248
249 private Set<KubevirtNode> readNodeConfiguration(InputStream input) {
250 Set<KubevirtNode> nodeSet = Sets.newHashSet();
251 try {
252 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
253 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
254 nodes.forEach(node -> {
255 try {
256 ObjectNode objectNode = node.deepCopy();
257 KubevirtNode kubevirtNode =
258 codec(KubevirtNode.class).decode(objectNode, this);
259 nodeSet.add(kubevirtNode);
260 } catch (Exception e) {
261 log.error("Exception occurred due to {}", e);
262 throw new IllegalArgumentException();
263 }
264 });
265 } catch (Exception e) {
266 throw new IllegalArgumentException(e);
267 }
268
269 return nodeSet;
270 }
Jian Li762bc222020-12-19 01:26:57 +0900271}