jskim | aa85193 | 2016-10-27 17:45:30 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.openstacknetworking.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.Host; |
| 25 | import org.onosproject.net.HostId; |
| 26 | import org.onosproject.net.host.HostService; |
| 27 | |
| 28 | import org.onosproject.openstacknetworking.OpenstackSwitchingService; |
| 29 | import org.onosproject.openstacknetworking.OpenstackSecurityGroupService; |
| 30 | import org.onosproject.openstacknetworking.OpenstackRoutingService; |
| 31 | import org.onosproject.openstacknetworking.OpenstackFloatingIpService; |
| 32 | |
| 33 | import static org.onosproject.openstacknetworking.Constants.*; |
| 34 | |
| 35 | /** |
| 36 | * Purge Flows of OpenstackInstance Data Plane. |
| 37 | */ |
| 38 | |
| 39 | @Command(scope = "onos", name = "openstack-purge-flows", |
| 40 | description = "Purge data plane flows of existing VM.") |
| 41 | public class OpenstackInstancePurgeFlowsCommand extends AbstractShellCommand { |
| 42 | |
| 43 | @Option(name = "-a", aliases = "--all", |
| 44 | description = "HostIDs are all existing VM", |
| 45 | required = false, multiValued = false) |
| 46 | private Boolean allhost = false; |
| 47 | |
| 48 | @Argument(index = 0, name = "hostId", description = "HostID(s)", |
| 49 | required = false, multiValued = true) |
| 50 | private String[] hostids = null; |
| 51 | |
| 52 | @Override |
| 53 | protected void execute() { |
| 54 | HostService hostService = AbstractShellCommand.get(HostService.class); |
| 55 | |
| 56 | OpenstackSwitchingService switchingService = getService(OpenstackSwitchingService.class); |
| 57 | OpenstackSecurityGroupService sgService = getService(OpenstackSecurityGroupService.class); |
| 58 | OpenstackRoutingService routingService = getService(OpenstackRoutingService.class); |
| 59 | OpenstackFloatingIpService floatingIpService = getService(OpenstackFloatingIpService.class); |
| 60 | |
| 61 | if (allhost) { |
| 62 | switchingService.purgeVmFlow(null); |
| 63 | sgService.purgeVmFlow(null); |
| 64 | routingService.purgeVmFlow(null); |
| 65 | floatingIpService.purgeVmFlow(null); |
| 66 | |
| 67 | hostService.getHosts().forEach(host -> { |
| 68 | printHost(host); |
| 69 | }); |
| 70 | } else if (hostids != null) { |
| 71 | for (String hostid : hostids) { |
| 72 | Host host = hostService.getHost(HostId.hostId(hostid)); |
| 73 | if (host == null) { |
| 74 | continue; |
| 75 | } |
| 76 | switchingService.purgeVmFlow(host); |
| 77 | sgService.purgeVmFlow(host); |
| 78 | routingService.purgeVmFlow(host); |
| 79 | floatingIpService.purgeVmFlow(host); |
| 80 | printHost(host); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private void printHost(Host host) { |
| 86 | print("Purge data plane flows of VM(hostid=%s, vni=%s, ip=%s, mac=%s).", |
| 87 | host.id(), host.annotations().value(VXLAN_ID), |
| 88 | host.ipAddresses().stream().findFirst().get().getIp4Address(), host.mac()); |
| 89 | } |
| 90 | } |