blob: 3193d2d54f2ebbf96c52f7d4cf54603363b86716 [file] [log] [blame]
yjimmyy646aa022016-07-05 12:09:50 -07001/*
2 * Copyright 2016 Open Networking Laboratory
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
17package org.onosproject.driver.optical.power;
18
19import java.util.Optional;
20
yjimmyy646aa022016-07-05 12:09:50 -070021import org.onosproject.net.driver.AbstractHandlerBehaviour;
22import org.onosproject.net.Direction;
23import org.onosproject.net.Port;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.behaviour.PowerConfig;
26import org.onosproject.net.device.DeviceService;
yjimmyyb94f93b2016-07-11 16:03:48 -070027import org.onosproject.net.optical.OpticalAnnotations;
yjimmyy646aa022016-07-05 12:09:50 -070028import org.onosproject.openflow.controller.Dpid;
29import org.onosproject.openflow.controller.OpenFlowController;
30import org.onosproject.openflow.controller.OpenFlowSwitch;
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34/**
35 * Port Power (Gain and attenuation) implementation for Oplink ROADM.
36 *
37 * An Oplink ROADM port exposes OchSignal resources.
38 * Optical Power can be set at port level or channel/wavelength level (attenuation).
39 *
40 */
41
42public class OplinkRoadmPowerConfig extends AbstractHandlerBehaviour
43 implements PowerConfig<Direction> {
44 protected final Logger log = LoggerFactory.getLogger(getClass());
45
46 private OpenFlowSwitch getOpenFlowDevice() {
47 final OpenFlowController controller = this.handler().get(OpenFlowController.class);
48 final Dpid dpid = Dpid.dpid(this.data().deviceId().uri());
49 OpenFlowSwitch sw = controller.getSwitch(dpid);
50 if (sw == null || !sw.isConnected()) {
51 return null;
52 } else {
53 return sw;
54 }
55 }
56
57 @Override
58 public Optional<Long> getTargetPower(PortNumber portNum, Direction component) {
59 // Will be implemented in the future.
60 return Optional.empty();
61 }
62
63 @Override
64 public Optional<Long> currentPower(PortNumber portNum, Direction component) {
65 Long returnVal = null;
66 // Check if switch is connected, otherwise do not return value in store,
67 // which is obsolete.
68 if (getOpenFlowDevice() != null) {
69 DeviceService deviceService = this.handler().get(DeviceService.class);
70 Port port = deviceService.getPort(this.data().deviceId(), portNum);
71 if (port != null) {
yjimmyyb94f93b2016-07-11 16:03:48 -070072 String currentPower = port.annotations().value(OpticalAnnotations.CURRENT_POWER);
yjimmyy646aa022016-07-05 12:09:50 -070073 if (currentPower != null) {
74 returnVal = Long.valueOf(currentPower);
75 }
76 }
77 }
78 return Optional.ofNullable(returnVal);
79 }
80
81 @Override
82 public void setTargetPower(PortNumber portNum, Direction component, long power) {
83 OpenFlowSwitch device = getOpenFlowDevice();
84 if (device != null) {
85 device.sendMsg(device.factory().buildOplinkPortPowerSet()
86 .setXid(0)
87 .setPort((int) portNum.toLong())
88 .setPowerValue((int) power)
89 .build());
90 } else {
91 log.warn("OpenFlow handshaker driver not found or device is not connected");
92 }
93 }
94}