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; |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 24 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 25 | import static java.lang.Thread.sleep; |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 26 | import static org.onosproject.cli.AbstractShellCommand.get; |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 27 | import static org.onosproject.openstacknode.api.NodeState.COMPLETE; |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 28 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE; |
| 29 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY; |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 30 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 31 | /** |
| 32 | * Re-installs flow rules for OpenStack networking. |
| 33 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 34 | @Service |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 35 | @Command(scope = "onos", name = "openstack-sync-rules", |
| 36 | description = "Re-installs flow rules for OpenStack networking") |
| 37 | public class OpenstackSyncRulesCommand extends AbstractShellCommand { |
| 38 | |
Jian Li | f7934d5 | 2018-07-10 16:27:02 +0900 | [diff] [blame] | 39 | private static final long SLEEP_MS = 3000; // we wait 3s for init each node |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 40 | private static final long TIMEOUT_MS = 10000; // we wait 10s |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 41 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 42 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 43 | protected void doExecute() { |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 44 | // All handlers in this application reacts the node complete event and |
| 45 | // tries to re-configure flow rules for the complete node. |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 46 | OpenstackNodeAdminService osNodeService = get(OpenstackNodeAdminService.class); |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 47 | if (osNodeService == null) { |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 48 | error("Failed to re-install flow rules for OpenStack networking."); |
| 49 | return; |
| 50 | } |
Jian Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 51 | |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 52 | // 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 Li | 88ae51e | 2018-07-10 02:23:35 +0900 | [diff] [blame] | 58 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 59 | print("Successfully requested re-installing flow rules."); |
| 60 | } |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 61 | |
| 62 | private void syncRulesBaseForNode(OpenstackNodeAdminService osNodeService, |
| 63 | OpenstackNode osNode) { |
| 64 | OpenstackNode updated = osNode.updateState(NodeState.INIT); |
| 65 | osNodeService.updateNode(updated); |
| 66 | |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 67 | 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 Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 91 | } |
| 92 | |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 93 | if (result) { |
| 94 | print("Successfully synchronize flow rules for node {}!", osNode.hostname()); |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 95 | } else { |
Jian Li | 8b5599b | 2018-11-19 18:45:46 +0900 | [diff] [blame] | 96 | error("Failed to synchronize flow rules for node {}.", osNode.hostname()); |
Jian Li | 5c777c6 | 2018-11-11 00:31:20 +0900 | [diff] [blame] | 97 | } |
| 98 | } |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 99 | } |