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 | |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 73 | @Context |
| 74 | private UriInfo uriInfo; |
| 75 | |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 76 | /** |
| 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 Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 85 | @Consumes(MediaType.APPLICATION_JSON) |
| 86 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 87 | public Response createNodes(InputStream input) { |
| 88 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
| 89 | |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 90 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 91 | |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 92 | readNodeConfiguration(input).forEach(node -> { |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 93 | KubevirtNode existing = service.node(node.hostname()); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 94 | if (existing == null) { |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 95 | service.createNode(node); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 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 | |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 120 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 121 | Set<KubevirtNode> nodes = readNodeConfiguration(input); |
| 122 | for (KubevirtNode node: nodes) { |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 123 | KubevirtNode existing = service.node(node.hostname()); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 124 | 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 Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 128 | service.updateNode(node); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Jian Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 132 | return Response.ok().build(); |
| 133 | } |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 134 | |
| 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 Li | c460430 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 143 | @Path("{hostname}") |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 144 | @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 Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 149 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 150 | KubevirtNode existing = service.node( |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 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 { |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 157 | service.removeNode(hostname); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 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 | |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 175 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 176 | KubevirtNode node = service.node(hostname); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 177 | 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 Li | c460430 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 190 | @Path("init/{hostname}") |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 191 | public Response initNode(@PathParam("hostname") String hostname) { |
| 192 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 193 | |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 194 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 195 | KubevirtNode node = service.node(hostname); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 196 | 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 Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 201 | service.updateNode(updated); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 202 | 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 Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 216 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 217 | |
| 218 | service.nodes() |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 219 | .forEach(n -> { |
| 220 | KubevirtNode updated = n.updateState(KubevirtNodeState.INIT); |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 221 | service.updateNode(updated); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 222 | }); |
| 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 Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 238 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 239 | service.nodes().stream() |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 240 | .filter(n -> n.state() != KubevirtNodeState.COMPLETE) |
| 241 | .forEach(n -> { |
| 242 | KubevirtNode updated = n.updateState(KubevirtNodeState.INIT); |
Jian Li | f868cd5 | 2021-02-19 10:28:01 +0900 | [diff] [blame^] | 243 | service.updateNode(updated); |
Jian Li | 7c4a882 | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 244 | }); |
| 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 Li | 762bc22 | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 271 | } |