blob: 33f1a03e24b59e4944bd659359e9675f06cbf1bc [file] [log] [blame]
Hyunsun Moon0e058f22017-04-19 17:00:52 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon0e058f22017-04-19 17:00:52 +09003 *
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.openstacknetworking.cli;
17
18import org.apache.karaf.shell.commands.Command;
19import org.onosproject.cli.AbstractShellCommand;
Hyunsun Moon0d457362017-06-27 17:19:41 +090020import org.onosproject.openstacknode.api.NodeState;
21import org.onosproject.openstacknode.api.OpenstackNode;
22import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
23import org.onosproject.openstacknode.api.OpenstackNodeService;
Hyunsun Moon0e058f22017-04-19 17:00:52 +090024
25/**
26 * Re-installs flow rules for OpenStack networking.
27 */
28@Command(scope = "onos", name = "openstack-sync-rules",
29 description = "Re-installs flow rules for OpenStack networking")
30public class OpenstackSyncRulesCommand extends AbstractShellCommand {
31
Jian Li88ae51e2018-07-10 02:23:35 +090032 private static final long TIMEOUT_MS = 10000; // we wait 10s for init each node
33
Hyunsun Moon0e058f22017-04-19 17:00:52 +090034 @Override
35 protected void execute() {
36 // All handlers in this application reacts the node complete event and
37 // tries to re-configure flow rules for the complete node.
Hyunsun Moon0d457362017-06-27 17:19:41 +090038 OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class);
39 OpenstackNodeAdminService osNodeAdminService = AbstractShellCommand.get(OpenstackNodeAdminService.class);
40 if (osNodeService == null) {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090041 error("Failed to re-install flow rules for OpenStack networking.");
42 return;
43 }
Hyunsun Moon0d457362017-06-27 17:19:41 +090044 osNodeService.completeNodes().forEach(osNode -> {
45 OpenstackNode updated = osNode.updateState(NodeState.INIT);
46 osNodeAdminService.updateNode(updated);
Jian Li88ae51e2018-07-10 02:23:35 +090047
48 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
49 while (updated.state() != NodeState.COMPLETE) {
50 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
51
52 if (updated.state() == NodeState.COMPLETE) {
53 print("Finished sync rules for node {}", updated.hostname());
54 break;
55 }
56
57 if (waitMs <= 0) {
58 error("Failed to sync rules for node {}", updated.hostname());
59 break;
60 }
61 }
Hyunsun Moon0d457362017-06-27 17:19:41 +090062 });
Hyunsun Moon0e058f22017-04-19 17:00:52 +090063 print("Successfully requested re-installing flow rules.");
64 }
65}