blob: 9ca71a4085f98272501e2c55f6224d3a785241cf [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 Li5ecfd1a2018-12-10 11:41:03 +090026import static org.onosproject.cli.AbstractShellCommand.get;
Jian Li8b5599b2018-11-19 18:45:46 +090027import static org.onosproject.openstacknode.api.NodeState.COMPLETE;
Jian Li5c777c62018-11-11 00:31:20 +090028import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
29import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
Jian Lif7934d52018-07-10 16:27:02 +090030
Hyunsun Moon0e058f22017-04-19 17:00:52 +090031/**
32 * Re-installs flow rules for OpenStack networking.
33 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070034@Service
Hyunsun Moon0e058f22017-04-19 17:00:52 +090035@Command(scope = "onos", name = "openstack-sync-rules",
36 description = "Re-installs flow rules for OpenStack networking")
37public class OpenstackSyncRulesCommand extends AbstractShellCommand {
38
Jian Lif7934d52018-07-10 16:27:02 +090039 private static final long SLEEP_MS = 3000; // we wait 3s for init each node
Jian Li8b5599b2018-11-19 18:45:46 +090040 private static final long TIMEOUT_MS = 10000; // we wait 10s
Jian Li88ae51e2018-07-10 02:23:35 +090041
Hyunsun Moon0e058f22017-04-19 17:00:52 +090042 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070043 protected void doExecute() {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090044 // All handlers in this application reacts the node complete event and
45 // tries to re-configure flow rules for the complete node.
Jian Li5ecfd1a2018-12-10 11:41:03 +090046 OpenstackNodeAdminService osNodeService = get(OpenstackNodeAdminService.class);
Hyunsun Moon0d457362017-06-27 17:19:41 +090047 if (osNodeService == null) {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090048 error("Failed to re-install flow rules for OpenStack networking.");
49 return;
50 }
Jian Li88ae51e2018-07-10 02:23:35 +090051
Jian Li5c777c62018-11-11 00:31:20 +090052 // we first initialize the COMPUTE node, in order to feed all instance ports
53 // by referring to ports' information obtained from neutron server
54 osNodeService.completeNodes(COMPUTE).forEach(osNode ->
55 syncRulesBaseForNode(osNodeService, osNode));
56 osNodeService.completeNodes(GATEWAY).forEach(osNode ->
57 syncRulesBaseForNode(osNodeService, osNode));
Jian Li88ae51e2018-07-10 02:23:35 +090058
Hyunsun Moon0e058f22017-04-19 17:00:52 +090059 print("Successfully requested re-installing flow rules.");
60 }
Jian Li5c777c62018-11-11 00:31:20 +090061
62 private void syncRulesBaseForNode(OpenstackNodeAdminService osNodeService,
63 OpenstackNode osNode) {
64 OpenstackNode updated = osNode.updateState(NodeState.INIT);
65 osNodeService.updateNode(updated);
66
Jian Li8b5599b2018-11-19 18:45:46 +090067 boolean result = true;
68 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
69
70 while (osNodeService.node(osNode.hostname()).state() != COMPLETE) {
71
72 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
73
74 try {
75 sleep(SLEEP_MS);
76 } catch (InterruptedException e) {
77 error("Exception caused during node synchronization...");
78 }
79
80 if (osNodeService.node(osNode.hostname()).state() == COMPLETE) {
81 break;
82 } else {
83 osNodeService.updateNode(updated);
84 print("Failed to synchronize flow rules, retrying...");
85 }
86
87 if (waitMs <= 0) {
88 result = false;
89 break;
90 }
Jian Li5c777c62018-11-11 00:31:20 +090091 }
92
Jian Li8b5599b2018-11-19 18:45:46 +090093 if (result) {
94 print("Successfully synchronize flow rules for node {}!", osNode.hostname());
Jian Li5c777c62018-11-11 00:31:20 +090095 } else {
Jian Li8b5599b2018-11-19 18:45:46 +090096 error("Failed to synchronize flow rules for node {}.", osNode.hostname());
Jian Li5c777c62018-11-11 00:31:20 +090097 }
98 }
Hyunsun Moon0e058f22017-04-19 17:00:52 +090099}