blob: 1658a35053788364f00f718764e8acc84ca6f466 [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;
23import org.onosproject.net.flow.FlowRuleService;
24
25import static java.lang.Thread.sleep;
26import static java.util.stream.StreamSupport.stream;
27import static org.onosproject.k8snetworking.api.Constants.K8S_NETWORKING_APP_ID;
28
29/**
30 * Purges all existing flow rules installed for kubernetes networks.
31 */
32@Service
33@Command(scope = "onos", name = "k8s-purge-rules",
34 description = "Purges all flow rules installed by kubernetes networking app")
35public class K8sPurgeRulesCommand extends AbstractShellCommand {
36
37 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
40 @Override
41 protected void doExecute() {
42 FlowRuleService flowRuleService = get(FlowRuleService.class);
43 CoreService coreService = get(CoreService.class);
44 ApplicationId appId = coreService.getAppId(K8S_NETWORKING_APP_ID);
45
46 if (appId == null) {
47 error("Failed to purge kubernetes networking flow rules.");
48 return;
49 }
50
51 flowRuleService.removeFlowRulesById(appId);
52 print("Successfully purged flow rules installed by kubernetes networking app.");
53
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 }
88 }
89}