Jian Li | e2a53cb | 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 | b230e07c | 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; |
Jian Li | f54f8a6 | 2021-05-22 00:10:15 +0900 | [diff] [blame^] | 22 | import org.onosproject.kubevirtnode.api.KubevirtApiConfig; |
| 23 | import org.onosproject.kubevirtnode.api.KubevirtApiConfigService; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 24 | import org.onosproject.kubevirtnode.api.KubevirtNode; |
| 25 | import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService; |
| 26 | import org.onosproject.kubevirtnode.api.KubevirtNodeState; |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 27 | import org.onosproject.rest.AbstractWebResource; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import javax.ws.rs.Consumes; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 32 | import javax.ws.rs.DELETE; |
| 33 | import javax.ws.rs.GET; |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 34 | import javax.ws.rs.POST; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 35 | import javax.ws.rs.PUT; |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 36 | import javax.ws.rs.Path; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 37 | import javax.ws.rs.PathParam; |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 38 | import javax.ws.rs.Produces; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 39 | import javax.ws.rs.core.Context; |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 42 | import javax.ws.rs.core.UriBuilder; |
| 43 | import javax.ws.rs.core.UriInfo; |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 44 | import java.io.InputStream; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 45 | import java.util.Set; |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 46 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 47 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
Jian Li | be478b7 | 2021-05-14 15:21:08 +0900 | [diff] [blame] | 48 | import static java.lang.Thread.sleep; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 49 | import static javax.ws.rs.core.Response.created; |
| 50 | import static org.onlab.util.Tools.nullIsIllegal; |
| 51 | import static org.onlab.util.Tools.readTreeFromStream; |
Jian Li | be478b7 | 2021-05-14 15:21:08 +0900 | [diff] [blame] | 52 | import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER; |
| 53 | import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE; |
| 54 | import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Handles REST API call of KubeVirt node config. |
| 58 | */ |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 59 | @Path("node") |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 60 | public class KubevirtNodeWebResource extends AbstractWebResource { |
| 61 | |
| 62 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 63 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 64 | private static final String MESSAGE_NODE = "Received node %s request"; |
| 65 | private static final String NODES = "nodes"; |
| 66 | private static final String CREATE = "CREATE"; |
| 67 | private static final String UPDATE = "UPDATE"; |
| 68 | private static final String NODE_ID = "NODE_ID"; |
| 69 | private static final String REMOVE = "REMOVE"; |
| 70 | private static final String QUERY = "QUERY"; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 71 | private static final String NOT_EXIST = "Not exist"; |
| 72 | private static final String STATE = "State"; |
Jian Li | f54f8a6 | 2021-05-22 00:10:15 +0900 | [diff] [blame^] | 73 | private static final String API_CONFIG = "apiConfig"; |
| 74 | private static final String OK = "ok"; |
| 75 | private static final String ERROR = "error"; |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 76 | private static final String RESULT = "Result"; |
| 77 | |
Jian Li | be478b7 | 2021-05-14 15:21:08 +0900 | [diff] [blame] | 78 | private static final long SLEEP_MS = 5000; // we wait 5s for init each node |
| 79 | private static final long TIMEOUT_MS = 10000; // we wait 10s |
| 80 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 81 | private static final String HOST_NAME = "hostname"; |
| 82 | private static final String ERROR_MESSAGE = " cannot be null"; |
| 83 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 84 | @Context |
| 85 | private UriInfo uriInfo; |
| 86 | |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 87 | /** |
| 88 | * Creates a set of KubeVirt nodes' config from the JSON input stream. |
| 89 | * |
| 90 | * @param input KubeVirt nodes JSON input stream |
| 91 | * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON |
| 92 | * is malformed |
| 93 | * @onos.rsModel KubevirtNode |
| 94 | */ |
| 95 | @POST |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 96 | @Consumes(MediaType.APPLICATION_JSON) |
| 97 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 98 | public Response createNodes(InputStream input) { |
| 99 | log.trace(String.format(MESSAGE_NODE, CREATE)); |
| 100 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 101 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 102 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 103 | readNodeConfiguration(input).forEach(node -> { |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 104 | KubevirtNode existing = service.node(node.hostname()); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 105 | if (existing == null) { |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 106 | service.createNode(node); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 107 | } |
| 108 | }); |
| 109 | |
| 110 | UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() |
| 111 | .path(NODES) |
| 112 | .path(NODE_ID); |
| 113 | |
| 114 | return created(locationBuilder.build()).build(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Updates a set of KubeVirt nodes' config from the JSON input stream. |
| 119 | * |
| 120 | * @param input KubeVirt nodes JSON input stream |
| 121 | * @return 200 OK with the updated KubeVirt node's config, 400 BAD_REQUEST |
| 122 | * if the JSON is malformed, and 304 NOT_MODIFIED without the updated config |
| 123 | * @onos.rsModel KubevirtNode |
| 124 | */ |
| 125 | @PUT |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 126 | @Consumes(MediaType.APPLICATION_JSON) |
| 127 | @Produces(MediaType.APPLICATION_JSON) |
| 128 | public Response updateNodes(InputStream input) { |
| 129 | log.trace(String.format(MESSAGE_NODE, UPDATE)); |
| 130 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 131 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 132 | Set<KubevirtNode> nodes = readNodeConfiguration(input); |
| 133 | for (KubevirtNode node: nodes) { |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 134 | KubevirtNode existing = service.node(node.hostname()); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 135 | if (existing == null) { |
| 136 | log.warn("There is no node configuration to update : {}", node.hostname()); |
| 137 | return Response.notModified().build(); |
| 138 | } else if (!existing.equals(node)) { |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 139 | service.updateNode(node); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 143 | return Response.ok().build(); |
| 144 | } |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * Removes a set of KubeVirt nodes' config from the JSON input stream. |
| 148 | * |
| 149 | * @param hostname host name contained in KubeVirt nodes configuration |
| 150 | * @return 204 NO_CONTENT, 400 BAD_REQUEST if the JSON is malformed, and |
| 151 | * 304 NOT_MODIFIED without the updated config |
| 152 | */ |
| 153 | @DELETE |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 154 | @Path("{hostname}") |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 155 | @Consumes(MediaType.APPLICATION_JSON) |
| 156 | @Produces(MediaType.APPLICATION_JSON) |
| 157 | public Response deleteNode(@PathParam("hostname") String hostname) { |
| 158 | log.trace(String.format(MESSAGE_NODE, REMOVE)); |
| 159 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 160 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 161 | KubevirtNode existing = service.node( |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 162 | nullIsIllegal(hostname, HOST_NAME + ERROR_MESSAGE)); |
| 163 | |
| 164 | if (existing == null) { |
| 165 | log.warn("There is no node configuration to delete : {}", hostname); |
| 166 | return Response.notModified().build(); |
| 167 | } else { |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 168 | service.removeNode(hostname); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return Response.noContent().build(); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Obtains the state of the KubeVirt node. |
| 176 | * |
| 177 | * @param hostname hostname of the KubeVirt |
| 178 | * @return the state of the KubeVirt node in Json |
| 179 | */ |
| 180 | @GET |
| 181 | @Produces(MediaType.APPLICATION_JSON) |
| 182 | @Path("state/{hostname}") |
| 183 | public Response stateOfNode(@PathParam("hostname") String hostname) { |
| 184 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 185 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 186 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 187 | KubevirtNode node = service.node(hostname); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 188 | String nodeState = node != null ? node.state().toString() : NOT_EXIST; |
| 189 | |
| 190 | return ok(mapper().createObjectNode().put(STATE, nodeState)).build(); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Initializes KubeVirt node. |
| 195 | * |
| 196 | * @param hostname hostname of KubeVirt node |
| 197 | * @return 200 OK with init result, 404 not found, 500 server error |
| 198 | */ |
| 199 | @GET |
| 200 | @Produces(MediaType.APPLICATION_JSON) |
Jian Li | f248307 | 2020-12-25 02:24:16 +0900 | [diff] [blame] | 201 | @Path("init/{hostname}") |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 202 | public Response initNode(@PathParam("hostname") String hostname) { |
| 203 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 204 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 205 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 206 | KubevirtNode node = service.node(hostname); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 207 | if (node == null) { |
| 208 | log.error("Given node {} does not exist", hostname); |
| 209 | return Response.serverError().build(); |
| 210 | } |
Jian Li | be478b7 | 2021-05-14 15:21:08 +0900 | [diff] [blame] | 211 | KubevirtNode updated = node.updateState(INIT); |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 212 | service.updateNode(updated); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 213 | return ok(mapper().createObjectNode()).build(); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Initializes all KubeVirt nodes. |
| 218 | * |
| 219 | * @return 200 OK with init result, 500 server error |
| 220 | */ |
| 221 | @GET |
| 222 | @Produces(MediaType.APPLICATION_JSON) |
| 223 | @Path("init/all") |
| 224 | public Response initAllNodes() { |
| 225 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 226 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 227 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 228 | |
| 229 | service.nodes() |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 230 | .forEach(n -> { |
Jian Li | be478b7 | 2021-05-14 15:21:08 +0900 | [diff] [blame] | 231 | KubevirtNode updated = n.updateState(INIT); |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 232 | service.updateNode(updated); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 233 | }); |
| 234 | |
| 235 | return ok(mapper().createObjectNode()).build(); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Initializes KubeVirt nodes which are in the stats other than COMPLETE. |
| 240 | * |
| 241 | * @return 200 OK with init result, 500 server error |
| 242 | */ |
| 243 | @GET |
| 244 | @Produces(MediaType.APPLICATION_JSON) |
| 245 | @Path("init/incomplete") |
| 246 | public Response initIncompleteNodes() { |
| 247 | log.trace(String.format(MESSAGE_NODE, QUERY)); |
| 248 | |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 249 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 250 | service.nodes().stream() |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 251 | .filter(n -> n.state() != KubevirtNodeState.COMPLETE) |
| 252 | .forEach(n -> { |
Jian Li | be478b7 | 2021-05-14 15:21:08 +0900 | [diff] [blame] | 253 | KubevirtNode updated = n.updateState(INIT); |
Jian Li | 3581300 | 2021-02-19 10:28:01 +0900 | [diff] [blame] | 254 | service.updateNode(updated); |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 255 | }); |
| 256 | |
| 257 | return ok(mapper().createObjectNode()).build(); |
| 258 | } |
| 259 | |
Jian Li | be478b7 | 2021-05-14 15:21:08 +0900 | [diff] [blame] | 260 | /** |
| 261 | * Synchronizes the flow rules. |
| 262 | * |
| 263 | * @return 200 OK with sync result, 404 not found |
| 264 | */ |
| 265 | @GET |
| 266 | @Produces(MediaType.APPLICATION_JSON) |
| 267 | @Path("sync/rules") |
| 268 | public Response syncRules() { |
| 269 | |
| 270 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 271 | |
| 272 | service.completeNodes(WORKER).forEach(this::syncRulesBase); |
| 273 | return ok(mapper().createObjectNode()).build(); |
| 274 | } |
| 275 | |
Jian Li | f54f8a6 | 2021-05-22 00:10:15 +0900 | [diff] [blame^] | 276 | /** |
| 277 | * Returns the health check result. |
| 278 | * |
| 279 | * @return 200 OK with health check result, 404 not found |
| 280 | */ |
| 281 | @GET |
| 282 | @Produces(MediaType.APPLICATION_JSON) |
| 283 | @Path("healthz") |
| 284 | public Response healthz() { |
| 285 | KubevirtApiConfigService configService = get(KubevirtApiConfigService.class); |
| 286 | KubevirtApiConfig config = configService.apiConfig(); |
| 287 | |
| 288 | // TODO: we need to add more health check items |
| 289 | ObjectNode jsonResult = mapper().createObjectNode(); |
| 290 | String result = config != null ? OK : ERROR; |
| 291 | jsonResult.put(API_CONFIG, result); |
| 292 | return ok(jsonResult).build(); |
| 293 | } |
| 294 | |
Jian Li | be478b7 | 2021-05-14 15:21:08 +0900 | [diff] [blame] | 295 | private void syncRulesBase(KubevirtNode node) { |
| 296 | KubevirtNode updated = node.updateState(INIT); |
| 297 | KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class); |
| 298 | service.updateNode(updated); |
| 299 | |
| 300 | boolean result = true; |
| 301 | long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS; |
| 302 | |
| 303 | while (service.node(node.hostname()).state() != COMPLETE) { |
| 304 | |
| 305 | long waitMs = timeoutExpiredMs - System.currentTimeMillis(); |
| 306 | |
| 307 | try { |
| 308 | sleep(SLEEP_MS); |
| 309 | } catch (InterruptedException e) { |
| 310 | log.error("Exception caused during node synchronization..."); |
| 311 | } |
| 312 | |
| 313 | if (service.node(node.hostname()).state() == COMPLETE) { |
| 314 | break; |
| 315 | } else { |
| 316 | service.updateNode(updated); |
| 317 | log.info("Failed to synchronize flow rules, retrying..."); |
| 318 | } |
| 319 | |
| 320 | if (waitMs <= 0) { |
| 321 | result = false; |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | if (result) { |
| 327 | log.info("Successfully synchronize flow rules for node {}!", node.hostname()); |
| 328 | } else { |
| 329 | log.warn("Failed to synchronize flow rules for node {}.", node.hostname()); |
| 330 | } |
| 331 | } |
| 332 | |
Jian Li | b230e07c | 2020-12-21 11:25:12 +0900 | [diff] [blame] | 333 | private Set<KubevirtNode> readNodeConfiguration(InputStream input) { |
| 334 | Set<KubevirtNode> nodeSet = Sets.newHashSet(); |
| 335 | try { |
| 336 | JsonNode jsonTree = readTreeFromStream(mapper().enable(INDENT_OUTPUT), input); |
| 337 | ArrayNode nodes = (ArrayNode) jsonTree.path(NODES); |
| 338 | nodes.forEach(node -> { |
| 339 | try { |
| 340 | ObjectNode objectNode = node.deepCopy(); |
| 341 | KubevirtNode kubevirtNode = |
| 342 | codec(KubevirtNode.class).decode(objectNode, this); |
| 343 | nodeSet.add(kubevirtNode); |
| 344 | } catch (Exception e) { |
| 345 | log.error("Exception occurred due to {}", e); |
| 346 | throw new IllegalArgumentException(); |
| 347 | } |
| 348 | }); |
| 349 | } catch (Exception e) { |
| 350 | throw new IllegalArgumentException(e); |
| 351 | } |
| 352 | |
| 353 | return nodeSet; |
| 354 | } |
Jian Li | e2a53cb | 2020-12-19 01:26:57 +0900 | [diff] [blame] | 355 | } |