blob: 0270b26285576fd9111b19653c48cc221014cea6 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070019import org.apache.karaf.shell.api.action.lifecycle.Service;
Hyunsun Moon0e058f22017-04-19 17:00:52 +090020import org.onosproject.cli.AbstractShellCommand;
Hyunsun Moon0d457362017-06-27 17:19:41 +090021import org.onosproject.openstacknode.api.NodeState;
22import org.onosproject.openstacknode.api.OpenstackNode;
23import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
Hyunsun Moon0e058f22017-04-19 17:00:52 +090024
Jian Lif7934d52018-07-10 16:27:02 +090025import static java.lang.Thread.sleep;
Jian Li8b5599b2018-11-19 18:45:46 +090026import static org.onosproject.openstacknode.api.NodeState.COMPLETE;
Jian Li5c777c62018-11-11 00:31:20 +090027import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
28import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
Jian Lif7934d52018-07-10 16:27:02 +090029
Hyunsun Moon0e058f22017-04-19 17:00:52 +090030/**
31 * Re-installs flow rules for OpenStack networking.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
Hyunsun Moon0e058f22017-04-19 17:00:52 +090034@Command(scope = "onos", name = "openstack-sync-rules",
35 description = "Re-installs flow rules for OpenStack networking")
36public class OpenstackSyncRulesCommand extends AbstractShellCommand {
37
Jian Lif7934d52018-07-10 16:27:02 +090038 private static final long SLEEP_MS = 3000; // we wait 3s for init each node
Jian Li8b5599b2018-11-19 18:45:46 +090039 private static final long TIMEOUT_MS = 10000; // we wait 10s
Jian Li88ae51e2018-07-10 02:23:35 +090040
Hyunsun Moon0e058f22017-04-19 17:00:52 +090041 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070042 protected void doExecute() {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090043 // All handlers in this application reacts the node complete event and
44 // tries to re-configure flow rules for the complete node.
Jian Li5c777c62018-11-11 00:31:20 +090045 OpenstackNodeAdminService osNodeService = AbstractShellCommand.get(OpenstackNodeAdminService.class);
Hyunsun Moon0d457362017-06-27 17:19:41 +090046 if (osNodeService == null) {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090047 error("Failed to re-install flow rules for OpenStack networking.");
48 return;
49 }
Jian Li88ae51e2018-07-10 02:23:35 +090050
Jian Li5c777c62018-11-11 00:31:20 +090051 // we first initialize the COMPUTE node, in order to feed all instance ports
52 // by referring to ports' information obtained from neutron server
53 osNodeService.completeNodes(COMPUTE).forEach(osNode ->
54 syncRulesBaseForNode(osNodeService, osNode));
55 osNodeService.completeNodes(GATEWAY).forEach(osNode ->
56 syncRulesBaseForNode(osNodeService, osNode));
Jian Li88ae51e2018-07-10 02:23:35 +090057
Hyunsun Moon0e058f22017-04-19 17:00:52 +090058 print("Successfully requested re-installing flow rules.");
59 }
Jian Li5c777c62018-11-11 00:31:20 +090060
61 private void syncRulesBaseForNode(OpenstackNodeAdminService osNodeService,
62 OpenstackNode osNode) {
63 OpenstackNode updated = osNode.updateState(NodeState.INIT);
64 osNodeService.updateNode(updated);
65
Jian Li8b5599b2018-11-19 18:45:46 +090066 boolean result = true;
67 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
68
69 while (osNodeService.node(osNode.hostname()).state() != COMPLETE) {
70
71 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
72
73 try {
74 sleep(SLEEP_MS);
75 } catch (InterruptedException e) {
76 error("Exception caused during node synchronization...");
77 }
78
79 if (osNodeService.node(osNode.hostname()).state() == COMPLETE) {
80 break;
81 } else {
82 osNodeService.updateNode(updated);
83 print("Failed to synchronize flow rules, retrying...");
84 }
85
86 if (waitMs <= 0) {
87 result = false;
88 break;
89 }
Jian Li5c777c62018-11-11 00:31:20 +090090 }
91
Jian Li8b5599b2018-11-19 18:45:46 +090092 if (result) {
93 print("Successfully synchronize flow rules for node {}!", osNode.hostname());
Jian Li5c777c62018-11-11 00:31:20 +090094 } else {
Jian Li8b5599b2018-11-19 18:45:46 +090095 error("Failed to synchronize flow rules for node {}.", osNode.hostname());
Jian Li5c777c62018-11-11 00:31:20 +090096 }
97 }
Hyunsun Moon0e058f22017-04-19 17:00:52 +090098}