yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 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 | |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 19 | import java.util.List; |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 20 | import java.util.Optional; |
| 21 | |
Jimmy Yan | 32bceca | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Range; |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 23 | import org.onosproject.driver.extensions.OplinkAttenuation; |
| 24 | import org.onosproject.net.OchSignal; |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 26 | import org.onosproject.net.Direction; |
| 27 | import org.onosproject.net.Port; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.behaviour.PowerConfig; |
| 30 | import org.onosproject.net.device.DeviceService; |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.DefaultFlowRule; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 33 | import org.onosproject.net.flow.FlowEntry; |
| 34 | import org.onosproject.net.flow.FlowRule; |
| 35 | import org.onosproject.net.flow.FlowRuleService; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.flow.criteria.Criterion; |
| 39 | import org.onosproject.net.flow.criteria.OchSignalCriterion; |
| 40 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 41 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 42 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 43 | import org.onosproject.net.flow.instructions.Instruction; |
| 44 | import org.onosproject.net.flow.instructions.Instructions; |
yjimmyy | b94f93b | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 45 | import org.onosproject.net.optical.OpticalAnnotations; |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 46 | import org.onosproject.openflow.controller.Dpid; |
| 47 | import org.onosproject.openflow.controller.OpenFlowController; |
| 48 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
| 51 | |
| 52 | /** |
Jimmy Yan | 32bceca | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 53 | * Port Power (Gain and attenuation) implementation for Oplink 1-SLOT-8D ROADM. |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 54 | * |
| 55 | * An Oplink ROADM port exposes OchSignal resources. |
| 56 | * Optical Power can be set at port level or channel/wavelength level (attenuation). |
| 57 | * |
| 58 | */ |
| 59 | |
| 60 | public class OplinkRoadmPowerConfig extends AbstractHandlerBehaviour |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 61 | implements PowerConfig<Object> { |
| 62 | |
Jimmy Yan | 32bceca | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 63 | private static final int LINE_IN = 1; |
| 64 | private static final int LINE_OUT = 2; |
| 65 | private static final int AUX_OUT_1 = 3; |
| 66 | private static final int AUX_OUT_2 = 4; |
| 67 | private static final int EXPRESS_OUT_1 = 5; |
| 68 | private static final int EXPRESS_OUT_2 = 6; |
| 69 | private static final int EXPRESS_OUT_3 = 7; |
| 70 | private static final int EXPRESS_OUT_4 = 8; |
| 71 | private static final int EXPRESS_OUT_5 = 9; |
| 72 | private static final int EXPRESS_OUT_6 = 10; |
| 73 | private static final int EXPRESS_OUT_7 = 11; |
| 74 | private static final int AUX_IN_1 = 12; |
| 75 | private static final int AUX_IN_2 = 13; |
| 76 | private static final int EXPRESS_IN_1 = 14; |
| 77 | private static final int EXPRESS_IN_2 = 15; |
| 78 | private static final int EXPRESS_IN_3 = 16; |
| 79 | private static final int EXPRESS_IN_4 = 17; |
| 80 | private static final int EXPRESS_IN_5 = 18; |
| 81 | private static final int EXPRESS_IN_6 = 19; |
| 82 | private static final int EXPRESS_IN_7 = 20; |
| 83 | |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 84 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 85 | |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 86 | // Component type |
| 87 | private enum Type { |
| 88 | NONE, |
| 89 | PORT, |
| 90 | CHANNEL |
| 91 | } |
| 92 | |
| 93 | // Get the type if component is valid |
| 94 | private Type getType(Object component) { |
| 95 | if (component == null || component instanceof Direction) { |
| 96 | return Type.PORT; |
| 97 | } else if (component instanceof OchSignal) { |
| 98 | return Type.CHANNEL; |
| 99 | } else { |
| 100 | return Type.NONE; |
| 101 | } |
| 102 | } |
| 103 | |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 104 | private OpenFlowSwitch getOpenFlowDevice() { |
| 105 | final OpenFlowController controller = this.handler().get(OpenFlowController.class); |
| 106 | final Dpid dpid = Dpid.dpid(this.data().deviceId().uri()); |
| 107 | OpenFlowSwitch sw = controller.getSwitch(dpid); |
| 108 | if (sw == null || !sw.isConnected()) { |
| 109 | return null; |
| 110 | } else { |
| 111 | return sw; |
| 112 | } |
| 113 | } |
| 114 | |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 115 | // Find matching flow on device |
| 116 | private FlowEntry findFlow(PortNumber portNum, OchSignal och) { |
| 117 | FlowRuleService service = this.handler().get(FlowRuleService.class); |
| 118 | Iterable<FlowEntry> flowEntries = service.getFlowEntries(this.data().deviceId()); |
| 119 | |
| 120 | // Return first matching flow |
| 121 | for (FlowEntry entry : flowEntries) { |
| 122 | TrafficSelector selector = entry.selector(); |
| 123 | OchSignalCriterion entrySigid = |
| 124 | (OchSignalCriterion) selector.getCriterion(Criterion.Type.OCH_SIGID); |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame^] | 125 | // Check channel |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 126 | if (entrySigid != null && och.equals(entrySigid.lambda())) { |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame^] | 127 | // Check input port |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 128 | PortCriterion entryPort = |
| 129 | (PortCriterion) selector.getCriterion(Criterion.Type.IN_PORT); |
| 130 | if (entryPort != null && portNum.equals(entryPort.port())) { |
| 131 | return entry; |
| 132 | } |
Jimmy Yan | da878fc | 2016-09-02 16:32:01 -0700 | [diff] [blame^] | 133 | |
| 134 | // Check output port |
| 135 | TrafficTreatment treatment = entry.treatment(); |
| 136 | for (Instruction instruction : treatment.allInstructions()) { |
| 137 | if (instruction.type() == Instruction.Type.OUTPUT && |
| 138 | ((Instructions.OutputInstruction) instruction).port().equals(portNum)) { |
| 139 | return entry; |
| 140 | } |
| 141 | } |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | log.warn("No matching flow found"); |
| 145 | return null; |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | @Override |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 149 | public Optional<Long> getTargetPower(PortNumber portNum, Object component) { |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 150 | Long returnVal = null; |
| 151 | // Check if switch is connected, otherwise do not return value in store, |
| 152 | // which is obsolete. |
| 153 | if (getOpenFlowDevice() != null) { |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 154 | switch (getType(component)) { |
| 155 | case PORT: |
| 156 | // Will be implemented in the future. |
| 157 | break; |
| 158 | case CHANNEL: |
| 159 | returnVal = getChannelAttenuation(portNum, (OchSignal) component); |
| 160 | break; |
| 161 | default: |
| 162 | break; |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | return Optional.ofNullable(returnVal); |
| 166 | } |
| 167 | |
| 168 | @Override |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 169 | public Optional<Long> currentPower(PortNumber portNum, Object component) { |
| 170 | Long returnVal = null; |
| 171 | // Check if switch is connected, otherwise do not return value in store, |
| 172 | // which is obsolete. |
| 173 | if (getOpenFlowDevice() != null) { |
| 174 | switch (getType(component)) { |
| 175 | case PORT: |
| 176 | returnVal = getCurrentPortPower(portNum); |
| 177 | break; |
| 178 | case CHANNEL: |
| 179 | returnVal = getCurrentChannelPower(portNum, (OchSignal) component); |
| 180 | break; |
| 181 | default: |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | return Optional.ofNullable(returnVal); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public void setTargetPower(PortNumber portNum, Object component, long power) { |
| 190 | if (getOpenFlowDevice() != null) { |
| 191 | switch (getType(component)) { |
| 192 | case PORT: |
| 193 | setTargetPortPower(portNum, power); |
| 194 | break; |
| 195 | case CHANNEL: |
| 196 | setChannelAttenuation(portNum, (OchSignal) component, power); |
| 197 | break; |
| 198 | default: |
| 199 | break; |
| 200 | } |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 201 | } else { |
| 202 | log.warn("OpenFlow handshaker driver not found or device is not connected"); |
| 203 | } |
| 204 | } |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 205 | |
Jimmy Yan | 32bceca | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 206 | @Override |
| 207 | public Optional<Range<Long>> getTargetPowerRange(PortNumber port, Object component) { |
| 208 | Range<Long> range = null; |
| 209 | switch (getType(component)) { |
| 210 | case PORT: |
| 211 | range = getTargetPortPowerRange(port); |
| 212 | break; |
| 213 | case CHANNEL: |
| 214 | range = getChannelAttenuationRange(port); |
| 215 | break; |
| 216 | default: |
| 217 | break; |
| 218 | } |
| 219 | return Optional.ofNullable(range); |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public Optional<Range<Long>> getInputPowerRange(PortNumber port, Object component) { |
| 224 | Range<Long> range = null; |
| 225 | switch (getType(component)) { |
| 226 | case PORT: |
| 227 | range = getInputPortPowerRange(port); |
| 228 | break; |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | return Optional.ofNullable(range); |
| 233 | } |
| 234 | |
yjimmyy | cfcb053 | 2016-07-11 16:03:48 -0700 | [diff] [blame] | 235 | private Long getChannelAttenuation(PortNumber portNum, OchSignal och) { |
| 236 | FlowEntry flowEntry = findFlow(portNum, och); |
| 237 | if (flowEntry != null) { |
| 238 | List<Instruction> instructions = flowEntry.treatment().allInstructions(); |
| 239 | for (Instruction ins : instructions) { |
| 240 | if (ins.type() == Instruction.Type.EXTENSION) { |
| 241 | ExtensionTreatment ext = ((Instructions.ExtensionInstructionWrapper) ins).extensionInstruction(); |
| 242 | if (ext.type() == ExtensionTreatmentType.ExtensionTreatmentTypes.OPLINK_ATTENUATION.type()) { |
| 243 | return (long) ((OplinkAttenuation) ext).getAttenuation(); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | return null; |
| 249 | } |
| 250 | |
| 251 | private Long getCurrentPortPower(PortNumber portNum) { |
| 252 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 253 | Port port = deviceService.getPort(this.data().deviceId(), portNum); |
| 254 | if (port != null) { |
| 255 | String currentPower = port.annotations().value(OpticalAnnotations.CURRENT_POWER); |
| 256 | if (currentPower != null) { |
| 257 | return Long.valueOf(currentPower); |
| 258 | } |
| 259 | } |
| 260 | return null; |
| 261 | } |
| 262 | |
| 263 | private Long getCurrentChannelPower(PortNumber portNum, OchSignal och) { |
| 264 | FlowEntry flowEntry = findFlow(portNum, och); |
| 265 | if (flowEntry != null) { |
| 266 | // TODO put somewhere else if possible |
| 267 | // We put channel power in packets |
| 268 | return flowEntry.packets(); |
| 269 | } |
| 270 | return null; |
| 271 | } |
| 272 | |
| 273 | private void setTargetPortPower(PortNumber portNum, long power) { |
| 274 | OpenFlowSwitch device = getOpenFlowDevice(); |
| 275 | device.sendMsg(device.factory().buildOplinkPortPowerSet() |
| 276 | .setXid(0) |
| 277 | .setPort((int) portNum.toLong()) |
| 278 | .setPowerValue((int) power) |
| 279 | .build()); |
| 280 | } |
| 281 | |
| 282 | private void setChannelAttenuation(PortNumber portNum, OchSignal och, long power) { |
| 283 | FlowEntry flowEntry = findFlow(portNum, och); |
| 284 | if (flowEntry != null) { |
| 285 | List<Instruction> instructions = flowEntry.treatment().allInstructions(); |
| 286 | for (Instruction ins : instructions) { |
| 287 | if (ins.type() == Instruction.Type.EXTENSION) { |
| 288 | ExtensionTreatment ext = ((Instructions.ExtensionInstructionWrapper) ins).extensionInstruction(); |
| 289 | if (ext.type() == ExtensionTreatmentType.ExtensionTreatmentTypes.OPLINK_ATTENUATION.type()) { |
| 290 | ((OplinkAttenuation) ext).setAttenuation((int) power); |
| 291 | FlowRuleService service = this.handler().get(FlowRuleService.class); |
| 292 | service.applyFlowRules(flowEntry); |
| 293 | return; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | addAttenuation(flowEntry, power); |
| 298 | } else { |
| 299 | log.warn("Target channel power not set"); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Replace flow with new flow containing Oplink attenuation extension instruction. Also resets |
| 304 | // metrics. |
| 305 | private void addAttenuation(FlowEntry flowEntry, long power) { |
| 306 | FlowRule.Builder flowBuilder = new DefaultFlowRule.Builder(); |
| 307 | flowBuilder.withCookie(flowEntry.id().value()); |
| 308 | flowBuilder.withPriority(flowEntry.priority()); |
| 309 | flowBuilder.forDevice(flowEntry.deviceId()); |
| 310 | flowBuilder.forTable(flowEntry.tableId()); |
| 311 | if (flowEntry.isPermanent()) { |
| 312 | flowBuilder.makePermanent(); |
| 313 | } else { |
| 314 | flowBuilder.makeTemporary(flowEntry.timeout()); |
| 315 | } |
| 316 | |
| 317 | flowBuilder.withSelector(flowEntry.selector()); |
| 318 | |
| 319 | // Copy original instructions and add attenuation instruction |
| 320 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 321 | flowEntry.treatment().allInstructions().forEach(ins -> treatmentBuilder.add(ins)); |
| 322 | treatmentBuilder.add(Instructions.extension(new OplinkAttenuation((int) power), this.data().deviceId())); |
| 323 | flowBuilder.withTreatment(treatmentBuilder.build()); |
| 324 | |
| 325 | FlowRuleService service = this.handler().get(FlowRuleService.class); |
| 326 | service.applyFlowRules(flowBuilder.build()); |
| 327 | } |
Jimmy Yan | 32bceca | 2016-09-02 16:32:01 -0700 | [diff] [blame] | 328 | |
| 329 | // Returns the acceptable target range for an output Port, null otherwise |
| 330 | private Range<Long> getTargetPortPowerRange(PortNumber port) { |
| 331 | Range<Long> range = null; |
| 332 | long num = port.toLong(); |
| 333 | if (num == LINE_OUT) { |
| 334 | range = Range.closed(100L, 2040L); |
| 335 | } else if (num >= AUX_OUT_1 && num <= EXPRESS_OUT_7) { |
| 336 | range = Range.closed(-680L, 1530L); |
| 337 | } |
| 338 | return range; |
| 339 | } |
| 340 | |
| 341 | // Returns the acceptable attenuation range for a connection (represented as |
| 342 | // a flow with attenuation instruction). Port can be either the input or |
| 343 | // output port of the connection. Returns null if the connection does not |
| 344 | // support attenuation. |
| 345 | private Range<Long> getChannelAttenuationRange(PortNumber port) { |
| 346 | Range<Long> range = null; |
| 347 | long num = port.toLong(); |
| 348 | // Only connections from AuxIn to LineOut or ExpressIn to LineOut support |
| 349 | // attenuation. |
| 350 | if (num == LINE_OUT || |
| 351 | num >= AUX_IN_1 && num <= EXPRESS_IN_7) { |
| 352 | range = Range.closed(0L, 2550L); |
| 353 | } |
| 354 | return range; |
| 355 | } |
| 356 | |
| 357 | // Returns the working input power range for an input port, null if the port |
| 358 | // is not an input port. |
| 359 | private Range<Long> getInputPortPowerRange(PortNumber port) { |
| 360 | Range<Long> range = null; |
| 361 | long portNum = port.toLong(); |
| 362 | if (portNum == LINE_IN) { |
| 363 | // TODO implement support for IR and ER range |
| 364 | // only supports LR right now |
| 365 | range = Range.closed(-2600L, 540L); |
| 366 | } else if (portNum == AUX_IN_1 || portNum == AUX_IN_2) { |
| 367 | range = Range.closed(-1250L, 1590L); |
| 368 | } else if (portNum >= EXPRESS_IN_1 && portNum <= EXPRESS_IN_7) { |
| 369 | range = Range.closed(-1420L, 1420L); |
| 370 | } |
| 371 | return range; |
| 372 | } |
yjimmyy | 646aa02 | 2016-07-05 12:09:50 -0700 | [diff] [blame] | 373 | } |