blob: 66f572527482a41ab5a9fb7895d03fb00c60055a [file] [log] [blame]
jskimaa851932016-10-27 17:45:30 +09001/*
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
17package org.onosproject.openstacknetworking.cli;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.apache.karaf.shell.commands.Option;
22
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.Host;
25import org.onosproject.net.HostId;
26import org.onosproject.net.host.HostService;
27
28import org.onosproject.openstacknetworking.OpenstackSwitchingService;
29import org.onosproject.openstacknetworking.OpenstackSecurityGroupService;
30import org.onosproject.openstacknetworking.OpenstackRoutingService;
31import org.onosproject.openstacknetworking.OpenstackFloatingIpService;
32
33import 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.")
41public 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}