blob: 69e90c6c191ca4c57804b9a2c1b807f05b61293f [file] [log] [blame]
Jian Li858ccd72021-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.cli;
17
18import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.lifecycle.Service;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.kubevirtnode.api.KubevirtNode;
22import org.onosproject.kubevirtnode.api.KubevirtNodeAdminService;
23
Jian Li528335e2021-07-08 20:52:50 +090024import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.waitFor;
Jian Li858ccd72021-02-04 17:25:01 +090025import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE;
26import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT;
27
28/**
29 * Re-installs flow rules for KubeVirt networking.
30 */
31@Service
32@Command(scope = "onos", name = "kubevirt-sync-rules",
33 description = "Re-installs flow rules for KubeVirt networking")
34public class KubevirtSyncRulesCommand extends AbstractShellCommand {
35
Jian Li528335e2021-07-08 20:52:50 +090036 private static final int SLEEP_S = 1; // we re-check the status on every 1s
37 private static final long TIMEOUT_MS = 15000;
Jian Li858ccd72021-02-04 17:25:01 +090038
39 private static final String SUCCESS_MSG = "Successfully synchronize flow rules for node %s!";
40 private static final String FAIL_MSG = "Failed to synchronize flow rules for node %s.";
41
42 @Override
43 protected void doExecute() throws Exception {
44 // All handlers in this application reacts the node complete event and
45 // tries to re-configure flow rules for the complete node.
46 KubevirtNodeAdminService nodeAdminService = get(KubevirtNodeAdminService.class);
47 if (nodeAdminService == null) {
Jian Li528335e2021-07-08 20:52:50 +090048 error("Failed to re-install flow rules for kubevirt networking.");
Jian Li858ccd72021-02-04 17:25:01 +090049 return;
50 }
51
Jian Li394bef52021-05-27 18:53:45 +090052 nodeAdminService.completeNodes().forEach(node ->
Jian Li858ccd72021-02-04 17:25:01 +090053 syncRulesBaseForNode(nodeAdminService, node));
54
Jian Li528335e2021-07-08 20:52:50 +090055 print("Done all flow rules synchronization, but some nodes may have issues.");
Jian Li858ccd72021-02-04 17:25:01 +090056 }
57
58 private void syncRulesBaseForNode(KubevirtNodeAdminService service, KubevirtNode node) {
59 KubevirtNode updated = node.updateState(INIT);
60 service.updateNode(updated);
61
62 boolean result = true;
63 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
64
65 while (service.node(node.hostname()).state() != COMPLETE) {
66 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
67
Jian Li528335e2021-07-08 20:52:50 +090068 waitFor(SLEEP_S);
Jian Li858ccd72021-02-04 17:25:01 +090069
70 if (waitMs <= 0) {
71 result = false;
72 break;
73 }
74 }
75
76 if (result) {
77 print(SUCCESS_MSG, node.hostname());
78 } else {
79 error(FAIL_MSG, node.hostname());
80 }
81 }
82}