blob: 7f1435868c1f0b10602c1e9c25e20f7ba4acb93a [file] [log] [blame]
Jongsik Jungb7a44352018-08-21 12:19:48 +09001/*
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 */
16package org.onosproject.openstacknetworking.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Jian Lidb521c12018-11-19 17:42:35 +090020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070021import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Jongsik Jungb7a44352018-08-21 12:19:48 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.InstancePort;
25import org.onosproject.openstacknetworking.api.InstancePortAdminService;
26
27import static java.lang.Thread.sleep;
28import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE;
29
30/**
31 * Purges existing instance ports.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
Jongsik Jungb7a44352018-08-21 12:19:48 +090034@Command(scope = "onos", name = "openstack-reset-ports",
35 description = "Reset existing instance ports created by OpenStack networking app")
36public class OpenstackResetPortsCommand 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
47 @Argument(index = 0, name = "portIds", description = "Instance Port IDs",
48 required = false, multiValued = true)
Jian Lidb521c12018-11-19 17:42:35 +090049 @Completion(InstancePortIdCompleter.class)
Jongsik Jungb7a44352018-08-21 12:19:48 +090050 private String[] portIds = null;
51
52 private static final long SLEEP_MS = 1000; // we wait 1s for reset each port
53
54 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070055 protected void doExecute() {
Jongsik Jungb7a44352018-08-21 12:19:48 +090056 InstancePortAdminService service = get(InstancePortAdminService.class);
57
58 if ((!isAll && !isInactive && portIds == null) ||
59 (isAll && isInactive) ||
60 (isInactive && portIds != null) ||
61 (portIds != null && isAll)) {
62 print("Please specify one of portIds, --all, and --inactive options.");
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);
73 }
74
75 for (String portId : portIds) {
76 resetPort(service, portId);
77 print("Successfully requested re-installing flow rules for port %s!", portId);
78 }
79 print("Done.");
80 }
81
82 private void resetPort(InstancePortAdminService service, String portId) {
83 InstancePort instancePort = service.instancePort(portId);
84
85 service.removeInstancePort(portId);
86 try {
87 sleep(SLEEP_MS);
88 } catch (InterruptedException e) {
89 log.error("Exception caused during port synchronization...");
90 }
91 service.createInstancePort(instancePort);
92 }
93}