MaoLu | c201ae4 | 2017-02-06 17:57:01 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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.concurrent.atomic.AtomicInteger; |
| 20 | import java.util.Optional; |
| 21 | |
| 22 | import com.google.common.collect.Range; |
| 23 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 24 | import org.onosproject.net.Port; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.behaviour.PowerConfig; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.optical.OpticalAnnotations; |
| 29 | import org.onosproject.openflow.controller.Dpid; |
| 30 | import org.onosproject.openflow.controller.OpenFlowController; |
| 31 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | /** |
| 36 | * Port Power (Gain and attenuation) implementation for Oplink EDFA device. |
| 37 | * |
| 38 | * An Oplink EDFA port exposes Direction resources. |
| 39 | * Set gain(attenuation) at AGC mode or set output level at APC mode. |
| 40 | * |
| 41 | */ |
| 42 | |
| 43 | public class OplinkEdfaPowerConfig extends AbstractHandlerBehaviour |
| 44 | implements PowerConfig<Object> { |
| 45 | |
| 46 | /** |
| 47 | * Input and ouput port number |
| 48 | * Note: |
| 49 | * These port number configurations are just in use for a short time. |
| 50 | * In the future, the port number and direction type would be obtained from physical device. |
| 51 | */ |
| 52 | private static final int LINE_IN_WEST = 1; |
| 53 | private static final int LINE_OUT_WEST = 2; |
| 54 | private static final int LINE_IN_EAST = 3; |
| 55 | private static final int LINE_OUT_EAST = 4; |
| 56 | |
| 57 | /** |
| 58 | * Power threshold of each port, magnified 100 times |
| 59 | * Note: |
| 60 | * These threshold configurations are just in use for a short time. |
| 61 | * In the future, the power threshold would be obtained from physical device. |
| 62 | */ |
| 63 | private static final long POWER_IN_WEST_LOW_THRES = -1900L; |
| 64 | private static final long POWER_IN_WEST_HIGH_THRES = 0L; |
| 65 | private static final long POWER_IN_EAST_LOW_THRES = -3100L; |
| 66 | private static final long POWER_IN_EAST_HIGH_THRES = 700L; |
| 67 | private static final long POWER_OUT_LOW_THRES = 0L; |
| 68 | private static final long POWER_OUT_HIGH_THRES = 1900L; |
| 69 | |
| 70 | // Transaction id to use. |
| 71 | private final AtomicInteger xidCounter = new AtomicInteger(0); |
| 72 | // Log |
| 73 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 74 | |
| 75 | @Override |
| 76 | public Optional<Long> getTargetPower(PortNumber portNum, Object component) { |
| 77 | Long returnVal = getTargetPortPower(portNum); |
| 78 | return Optional.ofNullable(returnVal); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Optional<Long> currentPower(PortNumber portNum, Object component) { |
| 83 | Long returnVal = getCurrentPortPower(portNum); |
| 84 | return Optional.ofNullable(returnVal); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void setTargetPower(PortNumber portNum, Object component, long power) { |
| 89 | setTargetPortPower(portNum, power); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public Optional<Range<Long>> getTargetPowerRange(PortNumber port, Object component) { |
| 94 | Range<Long> range = getTargetPortPowerRange(port); |
| 95 | return Optional.ofNullable(range); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Optional<Range<Long>> getInputPowerRange(PortNumber port, Object component) { |
| 100 | Range<Long> range = getInputPortPowerRange(port); |
| 101 | return Optional.ofNullable(range); |
| 102 | } |
| 103 | |
| 104 | private OpenFlowSwitch getOpenFlowDevice() { |
| 105 | final OpenFlowController controller = handler().get(OpenFlowController.class); |
| 106 | final Dpid dpid = Dpid.dpid(data().deviceId().uri()); |
| 107 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 108 | if (sw == null || !sw.isConnected()) { |
| 109 | log.warn("OpenFlow handshaker driver not found or device is not connected"); |
| 110 | return null; |
| 111 | } |
| 112 | return sw; |
| 113 | } |
| 114 | |
| 115 | private Long getPowerFromPort(PortNumber portNum, String annotation) { |
| 116 | // Check if switch is connected, otherwise do not return value in store, which is obsolete. |
| 117 | if (getOpenFlowDevice() == null) { |
| 118 | // Warning already exists in method getOpenFlowDevice() |
| 119 | return null; |
| 120 | } |
| 121 | DeviceService deviceService = handler().get(DeviceService.class); |
| 122 | Port port = deviceService.getPort(data().deviceId(), portNum); |
| 123 | if (port == null) { |
| 124 | log.warn("Unexpected port: {}", portNum); |
| 125 | return null; |
| 126 | } |
| 127 | String power = port.annotations().value(annotation); |
| 128 | if (power == null) { |
| 129 | log.warn("Cannot get {} from port {}.", annotation, portNum); |
| 130 | return null; |
| 131 | } |
| 132 | return Long.valueOf(power); |
| 133 | } |
| 134 | |
| 135 | private Long getTargetPortPower(PortNumber portNum) { |
| 136 | return getPowerFromPort(portNum, OpticalAnnotations.TARGET_POWER); |
| 137 | } |
| 138 | |
| 139 | private Long getCurrentPortPower(PortNumber portNum) { |
| 140 | return getPowerFromPort(portNum, OpticalAnnotations.CURRENT_POWER); |
| 141 | } |
| 142 | |
| 143 | private void setTargetPortPower(PortNumber portNum, long power) { |
| 144 | OpenFlowSwitch device = getOpenFlowDevice(); |
| 145 | // Check if switch is connected, otherwise do not return value in store, which is obsolete. |
| 146 | if (device == null) { |
| 147 | // Warning already exists in method getOpenFlowDevice() |
| 148 | return; |
| 149 | } |
| 150 | device.sendMsg(device.factory().buildOplinkPortPowerSet() |
| 151 | .setXid(xidCounter.getAndIncrement()) |
| 152 | .setPort((int) portNum.toLong()) |
| 153 | .setPowerValue((int) power) |
| 154 | .build()); |
| 155 | } |
| 156 | |
| 157 | // Returns the acceptable target range for an output Port, null otherwise |
| 158 | private Range<Long> getTargetPortPowerRange(PortNumber port) { |
| 159 | long portNum = port.toLong(); |
| 160 | // FIXME |
| 161 | // Short time hard code, we will use port direction type instead in the future. |
| 162 | // And more, the power range will be also obtained from device configuration. |
| 163 | switch ((int) portNum) { |
| 164 | case LINE_OUT_EAST: |
| 165 | case LINE_OUT_WEST: |
| 166 | return Range.closed(POWER_OUT_LOW_THRES, POWER_OUT_HIGH_THRES); |
| 167 | default: |
| 168 | // Unexpected port. Do not need warning here for port polling. |
| 169 | return null; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // Returns the working input power range for an input port, null if the port |
| 174 | // is not an input port. |
| 175 | private Range<Long> getInputPortPowerRange(PortNumber port) { |
| 176 | long portNum = port.toLong(); |
| 177 | // FIXME |
| 178 | // Short time hard code, we will use port direction type instead in the future. |
| 179 | // And more, the power range will be also obtained from device configuration. |
| 180 | switch ((int) portNum) { |
| 181 | case LINE_IN_EAST: |
| 182 | return Range.closed(POWER_IN_EAST_LOW_THRES, POWER_IN_EAST_HIGH_THRES); |
| 183 | case LINE_IN_WEST: |
| 184 | return Range.closed(POWER_IN_WEST_LOW_THRES, POWER_IN_WEST_HIGH_THRES); |
| 185 | default: |
| 186 | // Unexpected port. Do not need warning here for port polling. |
| 187 | return null; |
| 188 | } |
| 189 | } |
| 190 | } |