blob: e12c3edaf0ed14886743b8610fd22690e509e771 [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;
28
Hyunsun Moon0e058f22017-04-19 17:00:52 +090029/**
30 * Purges all existing network states.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Hyunsun Moon0e058f22017-04-19 17:00:52 +090033@Command(scope = "onos", name = "openstack-purge-rules",
34 description = "Purges all flow rules installed by OpenStack networking app")
35public class OpenstackPurgeRulesCommand extends AbstractShellCommand {
36
Jian Li167f0c42018-11-18 11:03:51 +090037 private static final long TIMEOUT_MS = 10000; // we wait 10s
38 private static final long SLEEP_MS = 2000; // we wait 2s for init each node
39
Hyunsun Moon0e058f22017-04-19 17:00:52 +090040 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070041 protected void doExecute() {
Hyunsun Moon0e058f22017-04-19 17:00:52 +090042 FlowRuleService flowRuleService = AbstractShellCommand.get(FlowRuleService.class);
43 CoreService coreService = AbstractShellCommand.get(CoreService.class);
44 ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID);
Jian Li167f0c42018-11-18 11:03:51 +090045
Hyunsun Moon0e058f22017-04-19 17:00:52 +090046 if (appId == null) {
47 error("Failed to purge OpenStack networking flow rules.");
48 return;
49 }
Jian Li167f0c42018-11-18 11:03:51 +090050
Hyunsun Moon0e058f22017-04-19 17:00:52 +090051 flowRuleService.removeFlowRulesById(appId);
52 print("Successfully purged flow rules installed by OpenStack networking application.");
Jian Li167f0c42018-11-18 11:03:51 +090053
54 boolean result = true;
55 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
56
57 // we make sure all flow rules are removed from the store
58 while (stream(flowRuleService.getFlowEntriesById(appId)
59 .spliterator(), false).count() > 0) {
60
61 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
62
63 try {
64 sleep(SLEEP_MS);
65 } catch (InterruptedException e) {
66 log.error("Exception caused during rule purging...");
67 }
68
69 if (stream(flowRuleService.getFlowEntriesById(appId)
70 .spliterator(), false).count() == 0) {
71 break;
72 } else {
73 flowRuleService.removeFlowRulesById(appId);
74 print("Failed to purging flow rules, retrying rule purging...");
75 }
76
77 if (waitMs <= 0) {
78 result = false;
79 break;
80 }
81 }
82
83 if (result) {
84 print("Successfully purged flow rules!");
85 } else {
86 error("Failed to purge flow rules.");
87 }
Hyunsun Moon0e058f22017-04-19 17:00:52 +090088 }
89}