blob: b7d5020d6b11c14190d0d76f5631b6300d43c337 [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;
27import org.onosproject.net.Port;
28import org.onosproject.net.behaviour.PowerConfig;
29import org.onosproject.net.device.DeviceService;
30import org.slf4j.Logger;
31
32import java.util.Optional;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35import static org.slf4j.LoggerFactory.getLogger;
36
37/**
38 * Get the target-output-power for specific optical-channel.
39 * This is a command for PowerConfig.
40 */
41@Service
42@Command(scope = "onos", name = "power-config",
43 description = "Get/Edit the target-output-power for specific optical-channel")
44public class PowerConfigCommand extends AbstractShellCommand {
45
46 private static final Logger log = getLogger(PowerConfigCommand.class);
47
48 @Argument(index = 0, name = "operation", description = "Netconf Operation including get, edit-config, etc.",
49 required = true, multiValued = false)
50 @Completion(NetconfOperationCompleter.class)
51 private String operation = null;
52
53 @Argument(index = 1, name = "connection point", description = "{DeviceID}/{PortNumber}",
54 required = true, multiValued = false)
55 @Completion(ConnectPointCompleter.class)
56 private String connectPoint = null;
57
58 @Argument(index = 2, name = "value", description = "target-output-power value. Unit: dBm",
59 required = false, multiValued = false)
60 private Long value = null;
61
62 @Override
63 protected void doExecute() throws Exception {
64 DeviceService deviceService = get(DeviceService.class);
65 ConnectPoint cp = ConnectPoint.deviceConnectPoint(connectPoint);
66 Port port = deviceService.getPort(cp);
67 if (port == null) {
68 print("[ERROR] %s does not exist", cp);
69 return;
70 }
71 Device device = deviceService.getDevice(cp.deviceId());
72 PowerConfig powerConfig = device.as(PowerConfig.class);
73 // FIXME the parameter "component" equals NULL now, because there is one-to-one mapping between
74 // <component> and <optical-channel>.
75 if (operation.equals("get")) {
76 Optional<Long> val = powerConfig.getTargetPower(cp.port(), null);
77 long power = val.isPresent() ? val.get() : Long.MIN_VALUE;
78 print("The target-output-power value in port %s on device %s is %d.",
79 cp.port().toString(), cp.deviceId().toString(), power);
80 } else if (operation.equals("edit-config")) {
81 checkNotNull(value);
82 powerConfig.setTargetPower(cp.port(), null, value);
83 } else {
84 log.warn("Operation {} are not supported now.", operation);
85 }
86 }
87}