blob: 80db82adfc3eb9d7430c6ed62ca9274132e39882 [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
24import static java.lang.Thread.sleep;
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
36 private static final long SLEEP_MS = 5000; // we wait 5s for init each node
37 private static final long TIMEOUT_MS = 10000; // we wait 10s
38
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) {
48 error("Failed to re-install flow rules for OpenStack networking.");
49 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
55 print("Successfully requested re-installing flow rules.");
56 }
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
68 try {
69 sleep(SLEEP_MS);
70 } catch (InterruptedException e) {
71 error("Exception caused during node synchronization...");
72 }
73
74 if (service.node(node.hostname()).state() == COMPLETE) {
75 break;
76 } else {
77 service.updateNode(updated);
78 print("Failed to synchronize flow rules, retrying...");
79 }
80
81 if (waitMs <= 0) {
82 result = false;
83 break;
84 }
85 }
86
87 if (result) {
88 print(SUCCESS_MSG, node.hostname());
89 } else {
90 error(FAIL_MSG, node.hostname());
91 }
92 }
93}