Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.device.DeviceAdminService; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | |
| 30 | /** |
| 31 | * Administratively enables or disabled a port on a device. |
| 32 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | @Service |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 34 | @Command(scope = "onos", name = "portstate", |
| 35 | description = "Administratively enables or disabled a port on a device") |
| 36 | public class DevicePortStateCommand extends AbstractShellCommand { |
| 37 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 38 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 39 | @Completion(DeviceIdCompleter.class) |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 40 | String uri = null; |
| 41 | |
| 42 | @Argument(index = 1, name = "portNumber", description = "Port Number", |
| 43 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 44 | @Completion(PortNumberCompleter.class) |
Yuta HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame] | 45 | String portNumber = null; |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 46 | |
Charles Chan | 1db7ff1 | 2016-04-07 15:03:24 -0700 | [diff] [blame] | 47 | @Argument(index = 2, name = "portState", |
| 48 | description = "Desired State. Either \"enable\" or \"disable\".", |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 49 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 50 | @Completion(PortStateCompleter.class) |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 51 | String portState = null; |
| 52 | |
| 53 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 54 | protected void doExecute() { |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 55 | 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 HIGUCHI | 820f034 | 2017-11-28 11:27:42 -0800 | [diff] [blame] | 62 | PortNumber pnum = PortNumber.fromString(portNumber); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 63 | Port p = deviceService.getPort(dev.id(), pnum); |
| 64 | if (p == null) { |
| 65 | print(" %s", "Port does not exist"); |
| 66 | return; |
| 67 | } |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 68 | if ("enable".equals(portState)) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 69 | deviceAdminService.changePortState(dev.id(), p.number(), true); |
Jon Hall | a3fcf67 | 2017-03-28 16:53:22 -0700 | [diff] [blame] | 70 | } else if ("disable".equals(portState)) { |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 71 | deviceAdminService.changePortState(dev.id(), p.number(), false); |
Saurav Das | a2d3750 | 2016-03-25 17:50:40 -0700 | [diff] [blame] | 72 | } else { |
| 73 | print(" %s", "State must be enable or disable"); |
| 74 | } |
| 75 | } |
| 76 | } |