blob: 7cd177fcc206364ad2e93b4f4a9382e23cd99189 [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.net.Device;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.Port;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.device.DeviceAdminService;
26import org.onosproject.net.device.DeviceService;
27
28/**
29 * Administratively enables or disabled a port on a device.
30 */
31@Command(scope = "onos", name = "portstate",
32 description = "Administratively enables or disabled a port on a device")
33public class DevicePortStateCommand extends AbstractShellCommand {
34 @Argument(index = 0, name = "uri", description = "Device ID",
35 required = true, multiValued = false)
36 String uri = null;
37
38 @Argument(index = 1, name = "portNumber", description = "Port Number",
39 required = true, multiValued = false)
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080040 String portNumber = null;
Saurav Dasa2d37502016-03-25 17:50:40 -070041
Charles Chan1db7ff12016-04-07 15:03:24 -070042 @Argument(index = 2, name = "portState",
43 description = "Desired State. Either \"enable\" or \"disable\".",
Saurav Dasa2d37502016-03-25 17:50:40 -070044 required = true, multiValued = false)
45 String portState = null;
46
47 @Override
48 protected void execute() {
49 DeviceService deviceService = get(DeviceService.class);
50 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
51 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
52 if (dev == null) {
53 print(" %s", "Device does not exist");
54 return;
55 }
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080056 PortNumber pnum = PortNumber.fromString(portNumber);
Saurav Dasa2d37502016-03-25 17:50:40 -070057 Port p = deviceService.getPort(dev.id(), pnum);
58 if (p == null) {
59 print(" %s", "Port does not exist");
60 return;
61 }
Jon Halla3fcf672017-03-28 16:53:22 -070062 if ("enable".equals(portState)) {
Saurav Dasa2d37502016-03-25 17:50:40 -070063 deviceAdminService.changePortState(dev.id(), pnum, true);
Jon Halla3fcf672017-03-28 16:53:22 -070064 } else if ("disable".equals(portState)) {
Saurav Dasa2d37502016-03-25 17:50:40 -070065 deviceAdminService.changePortState(dev.id(), pnum, false);
66 } else {
67 print(" %s", "State must be enable or disable");
68 }
69 }
70}