blob: dca8a90b1b5711cd1f25574db1c4285a80d09d7e [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;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Saurav Dasa2d37502016-03-25 17:50:40 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.Port;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.device.DeviceAdminService;
27import org.onosproject.net.device.DeviceService;
28
29/**
30 * Administratively enables or disabled a port on a device.
31 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070032@Service
Saurav Dasa2d37502016-03-25 17:50:40 -070033@Command(scope = "onos", name = "portstate",
34 description = "Administratively enables or disabled a port on a device")
35public class DevicePortStateCommand extends AbstractShellCommand {
36 @Argument(index = 0, name = "uri", description = "Device ID",
37 required = true, multiValued = false)
38 String uri = null;
39
40 @Argument(index = 1, name = "portNumber", description = "Port Number",
41 required = true, multiValued = false)
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080042 String portNumber = null;
Saurav Dasa2d37502016-03-25 17:50:40 -070043
Charles Chan1db7ff12016-04-07 15:03:24 -070044 @Argument(index = 2, name = "portState",
45 description = "Desired State. Either \"enable\" or \"disable\".",
Saurav Dasa2d37502016-03-25 17:50:40 -070046 required = true, multiValued = false)
47 String portState = null;
48
49 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050 protected void doExecute() {
Saurav Dasa2d37502016-03-25 17:50:40 -070051 DeviceService deviceService = get(DeviceService.class);
52 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
53 Device dev = deviceService.getDevice(DeviceId.deviceId(uri));
54 if (dev == null) {
55 print(" %s", "Device does not exist");
56 return;
57 }
Yuta HIGUCHI820f0342017-11-28 11:27:42 -080058 PortNumber pnum = PortNumber.fromString(portNumber);
Saurav Dasa2d37502016-03-25 17:50:40 -070059 Port p = deviceService.getPort(dev.id(), pnum);
60 if (p == null) {
61 print(" %s", "Port does not exist");
62 return;
63 }
Jon Halla3fcf672017-03-28 16:53:22 -070064 if ("enable".equals(portState)) {
Saurav Dasa2d37502016-03-25 17:50:40 -070065 deviceAdminService.changePortState(dev.id(), pnum, true);
Jon Halla3fcf672017-03-28 16:53:22 -070066 } else if ("disable".equals(portState)) {
Saurav Dasa2d37502016-03-25 17:50:40 -070067 deviceAdminService.changePortState(dev.id(), pnum, false);
68 } else {
69 print(" %s", "State must be enable or disable");
70 }
71 }
72}