blob: 431472c2a53384e5a1448962a3498111576a6393 [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;
27
28/**
29 * Purges existing instance ports.
30 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070031@Service
Jian Licd806b02018-07-19 02:39:47 +090032@Command(scope = "onos", name = "purge-instance-ports",
33 description = "Purges existing instance ports created by OpenStack networking app")
34public class PurgeInstancePortsCommand extends AbstractShellCommand {
35
36 @Option(name = "-a", aliases = "--all", description = "All of instance ports",
37 required = false, multiValued = false)
38 private boolean isAll = false;
39
40 @Option(name = "-i", aliases = "--inactive",
41 description = "Instance ports in inactive state",
42 required = false, multiValued = false)
43 private boolean isInactive = false;
44
45 @Argument(index = 0, name = "portIds", description = "Instance Port IDs",
46 required = false, multiValued = true)
47 private String[] portIds = null;
48
49 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050 protected void doExecute() {
Jian Licd806b02018-07-19 02:39:47 +090051 InstancePortAdminService service = get(InstancePortAdminService.class);
52
53 if ((!isAll && !isInactive && portIds == null) ||
54 (isAll && isInactive) ||
55 (isInactive && portIds != null) ||
56 (portIds != null && isAll)) {
57 print("Please specify one of portIds, --all, and --inactive options.");
58 return;
59 }
60
61 if (isAll) {
62 portIds = service.instancePorts().stream()
63 .map(InstancePort::portId).toArray(String[]::new);
64 } else if (isInactive) {
65 portIds = service.instancePorts().stream()
66 .filter(p -> p.state() == INACTIVE)
67 .map(InstancePort::portId).toArray(String[]::new);
68 }
69
70 for (String portId : portIds) {
71 service.removeInstancePort(portId);
72 print("Instance port %s has been removed!", portId);
73 }
74 print("Done.");
75 }
76}