blob: 043899f1a223bf17117c4cf9356e5e21b1c1ff5a [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
Jian Li543fe852021-02-04 17:25:01 +090045 /**
46 * Synchronizes the flow rules.
47 *
48 * @return 200 OK with sync result, 404 not found
49 */
50 @GET
51 @Produces(MediaType.APPLICATION_JSON)
52 @Path("sync/rules")
53 public Response syncRules() {
54
Jian Lif868cd52021-02-19 10:28:01 +090055 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
56
57 service.completeNodes(WORKER).forEach(this::syncRulesBase);
Jian Li543fe852021-02-04 17:25:01 +090058 return ok(mapper().createObjectNode()).build();
59 }
60
61 private void syncRulesBase(KubevirtNode node) {
62 KubevirtNode updated = node.updateState(INIT);
Jian Lif868cd52021-02-19 10:28:01 +090063 KubevirtNodeAdminService service = get(KubevirtNodeAdminService.class);
64 service.updateNode(updated);
Jian Li543fe852021-02-04 17:25:01 +090065
66 boolean result = true;
67 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
68
Jian Lif868cd52021-02-19 10:28:01 +090069 while (service.node(node.hostname()).state() != COMPLETE) {
Jian Li543fe852021-02-04 17:25:01 +090070
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
Jian Lif868cd52021-02-19 10:28:01 +090079 if (service.node(node.hostname()).state() == COMPLETE) {
Jian Li543fe852021-02-04 17:25:01 +090080 break;
81 } else {
Jian Lif868cd52021-02-19 10:28:01 +090082 service.updateNode(updated);
Jian Li543fe852021-02-04 17:25:01 +090083 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}