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; |
Jian Li | 67c6b81 | 2018-10-08 16:05:53 +0900 | [diff] [blame] | 26 | import static org.onosproject.openstacknetworking.api.InstancePort.State.REMOVE_PENDING; |
Jian Li | cd806b0 | 2018-07-19 02:39:47 +0900 | [diff] [blame] | 27 | |
| 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") |
| 33 | public 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 Li | 67c6b81 | 2018-10-08 16:05:53 +0900 | [diff] [blame] | 44 | @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 Li | cd806b0 | 2018-07-19 02:39:47 +0900 | [diff] [blame] | 49 | @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 Li | 67c6b81 | 2018-10-08 16:05:53 +0900 | [diff] [blame] | 57 | 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 Li | cd806b0 | 2018-07-19 02:39:47 +0900 | [diff] [blame] | 63 | 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 Li | 67c6b81 | 2018-10-08 16:05:53 +0900 | [diff] [blame] | 73 | } else if (isPending) { |
| 74 | portIds = service.instancePorts().stream() |
| 75 | .filter(p -> p.state() == REMOVE_PENDING) |
| 76 | .map(InstancePort::portId).toArray(String[]::new); |
Jian Li | cd806b0 | 2018-07-19 02:39:47 +0900 | [diff] [blame] | 77 | } |
| 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 | } |