blob: bdf1c65ecaa020a1ff86e6f71a3d619b29397f2c [file] [log] [blame]
Saurav Dasa2d37502016-03-25 17:50:40 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Saurav Dasa2d37502016-03-25 17:50:40 -07003 *
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.cli.net;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Saurav Dasa2d37502016-03-25 17:50:40 -070022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.Port;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.device.DeviceAdminService;
28import org.onosproject.net.device.DeviceService;
29
30/**
31 * Administratively enables or disabled a port on a device.
32 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033@Service
Saurav Dasa2d37502016-03-25 17:50:40 -070034@Command(scope = "onos", name = "portstate",
35 description = "Administratively enables or disabled a port on a device")
36public class DevicePortStateCommand extends AbstractShellCommand {
37 @Argument(index = 0, name = "uri", description = "Device ID",
38 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070039 @Completion(DeviceIdCompleter.class)
Saurav Dasa2d37502016-03-25 17:50:40 -070040 String uri = null;
41
42 @Argument(index = 1, name = "portNumber", description = "Port Number",
43 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070044 @Completion(PortNumberCompleter.class)
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080045 String portNumber = null;
Saurav Dasa2d37502016-03-25 17:50:40 -070046
Charles Chan1db7ff12016-04-07 15:03:24 -070047 @Argument(index = 2, name = "portState",
48 description = "Desired State. Either \"enable\" or \"disable\".",
Saurav Dasa2d37502016-03-25 17:50:40 -070049 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070050 @Completion(PortStateCompleter.class)
Saurav Dasa2d37502016-03-25 17:50:40 -070051 String portState = null;
52
53 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054 protected void doExecute() {
Saurav Dasa2d37502016-03-25 17:50:40 -070055 DeviceService deviceService = get(DeviceService.class);
56 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
57 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
58 if (dev == null) {
59 print(" %s", "Device does not exist");
60 return;
61 }
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080062 PortNumber pnum = PortNumber.fromString(portNumber);
Saurav Dasa2d37502016-03-25 17:50:40 -070063 Port p = deviceService.getPort(dev.id(), pnum);
64 if (p == null) {
65 print(" %s", "Port does not exist");
66 return;
67 }
Jon Halla3fcf672017-03-28 16:53:22 -070068 if ("enable".equals(portState)) {
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080069 deviceAdminService.changePortState(dev.id(), p.number(), true);
Jon Halla3fcf672017-03-28 16:53:22 -070070 } else if ("disable".equals(portState)) {
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080071 deviceAdminService.changePortState(dev.id(), p.number(), false);
Saurav Dasa2d37502016-03-25 17:50:40 -070072 } else {
73 print(" %s", "State must be enable or disable");
74 }
75 }
76}