Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.kubevirtnode.web; |
| 17 | |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 23 | import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService; |
| 24 | import org.onosproject.kubevirtnode.api.KubevirtNodeState; |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 25 | import org.onosproject.rest.AbstractWebResource; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import javax.ws.rs.Consumes; |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 30 | import javax.ws.rs.DELETE; |
| 31 | import javax.ws.rs.GET; |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 32 | import javax.ws.rs.POST; |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 33 | import javax.ws.rs.PUT; |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 34 | import javax.ws.rs.Path; |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 35 | import javax.ws.rs.PathParam; |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 36 | import javax.ws.rs.Produces; |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 37 | import javax.ws.rs.core.Context; |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 40 | import javax.ws.rs.core.UriBuilder; |
| 41 | import javax.ws.rs.core.UriInfo; |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 42 | import java.io.InputStream; |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 43 | import java.util.Set; |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 44 | |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 45 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 46 | import static javax.ws.rs.core.Response.created; |
| 47 | import static org.onlab.util.Tools.nullIsIllegal; |
| 48 | import static org.onlab.util.Tools.readTreeFromStream; |
| 49 | |
| 50 | /** |
| 51 | * Handles REST API call of KubeVirt node config. |
| 52 | */ |
Jian Li | c460430 | 2020-12-25 02:24:16 +0900 | [diff] [blame^] | 53 | @Path("node") |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 54 | public class KubevirtNodeWebResource extends AbstractWebResource { |
| 55 | |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 58 | 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 Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 78 | /** |
| 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 |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 87 | @Consumes(MediaType.APPLICATION_JSON) |
| 88 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 89 | public Response createNodes(InputStream input) { |
| 90 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
| 91 | |
| 92 | readNodeConfiguration(input).forEach(node -> { |
| 93 | KubevirtNode existing = nodeAdminService.node(node.hostname()); |
| 94 | if (existing == null) { |
| 95 | nodeAdminService.createNode(node); |
| 96 | } |
| 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 Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 115 | @Consumes(MediaType.APPLICATION_JSON) |
| 116 | @Produces(MediaType.APPLICATION_JSON) |
| 117 | public Response updateNodes(InputStream input) { |
| 118 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
| 119 | |
| 120 | Set<KubevirtNode> nodes = readNodeConfiguration(input); |
| 121 | for (KubevirtNode node: nodes) { |
| 122 | KubevirtNode existing = nodeAdminService.node(node.hostname()); |
| 123 | if (existing == null) { |
| 124 | log.warn("There is no node configuration to update : {}", node.hostname()); |
| 125 | return Response.notModified().build(); |
| 126 | } else if (!existing.equals(node)) { |
| 127 | nodeAdminService.updateNode(node); |
| 128 | } |
| 129 | } |
| 130 | |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 131 | return Response.ok().build(); |
| 132 | } |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Removes a set of KubeVirt nodes' config from the JSON input stream. |
| 136 | * |
| 137 | * @param hostname host name contained in KubeVirt nodes configuration |
| 138 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and |
| 139 | * 304 NOT_MODIFIED without the updated config |
| 140 | */ |
| 141 | @DELETE |
Jian Li | c460430 | 2020-12-25 02:24:16 +0900 | [diff] [blame^] | 142 | @Path("{hostname}") |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 143 | @Consumes(MediaType.APPLICATION_JSON) |
| 144 | @Produces(MediaType.APPLICATION_JSON) |
| 145 | public Response deleteNode(@PathParam("hostname") String hostname) { |
| 146 | log.trace(String.format(MESSAGE_NODE, REMOVE)); |
| 147 | |
| 148 | KubevirtNode existing = nodeAdminService.node( |
| 149 | nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE)); |
| 150 | |
| 151 | if (existing == null) { |
| 152 | log.warn("There is no node configuration to delete : {}", hostname); |
| 153 | return Response.notModified().build(); |
| 154 | } else { |
| 155 | nodeAdminService.removeNode(hostname); |
| 156 | } |
| 157 | |
| 158 | return Response.noContent().build(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Obtains the state of the KubeVirt node. |
| 163 | * |
| 164 | * @param hostname hostname of the KubeVirt |
| 165 | * @return the state of the KubeVirt node in Json |
| 166 | */ |
| 167 | @GET |
| 168 | @Produces(MediaType.APPLICATION_JSON) |
| 169 | @Path("state/{hostname}") |
| 170 | public Response stateOfNode(@PathParam("hostname") String hostname) { |
| 171 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 172 | |
| 173 | KubevirtNode node = nodeAdminService.node(hostname); |
| 174 | String nodeState = node != null ? node.state().toString() : NOT_EXIST; |
| 175 | |
| 176 | return ok(mapper().createObjectNode().put(STATE, nodeState)).build(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Initializes KubeVirt node. |
| 181 | * |
| 182 | * @param hostname hostname of KubeVirt node |
| 183 | * @return 200 OK with init result, 404 not found, 500 server error |
| 184 | */ |
| 185 | @GET |
| 186 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | c460430 | 2020-12-25 02:24:16 +0900 | [diff] [blame^] | 187 | @Path("init/{hostname}") |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 188 | public Response initNode(@PathParam("hostname") String hostname) { |
| 189 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 190 | |
| 191 | KubevirtNode node = nodeAdminService.node(hostname); |
| 192 | if (node == null) { |
| 193 | log.error("Given node {} does not exist", hostname); |
| 194 | return Response.serverError().build(); |
| 195 | } |
| 196 | KubevirtNode updated = node.updateState(KubevirtNodeState.INIT); |
| 197 | nodeAdminService.updateNode(updated); |
| 198 | return ok(mapper().createObjectNode()).build(); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Initializes all KubeVirt nodes. |
| 203 | * |
| 204 | * @return 200 OK with init result, 500 server error |
| 205 | */ |
| 206 | @GET |
| 207 | @Produces(MediaType.APPLICATION_JSON) |
| 208 | @Path("init/all") |
| 209 | public Response initAllNodes() { |
| 210 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 211 | |
| 212 | nodeAdminService.nodes() |
| 213 | .forEach(n -> { |
| 214 | KubevirtNode updated = n.updateState(KubevirtNodeState.INIT); |
| 215 | nodeAdminService.updateNode(updated); |
| 216 | }); |
| 217 | |
| 218 | return ok(mapper().createObjectNode()).build(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Initializes KubeVirt nodes which are in the stats other than COMPLETE. |
| 223 | * |
| 224 | * @return 200 OK with init result, 500 server error |
| 225 | */ |
| 226 | @GET |
| 227 | @Produces(MediaType.APPLICATION_JSON) |
| 228 | @Path("init/incomplete") |
| 229 | public Response initIncompleteNodes() { |
| 230 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 231 | |
| 232 | nodeAdminService.nodes().stream() |
| 233 | .filter(n -> n.state() != KubevirtNodeState.COMPLETE) |
| 234 | .forEach(n -> { |
| 235 | KubevirtNode updated = n.updateState(KubevirtNodeState.INIT); |
| 236 | nodeAdminService.updateNode(updated); |
| 237 | }); |
| 238 | |
| 239 | return ok(mapper().createObjectNode()).build(); |
| 240 | } |
| 241 | |
| 242 | private Set<KubevirtNode> readNodeConfiguration(InputStream input) { |
| 243 | Set<KubevirtNode> nodeSet = Sets.newHashSet(); |
| 244 | try { |
| 245 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 246 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
| 247 | nodes.forEach(node -> { |
| 248 | try { |
| 249 | ObjectNode objectNode = node.deepCopy(); |
| 250 | KubevirtNode kubevirtNode = |
| 251 | codec(KubevirtNode.class).decode(objectNode, this); |
| 252 | nodeSet.add(kubevirtNode); |
| 253 | } catch (Exception e) { |
| 254 | log.error("Exception occurred due to {}", e); |
| 255 | throw new IllegalArgumentException(); |
| 256 | } |
| 257 | }); |
| 258 | } catch (Exception e) { |
| 259 | throw new IllegalArgumentException(e); |
| 260 | } |
| 261 | |
| 262 | return nodeSet; |
| 263 | } |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 264 | } |