blob: 477ec779a29278ea0339d676a169bae0b0aaebc2 [file] [log] [blame]
Jian Li3d1111e2019-02-22 02:02:13 +09001/*
2 * Copyright 2019-present Open Networking Foundation
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 */
16package org.onosproject.k8snetworking.cli;
17
18import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.lifecycle.Service;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.core.CoreService;
Jian Li4a7ce672019-04-09 15:20:25 +090023import org.onosproject.k8snode.api.K8sNode;
24import org.onosproject.k8snode.api.K8sNodeService;
Jian Li3d1111e2019-02-22 02:02:13 +090025import org.onosproject.net.flow.FlowRuleService;
Jian Li4a7ce672019-04-09 15:20:25 +090026import org.onosproject.net.group.Group;
27import org.onosproject.net.group.GroupService;
Jian Li3d1111e2019-02-22 02:02:13 +090028
29import static java.lang.Thread.sleep;
30import static java.util.stream.StreamSupport.stream;
31import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
32
33/**
34 * Purges all existing flow rules installed for kubernetes networks.
35 */
36@Service
37@Command(scope = "onos", name = "k8s-purge-rules",
38 description = "Purges all flow rules installed by kubernetes networking app")
39public class K8sPurgeRulesCommand extends AbstractShellCommand {
40
41 private static final long TIMEOUT_MS = 10000; // we wait 10s
42 private static final long SLEEP_MS = 2000; // we wait 2s for init each node
43
44 @Override
45 protected void doExecute() {
46 FlowRuleService flowRuleService = get(FlowRuleService.class);
Jian Li4a7ce672019-04-09 15:20:25 +090047 GroupService groupService = get(GroupService.class);
Jian Li3d1111e2019-02-22 02:02:13 +090048 CoreService coreService = get(CoreService.class);
Jian Li4a7ce672019-04-09 15:20:25 +090049 K8sNodeService k8sNodeService = get(K8sNodeService.class);
Jian Li3d1111e2019-02-22 02:02:13 +090050 ApplicationId appId = coreService.getAppId(K8S_NETWORKING_APP_ID);
51
52 if (appId == null) {
53 error("Failed to purge kubernetes networking flow rules.");
54 return;
55 }
56
57 flowRuleService.removeFlowRulesById(appId);
58 print("Successfully purged flow rules installed by kubernetes networking app.");
59
60 boolean result = true;
61 long timeoutExpiredMs = System.currentTimeMillis() + TIMEOUT_MS;
62
63 // we make sure all flow rules are removed from the store
64 while (stream(flowRuleService.getFlowEntriesById(appId)
65 .spliterator(), false).count() > 0) {
66
67 long waitMs = timeoutExpiredMs - System.currentTimeMillis();
68
69 try {
70 sleep(SLEEP_MS);
71 } catch (InterruptedException e) {
72 log.error("Exception caused during rule purging...");
73 }
74
75 if (stream(flowRuleService.getFlowEntriesById(appId)
76 .spliterator(), false).count() == 0) {
77 break;
78 } else {
79 flowRuleService.removeFlowRulesById(appId);
80 print("Failed to purging flow rules, retrying rule purging...");
81 }
82
83 if (waitMs <= 0) {
84 result = false;
85 break;
86 }
87 }
88
Jian Li4a7ce672019-04-09 15:20:25 +090089 for (K8sNode node : k8sNodeService.completeNodes()) {
90 for (Group group : groupService.getGroups(node.intgBridge(), appId)) {
91 groupService.removeGroup(node.intgBridge(), group.appCookie(), appId);
92 }
93 }
94
Jian Li3d1111e2019-02-22 02:02:13 +090095 if (result) {
96 print("Successfully purged flow rules!");
97 } else {
98 error("Failed to purge flow rules.");
99 }
100 }
101}