blob: a1587a54740600273533bca681cc34b89bd68ec8 [file] [log] [blame]
Jian Li543fe852021-02-04 17:25:01 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.web;
17
18import org.onosproject.kubevirtnode.api.KubevirtNode;
19import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService;
20import org.onosproject.rest.AbstractWebResource;
21import org.slf4j.Logger;
22import org.slf4j.LoggerFactory;
23
24import javax.ws.rs.GET;
25import javax.ws.rs.Path;
26import javax.ws.rs.Produces;
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response;
29
30import static java.lang.Thread.sleep;
31import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER;
32import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE;
33import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT;
34
35/**
36 * REST interface for synchronizing kubevirt network states and rules.
37 */
38@Path("management")
39public class KubevirtManagementWebResource extends AbstractWebResource {
40 private final Logger log = LoggerFactory.getLogger(getClass());
41
42 private static final long SLEEP_MS = 5000; // we wait 5s for init each node
43 private static final long TIMEOUT_MS = 10000; // we wait 10s
44
45 private final KubevirtNodeAdminService nodeAdminService =
46 get(KubevirtNodeAdminService.class);
47
48 /**
49 * Synchronizes the flow rules.
50 *
51 * @return 200 OK with sync result, 404 not found
52 */
53 @GET
54 @Produces(MediaType.APPLICATION_JSON)
55 @Path("sync/rules")
56 public Response syncRules() {
57
58 nodeAdminService.completeNodes(WORKER).forEach(this::syncRulesBase);
59 return ok(mapper().createObjectNode()).build();
60 }
61
62 private void syncRulesBase(KubevirtNode node) {
63 KubevirtNode updated = node.updateState(INIT);
64 nodeAdminService.updateNode(updated);
65
66 boolean result = true;
67 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
68
69 while (nodeAdminService.node(node.hostname()).state() != COMPLETE) {
70
71 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
72
73 try {
74 sleep(SLEEP_MS);
75 } catch (InterruptedException e) {
76 log.error("Exception caused during node synchronization...");
77 }
78
79 if (nodeAdminService.node(node.hostname()).state() == COMPLETE) {
80 break;
81 } else {
82 nodeAdminService.updateNode(updated);
83 log.info("Failed to synchronize flow rules, retrying...");
84 }
85
86 if (waitMs <= 0) {
87 result = false;
88 break;
89 }
90 }
91
92 if (result) {
93 log.info("Successfully synchronize flow rules for node {}!", node.hostname());
94 } else {
95 log.warn("Failed to synchronize flow rules for node {}.", node.hostname());
96 }
97 }
98}