Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 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.cli.net; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.Port; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.device.DeviceAdminService; |
| 26 | import 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") |
| 33 | public 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) |
| 40 | Integer portNumber = null; |
| 41 | |
Charles Chan | 1db7ff1 | 2016-04-07 15:03:24 -0700 | [diff] [blame] | 42 | @Argument(index = 2, name = "portState", |
| 43 | description = "Desired State. Either \"enable\" or \"disable\".", |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 44 | 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 | } |
| 56 | PortNumber pnum = PortNumber.portNumber(portNumber); |
| 57 | Port p = deviceService.getPort(dev.id(), pnum); |
| 58 | if (p == null) { |
| 59 | print(" %s", "Port does not exist"); |
| 60 | return; |
| 61 | } |
| 62 | if (portState.equals("enable")) { |
| 63 | deviceAdminService.changePortState(dev.id(), pnum, true); |
| 64 | } else if (portState.equals("disable")) { |
| 65 | deviceAdminService.changePortState(dev.id(), pnum, false); |
| 66 | } else { |
| 67 | print(" %s", "State must be enable or disable"); |
| 68 | } |
| 69 | } |
| 70 | } |