Jongsik Jung | b7a4435 | 2018-08-21 12:19:48 +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; |
Jongsik Jung | b7a4435 | 2018-08-21 12:19:48 +0900 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 23 | import org.onosproject.openstacknetworking.api.InstancePortAdminService; |
| 24 | |
| 25 | import static java.lang.Thread.sleep; |
| 26 | import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE; |
| 27 | |
| 28 | /** |
| 29 | * Purges existing instance ports. |
| 30 | */ |
| 31 | @Command(scope = "onos", name = "openstack-reset-ports", |
| 32 | description = "Reset existing instance ports created by OpenStack networking app") |
| 33 | public class OpenstackResetPortsCommand 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 | |
| 44 | @Argument(index = 0, name = "portIds", description = "Instance Port IDs", |
| 45 | required = false, multiValued = true) |
| 46 | private String[] portIds = null; |
| 47 | |
| 48 | private static final long SLEEP_MS = 1000; // we wait 1s for reset each port |
| 49 | |
| 50 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 51 | protected void doExecute() { |
Jongsik Jung | b7a4435 | 2018-08-21 12:19:48 +0900 | [diff] [blame] | 52 | InstancePortAdminService service = get(InstancePortAdminService.class); |
| 53 | |
| 54 | if ((!isAll && !isInactive && portIds == null) || |
| 55 | (isAll && isInactive) || |
| 56 | (isInactive && portIds != null) || |
| 57 | (portIds != null && isAll)) { |
| 58 | print("Please specify one of portIds, --all, and --inactive options."); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (isAll) { |
| 63 | portIds = service.instancePorts().stream() |
| 64 | .map(InstancePort::portId).toArray(String[]::new); |
| 65 | } else if (isInactive) { |
| 66 | portIds = service.instancePorts().stream() |
| 67 | .filter(p -> p.state() == INACTIVE) |
| 68 | .map(InstancePort::portId).toArray(String[]::new); |
| 69 | } |
| 70 | |
| 71 | for (String portId : portIds) { |
| 72 | resetPort(service, portId); |
| 73 | print("Successfully requested re-installing flow rules for port %s!", portId); |
| 74 | } |
| 75 | print("Done."); |
| 76 | } |
| 77 | |
| 78 | private void resetPort(InstancePortAdminService service, String portId) { |
| 79 | InstancePort instancePort = service.instancePort(portId); |
| 80 | |
| 81 | service.removeInstancePort(portId); |
| 82 | try { |
| 83 | sleep(SLEEP_MS); |
| 84 | } catch (InterruptedException e) { |
| 85 | log.error("Exception caused during port synchronization..."); |
| 86 | } |
| 87 | service.createInstancePort(instancePort); |
| 88 | } |
| 89 | } |