blob: 257ea11327a9cb64917cb17bc7c65b53d0400e5e [file] [log] [blame]
Andrea Campanella99ab7102019-07-26 14:43:14 +02001/*
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 */
18package org.onosproject.net.optical.cli;
19
20import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.Completion;
23import org.apache.karaf.shell.api.action.lifecycle.Service;
24import org.onosproject.cli.AbstractShellCommand;
Andrea Campanella3a361452019-08-02 10:17:53 +020025import org.onosproject.cli.net.NetconfOperationCompleter;
Andrea Campanella99ab7102019-07-26 14:43:14 +020026import org.onosproject.cli.net.OpticalConnectPointCompleter;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.Device;
29import org.onosproject.net.Direction;
30import org.onosproject.net.ModulationScheme;
31import org.onosproject.net.Port;
32import org.onosproject.net.behaviour.ModulationConfig;
33import org.onosproject.net.device.DeviceService;
34import org.slf4j.Logger;
35
36import java.util.Optional;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39import 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")
48public class OpticalModulationCommand extends AbstractShellCommand {
49
50 private static final Logger log = getLogger(OpticalModulationCommand.class);
51
Andrea Campanella3a361452019-08-02 10:17:53 +020052 @Argument(index = 0, name = "operation", description = "set modulation",
Andrea Campanella99ab7102019-07-26 14:43:14 +020053 required = true, multiValued = false)
Andrea Campanella3a361452019-08-02 10:17:53 +020054 @Completion(NetconfOperationCompleter.class)
Andrea Campanella99ab7102019-07-26 14:43:14 +020055 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 Campanella3a361452019-08-02 10:17:53 +020062 @Argument(index = 2, name = "value", description = "example: dp_qpsk, dp_8qam, dp_16qam",
Andrea Campanella99ab7102019-07-26 14:43:14 +020063 required = false, multiValued = false)
Andrea Campanella3a361452019-08-02 10:17:53 +020064 private String value = null;
Andrea Campanella99ab7102019-07-26 14:43:14 +020065
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 Campanella3a361452019-08-02 10:17:53 +020083 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 Campanella99ab7102019-07-26 14:43:14 +0200108 cp.port().toString(), cp.deviceId().toString());
Andrea Campanella3a361452019-08-02 10:17:53 +0200109 } else {
110 log.warn("Operation {} are not supported now.", operation);
Andrea Campanella99ab7102019-07-26 14:43:14 +0200111 }
Andrea Campanella99ab7102019-07-26 14:43:14 +0200112 } else {
Andrea Campanella3a361452019-08-02 10:17:53 +0200113 print("Device is not capable of handling modulation");
Andrea Campanella99ab7102019-07-26 14:43:14 +0200114 }
115 }
116}