blob: 6109a771376493113a7d4675410ebe290af6ef06 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Licd806b02018-07-19 02:39:47 +090022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.openstacknetworking.api.InstancePort;
24import org.onosproject.openstacknetworking.api.InstancePortAdminService;
25
26import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE;
Jian Li67c6b812018-10-08 16:05:53 +090027import static org.onosproject.openstacknetworking.api.InstancePort.State.REMOVE_PENDING;
Jian Licd806b02018-07-19 02:39:47 +090028
29/**
30 * Purges existing instance ports.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Jian Licd806b02018-07-19 02:39:47 +090033@Command(scope = "onos", name = "purge-instance-ports",
34 description = "Purges existing instance ports created by OpenStack networking app")
35public class PurgeInstancePortsCommand extends AbstractShellCommand {
36
37 @Option(name = "-a", aliases = "--all", description = "All of instance ports",
38 required = false, multiValued = false)
39 private boolean isAll = false;
40
41 @Option(name = "-i", aliases = "--inactive",
42 description = "Instance ports in inactive state",
43 required = false, multiValued = false)
44 private boolean isInactive = false;
45
Jian Li67c6b812018-10-08 16:05:53 +090046 @Option(name = "-p", aliases = "--pending",
47 description = "Instance ports in pending removal state",
48 required = false, multiValued = false)
49 private boolean isPending = false;
50
Jian Licd806b02018-07-19 02:39:47 +090051 @Argument(index = 0, name = "portIds", description = "Instance Port IDs",
52 required = false, multiValued = true)
53 private String[] portIds = null;
54
55 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070056 protected void doExecute() {
Jian Licd806b02018-07-19 02:39:47 +090057 InstancePortAdminService service = get(InstancePortAdminService.class);
58
Jian Li67c6b812018-10-08 16:05:53 +090059 if ((!isAll && !isInactive && !isPending && portIds == null) ||
60 (isAll && isInactive && isPending) ||
61 (isInactive && isPending && portIds != null) ||
62 (portIds != null && isAll && isPending) ||
63 (isAll && isInactive && portIds != null)) {
64 print("Please specify one of portIds, --all or --inactive or --pending options.");
Jian Licd806b02018-07-19 02:39:47 +090065 return;
66 }
67
68 if (isAll) {
69 portIds = service.instancePorts().stream()
70 .map(InstancePort::portId).toArray(String[]::new);
71 } else if (isInactive) {
72 portIds = service.instancePorts().stream()
73 .filter(p -> p.state() == INACTIVE)
74 .map(InstancePort::portId).toArray(String[]::new);
Jian Li67c6b812018-10-08 16:05:53 +090075 } else if (isPending) {
76 portIds = service.instancePorts().stream()
77 .filter(p -> p.state() == REMOVE_PENDING)
78 .map(InstancePort::portId).toArray(String[]::new);
Jian Licd806b02018-07-19 02:39:47 +090079 }
80
81 for (String portId : portIds) {
82 service.removeInstancePort(portId);
83 print("Instance port %s has been removed!", portId);
84 }
85 print("Done.");
86 }
87}