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