yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.driver.optical.power; |
| 18 | |
| 19 | import java.util.Optional; |
| 20 | |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 21 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 22 | import org.onosproject.net.Direction; |
| 23 | import org.onosproject.net.Port; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.behaviour.PowerConfig; |
| 26 | import org.onosproject.net.device.DeviceService; |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame^] | 27 | import org.onosproject.net.optical.OpticalAnnotations; |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 28 | import org.onosproject.openflow.controller.Dpid; |
| 29 | import org.onosproject.openflow.controller.OpenFlowController; |
| 30 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 31 | import org.slf4j.Logger; |
| 32 | import 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 | |
| 42 | public 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) { |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame^] | 72 | String currentPower = port.annotations().value(OpticalAnnotations.CURRENT_POWER); |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 73 | 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 | } |