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 | |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Command; |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.flow.FlowEntry; |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 27 | import org.onosproject.net.flow.FlowRuleService; |
| 28 | import org.onosproject.openstacknetworking.api.Constants; |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 29 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 30 | |
| 31 | import java.util.Set; |
| 32 | import java.util.stream.Collectors; |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 33 | |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 34 | import static java.lang.Thread.sleep; |
| 35 | import static java.util.stream.StreamSupport.stream; |
| 36 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 37 | /** |
| 38 | * Purges all existing network states. |
| 39 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 40 | @Service |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 41 | @Command(scope = "onos", name = "openstack-purge-rules", |
| 42 | description = "Purges all flow rules installed by OpenStack networking app") |
| 43 | public class OpenstackPurgeRulesCommand extends AbstractShellCommand { |
| 44 | |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 45 | private static final long TIMEOUT_MS = 10000; // we wait 10s |
| 46 | private static final long SLEEP_MS = 2000; // we wait 2s for init each node |
| 47 | |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 48 | @Argument(name = "hostname", description = "Hostname", |
| 49 | required = true, multiValued = true) |
| 50 | @Completion(OpenstackComputeNodeCompleter.class) |
| 51 | private String[] hostnames = null; |
| 52 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 53 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 54 | protected void doExecute() { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 55 | FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 56 | CoreService coreService = get(CoreService.class); |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 57 | OpenstackNodeService nodeService = get(OpenstackNodeService.class); |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 58 | ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID); |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 59 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 60 | if (appId == null) { |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 61 | error("Failed to purge OpenStack networking flow rules because of null app ID"); |
| 62 | return; |
| 63 | } |
| 64 | if (hostnames == null) { |
| 65 | error("Failed to purge OpenStack networking flow rules because of null hostnames"); |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 66 | return; |
| 67 | } |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 68 | |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 69 | for (String hostname : hostnames) { |
| 70 | if (nodeService.node(hostname) == null) { |
| 71 | error("Failed to purge OpenStack networking flow rules for %s because of null openstack node", |
| 72 | hostname); |
| 73 | continue; |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 74 | } |
| 75 | |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 76 | DeviceId deviceId = nodeService.node(hostname).intgBridge(); |
| 77 | if (deviceId == null) { |
| 78 | error("Failed to purge OpenStack networking flow rules because of null device ID"); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | removeFlowRulesByDeviceId(appId, flowRuleService, deviceId); |
| 83 | print("Successfully purged flow rules installed by" + |
| 84 | " OpenStack networking app on host %s.", hostname); |
| 85 | |
| 86 | boolean result = true; |
| 87 | long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS; |
| 88 | |
| 89 | // we make sure all flow rules are removed from the store |
| 90 | while (getFlowEntriesByDeviceId(appId, flowRuleService, deviceId).isEmpty()) { |
| 91 | |
| 92 | long waitMs = timeoutExpiredMs - System.currentTimeMillis(); |
| 93 | |
| 94 | try { |
| 95 | sleep(SLEEP_MS); |
| 96 | } catch (InterruptedException e) { |
| 97 | log.error("Exception caused during rule purging..."); |
| 98 | } |
| 99 | |
| 100 | if (getFlowEntriesByDeviceId(appId, flowRuleService, deviceId).isEmpty()) { |
| 101 | break; |
| 102 | } else { |
| 103 | removeFlowRulesByDeviceId(appId, flowRuleService, deviceId); |
| 104 | print("Failed to purging flow rules, retrying rule purging..."); |
| 105 | } |
| 106 | |
| 107 | if (waitMs <= 0) { |
| 108 | result = false; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | if (result) { |
| 113 | print("Successfully purged flow rules for %s!", hostname); |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 114 | } else { |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 115 | error("Failed to purge flow rules for %s.", hostname); |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 116 | } |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 117 | } |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 118 | } |
Jian Li | 2e3d86b | 2020-02-27 13:38:25 +0900 | [diff] [blame] | 119 | |
| 120 | private void removeFlowRulesByDeviceId(ApplicationId appId, |
| 121 | FlowRuleService flowRuleService, |
| 122 | DeviceId deviceId) { |
| 123 | stream(flowRuleService.getFlowEntriesById(appId).spliterator(), false) |
| 124 | .filter(flowEntry -> flowEntry.deviceId().equals(deviceId)) |
| 125 | .forEach(flowRuleService::removeFlowRules); |
| 126 | } |
| 127 | |
| 128 | private Set<FlowEntry> getFlowEntriesByDeviceId(ApplicationId appId, |
| 129 | FlowRuleService flowRuleService, |
| 130 | DeviceId deviceId) { |
| 131 | return stream(flowRuleService.getFlowEntriesById(appId).spliterator(), false) |
| 132 | .filter(flowEntry -> flowEntry.deviceId().equals(deviceId)) |
| 133 | .collect(Collectors.toSet()); |
| 134 | } |
| 135 | } |