blob: 08ccd45e375673b53a72643ef3568c7774bb4a9a [file] [log] [blame]
Jian Licd806b02018-07-19 02:39:47 +09001/*
2 * Copyright 2018-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.openstacknetworking.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.openstacknetworking.api.InstancePort;
23import org.onosproject.openstacknetworking.api.InstancePortAdminService;
24
25import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE;
Jian Li67c6b812018-10-08 16:05:53 +090026import static org.onosproject.openstacknetworking.api.InstancePort.State.REMOVE_PENDING;
Jian Licd806b02018-07-19 02:39:47 +090027
28/**
29 * Purges existing instance ports.
30 */
31@Command(scope = "onos", name = "purge-instance-ports",
32 description = "Purges existing instance ports created by OpenStack networking app")
33public class PurgeInstancePortsCommand extends AbstractShellCommand {
34
35 @Option(name = "-a", aliases = "--all", description = "All of instance ports",
36 required = false, multiValued = false)
37 private boolean isAll = false;
38
39 @Option(name = "-i", aliases = "--inactive",
40 description = "Instance ports in inactive state",
41 required = false, multiValued = false)
42 private boolean isInactive = false;
43
Jian Li67c6b812018-10-08 16:05:53 +090044 @Option(name = "-p", aliases = "--pending",
45 description = "Instance ports in pending removal state",
46 required = false, multiValued = false)
47 private boolean isPending = false;
48
Jian Licd806b02018-07-19 02:39:47 +090049 @Argument(index = 0, name = "portIds", description = "Instance Port IDs",
50 required = false, multiValued = true)
51 private String[] portIds = null;
52
53 @Override
54 protected void execute() {
55 InstancePortAdminService service = get(InstancePortAdminService.class);
56
Jian Li67c6b812018-10-08 16:05:53 +090057 if ((!isAll && !isInactive && !isPending && portIds == null) ||
58 (isAll && isInactive && isPending) ||
59 (isInactive && isPending && portIds != null) ||
60 (portIds != null && isAll && isPending) ||
61 (isAll && isInactive && portIds != null)) {
62 print("Please specify one of portIds, --all or --inactive or --pending options.");
Jian Licd806b02018-07-19 02:39:47 +090063 return;
64 }
65
66 if (isAll) {
67 portIds = service.instancePorts().stream()
68 .map(InstancePort::portId).toArray(String[]::new);
69 } else if (isInactive) {
70 portIds = service.instancePorts().stream()
71 .filter(p -> p.state() == INACTIVE)
72 .map(InstancePort::portId).toArray(String[]::new);
Jian Li67c6b812018-10-08 16:05:53 +090073 } else if (isPending) {
74 portIds = service.instancePorts().stream()
75 .filter(p -> p.state() == REMOVE_PENDING)
76 .map(InstancePort::portId).toArray(String[]::new);
Jian Licd806b02018-07-19 02:39:47 +090077 }
78
79 for (String portId : portIds) {
80 service.removeInstancePort(portId);
81 print("Instance port %s has been removed!", portId);
82 }
83 print("Done.");
84 }
85}