blob: 589ff2ab7d6ae3104f0d0f5ad52169c8e1bb8949 [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
Jian Li2e3d86b2020-02-27 13:38:25 +090018import org.apache.karaf.shell.api.action.Argument;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Command;
Jian Li2e3d86b2020-02-27 13:38:25 +090020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Hyunsun Moon0e058f22017-04-19 17:00:52 +090022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
Jian Li2e3d86b2020-02-27 13:38:25 +090025import org.onosproject.net.DeviceId;
26import org.onosproject.net.flow.FlowEntry;
Hyunsun Moon0e058f22017-04-19 17:00:52 +090027import org.onosproject.net.flow.FlowRuleService;
28import org.onosproject.openstacknetworking.api.Constants;
Jian Li2e3d86b2020-02-27 13:38:25 +090029import org.onosproject.openstacknode.api.OpenstackNodeService;
30
31import java.util.Set;
32import java.util.stream.Collectors;
Hyunsun Moon0e058f22017-04-19 17:00:52 +090033
Jian Li167f0c42018-11-18 11:03:51 +090034import static java.lang.Thread.sleep;
35import static java.util.stream.StreamSupport.stream;
36
Hyunsun Moon0e058f22017-04-19 17:00:52 +090037/**
38 * Purges all existing network states.
39 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070040@Service
Hyunsun Moon0e058f22017-04-19 17:00:52 +090041@Command(scope = "onos", name = "openstack-purge-rules",
42 description = "Purges all flow rules installed by OpenStack networking app")
43public class OpenstackPurgeRulesCommand extends AbstractShellCommand {
44
Jian Li167f0c42018-11-18 11:03:51 +090045 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 Li2e3d86b2020-02-27 13:38:25 +090048 @Argument(name = "hostname", description = "Hostname",
49 required = true, multiValued = true)
50 @Completion(OpenstackComputeNodeCompleter.class)
51 private String[] hostnames = null;
52
Hyunsun Moon0e058f22017-04-19 17:00:52 +090053 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070054 protected void doExecute() {
Jian Li5ecfd1a2018-12-10 11:41:03 +090055 FlowRuleService flowRuleService = get(FlowRuleService.class);
56 CoreService coreService = get(CoreService.class);
Jian Li2e3d86b2020-02-27 13:38:25 +090057 OpenstackNodeService nodeService = get(OpenstackNodeService.class);
Hyunsun Moon0e058f22017-04-19 17:00:52 +090058 ApplicationId appId = coreService.getAppId(Constants.OPENSTACK_NETWORKING_APP_ID);
Jian Li167f0c42018-11-18 11:03:51 +090059
Hyunsun Moon0e058f22017-04-19 17:00:52 +090060 if (appId == null) {
Jian Li2e3d86b2020-02-27 13:38:25 +090061 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 Moon0e058f22017-04-19 17:00:52 +090066 return;
67 }
Jian Li167f0c42018-11-18 11:03:51 +090068
Jian Li2e3d86b2020-02-27 13:38:25 +090069 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 Li167f0c42018-11-18 11:03:51 +090074 }
75
Jian Li2e3d86b2020-02-27 13:38:25 +090076 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 Li167f0c42018-11-18 11:03:51 +0900114 } else {
Jian Li2e3d86b2020-02-27 13:38:25 +0900115 error("Failed to purge flow rules for %s.", hostname);
Jian Li167f0c42018-11-18 11:03:51 +0900116 }
Jian Li167f0c42018-11-18 11:03:51 +0900117 }
Hyunsun Moon0e058f22017-04-19 17:00:52 +0900118 }
Jian Li2e3d86b2020-02-27 13:38:25 +0900119
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}