Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [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.net.optical.cli; |
| 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; |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 25 | import org.onosproject.cli.net.NetconfOperationCompleter; |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 26 | import org.onosproject.cli.net.OpticalConnectPointCompleter; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.Device; |
| 29 | import org.onosproject.net.Direction; |
| 30 | import org.onosproject.net.ModulationScheme; |
| 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.behaviour.ModulationConfig; |
| 33 | import org.onosproject.net.device.DeviceService; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.Optional; |
| 37 | |
| 38 | import static com.google.common.base.Preconditions.checkNotNull; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Get the target-output-power for specific optical-channel. |
| 43 | * This is a command for PowerConfig. |
| 44 | */ |
| 45 | @Service |
| 46 | @Command(scope = "onos", name = "modulation-config", |
| 47 | description = "Get/Edit the modulation for specific optical-channel") |
| 48 | public class OpticalModulationCommand extends AbstractShellCommand { |
| 49 | |
| 50 | private static final Logger log = getLogger(OpticalModulationCommand.class); |
| 51 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 52 | @Argument(index = 0, name = "operation", description = "set modulation", |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 53 | required = true, multiValued = false) |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 54 | @Completion(NetconfOperationCompleter.class) |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 55 | private String operation = null; |
| 56 | |
| 57 | @Argument(index = 1, name = "connection point", description = "{DeviceID}/{PortNumber}", |
| 58 | required = true, multiValued = false) |
| 59 | @Completion(OpticalConnectPointCompleter.class) |
| 60 | private String connectPoint = null; |
| 61 | |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 62 | @Argument(index = 2, name = "value", description = "example: dp_qpsk, dp_8qam, dp_16qam", |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 63 | required = false, multiValued = false) |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 64 | private String value = null; |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 65 | |
| 66 | @Override |
| 67 | protected void doExecute() throws Exception { |
| 68 | DeviceService deviceService = get(DeviceService.class); |
| 69 | ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPoint); |
| 70 | Port port = deviceService.getPort(cp); |
| 71 | if (port == null) { |
| 72 | print("[ERROR] %s does not exist", cp); |
| 73 | return; |
| 74 | } |
| 75 | if (!port.type().equals(Port.Type.OCH) && |
| 76 | !port.type().equals(Port.Type.OTU) && |
| 77 | !port.type().equals(Port.Type.OMS)) { |
| 78 | log.warn("The modulation of selected port {} isn't editable.", port.number().toString()); |
| 79 | print("The modulation of selected port %s isn't editable.", port.number().toString()); |
| 80 | return; |
| 81 | } |
| 82 | Device device = deviceService.getDevice(cp.deviceId()); |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 83 | if (device.is(ModulationConfig.class)) { |
| 84 | ModulationConfig<Object> modulationConfig = device.as(ModulationConfig.class); |
| 85 | // FIXME the parameter "component" equals NULL now, because there is one-to-one mapping between |
| 86 | // <component> and <optical-channel>. |
| 87 | if (operation.equals("get")) { |
| 88 | Direction component = Direction.ALL; |
| 89 | Optional<ModulationScheme> scheme = modulationConfig.getModulationScheme(cp.port(), component); |
| 90 | if (scheme.isPresent()) { |
| 91 | print("The modulation value in port %s on device %s is %s.", |
| 92 | cp.port().toString(), cp.deviceId().toString(), scheme.get().name()); |
| 93 | } else { |
| 94 | print("Can't get modulation for port %s on device %s.", |
| 95 | cp.port().toString(), cp.deviceId().toString()); |
| 96 | } |
| 97 | } else if (operation.equals("edit-config")) { |
| 98 | long bitRate = 0; |
| 99 | if (value.equalsIgnoreCase(ModulationScheme.DP_QPSK.name())) { |
| 100 | bitRate = 100; |
| 101 | } else { |
| 102 | bitRate = 200; |
| 103 | } |
| 104 | checkNotNull(value); |
| 105 | Direction component = Direction.ALL; |
| 106 | modulationConfig.setModulationScheme(cp.port(), component, bitRate); |
| 107 | print("Set modulation for " + value + " for port %s on device %s.", |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 108 | cp.port().toString(), cp.deviceId().toString()); |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 109 | } else { |
| 110 | log.warn("Operation {} are not supported now.", operation); |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 111 | } |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 112 | } else { |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame] | 113 | print("Device is not capable of handling modulation"); |
Andrea Campanella | 99ab710 | 2019-07-26 14:43:14 +0200 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |