Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snetworking.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.api.action.Command; |
| 19 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.core.CoreService; |
Jian Li | 4a7ce67 | 2019-04-09 15:20:25 +0900 | [diff] [blame] | 23 | import org.onosproject.k8snode.api.K8sNode; |
| 24 | import org.onosproject.k8snode.api.K8sNodeService; |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 25 | import org.onosproject.net.flow.FlowRuleService; |
Jian Li | 4a7ce67 | 2019-04-09 15:20:25 +0900 | [diff] [blame] | 26 | import org.onosproject.net.group.Group; |
| 27 | import org.onosproject.net.group.GroupService; |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 28 | |
| 29 | import static java.lang.Thread.sleep; |
| 30 | import static java.util.stream.StreamSupport.stream; |
| 31 | import 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") |
| 39 | public 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 Li | 4a7ce67 | 2019-04-09 15:20:25 +0900 | [diff] [blame] | 47 | GroupService groupService = get(GroupService.class); |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 48 | CoreService coreService = get(CoreService.class); |
Jian Li | 4a7ce67 | 2019-04-09 15:20:25 +0900 | [diff] [blame] | 49 | K8sNodeService k8sNodeService = get(K8sNodeService.class); |
Jian Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 50 | 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 Li | 4a7ce67 | 2019-04-09 15:20:25 +0900 | [diff] [blame] | 89 | 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 Li | 3d1111e | 2019-02-22 02:02:13 +0900 | [diff] [blame] | 95 | if (result) { |
| 96 | print("Successfully purged flow rules!"); |
| 97 | } else { |
| 98 | error("Failed to purge flow rules."); |
| 99 | } |
| 100 | } |
| 101 | } |