blob: e3b18baa632ed9c7b59eace6952ddc3294a545db [file] [log] [blame]
Jian Lie2a53cb2020-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 Lib230e07c2020-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 Lie2a53cb2020-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 Lib230e07c2020-12-21 11:25:12 +090030import javax.ws.rs.DELETE;
31import javax.ws.rs.GET;
Jian Lie2a53cb2020-12-19 01:26:57 +090032import javax.ws.rs.POST;
Jian Lib230e07c2020-12-21 11:25:12 +090033import javax.ws.rs.PUT;
Jian Lie2a53cb2020-12-19 01:26:57 +090034import javax.ws.rs.Path;
Jian Lib230e07c2020-12-21 11:25:12 +090035import javax.ws.rs.PathParam;
Jian Lie2a53cb2020-12-19 01:26:57 +090036import javax.ws.rs.Produces;
Jian Lib230e07c2020-12-21 11:25:12 +090037import javax.ws.rs.core.Context;
Jian Lie2a53cb2020-12-19 01:26:57 +090038import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
Jian Lib230e07c2020-12-21 11:25:12 +090040import javax.ws.rs.core.UriBuilder;
41import javax.ws.rs.core.UriInfo;
Jian Lie2a53cb2020-12-19 01:26:57 +090042import java.io.InputStream;
Jian Lib230e07c2020-12-21 11:25:12 +090043import java.util.Set;
Jian Lie2a53cb2020-12-19 01:26:57 +090044
Jian Lib230e07c2020-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 Lie2a53cb2020-12-19 01:26:57 +090053@Path("configure")
54public class KubevirtNodeWebResource extends AbstractWebResource {
55
56 private final Logger log = LoggerFactory.getLogger(getClass());
57
Jian Lib230e07c2020-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
73 private final KubevirtNodeAdminService nodeAdminService = get(KubevirtNodeAdminService.class);
74
75 @Context
76 private UriInfo uriInfo;
77
Jian Lie2a53cb2020-12-19 01:26:57 +090078 /**
79 * Creates a set of KubeVirt nodes' config from the JSON input stream.
80 *
81 * @param input KubeVirt 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 KubevirtNode
85 */
86 @POST
87 @Path("node")
88 @Consumes(MediaType.APPLICATION_JSON)
89 @Produces(MediaType.APPLICATION_JSON)
Jian Lib230e07c2020-12-21 11:25:12 +090090 public Response createNodes(InputStream input) {
91 log.trace(String.format(MESSAGE_NODE, CREATE));
92
93 readNodeConfiguration(input).forEach(node -> {
94 KubevirtNode existing = nodeAdminService.node(node.hostname());
95 if (existing == null) {
96 nodeAdminService.createNode(node);
97 }
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 KubeVirt nodes' config from the JSON input stream.
109 *
110 * @param input KubeVirt nodes JSON input stream
111 * @return 200 OK with the updated KubeVirt node's config, 400 BAD_REQUEST
112 * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config
113 * @onos.rsModel KubevirtNode
114 */
115 @PUT
116 @Path("node")
117 @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<KubevirtNode> nodes = readNodeConfiguration(input);
123 for (KubevirtNode node: nodes) {
124 KubevirtNode existing = nodeAdminService.node(node.hostname());
125 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)) {
129 nodeAdminService.updateNode(node);
130 }
131 }
132
Jian Lie2a53cb2020-12-19 01:26:57 +0900133 return Response.ok().build();
134 }
Jian Lib230e07c2020-12-21 11:25:12 +0900135
136 /**
137 * Removes a set of KubeVirt nodes' config from the JSON input stream.
138 *
139 * @param hostname host name contained in KubeVirt nodes configuration
140 * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and
141 * 304 NOT_MODIFIED without the updated config
142 */
143 @DELETE
144 @Path("node/{hostname}")
145 @Consumes(MediaType.APPLICATION_JSON)
146 @Produces(MediaType.APPLICATION_JSON)
147 public Response deleteNode(@PathParam("hostname") String hostname) {
148 log.trace(String.format(MESSAGE_NODE, REMOVE));
149
150 KubevirtNode existing = nodeAdminService.node(
151 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 {
157 nodeAdminService.removeNode(hostname);
158 }
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
175 KubevirtNode node = nodeAdminService.node(hostname);
176 String nodeState = node != null ? node.state().toString() : NOT_EXIST;
177
178 return ok(mapper().createObjectNode().put(STATE, nodeState)).build();
179 }
180
181 /**
182 * Initializes KubeVirt node.
183 *
184 * @param hostname hostname of KubeVirt node
185 * @return 200 OK with init result, 404 not found, 500 server error
186 */
187 @GET
188 @Produces(MediaType.APPLICATION_JSON)
189 @Path("init/node/{hostname}")
190 public Response initNode(@PathParam("hostname") String hostname) {
191 log.trace(String.format(MESSAGE_NODE, QUERY));
192
193 KubevirtNode node = nodeAdminService.node(hostname);
194 if (node == null) {
195 log.error("Given node {} does not exist", hostname);
196 return Response.serverError().build();
197 }
198 KubevirtNode updated = node.updateState(KubevirtNodeState.INIT);
199 nodeAdminService.updateNode(updated);
200 return ok(mapper().createObjectNode()).build();
201 }
202
203 /**
204 * Initializes all KubeVirt nodes.
205 *
206 * @return 200 OK with init result, 500 server error
207 */
208 @GET
209 @Produces(MediaType.APPLICATION_JSON)
210 @Path("init/all")
211 public Response initAllNodes() {
212 log.trace(String.format(MESSAGE_NODE, QUERY));
213
214 nodeAdminService.nodes()
215 .forEach(n -> {
216 KubevirtNode updated = n.updateState(KubevirtNodeState.INIT);
217 nodeAdminService.updateNode(updated);
218 });
219
220 return ok(mapper().createObjectNode()).build();
221 }
222
223 /**
224 * Initializes KubeVirt nodes which are in the stats other than COMPLETE.
225 *
226 * @return 200 OK with init result, 500 server error
227 */
228 @GET
229 @Produces(MediaType.APPLICATION_JSON)
230 @Path("init/incomplete")
231 public Response initIncompleteNodes() {
232 log.trace(String.format(MESSAGE_NODE, QUERY));
233
234 nodeAdminService.nodes().stream()
235 .filter(n -> n.state() != KubevirtNodeState.COMPLETE)
236 .forEach(n -> {
237 KubevirtNode updated = n.updateState(KubevirtNodeState.INIT);
238 nodeAdminService.updateNode(updated);
239 });
240
241 return ok(mapper().createObjectNode()).build();
242 }
243
244 private Set<KubevirtNode> readNodeConfiguration(InputStream input) {
245 Set<KubevirtNode> nodeSet = Sets.newHashSet();
246 try {
247 JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input);
248 ArrayNode nodes = (ArrayNode) jsonTree.path(NODES);
249 nodes.forEach(node -> {
250 try {
251 ObjectNode objectNode = node.deepCopy();
252 KubevirtNode kubevirtNode =
253 codec(KubevirtNode.class).decode(objectNode, this);
254 nodeSet.add(kubevirtNode);
255 } catch (Exception e) {
256 log.error("Exception occurred due to {}", e);
257 throw new IllegalArgumentException();
258 }
259 });
260 } catch (Exception e) {
261 throw new IllegalArgumentException(e);
262 }
263
264 return nodeSet;
265 }
Jian Lie2a53cb2020-12-19 01:26:57 +0900266}