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; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
| 23 | import org.onosproject.net.flow.FlowRuleService; |
| 24 | import org.onosproject.openstacknetworking.api.Constants; |
| 25 | |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 26 | import static java.lang.Thread.sleep; |
| 27 | import static java.util.stream.StreamSupport.stream; |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 28 | import static org.onosproject.cli.AbstractShellCommand.get; |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 29 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 30 | /** |
| 31 | * Purges all existing network states. |
| 32 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 33 | @Service |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 34 | @Command(scope = "onos", name = "openstack-purge-rules", |
| 35 | description = "Purges all flow rules installed by OpenStack networking app") |
| 36 | public class OpenstackPurgeRulesCommand extends AbstractShellCommand { |
| 37 | |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 38 | private static final long TIMEOUT_MS = 10000; // we wait 10s |
| 39 | private static final long SLEEP_MS = 2000; // we wait 2s for init each node |
| 40 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 41 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 42 | protected void doExecute() { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 43 | FlowRuleService flowRuleService = get(FlowRuleService.class); |
| 44 | CoreService coreService = get(CoreService.class); |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 45 | ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID); |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 46 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 47 | if (appId == null) { |
| 48 | error("Failed to purge OpenStack networking flow rules."); |
| 49 | return; |
| 50 | } |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 51 | |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 52 | flowRuleService.removeFlowRulesById(appId); |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame^] | 53 | print("Successfully purged flow rules installed by OpenStack networking app."); |
Jian Li | 167f0c4 | 2018-11-18 11:03:51 +0900 | [diff] [blame] | 54 | |
| 55 | boolean result = true; |
| 56 | long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS; |
| 57 | |
| 58 | // we make sure all flow rules are removed from the store |
| 59 | while (stream(flowRuleService.getFlowEntriesById(appId) |
| 60 | .spliterator(), false).count() > 0) { |
| 61 | |
| 62 | long waitMs = timeoutExpiredMs - System.currentTimeMillis(); |
| 63 | |
| 64 | try { |
| 65 | sleep(SLEEP_MS); |
| 66 | } catch (InterruptedException e) { |
| 67 | log.error("Exception caused during rule purging..."); |
| 68 | } |
| 69 | |
| 70 | if (stream(flowRuleService.getFlowEntriesById(appId) |
| 71 | .spliterator(), false).count() == 0) { |
| 72 | break; |
| 73 | } else { |
| 74 | flowRuleService.removeFlowRulesById(appId); |
| 75 | print("Failed to purging flow rules, retrying rule purging..."); |
| 76 | } |
| 77 | |
| 78 | if (waitMs <= 0) { |
| 79 | result = false; |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (result) { |
| 85 | print("Successfully purged flow rules!"); |
| 86 | } else { |
| 87 | error("Failed to purge flow rules."); |
| 88 | } |
Hyunsun Moon | 0e058f2 | 2017-04-19 17:00:52 +0900 | [diff] [blame] | 89 | } |
| 90 | } |