blob: 1db9704b8dc751035e2f19bc5d927f6207e73ac2 [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
Jian Lib50758f2019-01-31 18:24:38 +090041 private static final String SUCCESS_MSG = "Successfully synchronize flow rules for node %s!";
42 private static final String FAIL_MSG = "Failed to synchronize flow rules for node %s.";
43
Hyunsun Moon0e058f22017-04-19 17:00:52 +090044 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070045 protected void doExecute() {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090046 // All handlers in this application reacts the node complete event and
47 // tries to re-configure flow rules for the complete node.
Jian Li5ecfd1a2018-12-10 11:41:03 +090048 OpenstackNodeAdminService osNodeService = get(OpenstackNodeAdminService.class);
Hyunsun Moon0d457362017-06-27 17:19:41 +090049 if (osNodeService == null) {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090050 error("Failed to re-install flow rules for OpenStack networking.");
51 return;
52 }
Jian Li88ae51e2018-07-10 02:23:35 +090053
Jian Li5c777c62018-11-11 00:31:20 +090054 // we first initialize the COMPUTE node, in order to feed all instance ports
55 // by referring to ports' information obtained from neutron server
56 osNodeService.completeNodes(COMPUTE).forEach(osNode ->
57 syncRulesBaseForNode(osNodeService, osNode));
58 osNodeService.completeNodes(GATEWAY).forEach(osNode ->
59 syncRulesBaseForNode(osNodeService, osNode));
Jian Li88ae51e2018-07-10 02:23:35 +090060
Hyunsun Moon0e058f22017-04-19 17:00:52 +090061 print("Successfully requested re-installing flow rules.");
62 }
Jian Li5c777c62018-11-11 00:31:20 +090063
64 private void syncRulesBaseForNode(OpenstackNodeAdminService osNodeService,
65 OpenstackNode osNode) {
66 OpenstackNode updated = osNode.updateState(NodeState.INIT);
67 osNodeService.updateNode(updated);
68
Jian Li8b5599b2018-11-19 18:45:46 +090069 boolean result = true;
70 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
71
72 while (osNodeService.node(osNode.hostname()).state() != COMPLETE) {
73
74 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
75
76 try {
77 sleep(SLEEP_MS);
78 } catch (InterruptedException e) {
79 error("Exception caused during node synchronization...");
80 }
81
82 if (osNodeService.node(osNode.hostname()).state() == COMPLETE) {
83 break;
84 } else {
85 osNodeService.updateNode(updated);
86 print("Failed to synchronize flow rules, retrying...");
87 }
88
89 if (waitMs <= 0) {
90 result = false;
91 break;
92 }
Jian Li5c777c62018-11-11 00:31:20 +090093 }
94
Jian Li8b5599b2018-11-19 18:45:46 +090095 if (result) {
Jian Lib50758f2019-01-31 18:24:38 +090096 print(SUCCESS_MSG, osNode.hostname());
Jian Li5c777c62018-11-11 00:31:20 +090097 } else {
Jian Lib50758f2019-01-31 18:24:38 +090098 error(FAIL_MSG, osNode.hostname());
Jian Li5c777c62018-11-11 00:31:20 +090099 }
100 }
Hyunsun Moon0e058f22017-04-19 17:00:52 +0900101}