blob: 0ef23a2a9725e5b2739eb0dd9d83c8cbd9c2fb23 [file] [log] [blame]
Boyuan Yana00e3d02019-04-10 23:43:12 -07001/*
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.cli.net;
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;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.Device;
Boyuan Yan53c2c682019-07-17 11:22:31 -070027import org.onosproject.net.Direction;
Boyuan Yana00e3d02019-04-10 23:43:12 -070028import org.onosproject.net.Port;
29import org.onosproject.net.behaviour.PowerConfig;
30import org.onosproject.net.device.DeviceService;
31import org.slf4j.Logger;
32
33import java.util.Optional;
34
35import static com.google.common.base.Preconditions.checkNotNull;
36import 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")
45public 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 Yan53c2c682019-07-17 11:22:31 -070056 @Completion(OpticalConnectPointCompleter.class)
Boyuan Yana00e3d02019-04-10 23:43:12 -070057 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 Yan53c2c682019-07-17 11:22:31 -070072 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 Yana00e3d02019-04-10 23:43:12 -070079 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 Yan53c2c682019-07-17 11:22:31 -070084 Optional<Long> val = powerConfig.getTargetPower(cp.port(), Direction.ALL);
Boyuan Yana00e3d02019-04-10 23:43:12 -070085 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 Yan53c2c682019-07-17 11:22:31 -070090 powerConfig.setTargetPower(cp.port(), Direction.ALL, value);
Andrea Campanella3a361452019-08-02 10:17:53 +020091 print("Set %s power on port", value, connectPoint);
Boyuan Yana00e3d02019-04-10 23:43:12 -070092 } else {
Andrea Campanella3a361452019-08-02 10:17:53 +020093 print("Operation %s are not supported now.", operation);
Boyuan Yana00e3d02019-04-10 23:43:12 -070094 }
95 }
96}