MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016 Open Networking Foundation |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -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.drivers.oplink; |
| 18 | |
| 19 | import com.google.common.collect.Range; |
| 20 | import org.apache.commons.configuration.HierarchicalConfiguration; |
| 21 | import org.onosproject.driver.extensions.OplinkAttenuation; |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Device; |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 23 | import org.onosproject.net.Direction; |
| 24 | import org.onosproject.net.OchSignal; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.behaviour.PowerConfig; |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 27 | import org.onosproject.net.device.DeviceService; |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.onosproject.net.flow.DefaultFlowRule; |
| 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onosproject.net.flow.FlowEntry; |
| 32 | import org.onosproject.net.flow.FlowRuleService; |
| 33 | import org.onosproject.net.flow.TrafficTreatment; |
| 34 | import org.slf4j.Logger; |
| 35 | |
| 36 | import java.util.Optional; |
| 37 | |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 38 | import static org.onosproject.drivers.oplink.OplinkOpticalUtility.POWER_MULTIPLIER; |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 39 | import static org.onosproject.drivers.oplink.OplinkOpticalUtility.RANGE_ATT; |
| 40 | import static org.onosproject.drivers.oplink.OplinkOpticalUtility.RANGE_GENERAL; |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 41 | import static org.onosproject.drivers.oplink.OplinkNetconfUtility.*; |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 42 | import static org.slf4j.LoggerFactory.getLogger; |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Get current or target port/channel power from an Oplink optical netconf device. |
| 46 | * Set target port power or channel attenuation to an optical netconf device. |
| 47 | */ |
| 48 | public class OplinkOpticalPowerConfig<T> extends AbstractHandlerBehaviour |
| 49 | implements PowerConfig<T> { |
| 50 | |
| 51 | // key |
| 52 | public static final String KEY_CHNUM = "wavelength-number"; |
| 53 | public static final String KEY_CHPWR = "wavelength-power"; |
| 54 | public static final String KEY_CHSTATS = "wavelength-stats"; |
| 55 | public static final String KEY_OCMSTATS = "ocm-stats"; |
| 56 | public static final String KEY_PORTDIRECT_RX = "rx"; |
| 57 | public static final String KEY_PORTDIRECT_TX = "tx"; |
| 58 | public static final String KEY_PORTTARPWR = "port-target-power"; |
| 59 | public static final String KEY_PORTCURPWR = "port-current-power"; |
| 60 | public static final String KEY_PORTPROPERTY = "port-property"; |
| 61 | public static final String KEY_PORTPWRCAPMINRX = "port-power-capability-min-rx"; |
| 62 | public static final String KEY_PORTPWRCAPMAXRX = "port-power-capability-max-rx"; |
| 63 | public static final String KEY_PORTPWRCAPMINTX = "port-power-capability-min-tx"; |
| 64 | public static final String KEY_PORTPWRCAPMAXTX = "port-power-capability-max-tx"; |
| 65 | public static final String KEY_PORTS_PORT = String.format("%s.%s", KEY_DATA_PORTS, KEY_PORT); |
| 66 | public static final String KEY_PORTS_PORT_PROPERTY = String.format("%s.%s", KEY_PORTS_PORT, KEY_PORTPROPERTY); |
wei wang | d271fbc | 2017-12-04 15:36:31 -0800 | [diff] [blame^] | 67 | public static final String KEY_OCMS = "ocms"; |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 68 | // log |
| 69 | private static final Logger log = getLogger(OplinkOpticalPowerConfig.class); |
| 70 | |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 71 | @Override |
| 72 | public Optional<Long> getTargetPower(PortNumber port, T component) { |
| 73 | return Optional.ofNullable(acquireTargetPower(port, component)); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void setTargetPower(PortNumber port, T component, long power) { |
| 78 | if (component instanceof OchSignal) { |
| 79 | setChannelTargetPower(port, (OchSignal) component, power); |
| 80 | } else { |
| 81 | setPortTargetPower(port, power); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Optional<Long> currentPower(PortNumber port, T component) { |
| 87 | return Optional.ofNullable(acquireCurrentPower(port, component)); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Optional<Range<Long>> getTargetPowerRange(PortNumber port, T component) { |
| 92 | return Optional.ofNullable(getTxPowerRange(port, component)); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Optional<Range<Long>> getInputPowerRange(PortNumber port, T component) { |
| 97 | return Optional.ofNullable(getRxPowerRange(port, component)); |
| 98 | } |
| 99 | |
| 100 | private String getPortPowerFilter(PortNumber port, String selection) { |
| 101 | return new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
| 102 | .append(xmlOpen(KEY_PORTS)) |
| 103 | .append(xml(KEY_PORTID, Long.toString(port.toLong()))) |
| 104 | .append(xmlOpen(KEY_PORT)) |
| 105 | .append(xmlEmpty(selection)) |
| 106 | .append(xmlClose(KEY_PORT)) |
| 107 | .append(xmlClose(KEY_PORTS)) |
| 108 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 109 | .toString(); |
| 110 | } |
| 111 | |
| 112 | private String getChannelPowerFilter(PortNumber port, OchSignal channel) { |
| 113 | return new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
wei wang | d271fbc | 2017-12-04 15:36:31 -0800 | [diff] [blame^] | 114 | .append(xmlOpen(KEY_OCMS)) |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 115 | .append(xml(KEY_PORTID, Long.toString(port.toLong()))) |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 116 | .append(xmlOpen(KEY_OCMSTATS)) |
| 117 | .append(xml(KEY_CHNUM, Integer.toString(channel.spacingMultiplier()))) |
| 118 | .append(xmlEmpty(KEY_CHSTATS)) |
| 119 | .append(xmlClose(KEY_OCMSTATS)) |
wei wang | d271fbc | 2017-12-04 15:36:31 -0800 | [diff] [blame^] | 120 | .append(xmlClose(KEY_OCMS)) |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 121 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 122 | .toString(); |
| 123 | } |
| 124 | |
| 125 | private String getChannelAttenuationFilter(PortNumber port, OchSignal channel) { |
| 126 | return new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
| 127 | .append(xmlOpen(KEY_CONNS)) |
| 128 | .append(xml(KEY_CONNID, Integer.toString(channel.spacingMultiplier()))) |
| 129 | .append(xmlEmpty(KEY_CHATT)) |
| 130 | .append(xmlClose(KEY_CONNS)) |
| 131 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 132 | .toString(); |
| 133 | } |
| 134 | |
| 135 | private String getPowerRangeFilter(PortNumber port, String direction) { |
| 136 | return new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
| 137 | .append(xmlOpen(KEY_PORTS)) |
| 138 | .append(xml(KEY_PORTID, Long.toString(port.toLong()))) |
| 139 | .append(xmlOpen(KEY_PORT)) |
| 140 | .append(xml(KEY_PORTDIRECT, direction)) |
| 141 | .append(xmlEmpty(KEY_PORTPROPERTY)) |
| 142 | .append(xmlClose(KEY_PORT)) |
| 143 | .append(xmlClose(KEY_PORTS)) |
| 144 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 145 | .toString(); |
| 146 | } |
| 147 | |
| 148 | private Long acquireTargetPower(PortNumber port, T component) { |
| 149 | if (component instanceof OchSignal) { |
| 150 | return acquireChannelAttenuation(port, (OchSignal) component); |
| 151 | } |
| 152 | log.debug("Get port{} target power...", port); |
| 153 | return acquirePortPower(port, KEY_PORTTARPWR); |
| 154 | } |
| 155 | |
| 156 | private Long acquireCurrentPower(PortNumber port, T component) { |
| 157 | if (component instanceof OchSignal) { |
| 158 | return acquireChannelPower(port, (OchSignal) component); |
| 159 | } |
| 160 | log.debug("Get port{} current power...", port); |
| 161 | return acquirePortPower(port, KEY_PORTCURPWR); |
| 162 | } |
| 163 | |
| 164 | private Long acquirePortPower(PortNumber port, String selection) { |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 165 | String reply = netconfGet(handler(), getPortPowerFilter(port, selection)); |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 166 | HierarchicalConfiguration info = configAt(reply, KEY_PORTS_PORT); |
| 167 | if (info == null) { |
| 168 | return null; |
| 169 | } |
| 170 | return (long) (info.getDouble(selection) * POWER_MULTIPLIER); |
| 171 | } |
| 172 | |
| 173 | private Long acquireChannelAttenuation(PortNumber port, OchSignal channel) { |
| 174 | log.debug("Get port{} channel{} attenuation...", port, channel.channelSpacing()); |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 175 | String reply = netconfGet(handler(), getChannelAttenuationFilter(port, channel)); |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 176 | HierarchicalConfiguration info = configAt(reply, KEY_CONNS); |
| 177 | if (info == null) { |
| 178 | return null; |
| 179 | } |
| 180 | return (long) (info.getDouble(KEY_CHATT) * POWER_MULTIPLIER); |
| 181 | } |
| 182 | |
| 183 | private Long acquireChannelPower(PortNumber port, OchSignal channel) { |
| 184 | log.debug("Get port{} channel{} power...", port, channel.channelSpacing()); |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 185 | String reply = netconfGet(handler(), getChannelPowerFilter(port, channel)); |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 186 | HierarchicalConfiguration info = configAt(reply, KEY_DATA_CONNS); |
| 187 | if (info == null) { |
| 188 | return null; |
| 189 | } |
| 190 | return (long) (info.getDouble(KEY_CHPWR) * POWER_MULTIPLIER); |
| 191 | } |
| 192 | |
| 193 | private boolean setPortTargetPower(PortNumber port, long power) { |
| 194 | log.debug("Set port{} target power...", port); |
| 195 | String cfg = new StringBuilder(xmlOpen(KEY_OPENOPTICALDEV_XMLNS)) |
| 196 | .append(xmlOpen(KEY_PORTS)) |
| 197 | .append(xml(KEY_PORTID, Long.toString(port.toLong()))) |
| 198 | .append(xmlOpen(KEY_PORT)) |
| 199 | .append(xml(KEY_PORTTARPWR, Long.toString(power))) |
| 200 | .append(xmlClose(KEY_PORT)) |
| 201 | .append(xmlClose(KEY_PORTS)) |
| 202 | .append(xmlClose(KEY_OPENOPTICALDEV)) |
| 203 | .toString(); |
| 204 | return netconfEditConfig(handler(), CFG_MODE_MERGE, cfg); |
| 205 | } |
| 206 | |
| 207 | private boolean setChannelTargetPower(PortNumber port, OchSignal channel, long power) { |
| 208 | log.debug("Set port{} channel{} attenuation.", port, channel.channelSpacing()); |
| 209 | FlowRuleService service = handler().get(FlowRuleService.class); |
| 210 | Iterable<FlowEntry> entries = service.getFlowEntries(data().deviceId()); |
| 211 | for (FlowEntry entry : entries) { |
| 212 | OplinkCrossConnect crossConnect = OplinkOpticalUtility.fromFlowRule(this, entry); |
| 213 | // The channel port might be input port or output port. |
| 214 | if ((port.equals(crossConnect.getInPort()) || port.equals(crossConnect.getOutPort())) && |
| 215 | channel.spacingMultiplier() == crossConnect.getChannel()) { |
| 216 | log.debug("Flow is found, modify the flow with attenuation."); |
| 217 | // Modify attenuation in treatment |
| 218 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 219 | .setOutput(crossConnect.getOutPort()) |
| 220 | .extension(new OplinkAttenuation((int) power), data().deviceId()) |
| 221 | .build(); |
| 222 | // Apply the new flow rule |
| 223 | service.applyFlowRules(DefaultFlowRule.builder() |
| 224 | .forDevice(data().deviceId()) |
| 225 | .makePermanent() |
| 226 | .withSelector(entry.selector()) |
| 227 | .withTreatment(treatment) |
| 228 | .withPriority(entry.priority()) |
| 229 | .withCookie(entry.id().value()) |
| 230 | .build()); |
| 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | private Range<Long> getPowerRange(PortNumber port, String directionKey, String minKey, String maxKey) { |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 238 | // TODO |
| 239 | // Optical protection switch does not support power range configuration, it'll reply error. |
| 240 | // To prevent replying error log flooding from netconf session when polling all ports information, |
| 241 | // use general power range of [-60, 60] instead. |
| 242 | if (handler().get(DeviceService.class).getDevice(data().deviceId()).type() |
| 243 | == Device.Type.FIBER_SWITCH) { |
| 244 | return RANGE_GENERAL; |
| 245 | } |
| 246 | String reply = netconfGet(handler(), getPowerRangeFilter(port, directionKey)); |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 247 | HierarchicalConfiguration info = configAt(reply, KEY_PORTS_PORT_PROPERTY); |
| 248 | if (info == null) { |
| 249 | return null; |
| 250 | } |
| 251 | long minPower = (long) (info.getDouble(minKey) * POWER_MULTIPLIER); |
| 252 | long maxPower = (long) (info.getDouble(maxKey) * POWER_MULTIPLIER); |
| 253 | return Range.closed(minPower, maxPower); |
| 254 | } |
| 255 | |
| 256 | private Range<Long> getTxPowerRange(PortNumber port, T component) { |
| 257 | if (component instanceof Direction) { |
| 258 | log.debug("Get target port{} power range...", port); |
| 259 | return getPowerRange(port, KEY_PORTDIRECT_TX, KEY_PORTPWRCAPMINTX, KEY_PORTPWRCAPMAXTX); |
| 260 | } else { |
| 261 | log.debug("Get channel attenuation range..."); |
MaoLu | e00b742 | 2017-05-11 20:27:35 -0700 | [diff] [blame] | 262 | return RANGE_ATT; |
MaoLu | 819fde2 | 2017-04-20 17:17:49 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | private Range<Long> getRxPowerRange(PortNumber port, T component) { |
| 267 | log.debug("Get input port{} power range...", port); |
| 268 | return getPowerRange(port, KEY_PORTDIRECT_RX, KEY_PORTPWRCAPMINRX, KEY_PORTPWRCAPMAXRX); |
| 269 | } |
| 270 | } |