Boyuan Yan | a00e3d0 | 2019-04-10 23:43:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | * This work was partially supported by EC H2020 project METRO-HAUL (761727). |
| 17 | */ |
| 18 | package org.onosproject.cli.net; |
| 19 | |
| 20 | import org.apache.karaf.shell.api.action.Argument; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.Completion; |
| 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.Device; |
Boyuan Yan | 53c2c68 | 2019-07-17 11:22:31 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Direction; |
Boyuan Yan | a00e3d0 | 2019-04-10 23:43:12 -0700 | [diff] [blame] | 28 | import org.onosproject.net.Port; |
| 29 | import org.onosproject.net.behaviour.PowerConfig; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | import java.util.Optional; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | /** |
| 39 | * Get the target-output-power for specific optical-channel. |
| 40 | * This is a command for PowerConfig. |
| 41 | */ |
| 42 | @Service |
| 43 | @Command(scope = "onos", name = "power-config", |
| 44 | description = "Get/Edit the target-output-power for specific optical-channel") |
| 45 | public class PowerConfigCommand extends AbstractShellCommand { |
| 46 | |
| 47 | private static final Logger log = getLogger(PowerConfigCommand.class); |
| 48 | |
| 49 | @Argument(index = 0, name = "operation", description = "Netconf Operation including get, edit-config, etc.", |
| 50 | required = true, multiValued = false) |
| 51 | @Completion(NetconfOperationCompleter.class) |
| 52 | private String operation = null; |
| 53 | |
| 54 | @Argument(index = 1, name = "connection point", description = "{DeviceID}/{PortNumber}", |
| 55 | required = true, multiValued = false) |
Boyuan Yan | 53c2c68 | 2019-07-17 11:22:31 -0700 | [diff] [blame] | 56 | @Completion(OpticalConnectPointCompleter.class) |
Boyuan Yan | a00e3d0 | 2019-04-10 23:43:12 -0700 | [diff] [blame] | 57 | private String connectPoint = null; |
| 58 | |
| 59 | @Argument(index = 2, name = "value", description = "target-output-power value. Unit: dBm", |
| 60 | required = false, multiValued = false) |
| 61 | private Long value = null; |
| 62 | |
| 63 | @Override |
| 64 | protected void doExecute() throws Exception { |
| 65 | DeviceService deviceService = get(DeviceService.class); |
| 66 | ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPoint); |
| 67 | Port port = deviceService.getPort(cp); |
| 68 | if (port == null) { |
| 69 | print("[ERROR] %s does not exist", cp); |
| 70 | return; |
| 71 | } |
Boyuan Yan | 53c2c68 | 2019-07-17 11:22:31 -0700 | [diff] [blame] | 72 | if (!port.type().equals(Port.Type.OCH) && |
| 73 | !port.type().equals(Port.Type.OTU) && |
| 74 | !port.type().equals(Port.Type.OMS)) { |
| 75 | log.warn("The power of selected port %s isn't editable.", port.number().toString()); |
| 76 | print("The power of selected port %s isn't editable.", port.number().toString()); |
| 77 | return; |
| 78 | } |
Boyuan Yan | a00e3d0 | 2019-04-10 23:43:12 -0700 | [diff] [blame] | 79 | Device device = deviceService.getDevice(cp.deviceId()); |
| 80 | PowerConfig powerConfig = device.as(PowerConfig.class); |
| 81 | // FIXME the parameter "component" equals NULL now, because there is one-to-one mapping between |
| 82 | // <component> and <optical-channel>. |
| 83 | if (operation.equals("get")) { |
Boyuan Yan | 53c2c68 | 2019-07-17 11:22:31 -0700 | [diff] [blame] | 84 | Optional<Long> val = powerConfig.getTargetPower(cp.port(), Direction.ALL); |
Boyuan Yan | a00e3d0 | 2019-04-10 23:43:12 -0700 | [diff] [blame] | 85 | long power = val.isPresent() ? val.get() : Long.MIN_VALUE; |
| 86 | print("The target-output-power value in port %s on device %s is %d.", |
| 87 | cp.port().toString(), cp.deviceId().toString(), power); |
| 88 | } else if (operation.equals("edit-config")) { |
| 89 | checkNotNull(value); |
Boyuan Yan | 53c2c68 | 2019-07-17 11:22:31 -0700 | [diff] [blame] | 90 | powerConfig.setTargetPower(cp.port(), Direction.ALL, value); |
Boyuan Yan | a00e3d0 | 2019-04-10 23:43:12 -0700 | [diff] [blame] | 91 | } else { |
| 92 | log.warn("Operation {} are not supported now.", operation); |
| 93 | } |
| 94 | } |
| 95 | } |