blob: 2c0f57dbfd07687331d6f9547f6e69cc8e08d52c [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.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;
25import static org.onosproject.kubevirtnode.api.KubevirtNode.Type.WORKER;
26import static org.onosproject.kubevirtnode.api.KubevirtNodeState.COMPLETE;
27import static org.onosproject.kubevirtnode.api.KubevirtNodeState.INIT;
28
29/**
30 * Re-installs flow rules for KubeVirt networking.
31 */
32@Service
33@Command(scope = "onos", name = "kubevirt-sync-rules",
34 description = "Re-installs flow rules for KubeVirt networking")
35public class KubevirtSyncRulesCommand extends AbstractShellCommand {
36
37 private static final long SLEEP_MS = 5000; // we wait 5s for init each node
38 private static final long TIMEOUT_MS = 10000; // we wait 10s
39
40 private static final String SUCCESS_MSG = "Successfully synchronize flow rules for node %s!";
41 private static final String FAIL_MSG = "Failed to synchronize flow rules for node %s.";
42
43 @Override
44 protected void doExecute() throws Exception {
45 // All handlers in this application reacts the node complete event and
46 // tries to re-configure flow rules for the complete node.
47 KubevirtNodeAdminService nodeAdminService = get(KubevirtNodeAdminService.class);
48 if (nodeAdminService == null) {
49 error("Failed to re-install flow rules for OpenStack networking.");
50 return;
51 }
52
53 nodeAdminService.completeNodes(WORKER).forEach(node ->
54 syncRulesBaseForNode(nodeAdminService, node));
55
56 print("Successfully requested re-installing flow rules.");
57 }
58
59 private void syncRulesBaseForNode(KubevirtNodeAdminService service, KubevirtNode node) {
60 KubevirtNode updated = node.updateState(INIT);
61 service.updateNode(updated);
62
63 boolean result = true;
64 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
65
66 while (service.node(node.hostname()).state() != COMPLETE) {
67 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
68
69 try {
70 sleep(SLEEP_MS);
71 } catch (InterruptedException e) {
72 error("Exception caused during node synchronization...");
73 }
74
75 if (service.node(node.hostname()).state() == COMPLETE) {
76 break;
77 } else {
78 service.updateNode(updated);
79 print("Failed to synchronize flow rules, retrying...");
80 }
81
82 if (waitMs <= 0) {
83 result = false;
84 break;
85 }
86 }
87
88 if (result) {
89 print(SUCCESS_MSG, node.hostname());
90 } else {
91 error(FAIL_MSG, node.hostname());
92 }
93 }
94}