Jimmy Jin | 1085239 | 2017-01-24 13:45:13 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.protection; |
| 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.ConnectPoint; |
| 21 | import org.onosproject.net.FilteredConnectPoint; |
| 22 | import org.onosproject.net.Link; |
| 23 | import org.onosproject.net.LinkKey; |
| 24 | import org.onosproject.net.Port; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointDescription; |
| 27 | import org.onosproject.net.behaviour.protection.ProtectedTransportEndpointState; |
| 28 | import org.onosproject.net.behaviour.protection.ProtectionConfigBehaviour; |
| 29 | import org.onosproject.net.behaviour.protection.TransportEndpointDescription; |
| 30 | import org.onosproject.net.behaviour.protection.TransportEndpointId; |
| 31 | import org.onosproject.net.behaviour.protection.TransportEndpointState; |
| 32 | import org.onosproject.net.config.NetworkConfigService; |
| 33 | import org.onosproject.net.config.basics.BasicLinkConfig; |
| 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 36 | import org.onosproject.net.flow.DefaultFlowRule; |
| 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.FlowRule; |
| 40 | import org.onosproject.net.flow.FlowRuleService; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
| 43 | import org.onosproject.net.link.LinkService; |
| 44 | import org.onosproject.net.optical.OpticalAnnotations; |
| 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
| 48 | import java.util.ArrayList; |
| 49 | import java.util.HashMap; |
| 50 | import java.util.List; |
| 51 | import java.util.Map; |
| 52 | import java.util.Set; |
| 53 | import java.util.concurrent.CompletableFuture; |
| 54 | |
| 55 | import static org.onosproject.net.LinkKey.linkKey; |
| 56 | |
| 57 | /** |
| 58 | * Implementations of the protection behaviours for Oplink Optical Protection Switch (OPS). |
| 59 | * - Oplink OPS has 3 logical bi-directional ports (6 uni-directional physical ports): |
| 60 | * - port 1 is primary port for network side |
| 61 | * - port 2 is secondary port for network side. |
| 62 | * - port 3 is connected to the client side port; |
| 63 | * - Traffic protection |
| 64 | * - Traffic(Optical light) from client port is broadcasted (50/50 split) to |
| 65 | * both primary and secondary ports all the time. |
| 66 | * - In fault free condition, traffic from primary port is bridged to client port and |
| 67 | * in the case of primary port fails (LOS), traffic is bridged from secondary port to client port. |
| 68 | * - User initiated switch (to primary or secondary) is also supported. |
| 69 | */ |
| 70 | public class OplinkSwitchProtection extends AbstractHandlerBehaviour implements ProtectionConfigBehaviour { |
| 71 | |
| 72 | private static final int VIRTUAL_PORT = 0; |
| 73 | private static final int PRIMARY_PORT = 1; |
| 74 | private static final int SECONDARY_PORT = 2; |
| 75 | private static final int CLIENT_PORT = 3; |
| 76 | private static final String PRIMARY_ID = "primary_port"; |
| 77 | private static final String SECONDARY_ID = "secondary_port"; |
| 78 | private static final String OPLINK_FINGERPRINT = "OplinkOPS"; |
| 79 | |
| 80 | protected final Logger log = LoggerFactory.getLogger(getClass()); |
| 81 | |
| 82 | private FlowRuleService flowRuleService = this.handler().get(FlowRuleService.class); |
| 83 | private DeviceService deviceService = this.handler().get(DeviceService.class); |
| 84 | private NetworkConfigService netCfgService = this.handler().get(NetworkConfigService.class); |
| 85 | |
| 86 | @Override |
| 87 | public CompletableFuture<ConnectPoint> createProtectionEndpoint( |
| 88 | ProtectedTransportEndpointDescription configuration) { |
| 89 | //This OPS device only support one protection group of port 2 and port 3 |
| 90 | |
| 91 | CompletableFuture result = new CompletableFuture<ConnectPoint>(); |
| 92 | |
| 93 | //add flow from client port to virtual port. This set device in auto switch mode |
| 94 | addFlow(PortNumber.portNumber(VIRTUAL_PORT)); |
| 95 | |
| 96 | //add a virtual link bewteen two virtual ports of this device and peer |
| 97 | addLinkToPeer(configuration.peer()); |
| 98 | |
| 99 | result.complete(new ConnectPoint(this.data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT))); |
| 100 | |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public CompletableFuture<ConnectPoint> updateProtectionEndpoint( |
| 106 | ConnectPoint identifier, ProtectedTransportEndpointDescription configuration) { |
| 107 | |
| 108 | log.warn("Update protection configuration is not supported by this device"); |
| 109 | |
| 110 | CompletableFuture result = new CompletableFuture<ConnectPoint>(); |
| 111 | result.complete(new ConnectPoint(this.data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT))); |
| 112 | |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public CompletableFuture<Boolean> deleteProtectionEndpoint(ConnectPoint identifier) { |
| 118 | //OPS has only one protection group |
| 119 | CompletableFuture result = new CompletableFuture<Boolean>(); |
| 120 | |
| 121 | if (identifier.port().toLong() == VIRTUAL_PORT) { |
| 122 | //add a link bewteen two virtual ports of this device and peer |
| 123 | removeLinkToPeer(getPeerId()); |
| 124 | deleteFlow(); |
| 125 | result.complete(true); |
| 126 | } else { |
| 127 | result.complete(false); |
| 128 | } |
| 129 | |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointDescription>> getProtectionEndpointConfigs() { |
| 135 | ConnectPoint cp = new ConnectPoint(this.data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT)); |
| 136 | |
| 137 | Map<ConnectPoint, ProtectedTransportEndpointDescription> protectedGroups = new HashMap<>(); |
| 138 | CompletableFuture result = new CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointDescription>>(); |
| 139 | |
| 140 | protectedGroups.put(cp, getProtectedTransportEndpointDescription()); |
| 141 | result.complete(protectedGroups); |
| 142 | |
| 143 | return result; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointState>> getProtectionEndpointStates() { |
| 148 | ConnectPoint cp = new ConnectPoint(this.data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT)); |
| 149 | |
| 150 | Map<ConnectPoint, ProtectedTransportEndpointState> protectedGroups = new HashMap<>(); |
| 151 | CompletableFuture result = new CompletableFuture<Map<ConnectPoint, ProtectedTransportEndpointState>>(); |
| 152 | |
| 153 | protectedGroups.put(cp, getProtectedTransportEndpointState()); |
| 154 | result.complete(protectedGroups); |
| 155 | |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | ** index: PRIMARY_PORT - manual switch to primary port |
| 161 | * SECONDARY_PORT - manual switch to Secondary port |
| 162 | * VIRTUAL_PORT - automatic switch mode |
| 163 | */ |
| 164 | @Override |
| 165 | public CompletableFuture<Void> switchWorkingPath(ConnectPoint identifier, int index) { |
| 166 | CompletableFuture result = new CompletableFuture<Boolean>(); |
| 167 | |
| 168 | //send switch command to switch to device by sending a flow-mod message. |
| 169 | if (identifier.port().toLong() == VIRTUAL_PORT) { |
| 170 | deleteFlow(); |
| 171 | addFlow(PortNumber.portNumber(index)); |
| 172 | result.complete(true); |
| 173 | } else { |
| 174 | result.complete(false); |
| 175 | } |
| 176 | |
| 177 | return result; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * return the protected endpoint description of this devices |
| 182 | */ |
| 183 | private ProtectedTransportEndpointDescription getProtectedTransportEndpointDescription() { |
| 184 | List<TransportEndpointDescription> teds = new ArrayList<>(); |
| 185 | FilteredConnectPoint fcpPrimary = new FilteredConnectPoint( |
| 186 | new ConnectPoint(data().deviceId(), PortNumber.portNumber(PRIMARY_PORT))); |
| 187 | FilteredConnectPoint fcpSecondary = new FilteredConnectPoint( |
| 188 | new ConnectPoint(data().deviceId(), PortNumber.portNumber(SECONDARY_PORT))); |
| 189 | TransportEndpointDescription tedPrimary = TransportEndpointDescription.builder() |
| 190 | .withOutput(fcpPrimary).build(); |
| 191 | TransportEndpointDescription tedSecondary = TransportEndpointDescription.builder() |
| 192 | .withOutput(fcpSecondary).build(); |
| 193 | |
| 194 | teds.add(tedPrimary); |
| 195 | teds.add(tedSecondary); |
| 196 | return ProtectedTransportEndpointDescription.of(teds, |
| 197 | getPeerId(), |
| 198 | OPLINK_FINGERPRINT); |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * get endpoint state attributes |
| 203 | */ |
| 204 | private Map<String, String> getProtectionStateAttributes(PortNumber portNumber) { |
| 205 | Map<String, String> attributes = new HashMap<>(); |
| 206 | |
| 207 | //get status form port annotations, the status is update by hand shaker driver periodically |
| 208 | Port port = deviceService.getPort(this.data().deviceId(), portNumber); |
| 209 | if (port != null) { |
| 210 | String portStatus = port.annotations().value(OpticalAnnotations.INPUT_PORT_STATUS); |
| 211 | attributes.put(OpticalAnnotations.INPUT_PORT_STATUS, portStatus); |
| 212 | } |
| 213 | return attributes; |
| 214 | } |
| 215 | |
| 216 | private int getActiveIndex() { |
| 217 | Port port = deviceService.getPort(this.data().deviceId(), PortNumber.portNumber(PRIMARY_PORT)); |
| 218 | if (port != null) { |
| 219 | if (port.annotations().value(OpticalAnnotations.INPUT_PORT_STATUS) |
| 220 | .equals(OpticalAnnotations.STATUS_IN_SERVICE)) { |
| 221 | return PRIMARY_PORT; |
| 222 | } |
| 223 | } |
| 224 | return SECONDARY_PORT; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * get protected endpoint state |
| 229 | */ |
| 230 | private ProtectedTransportEndpointState getProtectedTransportEndpointState() { |
| 231 | List<TransportEndpointState> tess = new ArrayList<>(); |
| 232 | FilteredConnectPoint fcpPrimary = new FilteredConnectPoint( |
| 233 | new ConnectPoint(data().deviceId(), PortNumber.portNumber(PRIMARY_PORT))); |
| 234 | FilteredConnectPoint fcpSecondary = new FilteredConnectPoint( |
| 235 | new ConnectPoint(data().deviceId(), PortNumber.portNumber(SECONDARY_PORT))); |
| 236 | TransportEndpointDescription tedPrimary = TransportEndpointDescription.builder() |
| 237 | .withOutput(fcpPrimary).build(); |
| 238 | TransportEndpointDescription tedSecondary = TransportEndpointDescription.builder() |
| 239 | .withOutput(fcpSecondary).build(); |
| 240 | |
| 241 | TransportEndpointState tesPrimary = TransportEndpointState.builder() |
| 242 | .withDescription(tedPrimary) |
| 243 | .withId(TransportEndpointId.of(PRIMARY_ID)) |
| 244 | .addAttributes(getProtectionStateAttributes(PortNumber.portNumber(PRIMARY_PORT))) |
| 245 | .build(); |
| 246 | TransportEndpointState tesSecondary = TransportEndpointState.builder() |
| 247 | .withDescription(tedSecondary) |
| 248 | .withId(TransportEndpointId.of(SECONDARY_ID)) |
| 249 | .addAttributes(getProtectionStateAttributes((PortNumber.portNumber(SECONDARY_PORT)))) |
| 250 | .build(); |
| 251 | |
| 252 | tess.add(tesPrimary); |
| 253 | tess.add(tesSecondary); |
| 254 | return ProtectedTransportEndpointState.builder() |
| 255 | .withDescription(getProtectedTransportEndpointDescription()) |
| 256 | .withPathStates(tess) |
| 257 | .withActivePathIndex(getActiveIndex()) |
| 258 | .build(); |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * - Protection switch is controlled by setting up flow on the device |
| 263 | * - There is only one flow on the device at any point |
| 264 | * - A flow from virtual port to client port indicates the device is in auto switch mode |
| 265 | * - A flow from primary port to client port indicates the device is manually switched to primary |
| 266 | * - A flow from secondary port to client port indicates the device is manually switched to secondary |
| 267 | */ |
| 268 | private void addFlow(PortNumber workingPort) { |
| 269 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 270 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 271 | FlowRule.Builder flowRule = DefaultFlowRule.builder(); |
| 272 | |
| 273 | //set working port as flow's input port |
| 274 | selectorBuilder.matchInPort(workingPort); |
| 275 | |
| 276 | //the flow's output port is always the clinet port |
| 277 | treatment.setOutput(PortNumber.portNumber(CLIENT_PORT)); |
| 278 | |
| 279 | flowRule.forDevice(this.data().deviceId()) |
| 280 | .withSelector(selectorBuilder.build()) |
| 281 | .withTreatment(treatment.build()) |
| 282 | .makePermanent(); |
| 283 | |
| 284 | // install flow rule |
| 285 | flowRuleService.applyFlowRules(flowRule.build()); |
| 286 | } |
| 287 | /* |
| 288 | Delete all the flows to put device in default mode. |
| 289 | */ |
| 290 | private void deleteFlow() { |
| 291 | // remove all the flows. |
| 292 | flowRuleService.purgeFlowRules(this.data().deviceId()); |
| 293 | } |
| 294 | |
| 295 | private void addLinkToPeer(DeviceId peerId) { |
| 296 | |
| 297 | if (peerId == null) { |
| 298 | log.warn("PeerID is null for device {}", data().deviceId()); |
| 299 | return; |
| 300 | } |
| 301 | ConnectPoint dstCp = new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT)); |
| 302 | ConnectPoint srcCp = new ConnectPoint(peerId, PortNumber.portNumber(VIRTUAL_PORT)); |
| 303 | LinkKey link = linkKey(srcCp, dstCp); |
| 304 | BasicLinkConfig cfg = netCfgService.addConfig(link, BasicLinkConfig.class); |
| 305 | cfg.type(Link.Type.VIRTUAL); |
| 306 | cfg.apply(); |
| 307 | } |
| 308 | |
| 309 | private void removeLinkToPeer(DeviceId peerId) { |
| 310 | if (peerId == null) { |
| 311 | log.warn("PeerID is null for device {}", data().deviceId()); |
| 312 | return; |
| 313 | } |
| 314 | ConnectPoint dstCp = new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT)); |
| 315 | ConnectPoint srcCp = new ConnectPoint(peerId, PortNumber.portNumber(VIRTUAL_PORT)); |
| 316 | LinkKey link = linkKey(srcCp, dstCp); |
| 317 | netCfgService.removeConfig(link, BasicLinkConfig.class); |
| 318 | } |
| 319 | |
| 320 | private DeviceId getPeerId() { |
| 321 | ConnectPoint dstCp = new ConnectPoint(data().deviceId(), PortNumber.portNumber(VIRTUAL_PORT)); |
| 322 | Set<Link> links = this.handler().get(LinkService.class).getIngressLinks(dstCp); |
| 323 | |
| 324 | for (Link l : links) { |
| 325 | if (l.type() == Link.Type.VIRTUAL) { |
| 326 | // this devide is the destination and peer is the source. |
| 327 | return l.src().deviceId(); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return null; |
| 332 | } |
| 333 | } |