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 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jian Li | cd806b0 | 2018-07-19 02:39:47 +0900 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 24 | import org.onosproject.openstacknetworking.api.InstancePortAdminService; |
| 25 | |
| 26 | import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE; |
| 27 | |
| 28 | /** |
| 29 | * Purges existing instance ports. |
| 30 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame^] | 31 | @Service |
Jian Li | cd806b0 | 2018-07-19 02:39:47 +0900 | [diff] [blame] | 32 | @Command(scope = "onos", name = "purge-instance-ports", |
| 33 | description = "Purges existing instance ports created by OpenStack networking app") |
| 34 | public 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 50 | protected void doExecute() { |
Jian Li | cd806b0 | 2018-07-19 02:39:47 +0900 | [diff] [blame] | 51 | 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 | } |