Jian Li | cd806b0 | 2018-07-19 02:39:47 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.apache.karaf.shell.commands.Option; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 23 | import org.onosproject.openstacknetworking.api.InstancePortAdminService; |
| 24 | |
| 25 | import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE; |
| 26 | |
| 27 | /** |
| 28 | * Purges existing instance ports. |
| 29 | */ |
| 30 | @Command(scope = "onos", name = "purge-instance-ports", |
| 31 | description = "Purges existing instance ports created by OpenStack networking app") |
| 32 | public class PurgeInstancePortsCommand extends AbstractShellCommand { |
| 33 | |
| 34 | @Option(name = "-a", aliases = "--all", description = "All of instance ports", |
| 35 | required = false, multiValued = false) |
| 36 | private boolean isAll = false; |
| 37 | |
| 38 | @Option(name = "-i", aliases = "--inactive", |
| 39 | description = "Instance ports in inactive state", |
| 40 | required = false, multiValued = false) |
| 41 | private boolean isInactive = false; |
| 42 | |
| 43 | @Argument(index = 0, name = "portIds", description = "Instance Port IDs", |
| 44 | required = false, multiValued = true) |
| 45 | private String[] portIds = null; |
| 46 | |
| 47 | @Override |
| 48 | protected void execute() { |
| 49 | InstancePortAdminService service = get(InstancePortAdminService.class); |
| 50 | |
| 51 | if ((!isAll && !isInactive && portIds == null) || |
| 52 | (isAll && isInactive) || |
| 53 | (isInactive && portIds != null) || |
| 54 | (portIds != null && isAll)) { |
| 55 | print("Please specify one of portIds, --all, and --inactive options."); |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (isAll) { |
| 60 | portIds = service.instancePorts().stream() |
| 61 | .map(InstancePort::portId).toArray(String[]::new); |
| 62 | } else if (isInactive) { |
| 63 | portIds = service.instancePorts().stream() |
| 64 | .filter(p -> p.state() == INACTIVE) |
| 65 | .map(InstancePort::portId).toArray(String[]::new); |
| 66 | } |
| 67 | |
| 68 | for (String portId : portIds) { |
| 69 | service.removeInstancePort(portId); |
| 70 | print("Instance port %s has been removed!", portId); |
| 71 | } |
| 72 | print("Done."); |
| 73 | } |
| 74 | } |