Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 20 | import org.onosproject.cli.AbstractShellCommand; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 21 | import org.onosproject.openstacknode.api.NodeState; |
| 22 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 23 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 24 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 25 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 26 | import static java.lang.Thread.sleep; |
| 27 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 28 | /** |
| 29 | * Re-installs flow rules for OpenStack networking. |
| 30 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 31 | @Service |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 32 | @Command(scope = "onos", name = "openstack-sync-rules", |
| 33 | description = "Re-installs flow rules for OpenStack networking") |
| 34 | public class OpenstackSyncRulesCommand extends AbstractShellCommand { |
| 35 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 36 | private static final long SLEEP_MS = 3000; // we wait 3s for init each node |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 37 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 38 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 39 | protected void doExecute() { |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 40 | // All handlers in this application reacts the node complete event and |
| 41 | // tries to re-configure flow rules for the complete node. |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 42 | OpenstackNodeService osNodeService = AbstractShellCommand.get(OpenstackNodeService.class); |
| 43 | OpenstackNodeAdminService osNodeAdminService = AbstractShellCommand.get(OpenstackNodeAdminService.class); |
| 44 | if (osNodeService == null) { |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 45 | error("Failed to re-install flow rules for OpenStack networking."); |
| 46 | return; |
| 47 | } |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 48 | osNodeService.completeNodes().forEach(osNode -> { |
| 49 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 50 | osNodeAdminService.updateNode(updated); |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 51 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 52 | try { |
| 53 | sleep(SLEEP_MS); |
| 54 | } catch (InterruptedException e) { |
| 55 | log.error("Exception caused during node synchronization..."); |
| 56 | } |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 57 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 58 | if (osNodeService.node(osNode.hostname()).state() == NodeState.COMPLETE) { |
| 59 | print("Finished sync rules for node %s", osNode.hostname()); |
| 60 | } else { |
| 61 | error("Failed to sync rules for node %s", osNode.hostname()); |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 62 | } |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 63 | }); |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 64 | print("Successfully requested re-installing flow rules."); |
| 65 | } |
| 66 | } |