blob: 214cfe7ed6ae5a8554e466fe931bd29669ed2f1b [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 Li5c777c62018-11-11 00:31:20 +090026import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
27import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
Jian Lif7934d52018-07-10 16:27:02 +090028
Hyunsun Moon0e058f22017-04-19 17:00:52 +090029/**
30 * Re-installs flow rules for OpenStack networking.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Hyunsun Moon0e058f22017-04-19 17:00:52 +090033@Command(scope = "onos", name = "openstack-sync-rules",
34 description = "Re-installs flow rules for OpenStack networking")
35public class OpenstackSyncRulesCommand extends AbstractShellCommand {
36
Jian Lif7934d52018-07-10 16:27:02 +090037 private static final long SLEEP_MS = 3000; // we wait 3s for init each node
Jian Li88ae51e2018-07-10 02:23:35 +090038
Hyunsun Moon0e058f22017-04-19 17:00:52 +090039 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070040 protected void doExecute() {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090041 // All handlers in this application reacts the node complete event and
42 // tries to re-configure flow rules for the complete node.
Jian Li5c777c62018-11-11 00:31:20 +090043 OpenstackNodeAdminService osNodeService = AbstractShellCommand.get(OpenstackNodeAdminService.class);
Hyunsun Moon0d457362017-06-27 17:19:41 +090044 if (osNodeService == null) {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090045 error("Failed to re-install flow rules for OpenStack networking.");
46 return;
47 }
Jian Li88ae51e2018-07-10 02:23:35 +090048
Jian Li5c777c62018-11-11 00:31:20 +090049 // we first initialize the COMPUTE node, in order to feed all instance ports
50 // by referring to ports' information obtained from neutron server
51 osNodeService.completeNodes(COMPUTE).forEach(osNode ->
52 syncRulesBaseForNode(osNodeService, osNode));
53 osNodeService.completeNodes(GATEWAY).forEach(osNode ->
54 syncRulesBaseForNode(osNodeService, osNode));
Jian Li88ae51e2018-07-10 02:23:35 +090055
Hyunsun Moon0e058f22017-04-19 17:00:52 +090056 print("Successfully requested re-installing flow rules.");
57 }
Jian Li5c777c62018-11-11 00:31:20 +090058
59 private void syncRulesBaseForNode(OpenstackNodeAdminService osNodeService,
60 OpenstackNode osNode) {
61 OpenstackNode updated = osNode.updateState(NodeState.INIT);
62 osNodeService.updateNode(updated);
63
64 try {
65 sleep(SLEEP_MS);
66 } catch (InterruptedException e) {
67 log.error("Exception caused during node synchronization...");
68 }
69
70 if (osNodeService.node(osNode.hostname()).state() == NodeState.COMPLETE) {
71 print("Finished sync rules for node %s", osNode.hostname());
72 } else {
73 error("Failed to sync rules for node %s", osNode.hostname());
74 }
75 }
Hyunsun Moon0e058f22017-04-19 17:00:52 +090076}