blob: c9def057c86c4bb942a3bfa74d6aef07783f5e39 [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;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
23import org.onosproject.net.flow.FlowRuleService;
24import org.onosproject.openstacknetworking.api.Constants;
25
Jian Li167f0c42018-11-18 11:03:51 +090026import static java.lang.Thread.sleep;
27import static java.util.stream.StreamSupport.stream;
Jian Li5ecfd1a2018-12-10 11:41:03 +090028import static org.onosproject.cli.AbstractShellCommand.get;
Jian Li167f0c42018-11-18 11:03:51 +090029
Hyunsun Moon0e058f22017-04-19 17:00:52 +090030/**
31 * Purges all existing network states.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
Hyunsun Moon0e058f22017-04-19 17:00:52 +090034@Command(scope = "onos", name = "openstack-purge-rules",
35 description = "Purges all flow rules installed by OpenStack networking app")
36public class OpenstackPurgeRulesCommand extends AbstractShellCommand {
37
Jian Li167f0c42018-11-18 11:03:51 +090038 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 Moon0e058f22017-04-19 17:00:52 +090041 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070042 protected void doExecute() {
Jian Li5ecfd1a2018-12-10 11:41:03 +090043 FlowRuleService flowRuleService = get(FlowRuleService.class);
44 CoreService coreService = get(CoreService.class);
Hyunsun Moon0e058f22017-04-19 17:00:52 +090045 ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID);
Jian Li167f0c42018-11-18 11:03:51 +090046
Hyunsun Moon0e058f22017-04-19 17:00:52 +090047 if (appId == null) {
48 error("Failed to purge OpenStack networking flow rules.");
49 return;
50 }
Jian Li167f0c42018-11-18 11:03:51 +090051
Hyunsun Moon0e058f22017-04-19 17:00:52 +090052 flowRuleService.removeFlowRulesById(appId);
Jian Li5ecfd1a2018-12-10 11:41:03 +090053 print("Successfully purged flow rules installed by OpenStack networking app.");
Jian Li167f0c42018-11-18 11:03:51 +090054
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 Moon0e058f22017-04-19 17:00:52 +090089 }
90}