Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0947d7e | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [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.segmentrouting.pwaas; |
| 18 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Iterables; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import org.apache.commons.lang3.RandomUtils; |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 22 | import org.onlab.packet.Ethernet; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.MplsLabel; |
| 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onosproject.net.ConnectPoint; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DefaultLink; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Link; |
Charles Chan | 8316381 | 2018-01-09 13:45:07 -0800 | [diff] [blame] | 31 | import org.onosproject.net.Path; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 32 | import org.onosproject.net.PortNumber; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 33 | import org.onosproject.net.config.NetworkConfigEvent; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
| 37 | import org.onosproject.net.flow.TrafficTreatment; |
| 38 | import org.onosproject.net.flow.criteria.Criteria; |
| 39 | import org.onosproject.net.flowobjective.DefaultFilteringObjective; |
| 40 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 41 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 42 | import org.onosproject.net.flowobjective.DefaultObjectiveContext; |
| 43 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 44 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 45 | import org.onosproject.net.flowobjective.NextObjective; |
| 46 | import org.onosproject.net.flowobjective.Objective; |
| 47 | import org.onosproject.net.flowobjective.ObjectiveContext; |
| 48 | import org.onosproject.net.flowobjective.ObjectiveError; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 49 | import org.onosproject.segmentrouting.SegmentRoutingManager; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 50 | import org.onosproject.segmentrouting.SegmentRoutingService; |
| 51 | import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 52 | import org.onosproject.segmentrouting.config.PwaasConfig; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 53 | import org.onosproject.store.serializers.KryoNamespaces; |
| 54 | import org.onosproject.store.service.ConsistentMap; |
| 55 | import org.onosproject.store.service.Serializer; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 56 | import org.onosproject.store.service.Versioned; |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 60 | import java.util.List; |
| 61 | import java.util.Set; |
| 62 | import java.util.concurrent.CompletableFuture; |
| 63 | import java.util.stream.Collectors; |
| 64 | |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 65 | import static com.google.common.base.Preconditions.checkArgument; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 66 | import static org.onosproject.net.flowobjective.ForwardingObjective.Flag.VERSATILE; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 67 | import static org.onosproject.segmentrouting.pwaas.L2TunnelHandler.Pipeline.INITIATION; |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 68 | import static org.onosproject.segmentrouting.pwaas.L2TunnelHandler.Pipeline.TERMINATION; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 69 | import static org.onosproject.segmentrouting.pwaas.L2TunnelHandler.Result.*; |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 70 | import static org.onosproject.segmentrouting.pwaas.L2TunnelHandler.Direction.FWD; |
| 71 | import static org.onosproject.segmentrouting.pwaas.L2TunnelHandler.Direction.REV; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 72 | |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 73 | /** |
| 74 | * Handles pwaas related events. |
| 75 | */ |
| 76 | public class L2TunnelHandler { |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 77 | |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 78 | private static final Logger log = LoggerFactory.getLogger(L2TunnelHandler.class); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 79 | |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 80 | private final SegmentRoutingManager srManager; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 81 | /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 82 | * To store the next objectives related to the initiation. |
| 83 | */ |
| 84 | private final ConsistentMap<String, NextObjective> l2InitiationNextObjStore; |
| 85 | /** |
| 86 | * To store the next objectives related to the termination. |
| 87 | */ |
| 88 | private final ConsistentMap<String, NextObjective> l2TerminationNextObjStore; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 89 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 90 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 91 | * To store policies. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 92 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 93 | private final ConsistentMap<String, DefaultL2TunnelPolicy> l2PolicyStore; |
| 94 | |
| 95 | /** |
| 96 | * To store tunnels. |
| 97 | */ |
| 98 | private final ConsistentMap<String, DefaultL2Tunnel> l2TunnelStore; |
| 99 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 100 | private final KryoNamespace.Builder l2TunnelKryo; |
| 101 | |
| 102 | /** |
| 103 | * Create a l2 tunnel handler for the deploy and |
| 104 | * for the tear down of pseudo wires. |
| 105 | * |
| 106 | * @param segmentRoutingManager the segment routing manager |
| 107 | */ |
| 108 | public L2TunnelHandler(SegmentRoutingManager segmentRoutingManager) { |
| 109 | srManager = segmentRoutingManager; |
| 110 | l2TunnelKryo = new KryoNamespace.Builder() |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 111 | .register(KryoNamespaces.API) |
| 112 | .register(DefaultL2Tunnel.class, |
| 113 | DefaultL2TunnelPolicy.class, |
| 114 | L2Mode.class, |
| 115 | MplsLabel.class, |
| 116 | VlanId.class, |
| 117 | ConnectPoint.class); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 118 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 119 | l2InitiationNextObjStore = srManager. |
| 120 | storageService. |
| 121 | <String, NextObjective>consistentMapBuilder(). |
| 122 | withName("onos-l2initiation-nextobj-store"). |
| 123 | withSerializer(Serializer.using(l2TunnelKryo.build())). |
| 124 | build(); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 125 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 126 | l2TerminationNextObjStore = srManager.storageService. |
| 127 | <String, NextObjective>consistentMapBuilder() |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 128 | .withName("onos-l2termination-nextobj-store") |
| 129 | .withSerializer(Serializer.using(l2TunnelKryo.build())) |
| 130 | .build(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 131 | |
| 132 | l2PolicyStore = srManager.storageService |
| 133 | .<String, DefaultL2TunnelPolicy>consistentMapBuilder() |
| 134 | .withName("onos-l2-policy-store") |
| 135 | .withSerializer(Serializer.using(l2TunnelKryo.build())) |
| 136 | .build(); |
| 137 | |
| 138 | l2TunnelStore = srManager.storageService |
| 139 | .<String, DefaultL2Tunnel>consistentMapBuilder() |
| 140 | .withName("onos-l2-tunnel-store") |
| 141 | .withSerializer(Serializer.using(l2TunnelKryo.build())) |
| 142 | .build(); |
| 143 | } |
| 144 | |
| 145 | /** |
Andreas Pantelopoulos | 75eef06 | 2018-01-11 07:53:48 -0800 | [diff] [blame] | 146 | * Deploys any pre-existing pseudowires in the configuration. |
| 147 | * Used by manager only in initialization. |
| 148 | */ |
| 149 | public void init() { |
| 150 | |
| 151 | PwaasConfig config = srManager.cfgService.getConfig(srManager.appId(), PwaasConfig.class); |
| 152 | if (config == null) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | log.info("Deploying existing pseudowires"); |
| 157 | |
| 158 | // gather pseudowires |
| 159 | Set<DefaultL2TunnelDescription> pwToAdd = config |
| 160 | .getPwIds() |
| 161 | .stream() |
| 162 | .map(config::getPwDescription) |
| 163 | .collect(Collectors.toSet()); |
| 164 | |
| 165 | // deploy pseudowires |
| 166 | deploy(pwToAdd); |
| 167 | } |
| 168 | |
| 169 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 170 | * Returns all L2 Policies. |
| 171 | * |
| 172 | * @return List of policies |
| 173 | */ |
| 174 | public List<DefaultL2TunnelPolicy> getL2Policies() { |
| 175 | |
| 176 | return l2PolicyStore |
| 177 | .values() |
| 178 | .stream() |
| 179 | .map(Versioned::value) |
| 180 | .map(DefaultL2TunnelPolicy::new) |
| 181 | .collect(Collectors.toList()); |
| 182 | |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns all L2 Tunnels. |
| 187 | * |
| 188 | * @return List of tunnels. |
| 189 | */ |
| 190 | public List<DefaultL2Tunnel> getL2Tunnels() { |
| 191 | |
| 192 | return l2TunnelStore |
| 193 | .values() |
| 194 | .stream() |
| 195 | .map(Versioned::value) |
| 196 | .map(DefaultL2Tunnel::new) |
| 197 | .collect(Collectors.toList()); |
| 198 | |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Processes a link removal. Finds affected pseudowires and rewires them. |
| 203 | * TODO: Make it also take into account failures of links that are used for pw |
| 204 | * traffic in the spine. |
| 205 | * @param link The link that failed |
| 206 | */ |
| 207 | public void processLinkDown(Link link) { |
| 208 | |
| 209 | List<DefaultL2Tunnel> tunnels = getL2Tunnels(); |
| 210 | List<DefaultL2TunnelPolicy> policies = getL2Policies(); |
| 211 | |
| 212 | // determine affected pseudowires and update them at once |
| 213 | Set<DefaultL2TunnelDescription> pwToUpdate = tunnels |
| 214 | .stream() |
| 215 | .filter(tun -> tun.pathUsed().contains(link)) |
| 216 | .map(l2Tunnel -> { |
| 217 | DefaultL2TunnelPolicy policy = null; |
| 218 | for (DefaultL2TunnelPolicy l2Policy : policies) { |
| 219 | if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) { |
| 220 | policy = l2Policy; |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return new DefaultL2TunnelDescription(l2Tunnel, policy); |
| 226 | }) |
| 227 | .collect(Collectors.toSet()); |
| 228 | |
| 229 | |
| 230 | log.info("Pseudowires affected by link failure : {}, rerouting them...", pwToUpdate); |
| 231 | |
| 232 | // update all pseudowires |
| 233 | pwToUpdate.forEach(tun -> updatePw(tun, tun)); |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Processes Pwaas Config added event. |
| 238 | * |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 239 | * @param event network config add event |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 240 | */ |
| 241 | public void processPwaasConfigAdded(NetworkConfigEvent event) { |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 242 | checkArgument(event.config().isPresent(), |
| 243 | "Config is not presented in PwaasConfigAdded event {}", event); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 244 | |
| 245 | log.info("Network event : Pseudowire configuration added!"); |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 246 | PwaasConfig config = (PwaasConfig) event.config().get(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 247 | |
| 248 | // gather pseudowires |
| 249 | Set<DefaultL2TunnelDescription> pwToAdd = config |
| 250 | .getPwIds() |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 251 | .stream() |
| 252 | .map(config::getPwDescription) |
| 253 | .collect(Collectors.toSet()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 254 | |
| 255 | // deploy pseudowires |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 256 | deploy(pwToAdd); |
| 257 | } |
| 258 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 259 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 260 | * Returns the new vlan id for an ingress point of a |
| 261 | * pseudowire. For double tagged, it is the outer, |
| 262 | * For single tagged it is the single tag, and for |
| 263 | * inner it is None. |
| 264 | * |
| 265 | * @param ingressOuter vlanid of ingress outer |
| 266 | * @param ingressInner vlanid of ingress inner |
| 267 | * @param egressOuter vlanid of egress outer |
| 268 | * @param egressInner vlanid of egress inner |
| 269 | * @return returns the vlan id which will be installed at vlan table 1. |
| 270 | */ |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 271 | private VlanId determineEgressVlan(VlanId ingressOuter, VlanId ingressInner, |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 272 | VlanId egressOuter, VlanId egressInner) { |
| 273 | |
| 274 | // validity of vlan combinations was checked at verifyPseudowire |
| 275 | if (!(ingressOuter.equals(VlanId.NONE))) { |
| 276 | return egressOuter; |
| 277 | } else if (!(ingressInner.equals(VlanId.NONE))) { |
| 278 | return egressInner; |
| 279 | } else { |
| 280 | return VlanId.vlanId("None"); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 285 | * Adds a single pseudowire from leaf to a leaf. |
| 286 | * This method can be called from cli commands |
Charles Chan | e5a3255 | 2018-01-06 17:57:27 -0800 | [diff] [blame] | 287 | * without configuration updates, thus it does not check for mastership |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 288 | * of the ingress pseudowire device. |
| 289 | * |
| 290 | * @param pw The pseudowire |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 291 | * @param spinePw True if pseudowire is from leaf to spine |
| 292 | * @return result of pseudowire deployment |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 293 | */ |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 294 | private Result deployPseudowire(DefaultL2TunnelDescription pw, boolean spinePw) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 295 | |
| 296 | Result result; |
| 297 | long l2TunnelId; |
| 298 | |
| 299 | l2TunnelId = pw.l2Tunnel().tunnelId(); |
| 300 | |
| 301 | // The tunnel id cannot be 0. |
| 302 | if (l2TunnelId == 0) { |
| 303 | log.warn("Tunnel id id must be > 0"); |
| 304 | return Result.ADDITION_ERROR; |
| 305 | } |
| 306 | |
| 307 | // get path here, need to use the same for fwd and rev direction |
| 308 | List<Link> path = getPath(pw.l2TunnelPolicy().cP1(), |
| 309 | pw.l2TunnelPolicy().cP2()); |
| 310 | if (path == null) { |
| 311 | log.info("Deploying process : No path between the connection points for pseudowire {}", l2TunnelId); |
| 312 | return WRONG_PARAMETERS; |
| 313 | } |
| 314 | |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 315 | Link fwdNextHop; |
| 316 | Link revNextHop; |
| 317 | if (!spinePw) { |
| 318 | if (path.size() != 2) { |
| 319 | log.info("Deploying process : Path between two leafs should have size of 2, for pseudowire {}", |
| 320 | l2TunnelId); |
| 321 | return INTERNAL_ERROR; |
| 322 | } |
| 323 | |
| 324 | fwdNextHop = path.get(0); |
| 325 | revNextHop = reverseLink(path.get(1)); |
| 326 | } else { |
| 327 | if (path.size() != 1) { |
Charles Chan | e5a3255 | 2018-01-06 17:57:27 -0800 | [diff] [blame] | 328 | log.info("Deploying process : Path between leaf spine should equal to 1, for pseudowire {}", |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 329 | l2TunnelId); |
| 330 | return INTERNAL_ERROR; |
| 331 | } |
| 332 | |
| 333 | fwdNextHop = path.get(0); |
| 334 | revNextHop = reverseLink(path.get(0)); |
| 335 | } |
| 336 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 337 | pw.l2Tunnel().setPath(path); |
| 338 | |
| 339 | // next hops for next objectives |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 340 | |
| 341 | log.info("Deploying process : Establishing forward direction for pseudowire {}", l2TunnelId); |
| 342 | |
| 343 | // We establish the tunnel. |
| 344 | // result.nextId will be used in fwd |
| 345 | result = deployPseudoWireInit(pw.l2Tunnel(), |
| 346 | pw.l2TunnelPolicy().cP1(), |
| 347 | pw.l2TunnelPolicy().cP2(), |
| 348 | FWD, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 349 | fwdNextHop, |
| 350 | spinePw); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 351 | if (result != SUCCESS) { |
| 352 | log.info("Deploying process : Error in deploying pseudowire initiation for CP1"); |
| 353 | return Result.ADDITION_ERROR; |
| 354 | } |
| 355 | |
| 356 | VlanId egressVlan = determineEgressVlan(pw.l2TunnelPolicy().cP1OuterTag(), |
| 357 | pw.l2TunnelPolicy().cP1InnerTag(), |
| 358 | pw.l2TunnelPolicy().cP2OuterTag(), |
| 359 | pw.l2TunnelPolicy().cP2InnerTag()); |
| 360 | |
| 361 | // We create the policy. |
| 362 | result = deployPolicy(l2TunnelId, |
| 363 | pw.l2TunnelPolicy().cP1(), |
| 364 | pw.l2TunnelPolicy().cP1InnerTag(), |
| 365 | pw.l2TunnelPolicy().cP1OuterTag(), |
| 366 | egressVlan, |
| 367 | result.nextId); |
| 368 | if (result != SUCCESS) { |
| 369 | log.info("Deploying process : Error in deploying pseudowire policy for CP1"); |
| 370 | return Result.ADDITION_ERROR; |
| 371 | } |
| 372 | |
| 373 | // We terminate the tunnel |
| 374 | result = deployPseudoWireTerm(pw.l2Tunnel(), |
| 375 | pw.l2TunnelPolicy().cP2(), |
Andreas Pantelopoulos | 5a243d6 | 2018-01-17 10:03:00 -0800 | [diff] [blame] | 376 | VlanId.NONE, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 377 | FWD, |
| 378 | spinePw); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 379 | |
| 380 | if (result != SUCCESS) { |
| 381 | log.info("Deploying process : Error in deploying pseudowire termination for CP1"); |
| 382 | return Result.ADDITION_ERROR; |
| 383 | |
| 384 | } |
| 385 | |
| 386 | log.info("Deploying process : Establishing reverse direction for pseudowire {}", l2TunnelId); |
| 387 | |
| 388 | // We establish the reverse tunnel. |
| 389 | result = deployPseudoWireInit(pw.l2Tunnel(), |
| 390 | pw.l2TunnelPolicy().cP2(), |
| 391 | pw.l2TunnelPolicy().cP1(), |
| 392 | REV, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 393 | revNextHop, |
| 394 | spinePw); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 395 | if (result != SUCCESS) { |
| 396 | log.info("Deploying process : Error in deploying pseudowire initiation for CP2"); |
| 397 | return Result.ADDITION_ERROR; |
| 398 | } |
| 399 | |
| 400 | egressVlan = determineEgressVlan(pw.l2TunnelPolicy().cP2OuterTag(), |
| 401 | pw.l2TunnelPolicy().cP2InnerTag(), |
| 402 | pw.l2TunnelPolicy().cP1OuterTag(), |
| 403 | pw.l2TunnelPolicy().cP1InnerTag()); |
| 404 | result = deployPolicy(l2TunnelId, |
| 405 | pw.l2TunnelPolicy().cP2(), |
| 406 | pw.l2TunnelPolicy().cP2InnerTag(), |
| 407 | pw.l2TunnelPolicy().cP2OuterTag(), |
| 408 | egressVlan, |
| 409 | result.nextId); |
| 410 | if (result != SUCCESS) { |
| 411 | log.info("Deploying process : Error in deploying policy for CP2"); |
| 412 | return Result.ADDITION_ERROR; |
| 413 | } |
| 414 | |
| 415 | result = deployPseudoWireTerm(pw.l2Tunnel(), |
| 416 | pw.l2TunnelPolicy().cP1(), |
Andreas Pantelopoulos | 5a243d6 | 2018-01-17 10:03:00 -0800 | [diff] [blame] | 417 | VlanId.NONE, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 418 | REV, |
| 419 | spinePw); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 420 | |
| 421 | if (result != SUCCESS) { |
| 422 | log.info("Deploying process : Error in deploying pseudowire termination for CP2"); |
| 423 | return Result.ADDITION_ERROR; |
| 424 | } |
| 425 | |
| 426 | log.info("Deploying process : Updating relevant information for pseudowire {}", l2TunnelId); |
| 427 | |
| 428 | // Populate stores |
| 429 | l2TunnelStore.put(Long.toString(l2TunnelId), pw.l2Tunnel()); |
| 430 | l2PolicyStore.put(Long.toString(l2TunnelId), pw.l2TunnelPolicy()); |
| 431 | |
| 432 | return Result.SUCCESS; |
| 433 | } |
| 434 | |
| 435 | /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 436 | * To deploy a number of pseudo wires. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 437 | * <p> |
| 438 | * Called ONLY when configuration changes, thus the check |
| 439 | * for the mastership of the device. |
| 440 | * <p> |
| 441 | * Only the master of CP1 will deploy this pseudowire. |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 442 | * |
| 443 | * @param pwToAdd the set of pseudo wires to add |
| 444 | */ |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 445 | private void deploy(Set<DefaultL2TunnelDescription> pwToAdd) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 446 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 447 | Result result; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 448 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 449 | for (DefaultL2TunnelDescription currentL2Tunnel : pwToAdd) { |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 450 | ConnectPoint cp1 = currentL2Tunnel.l2TunnelPolicy().cP1(); |
| 451 | ConnectPoint cp2 = currentL2Tunnel.l2TunnelPolicy().cP2(); |
| 452 | long tunnelId = currentL2Tunnel.l2TunnelPolicy().tunnelId(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 453 | |
| 454 | // only the master of CP1 will program this pseudowire |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 455 | if (!srManager.isMasterOf(cp1)) { |
| 456 | log.debug("Not the master of {}. Ignore pseudo wire deployment id={}", cp1, tunnelId); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 457 | continue; |
| 458 | } |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 459 | |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 460 | try { |
| 461 | // differentiate between leaf-leaf pseudowires and leaf-spine |
| 462 | // and pass the appropriate flag in them. |
| 463 | if (!srManager.deviceConfiguration().isEdgeDevice(cp1.deviceId()) && |
| 464 | !srManager.deviceConfiguration().isEdgeDevice(cp2.deviceId())) { |
| 465 | log.warn("Can not deploy pseudowire from spine to spine!"); |
| 466 | result = Result.INTERNAL_ERROR; |
| 467 | } else if (srManager.deviceConfiguration().isEdgeDevice(cp1.deviceId()) && |
| 468 | srManager.deviceConfiguration().isEdgeDevice(cp2.deviceId())) { |
| 469 | log.info("Deploying a leaf-leaf pseudowire {}", tunnelId); |
| 470 | result = deployPseudowire(currentL2Tunnel, false); |
| 471 | } else { |
| 472 | log.info("Deploying a leaf-spine pseudowire {}", tunnelId); |
| 473 | result = deployPseudowire(currentL2Tunnel, true); |
| 474 | } |
| 475 | } catch (DeviceConfigNotFoundException e) { |
| 476 | log.error("Exception caught when deploying pseudowire", e.toString()); |
| 477 | result = Result.INTERNAL_ERROR; |
| 478 | } |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 479 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 480 | switch (result) { |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 481 | case INTERNAL_ERROR: |
| 482 | log.warn("Could not deploy pseudowire {}, internal error!", tunnelId); |
| 483 | break; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 484 | case WRONG_PARAMETERS: |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 485 | log.warn("Could not deploy pseudowire {}, wrong parameters!", tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 486 | break; |
| 487 | case ADDITION_ERROR: |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 488 | log.warn("Could not deploy pseudowire {}, error in populating rules!", tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 489 | break; |
| 490 | default: |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 491 | log.info("Pseudowire with {} succesfully deployed!", tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 492 | break; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 493 | } |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 494 | } |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 498 | * Processes PWaaS Config updated event. |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 499 | * |
| 500 | * @param event network config updated event |
| 501 | */ |
| 502 | public void processPwaasConfigUpdated(NetworkConfigEvent event) { |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 503 | checkArgument(event.config().isPresent(), |
| 504 | "Config is not presented in PwaasConfigUpdated event {}", event); |
| 505 | checkArgument(event.prevConfig().isPresent(), |
| 506 | "PrevConfig is not presented in PwaasConfigUpdated event {}", event); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 507 | |
| 508 | log.info("Pseudowire configuration updated."); |
| 509 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 510 | // We retrieve the old pseudo wires. |
| 511 | PwaasConfig prevConfig = (PwaasConfig) event.prevConfig().get(); |
| 512 | Set<Long> prevPws = prevConfig.getPwIds(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 513 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 514 | // We retrieve the new pseudo wires. |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 515 | PwaasConfig config = (PwaasConfig) event.config().get(); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 516 | Set<Long> newPws = config.getPwIds(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 517 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 518 | // We compute the pseudo wires to update. |
| 519 | Set<Long> updPws = newPws.stream() |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 520 | .filter(tunnelId -> prevPws.contains(tunnelId) |
| 521 | && !config.getPwDescription(tunnelId).equals(prevConfig.getPwDescription(tunnelId))) |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 522 | .collect(Collectors.toSet()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 523 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 524 | // The pseudo wires to remove. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 525 | Set<Long> rmvPWs = prevPws.stream() |
| 526 | .filter(tunnelId -> !newPws.contains(tunnelId)).collect(Collectors.toSet()); |
| 527 | |
| 528 | Set<DefaultL2TunnelDescription> pwToRemove = rmvPWs.stream() |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 529 | .map(prevConfig::getPwDescription) |
| 530 | .collect(Collectors.toSet()); |
| 531 | tearDown(pwToRemove); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 532 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 533 | // The pseudo wires to add. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 534 | Set<Long> addedPWs = newPws.stream() |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 535 | .filter(tunnelId -> !prevPws.contains(tunnelId)) |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 536 | .collect(Collectors.toSet()); |
| 537 | Set<DefaultL2TunnelDescription> pwToAdd = addedPWs.stream() |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 538 | .map(config::getPwDescription) |
| 539 | .collect(Collectors.toSet()); |
| 540 | deploy(pwToAdd); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 541 | |
| 542 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 543 | // The pseudo wires to update. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 544 | updPws.forEach(tunnelId -> updatePw(prevConfig.getPwDescription(tunnelId), |
| 545 | config.getPwDescription(tunnelId))); |
| 546 | |
| 547 | log.info("Pseudowires removed : {}, Pseudowires updated : {}, Pseudowires added : {}", rmvPWs, |
| 548 | updPws, addedPWs); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Helper function to update a pw. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 553 | * <p> |
| 554 | * Called upon configuration changes that update existing pseudowires and |
| 555 | * when links fail. Checking of mastership for CP1 is mandatory because it is |
| 556 | * called in multiple instances for both cases. |
| 557 | * <p> |
| 558 | * Meant to call asynchronously for various events, thus this call can not block and need |
| 559 | * to perform asynchronous operations. |
| 560 | * <p> |
| 561 | * For this reason error checking is omitted. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 562 | * |
| 563 | * @param oldPw the pseudo wire to remove |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 564 | * @param newPw the pseudo wire to add |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 565 | */ |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 566 | private void updatePw(DefaultL2TunnelDescription oldPw, |
| 567 | DefaultL2TunnelDescription newPw) { |
| 568 | ConnectPoint oldCp1 = oldPw.l2TunnelPolicy().cP1(); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 569 | long tunnelId = oldPw.l2Tunnel().tunnelId(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 570 | |
| 571 | // only the master of CP1 will update this pseudowire |
| 572 | if (!srManager.isMasterOf(oldPw.l2TunnelPolicy().cP1())) { |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 573 | log.debug("Not the master of {}. Ignore pseudo wire update id={}", oldCp1, tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 574 | return; |
| 575 | } |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 576 | // only determine if the new pseudowire is leaf-spine, because |
| 577 | // removal process is the same for both leaf-leaf and leaf-spine |
| 578 | // pws. |
| 579 | boolean newPwSpine; |
| 580 | try { |
| 581 | newPwSpine = !srManager.deviceConfiguration().isEdgeDevice(newPw.l2TunnelPolicy().cP1().deviceId()) || |
| 582 | !srManager.deviceConfiguration().isEdgeDevice(newPw.l2TunnelPolicy().cP2().deviceId()); |
| 583 | } catch (DeviceConfigNotFoundException e) { |
| 584 | // if exception is caught treat the newpw as leaf-leaf |
| 585 | newPwSpine = false; |
| 586 | } |
| 587 | |
| 588 | // copy the variable here because we need to |
| 589 | // use it in lambda thus it needs to be final |
| 590 | boolean finalNewPwSpine = newPwSpine; |
| 591 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 592 | |
| 593 | log.info("Updating pseudowire {}", oldPw.l2Tunnel().tunnelId()); |
| 594 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 595 | // The async tasks to orchestrate the next and |
| 596 | // forwarding update. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 597 | CompletableFuture<ObjectiveError> fwdInitNextFuture = new CompletableFuture<>(); |
| 598 | CompletableFuture<ObjectiveError> revInitNextFuture = new CompletableFuture<>(); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 599 | CompletableFuture<ObjectiveError> fwdTermNextFuture = new CompletableFuture<>(); |
| 600 | CompletableFuture<ObjectiveError> revTermNextFuture = new CompletableFuture<>(); |
| 601 | CompletableFuture<ObjectiveError> fwdPwFuture = new CompletableFuture<>(); |
| 602 | CompletableFuture<ObjectiveError> revPwFuture = new CompletableFuture<>(); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 603 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 604 | // First we remove both policy. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 605 | log.debug("Start deleting fwd policy for {}", tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 606 | |
| 607 | // first delete all information from our stores |
| 608 | // we can not do it asynchronously |
| 609 | l2PolicyStore.remove(Long.toString(tunnelId)); |
| 610 | l2TunnelStore.remove(Long.toString(tunnelId)); |
| 611 | |
| 612 | VlanId egressVlan = determineEgressVlan(oldPw.l2TunnelPolicy().cP1OuterTag(), |
| 613 | oldPw.l2TunnelPolicy().cP1InnerTag(), |
| 614 | oldPw.l2TunnelPolicy().cP2OuterTag(), |
| 615 | oldPw.l2TunnelPolicy().cP2InnerTag()); |
| 616 | deletePolicy(tunnelId, |
| 617 | oldPw.l2TunnelPolicy().cP1(), |
| 618 | oldPw.l2TunnelPolicy().cP1InnerTag(), |
| 619 | oldPw.l2TunnelPolicy().cP1OuterTag(), |
| 620 | egressVlan, |
| 621 | fwdInitNextFuture, |
| 622 | FWD); |
| 623 | |
| 624 | log.debug("Update process : Start deleting rev policy for {}", tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 625 | egressVlan = determineEgressVlan(oldPw.l2TunnelPolicy().cP2OuterTag(), |
| 626 | oldPw.l2TunnelPolicy().cP2InnerTag(), |
| 627 | oldPw.l2TunnelPolicy().cP1OuterTag(), |
| 628 | oldPw.l2TunnelPolicy().cP1InnerTag()); |
| 629 | deletePolicy(tunnelId, |
| 630 | oldPw.l2TunnelPolicy().cP2(), |
| 631 | oldPw.l2TunnelPolicy().cP2InnerTag(), |
| 632 | oldPw.l2TunnelPolicy().cP2OuterTag(), |
| 633 | egressVlan, revInitNextFuture, |
| 634 | REV); |
| 635 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 636 | // Finally we remove both the tunnels. |
| 637 | fwdInitNextFuture.thenAcceptAsync(status -> { |
| 638 | if (status == null) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 639 | log.debug("Update process : Fwd policy removed. " + |
| 640 | "Now remove fwd {} for {}", INITIATION, tunnelId); |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 641 | tearDownPseudoWireInit(tunnelId, oldPw.l2TunnelPolicy().cP1(), fwdTermNextFuture, FWD); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 642 | } |
| 643 | }); |
| 644 | revInitNextFuture.thenAcceptAsync(status -> { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 645 | if (status == null) { |
| 646 | log.debug("Update process : Rev policy removed. " + |
| 647 | "Now remove rev {} for {}", INITIATION, tunnelId); |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 648 | tearDownPseudoWireInit(tunnelId, oldPw.l2TunnelPolicy().cP2(), revTermNextFuture, REV); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 649 | } |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 650 | }); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 651 | fwdTermNextFuture.thenAcceptAsync(status -> { |
| 652 | if (status == null) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 653 | log.debug("Update process : Fwd {} removed. " + |
| 654 | "Now remove fwd {} for {}", INITIATION, TERMINATION, tunnelId); |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 655 | tearDownPseudoWireTerm(oldPw.l2Tunnel(), oldPw.l2TunnelPolicy().cP2(), fwdPwFuture, FWD); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 656 | } |
| 657 | }); |
| 658 | revTermNextFuture.thenAcceptAsync(status -> { |
| 659 | if (status == null) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 660 | log.debug("Update process : Rev {} removed. " + |
| 661 | "Now remove rev {} for {}", INITIATION, TERMINATION, tunnelId); |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 662 | tearDownPseudoWireTerm(oldPw.l2Tunnel(), oldPw.l2TunnelPolicy().cP1(), revPwFuture, REV); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 663 | } |
| 664 | }); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 665 | |
| 666 | // get path here, need to use the same for fwd and rev direction |
| 667 | List<Link> path = getPath(newPw.l2TunnelPolicy().cP1(), |
| 668 | newPw.l2TunnelPolicy().cP2()); |
| 669 | if (path == null) { |
| 670 | log.info("Deploying process : " + |
| 671 | "No path between the connection points for pseudowire {}", newPw.l2Tunnel().tunnelId()); |
| 672 | return; |
| 673 | } |
| 674 | |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 675 | Link fwdNextHop, revNextHop; |
| 676 | if (!finalNewPwSpine) { |
| 677 | if (path.size() != 2) { |
| 678 | log.info("Update process : Error, path between two leafs should have size of 2, for pseudowire {}", |
| 679 | newPw.l2Tunnel().tunnelId()); |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | fwdNextHop = path.get(0); |
| 684 | revNextHop = reverseLink(path.get(1)); |
| 685 | } else { |
| 686 | if (path.size() != 1) { |
Charles Chan | e5a3255 | 2018-01-06 17:57:27 -0800 | [diff] [blame] | 687 | log.info("Update process : Error, path between leaf spine should equal to 1, for pseudowire {}", |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 688 | newPw.l2Tunnel().tunnelId()); |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | fwdNextHop = path.get(0); |
| 693 | revNextHop = reverseLink(path.get(0)); |
| 694 | } |
| 695 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 696 | newPw.l2Tunnel().setPath(path); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 697 | |
| 698 | // At the end we install the updated PW. |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 699 | fwdPwFuture.thenAcceptAsync(status -> { |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 700 | if (status == null) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 701 | |
| 702 | // Upgrade stores and book keeping information, need to move this here |
| 703 | // cause this call is asynchronous. |
| 704 | l2PolicyStore.put(Long.toString(tunnelId), newPw.l2TunnelPolicy()); |
| 705 | l2TunnelStore.put(Long.toString(tunnelId), newPw.l2Tunnel()); |
| 706 | |
| 707 | log.debug("Update process : Deploying new fwd pw for {}", tunnelId); |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 708 | Result lamdaResult = deployPseudoWireInit(newPw.l2Tunnel(), newPw.l2TunnelPolicy().cP1(), |
| 709 | newPw.l2TunnelPolicy().cP2(), FWD, |
| 710 | fwdNextHop, finalNewPwSpine); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 711 | if (lamdaResult != SUCCESS) { |
| 712 | return; |
| 713 | } |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 714 | |
| 715 | VlanId egressVlanId = determineEgressVlan(newPw.l2TunnelPolicy().cP1OuterTag(), |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 716 | newPw.l2TunnelPolicy().cP1InnerTag(), |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 717 | newPw.l2TunnelPolicy().cP2OuterTag(), |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 718 | newPw.l2TunnelPolicy().cP2InnerTag()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 719 | |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 720 | lamdaResult = deployPolicy(tunnelId, newPw.l2TunnelPolicy().cP1(), |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 721 | newPw.l2TunnelPolicy().cP1InnerTag(), |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 722 | newPw.l2TunnelPolicy().cP1OuterTag(), |
| 723 | egressVlanId, lamdaResult.nextId); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 724 | if (lamdaResult != SUCCESS) { |
| 725 | return; |
| 726 | } |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 727 | deployPseudoWireTerm(newPw.l2Tunnel(), newPw.l2TunnelPolicy().cP2(), |
Andreas Pantelopoulos | 5a243d6 | 2018-01-17 10:03:00 -0800 | [diff] [blame] | 728 | VlanId.NONE, FWD, finalNewPwSpine); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 729 | |
| 730 | } |
| 731 | }); |
| 732 | revPwFuture.thenAcceptAsync(status -> { |
| 733 | if (status == null) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 734 | log.debug("Update process : Deploying new rev pw for {}", tunnelId); |
| 735 | Result lamdaResult = deployPseudoWireInit(newPw.l2Tunnel(), |
| 736 | newPw.l2TunnelPolicy().cP2(), |
| 737 | newPw.l2TunnelPolicy().cP1(), |
| 738 | REV, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 739 | revNextHop, finalNewPwSpine); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 740 | if (lamdaResult != SUCCESS) { |
| 741 | return; |
| 742 | } |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 743 | |
| 744 | VlanId egressVlanId = determineEgressVlan(newPw.l2TunnelPolicy().cP2OuterTag(), |
| 745 | newPw.l2TunnelPolicy().cP2InnerTag(), |
| 746 | newPw.l2TunnelPolicy().cP1OuterTag(), |
| 747 | newPw.l2TunnelPolicy().cP1InnerTag()); |
| 748 | lamdaResult = deployPolicy(tunnelId, |
| 749 | newPw.l2TunnelPolicy().cP2(), |
| 750 | newPw.l2TunnelPolicy().cP2InnerTag(), |
| 751 | newPw.l2TunnelPolicy().cP2OuterTag(), |
| 752 | egressVlanId, |
| 753 | lamdaResult.nextId); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 754 | if (lamdaResult != SUCCESS) { |
| 755 | return; |
| 756 | } |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 757 | deployPseudoWireTerm(newPw.l2Tunnel(), |
| 758 | newPw.l2TunnelPolicy().cP1(), |
Andreas Pantelopoulos | 5a243d6 | 2018-01-17 10:03:00 -0800 | [diff] [blame] | 759 | VlanId.NONE, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 760 | REV, finalNewPwSpine); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 761 | } |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 762 | }); |
| 763 | } |
| 764 | |
| 765 | /** |
| 766 | * Processes Pwaas Config removed event. |
| 767 | * |
| 768 | * @param event network config removed event |
| 769 | */ |
| 770 | public void processPwaasConfigRemoved(NetworkConfigEvent event) { |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 771 | checkArgument(event.prevConfig().isPresent(), |
| 772 | "PrevConfig is not presented in PwaasConfigRemoved event {}", event); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 773 | |
| 774 | log.info("Network event : Pseudowire configuration removed!"); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 775 | PwaasConfig config = (PwaasConfig) event.prevConfig().get(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 776 | |
| 777 | Set<DefaultL2TunnelDescription> pwToRemove = config |
| 778 | .getPwIds() |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 779 | .stream() |
| 780 | .map(config::getPwDescription) |
| 781 | .collect(Collectors.toSet()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 782 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 783 | // We teardown all the pseudo wire deployed |
| 784 | tearDown(pwToRemove); |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 785 | } |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 786 | |
| 787 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 788 | * Helper function for removing a single pseudowire. |
| 789 | * <p> |
| 790 | * No mastership of CP1 is checked, because it can be called from |
| 791 | * the CLI for removal of pseudowires. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 792 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 793 | * @param l2TunnelId the id of the pseudowire to tear down |
| 794 | * @return Returns SUCCESS if no error is obeserved or an appropriate |
| 795 | * error on a failure |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 796 | */ |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 797 | private Result tearDownPseudowire(long l2TunnelId) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 798 | |
| 799 | CompletableFuture<ObjectiveError> fwdInitNextFuture = new CompletableFuture<>(); |
| 800 | CompletableFuture<ObjectiveError> fwdTermNextFuture = new CompletableFuture<>(); |
| 801 | |
| 802 | CompletableFuture<ObjectiveError> revInitNextFuture = new CompletableFuture<>(); |
| 803 | CompletableFuture<ObjectiveError> revTermNextFuture = new CompletableFuture<>(); |
| 804 | |
| 805 | if (l2TunnelId == 0) { |
| 806 | log.warn("Removal process : Tunnel id cannot be 0"); |
| 807 | return Result.WRONG_PARAMETERS; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 808 | } |
| 809 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 810 | // check existence of tunnels/policy in the store, if one is missing abort! |
| 811 | Versioned<DefaultL2Tunnel> l2TunnelVersioned = l2TunnelStore.get(Long.toString(l2TunnelId)); |
| 812 | Versioned<DefaultL2TunnelPolicy> l2TunnelPolicyVersioned = l2PolicyStore.get(Long.toString(l2TunnelId)); |
| 813 | if ((l2TunnelVersioned == null) || (l2TunnelPolicyVersioned == null)) { |
| 814 | log.warn("Removal process : Policy and/or tunnel missing for tunnel id {}", l2TunnelId); |
| 815 | return Result.REMOVAL_ERROR; |
| 816 | } |
| 817 | |
| 818 | DefaultL2TunnelDescription pwToRemove = new DefaultL2TunnelDescription(l2TunnelVersioned.value(), |
| 819 | l2TunnelPolicyVersioned.value()); |
| 820 | |
| 821 | // remove the tunnels and the policies from the store |
| 822 | l2PolicyStore.remove(Long.toString(l2TunnelId)); |
| 823 | l2TunnelStore.remove(Long.toString(l2TunnelId)); |
| 824 | |
| 825 | log.info("Removal process : Tearing down forward direction of pseudowire {}", l2TunnelId); |
| 826 | |
| 827 | VlanId egressVlan = determineEgressVlan(pwToRemove.l2TunnelPolicy().cP1OuterTag(), |
| 828 | pwToRemove.l2TunnelPolicy().cP1InnerTag(), |
| 829 | pwToRemove.l2TunnelPolicy().cP2OuterTag(), |
| 830 | pwToRemove.l2TunnelPolicy().cP2InnerTag()); |
| 831 | deletePolicy(l2TunnelId, |
| 832 | pwToRemove.l2TunnelPolicy().cP1(), |
| 833 | pwToRemove.l2TunnelPolicy().cP1InnerTag(), |
| 834 | pwToRemove.l2TunnelPolicy().cP1OuterTag(), |
| 835 | egressVlan, |
| 836 | fwdInitNextFuture, |
| 837 | FWD); |
| 838 | |
| 839 | fwdInitNextFuture.thenAcceptAsync(status -> { |
| 840 | if (status == null) { |
| 841 | // Finally we will tear down the pseudo wire. |
| 842 | tearDownPseudoWireInit(l2TunnelId, |
| 843 | pwToRemove.l2TunnelPolicy().cP1(), |
| 844 | fwdTermNextFuture, |
| 845 | FWD); |
| 846 | } |
| 847 | }); |
| 848 | |
| 849 | fwdTermNextFuture.thenAcceptAsync(status -> { |
| 850 | if (status == null) { |
| 851 | tearDownPseudoWireTerm(pwToRemove.l2Tunnel(), |
| 852 | pwToRemove.l2TunnelPolicy().cP2(), |
| 853 | null, |
| 854 | FWD); |
| 855 | } |
| 856 | }); |
| 857 | |
| 858 | log.info("Removal process : Tearing down reverse direction of pseudowire {}", l2TunnelId); |
| 859 | |
| 860 | egressVlan = determineEgressVlan(pwToRemove.l2TunnelPolicy().cP2OuterTag(), |
| 861 | pwToRemove.l2TunnelPolicy().cP2InnerTag(), |
| 862 | pwToRemove.l2TunnelPolicy().cP1OuterTag(), |
| 863 | pwToRemove.l2TunnelPolicy().cP1InnerTag()); |
| 864 | |
| 865 | // We do the same operations on the reverse side. |
| 866 | deletePolicy(l2TunnelId, |
| 867 | pwToRemove.l2TunnelPolicy().cP2(), |
| 868 | pwToRemove.l2TunnelPolicy().cP2InnerTag(), |
| 869 | pwToRemove.l2TunnelPolicy().cP2OuterTag(), |
| 870 | egressVlan, |
| 871 | revInitNextFuture, |
| 872 | REV); |
| 873 | |
| 874 | revInitNextFuture.thenAcceptAsync(status -> { |
| 875 | if (status == null) { |
| 876 | tearDownPseudoWireInit(l2TunnelId, |
| 877 | pwToRemove.l2TunnelPolicy().cP2(), |
| 878 | revTermNextFuture, |
| 879 | REV); |
| 880 | } |
| 881 | }); |
| 882 | |
| 883 | revTermNextFuture.thenAcceptAsync(status -> { |
| 884 | if (status == null) { |
| 885 | tearDownPseudoWireTerm(pwToRemove.l2Tunnel(), |
| 886 | pwToRemove.l2TunnelPolicy().cP1(), |
| 887 | null, |
| 888 | REV); |
| 889 | } |
| 890 | }); |
| 891 | |
| 892 | return Result.SUCCESS; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 896 | * Helper function to handle the pw removal. |
| 897 | * <p> |
| 898 | * This method checks for the mastership of the device because it is |
| 899 | * used only from network configuration updates, thus we only want |
| 900 | * one instance only to program each pseudowire. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 901 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 902 | * @param pwToRemove the pseudo wires to remove |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 903 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 904 | public void tearDown(Set<DefaultL2TunnelDescription> pwToRemove) { |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 905 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 906 | Result result; |
| 907 | |
| 908 | // We remove all the pw in the configuration file. |
| 909 | for (DefaultL2TunnelDescription currentL2Tunnel : pwToRemove) { |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 910 | ConnectPoint cp1 = currentL2Tunnel.l2TunnelPolicy().cP1(); |
| 911 | ConnectPoint cp2 = currentL2Tunnel.l2TunnelPolicy().cP2(); |
| 912 | long tunnelId = currentL2Tunnel.l2TunnelPolicy().tunnelId(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 913 | |
| 914 | // only the master of CP1 will program this pseudowire |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 915 | if (!srManager.isMasterOf(cp1)) { |
| 916 | log.debug("Not the master of {}. Ignore pseudo wire removal id={}", cp1, tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 917 | continue; |
| 918 | } |
| 919 | |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 920 | // no need to differentiate here between leaf-leaf and leaf-spine, because |
| 921 | // the only change is in the groups, which we do not remove either way |
| 922 | log.info("Removing pseudowire {}", tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 923 | |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 924 | result = tearDownPseudowire(tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 925 | switch (result) { |
| 926 | case WRONG_PARAMETERS: |
| 927 | log.warn("Error in supplied parameters for the pseudowire removal with tunnel id {}!", |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 928 | tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 929 | break; |
| 930 | case REMOVAL_ERROR: |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 931 | log.warn("Error in pseudowire removal with tunnel id {}!", tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 932 | break; |
| 933 | default: |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 934 | log.warn("Pseudowire with tunnel id {} was removed successfully", tunnelId); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | /** |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 940 | * Handles the policy establishment which consists in |
| 941 | * create the filtering and forwarding objectives related |
| 942 | * to the initiation and termination. |
| 943 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 944 | * @param tunnelId the tunnel id |
| 945 | * @param ingress the ingress point |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 946 | * @param ingressInner the ingress inner tag |
| 947 | * @param ingressOuter the ingress outer tag |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 948 | * @param nextId the next objective id |
| 949 | * @param egressVlan Vlan-id to set, depends on ingress vlan |
| 950 | * combinations. For example, if pw is double tagged |
| 951 | * then this is the value of the outer vlan, if single |
| 952 | * tagged then it is the new value of the single tag. |
| 953 | * Should be None for untagged traffic. |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 954 | * @return the result of the operation |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 955 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 956 | private Result deployPolicy(long tunnelId, ConnectPoint ingress, VlanId ingressInner, |
| 957 | VlanId ingressOuter, VlanId egressVlan, int nextId) { |
| 958 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 959 | List<Objective> objectives = Lists.newArrayList(); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 960 | // We create the forwarding objective for supporting |
| 961 | // the l2 tunnel. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 962 | ForwardingObjective.Builder fwdBuilder = createInitFwdObjective(tunnelId, ingress.port(), nextId); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 963 | // We create and add objective context. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 964 | ObjectiveContext context = new DefaultObjectiveContext((objective) -> |
| 965 | log.debug("FwdObj for tunnel {} populated", tunnelId), |
| 966 | (objective, error) -> |
| 967 | log.warn("Failed to populate fwdrObj " + |
| 968 | "for tunnel {}", tunnelId, error)); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 969 | objectives.add(fwdBuilder.add(context)); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 970 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 971 | // We create the filtering objective to define the |
| 972 | // permit traffic in the switch |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 973 | FilteringObjective.Builder filtBuilder = createFiltObjective(ingress.port(), ingressInner, ingressOuter); |
| 974 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 975 | // We add the metadata. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 976 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 977 | .builder() |
| 978 | .setTunnelId(tunnelId) |
| 979 | .setVlanId(egressVlan); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 980 | filtBuilder.withMeta(treatment.build()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 981 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 982 | // We create and add objective context. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 983 | context = new DefaultObjectiveContext((objective) -> log.debug("FilterObj for tunnel {} populated", tunnelId), |
| 984 | (objective, error) -> log.warn("Failed to populate filterObj for " + |
| 985 | "tunnel {}", tunnelId, error)); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 986 | objectives.add(filtBuilder.add(context)); |
| 987 | |
| 988 | for (Objective objective : objectives) { |
| 989 | if (objective instanceof ForwardingObjective) { |
| 990 | srManager.flowObjectiveService.forward(ingress.deviceId(), (ForwardingObjective) objective); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 991 | log.debug("Creating new FwdObj for initiation NextObj with id={} for tunnel {}", nextId, tunnelId); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 992 | } else { |
| 993 | srManager.flowObjectiveService.filter(ingress.deviceId(), (FilteringObjective) objective); |
| 994 | log.debug("Creating new FiltObj for tunnel {}", tunnelId); |
| 995 | } |
| 996 | } |
| 997 | return SUCCESS; |
| 998 | } |
| 999 | |
| 1000 | /** |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1001 | * Handles the tunnel establishment which consists in |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1002 | * create the next objectives related to the initiation. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1003 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1004 | * @param l2Tunnel the tunnel to deploy |
| 1005 | * @param ingress the ingress connect point |
| 1006 | * @param egress the egress connect point |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1007 | * @param direction the direction of the pw |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1008 | * @param spinePw if the pseudowire involves a spine switch |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1009 | * @return the result of the operation |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1010 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1011 | private Result deployPseudoWireInit(DefaultL2Tunnel l2Tunnel, ConnectPoint ingress, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1012 | ConnectPoint egress, Direction direction, Link nextHop, boolean spinePw) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1013 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1014 | if (nextHop == null) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1015 | log.warn("No path between ingress and egress cps for tunnel {}", l2Tunnel.tunnelId()); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1016 | return WRONG_PARAMETERS; |
| 1017 | } |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1018 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1019 | // We create the next objective without the metadata |
| 1020 | // context and id. We check if it already exists in the |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1021 | // store. If not we store as it is in the store. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1022 | NextObjective.Builder nextObjectiveBuilder = createNextObjective(INITIATION, |
| 1023 | nextHop.src(), |
| 1024 | nextHop.dst(), |
| 1025 | l2Tunnel, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1026 | egress.deviceId(), |
| 1027 | spinePw); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1028 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1029 | if (nextObjectiveBuilder == null) { |
| 1030 | return INTERNAL_ERROR; |
| 1031 | } |
| 1032 | // We set the metadata. We will use this metadata |
| 1033 | // to inform the driver we are doing a l2 tunnel. |
| 1034 | TrafficSelector metadata = DefaultTrafficSelector |
| 1035 | .builder() |
| 1036 | .matchTunnelId(l2Tunnel.tunnelId()) |
| 1037 | .build(); |
| 1038 | nextObjectiveBuilder.withMeta(metadata); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1039 | int nextId = srManager.flowObjectiveService.allocateNextId(); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1040 | if (nextId < 0) { |
| 1041 | log.warn("Not able to allocate a next id for initiation"); |
| 1042 | return INTERNAL_ERROR; |
| 1043 | } |
| 1044 | nextObjectiveBuilder.withId(nextId); |
| 1045 | String key = generateKey(l2Tunnel.tunnelId(), direction); |
| 1046 | l2InitiationNextObjStore.put(key, nextObjectiveBuilder.add()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1047 | ObjectiveContext context = new DefaultObjectiveContext((objective) -> |
| 1048 | log.debug("Initiation l2 tunnel rule " + |
| 1049 | "for {} populated", |
| 1050 | l2Tunnel.tunnelId()), |
| 1051 | (objective, error) -> |
| 1052 | log.warn("Failed to populate Initiation " + |
| 1053 | "l2 tunnel rule for {}: {}", |
| 1054 | l2Tunnel.tunnelId(), error)); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1055 | NextObjective nextObjective = nextObjectiveBuilder.add(context); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1056 | srManager.flowObjectiveService.next(ingress.deviceId(), nextObjective); |
| 1057 | log.debug("Initiation next objective for {} not found. Creating new NextObj with id={}", |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1058 | l2Tunnel.tunnelId(), nextObjective.id()); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1059 | Result result = SUCCESS; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1060 | result.nextId = nextObjective.id(); |
| 1061 | return result; |
| 1062 | } |
| 1063 | |
| 1064 | /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1065 | * Handles the tunnel termination, which consists in the creation |
| 1066 | * of a forwarding objective and a next objective. |
| 1067 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1068 | * @param l2Tunnel the tunnel to terminate |
| 1069 | * @param egress the egress point |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1070 | * @param egressVlan the expected vlan at egress |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1071 | * @param direction the direction |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1072 | * @param spinePw if the pseudowire involves a spine switch |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1073 | * @return the result of the operation |
| 1074 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1075 | private Result deployPseudoWireTerm(DefaultL2Tunnel l2Tunnel, ConnectPoint egress, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1076 | VlanId egressVlan, Direction direction, boolean spinePw) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1077 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1078 | // We create the group relative to the termination. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1079 | NextObjective.Builder nextObjectiveBuilder = createNextObjective(TERMINATION, egress, null, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1080 | null, egress.deviceId(), |
| 1081 | spinePw); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1082 | if (nextObjectiveBuilder == null) { |
| 1083 | return INTERNAL_ERROR; |
| 1084 | } |
| 1085 | TrafficSelector metadata = DefaultTrafficSelector |
| 1086 | .builder() |
| 1087 | .matchVlanId(egressVlan) |
| 1088 | .build(); |
| 1089 | nextObjectiveBuilder.withMeta(metadata); |
| 1090 | int nextId = srManager.flowObjectiveService.allocateNextId(); |
| 1091 | if (nextId < 0) { |
| 1092 | log.warn("Not able to allocate a next id for initiation"); |
| 1093 | return INTERNAL_ERROR; |
| 1094 | } |
| 1095 | nextObjectiveBuilder.withId(nextId); |
| 1096 | String key = generateKey(l2Tunnel.tunnelId(), direction); |
| 1097 | l2TerminationNextObjStore.put(key, nextObjectiveBuilder.add()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1098 | ObjectiveContext context = new DefaultObjectiveContext((objective) -> log.debug("Termination l2 tunnel rule " + |
| 1099 | "for {} populated", |
| 1100 | l2Tunnel.tunnelId()), |
| 1101 | (objective, error) -> log.warn("Failed to populate " + |
| 1102 | "termination l2 tunnel " + |
| 1103 | "rule for {}: {}", |
| 1104 | l2Tunnel.tunnelId(), |
| 1105 | error)); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1106 | NextObjective nextObjective = nextObjectiveBuilder.add(context); |
| 1107 | srManager.flowObjectiveService.next(egress.deviceId(), nextObjective); |
| 1108 | log.debug("Termination next objective for {} not found. Creating new NextObj with id={}", |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1109 | l2Tunnel.tunnelId(), nextObjective.id()); |
| 1110 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1111 | // We create the flow relative to the termination. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1112 | ForwardingObjective.Builder fwdBuilder = createTermFwdObjective(l2Tunnel.pwLabel(), l2Tunnel.tunnelId(), |
| 1113 | egress.port(), nextObjective.id()); |
| 1114 | context = new DefaultObjectiveContext((objective) -> log.debug("FwdObj for tunnel termination {} populated", |
| 1115 | l2Tunnel.tunnelId()), |
| 1116 | (objective, error) -> log.warn("Failed to populate fwdrObj" + |
| 1117 | " for tunnel termination {}", |
| 1118 | l2Tunnel.tunnelId(), error)); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1119 | srManager.flowObjectiveService.forward(egress.deviceId(), fwdBuilder.add(context)); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1120 | log.debug("Creating new FwdObj for termination NextObj with id={} for tunnel {}", |
| 1121 | nextId, l2Tunnel.tunnelId()); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1122 | return SUCCESS; |
| 1123 | |
| 1124 | } |
| 1125 | |
| 1126 | /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1127 | * Creates the filtering objective according to a given policy. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1128 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1129 | * @param inPort the in port |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1130 | * @param innerTag the inner vlan tag |
| 1131 | * @param outerTag the outer vlan tag |
| 1132 | * @return the filtering objective |
| 1133 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1134 | private FilteringObjective.Builder createFiltObjective(PortNumber inPort, VlanId innerTag, VlanId outerTag) { |
| 1135 | |
| 1136 | log.info("Creating filtering objective for vlans {} / {}", outerTag, innerTag); |
| 1137 | return DefaultFilteringObjective |
| 1138 | .builder() |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1139 | .withKey(Criteria.matchInPort(inPort)) |
| 1140 | .addCondition(Criteria.matchInnerVlanId(innerTag)) |
| 1141 | .addCondition(Criteria.matchVlanId(outerTag)) |
| 1142 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY) |
| 1143 | .permit() |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 1144 | .fromApp(srManager.appId()); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1148 | * Creates the forwarding objective for the termination. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1149 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1150 | * @param pwLabel the pseudo wire label |
| 1151 | * @param tunnelId the tunnel id |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1152 | * @param egressPort the egress port |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1153 | * @param nextId the next step |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1154 | * @return the forwarding objective to support the termination |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1155 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1156 | private ForwardingObjective.Builder createTermFwdObjective(MplsLabel pwLabel, long tunnelId, |
| 1157 | PortNumber egressPort, int nextId) { |
| 1158 | |
| 1159 | TrafficSelector.Builder trafficSelector = DefaultTrafficSelector.builder(); |
| 1160 | TrafficTreatment.Builder trafficTreatment = DefaultTrafficTreatment.builder(); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1161 | // The flow has to match on the pw label and bos |
| 1162 | trafficSelector.matchEthType(Ethernet.MPLS_UNICAST); |
| 1163 | trafficSelector.matchMplsLabel(pwLabel); |
| 1164 | trafficSelector.matchMplsBos(true); |
| 1165 | // The flow has to decrement ttl, restore ttl in |
| 1166 | // pop mpls, set tunnel id and port. |
| 1167 | trafficTreatment.decMplsTtl(); |
| 1168 | trafficTreatment.copyTtlIn(); |
| 1169 | trafficTreatment.popMpls(); |
| 1170 | trafficTreatment.setTunnelId(tunnelId); |
| 1171 | trafficTreatment.setOutput(egressPort); |
| 1172 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1173 | return DefaultForwardingObjective |
| 1174 | .builder() |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 1175 | .fromApp(srManager.appId()) |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1176 | .makePermanent() |
| 1177 | .nextStep(nextId) |
| 1178 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY) |
| 1179 | .withSelector(trafficSelector.build()) |
| 1180 | .withTreatment(trafficTreatment.build()) |
| 1181 | .withFlag(VERSATILE); |
| 1182 | } |
| 1183 | |
| 1184 | /** |
| 1185 | * Creates the forwarding objective for the initiation. |
| 1186 | * |
| 1187 | * @param tunnelId the tunnel id |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1188 | * @param inPort the input port |
| 1189 | * @param nextId the next step |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1190 | * @return the forwarding objective to support the initiation. |
| 1191 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1192 | private ForwardingObjective.Builder createInitFwdObjective(long tunnelId, PortNumber inPort, int nextId) { |
| 1193 | |
| 1194 | TrafficSelector.Builder trafficSelector = DefaultTrafficSelector.builder(); |
| 1195 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1196 | // The flow has to match on the mpls logical |
| 1197 | // port and the tunnel id. |
| 1198 | trafficSelector.matchTunnelId(tunnelId); |
| 1199 | trafficSelector.matchInPort(inPort); |
| 1200 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1201 | return DefaultForwardingObjective |
| 1202 | .builder() |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 1203 | .fromApp(srManager.appId()) |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1204 | .makePermanent() |
| 1205 | .nextStep(nextId) |
| 1206 | .withPriority(SegmentRoutingService.DEFAULT_PRIORITY) |
| 1207 | .withSelector(trafficSelector.build()) |
| 1208 | .withFlag(VERSATILE); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1209 | |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * Creates the next objective according to a given |
| 1214 | * pipeline. We don't set the next id and we don't |
| 1215 | * create the final meta to check if we are re-using |
| 1216 | * the same next objective for different tunnels. |
| 1217 | * |
| 1218 | * @param pipeline the pipeline to support |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1219 | * @param srcCp the source port |
| 1220 | * @param dstCp the destination port |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1221 | * @param l2Tunnel the tunnel to support |
| 1222 | * @param egressId the egress device id |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1223 | * @param spinePw if the pw involves a spine switch |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1224 | * @return the next objective to support the pipeline |
| 1225 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1226 | private NextObjective.Builder createNextObjective(Pipeline pipeline, ConnectPoint srcCp, |
| 1227 | ConnectPoint dstCp, DefaultL2Tunnel l2Tunnel, |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1228 | DeviceId egressId, boolean spinePw) { |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1229 | NextObjective.Builder nextObjBuilder; |
| 1230 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 1231 | if (pipeline == INITIATION) { |
| 1232 | nextObjBuilder = DefaultNextObjective |
| 1233 | .builder() |
| 1234 | .withType(NextObjective.Type.SIMPLE) |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 1235 | .fromApp(srManager.appId()); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1236 | // The pw label is the bottom of stack. It has to |
| 1237 | // be different -1. |
| 1238 | if (l2Tunnel.pwLabel().toInt() == MplsLabel.MAX_MPLS) { |
| 1239 | log.warn("Pw label not configured"); |
| 1240 | return null; |
| 1241 | } |
| 1242 | treatmentBuilder.pushMpls(); |
| 1243 | treatmentBuilder.setMpls(l2Tunnel.pwLabel()); |
| 1244 | treatmentBuilder.setMplsBos(true); |
| 1245 | treatmentBuilder.copyTtlOut(); |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1246 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1247 | // If the inter-co label is present we have to set the label. |
| 1248 | if (l2Tunnel.interCoLabel().toInt() != MplsLabel.MAX_MPLS) { |
| 1249 | treatmentBuilder.pushMpls(); |
| 1250 | treatmentBuilder.setMpls(l2Tunnel.interCoLabel()); |
| 1251 | treatmentBuilder.setMplsBos(false); |
| 1252 | treatmentBuilder.copyTtlOut(); |
| 1253 | } |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1254 | |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1255 | // if pw is leaf-to-leaf we need to |
| 1256 | // add the routing label also |
| 1257 | if (!spinePw) { |
| 1258 | // We retrieve the sr label from the config |
| 1259 | // specific for pseudowire traffic |
| 1260 | // using the egress leaf device id. |
| 1261 | MplsLabel srLabel; |
| 1262 | try { |
| 1263 | srLabel = MplsLabel.mplsLabel(srManager.deviceConfiguration().getPWRoutingLabel(egressId)); |
| 1264 | |
| 1265 | } catch (DeviceConfigNotFoundException e) { |
| 1266 | log.warn("Sr label for pw traffic not configured"); |
| 1267 | return null; |
| 1268 | } |
| 1269 | |
| 1270 | treatmentBuilder.pushMpls(); |
| 1271 | treatmentBuilder.setMpls(srLabel); |
| 1272 | treatmentBuilder.setMplsBos(false); |
| 1273 | treatmentBuilder.copyTtlOut(); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1274 | } |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1275 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1276 | // We have to rewrite the src and dst mac address. |
| 1277 | MacAddress ingressMac; |
| 1278 | try { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1279 | ingressMac = srManager.deviceConfiguration().getDeviceMac(srcCp.deviceId()); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1280 | } catch (DeviceConfigNotFoundException e) { |
| 1281 | log.warn("Was not able to find the ingress mac"); |
| 1282 | return null; |
| 1283 | } |
| 1284 | treatmentBuilder.setEthSrc(ingressMac); |
| 1285 | MacAddress neighborMac; |
| 1286 | try { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1287 | neighborMac = srManager.deviceConfiguration().getDeviceMac(dstCp.deviceId()); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1288 | } catch (DeviceConfigNotFoundException e) { |
| 1289 | log.warn("Was not able to find the neighbor mac"); |
| 1290 | return null; |
| 1291 | } |
| 1292 | treatmentBuilder.setEthDst(neighborMac); |
| 1293 | } else { |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1294 | // We create the next objective which |
| 1295 | // will be a simple l2 group. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1296 | nextObjBuilder = DefaultNextObjective |
| 1297 | .builder() |
| 1298 | .withType(NextObjective.Type.SIMPLE) |
Ray Milkey | b85de08 | 2017-04-05 09:42:04 -0700 | [diff] [blame] | 1299 | .fromApp(srManager.appId()); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1300 | } |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1301 | treatmentBuilder.setOutput(srcCp.port()); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1302 | nextObjBuilder.addTreatment(treatmentBuilder.build()); |
| 1303 | return nextObjBuilder; |
| 1304 | } |
| 1305 | |
| 1306 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1307 | * Reverses a link. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1308 | * |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1309 | * @param link link to be reversed |
| 1310 | * @return the reversed link |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1311 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1312 | private Link reverseLink(Link link) { |
| 1313 | |
| 1314 | DefaultLink.Builder linkBuilder = DefaultLink.builder(); |
| 1315 | |
| 1316 | linkBuilder.src(link.dst()); |
| 1317 | linkBuilder.dst(link.src()); |
| 1318 | linkBuilder.type(link.type()); |
| 1319 | linkBuilder.providerId(link.providerId()); |
| 1320 | |
| 1321 | return linkBuilder.build(); |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * Returns the path betwwen two connect points. |
| 1326 | * |
Andreas Pantelopoulos | afc637f | 2017-12-20 18:04:27 -0800 | [diff] [blame] | 1327 | * @param srcCp source connect point |
| 1328 | * @param dstCp destination connect point |
| 1329 | * @return the path |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1330 | */ |
| 1331 | private List<Link> getPath(ConnectPoint srcCp, ConnectPoint dstCp) { |
Charles Chan | 8316381 | 2018-01-09 13:45:07 -0800 | [diff] [blame] | 1332 | /* TODO We retrieve a set of paths in case of a link failure, what happens |
| 1333 | * if the TopologyService gets the link notification AFTER us and has not updated the paths? |
| 1334 | * |
| 1335 | * TODO This has the potential to act on old topology. |
| 1336 | * Maybe we should make SRManager be a listener on topology events instead raw link events. |
| 1337 | */ |
| 1338 | Set<Path> paths = srManager.topologyService.getPaths( |
| 1339 | srManager.topologyService.currentTopology(), |
| 1340 | srcCp.deviceId(), dstCp.deviceId()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1341 | |
Charles Chan | 8316381 | 2018-01-09 13:45:07 -0800 | [diff] [blame] | 1342 | log.debug("Paths obtained from topology service {}", paths); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1343 | |
| 1344 | // We randomly pick a path. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1345 | if (paths.isEmpty()) { |
| 1346 | return null; |
| 1347 | } |
| 1348 | int size = paths.size(); |
| 1349 | int index = RandomUtils.nextInt(0, size); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1350 | |
Charles Chan | 8316381 | 2018-01-09 13:45:07 -0800 | [diff] [blame] | 1351 | List<Link> result = Iterables.get(paths, index).links(); |
| 1352 | log.debug("Randomly picked a path {}", result); |
| 1353 | |
| 1354 | return result; |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | /** |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1358 | * Deletes a given policy using the parameter supplied. |
| 1359 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1360 | * @param tunnelId the tunnel id |
| 1361 | * @param ingress the ingress point |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1362 | * @param ingressInner the ingress inner vlan id |
| 1363 | * @param ingressOuter the ingress outer vlan id |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1364 | * @param future to perform the async operation |
| 1365 | * @param direction the direction: forward or reverse |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1366 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1367 | private void deletePolicy(long tunnelId, ConnectPoint ingress, VlanId ingressInner, VlanId ingressOuter, |
| 1368 | VlanId egressVlan, CompletableFuture<ObjectiveError> future, Direction direction) { |
| 1369 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1370 | String key = generateKey(tunnelId, direction); |
| 1371 | if (!l2InitiationNextObjStore.containsKey(key)) { |
| 1372 | log.warn("Abort delete of policy for tunnel {}: next does not exist in the store", tunnelId); |
| 1373 | if (future != null) { |
| 1374 | future.complete(null); |
| 1375 | } |
| 1376 | return; |
| 1377 | } |
| 1378 | NextObjective nextObjective = l2InitiationNextObjStore.get(key).value(); |
| 1379 | int nextId = nextObjective.id(); |
| 1380 | List<Objective> objectives = Lists.newArrayList(); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1381 | // We create the forwarding objective. |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1382 | ForwardingObjective.Builder fwdBuilder = createInitFwdObjective(tunnelId, ingress.port(), nextId); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1383 | ObjectiveContext context = new ObjectiveContext() { |
| 1384 | @Override |
| 1385 | public void onSuccess(Objective objective) { |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1386 | log.debug("Previous fwdObj for policy {} removed", tunnelId); |
| 1387 | if (future != null) { |
| 1388 | future.complete(null); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | @Override |
| 1393 | public void onError(Objective objective, ObjectiveError error) { |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1394 | log.warn("Failed to remove previous fwdObj for policy {}: {}", tunnelId, error); |
| 1395 | if (future != null) { |
| 1396 | future.complete(error); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | }; |
| 1400 | objectives.add(fwdBuilder.remove(context)); |
| 1401 | // We create the filtering objective to define the |
| 1402 | // permit traffic in the switch |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1403 | FilteringObjective.Builder filtBuilder = createFiltObjective(ingress.port(), ingressInner, ingressOuter); |
| 1404 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment |
| 1405 | .builder() |
| 1406 | .setTunnelId(tunnelId) |
| 1407 | .setVlanId(egressVlan); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1408 | filtBuilder.withMeta(treatment.build()); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1409 | context = new DefaultObjectiveContext((objective) -> log.debug("FilterObj for policy {} revoked", tunnelId), |
| 1410 | (objective, error) -> |
| 1411 | log.warn("Failed to revoke filterObj for policy {}", |
| 1412 | tunnelId, error)); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1413 | objectives.add(filtBuilder.remove(context)); |
| 1414 | |
| 1415 | for (Objective objective : objectives) { |
| 1416 | if (objective instanceof ForwardingObjective) { |
| 1417 | srManager.flowObjectiveService.forward(ingress.deviceId(), (ForwardingObjective) objective); |
| 1418 | } else { |
| 1419 | srManager.flowObjectiveService.filter(ingress.deviceId(), (FilteringObjective) objective); |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1425 | * Deletes the pseudo wire initiation. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1426 | * |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1427 | * @param l2TunnelId the tunnel id |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1428 | * @param ingress the ingress connect point |
| 1429 | * @param future to perform an async operation |
| 1430 | * @param direction the direction: reverse of forward |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1431 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1432 | private void tearDownPseudoWireInit(long l2TunnelId, ConnectPoint ingress, |
| 1433 | CompletableFuture<ObjectiveError> future, Direction direction) { |
| 1434 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1435 | String key = generateKey(l2TunnelId, direction); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1436 | if (!l2InitiationNextObjStore.containsKey(key)) { |
| 1437 | log.info("Abort delete of {} for {}: next does not exist in the store", INITIATION, key); |
| 1438 | if (future != null) { |
| 1439 | future.complete(null); |
| 1440 | } |
| 1441 | return; |
| 1442 | } |
| 1443 | NextObjective nextObjective = l2InitiationNextObjStore.get(key).value(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1444 | // un-comment in case you want to delete groups used by the pw |
| 1445 | // however, this will break the update of pseudowires cause the L2 interface group can |
| 1446 | // not be deleted (it is referenced by other groups) |
| 1447 | /* |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1448 | ObjectiveContext context = new ObjectiveContext() { |
| 1449 | @Override |
| 1450 | public void onSuccess(Objective objective) { |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1451 | log.debug("Previous {} next for {} removed", INITIATION, key); |
| 1452 | if (future != null) { |
| 1453 | future.complete(null); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | @Override |
| 1458 | public void onError(Objective objective, ObjectiveError error) { |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1459 | log.warn("Failed to remove previous {} next for {}: {}", INITIATION, key, error); |
| 1460 | if (future != null) { |
| 1461 | future.complete(error); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | }; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1465 | srManager.flowObjectiveService.next(ingress.deviceId(), (NextObjective) nextObjective.copy().remove(context)); |
| 1466 | */ |
| 1467 | |
| 1468 | future.complete(null); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1469 | l2InitiationNextObjStore.remove(key); |
| 1470 | } |
| 1471 | |
| 1472 | /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1473 | * Deletes the pseudo wire termination. |
| 1474 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1475 | * @param l2Tunnel the tunnel |
| 1476 | * @param egress the egress connect point |
| 1477 | * @param future the async task |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1478 | * @param direction the direction of the tunnel |
| 1479 | */ |
| 1480 | private void tearDownPseudoWireTerm(DefaultL2Tunnel l2Tunnel, |
| 1481 | ConnectPoint egress, |
| 1482 | CompletableFuture<ObjectiveError> future, |
| 1483 | Direction direction) { |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1484 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1485 | String key = generateKey(l2Tunnel.tunnelId(), direction); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1486 | if (!l2TerminationNextObjStore.containsKey(key)) { |
| 1487 | log.info("Abort delete of {} for {}: next does not exist in the store", TERMINATION, key); |
| 1488 | if (future != null) { |
| 1489 | future.complete(null); |
| 1490 | } |
| 1491 | return; |
| 1492 | } |
| 1493 | NextObjective nextObjective = l2TerminationNextObjStore.get(key).value(); |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1494 | ForwardingObjective.Builder fwdBuilder = createTermFwdObjective(l2Tunnel.pwLabel(), |
| 1495 | l2Tunnel.tunnelId(), |
| 1496 | egress.port(), |
| 1497 | nextObjective.id()); |
| 1498 | ObjectiveContext context = new DefaultObjectiveContext((objective) -> log.debug("FwdObj for {} {} removed", |
| 1499 | TERMINATION, |
| 1500 | l2Tunnel.tunnelId()), |
| 1501 | (objective, error) -> |
| 1502 | log.warn("Failed to remove fwdObj for {} {}", |
| 1503 | TERMINATION, |
| 1504 | l2Tunnel.tunnelId(), |
| 1505 | error)); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1506 | srManager.flowObjectiveService.forward(egress.deviceId(), fwdBuilder.remove(context)); |
| 1507 | |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1508 | // un-comment in case you want to delete groups used by the pw |
| 1509 | // however, this will break the update of pseudowires cause the L2 interface group can |
| 1510 | // not be deleted (it is referenced by other groups) |
| 1511 | /* |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1512 | context = new ObjectiveContext() { |
| 1513 | @Override |
| 1514 | public void onSuccess(Objective objective) { |
| 1515 | log.debug("Previous {} next for {} removed", TERMINATION, key); |
| 1516 | if (future != null) { |
| 1517 | future.complete(null); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | @Override |
| 1522 | public void onError(Objective objective, ObjectiveError error) { |
| 1523 | log.warn("Failed to remove previous {} next for {}: {}", TERMINATION, key, error); |
| 1524 | if (future != null) { |
| 1525 | future.complete(error); |
| 1526 | } |
| 1527 | } |
| 1528 | }; |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1529 | srManager.flowObjectiveService.next(egress.deviceId(), (NextObjective) nextObjective.copy().remove(context)); |
| 1530 | */ |
| 1531 | |
| 1532 | future.complete(null); |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1533 | l2TerminationNextObjStore.remove(key); |
| 1534 | } |
| 1535 | |
| 1536 | /** |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1537 | * Utilities to generate pw key. |
| 1538 | * |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1539 | * @param tunnelId the tunnel id |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1540 | * @param direction the direction of the pw |
| 1541 | * @return the key of the store |
| 1542 | */ |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1543 | private String generateKey(long tunnelId, Direction direction) { |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1544 | return String.format("%s-%s", tunnelId, direction); |
| 1545 | } |
| 1546 | |
| 1547 | /** |
Pier Luigi | 0be3f0f | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1548 | * Pwaas pipelines. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1549 | */ |
| 1550 | protected enum Pipeline { |
| 1551 | /** |
| 1552 | * The initiation pipeline. |
| 1553 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1554 | INITIATION, /** |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1555 | * The termination pipeline. |
| 1556 | */ |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1557 | TERMINATION |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1561 | * Enum helper to carry results of various operations. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1562 | */ |
| 1563 | public enum Result { |
| 1564 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1565 | * Happy ending scenario. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1566 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1567 | SUCCESS(0, "No error occurred"), |
| 1568 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1569 | /** |
| 1570 | * We have problems with the supplied parameters. |
| 1571 | */ |
| 1572 | WRONG_PARAMETERS(1, "Wrong parameters"), |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1573 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1574 | /** |
| 1575 | * We have an internal error during the deployment |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1576 | * or removal phase. |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1577 | */ |
| 1578 | INTERNAL_ERROR(3, "Internal error"), |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1579 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1580 | /** |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1581 | * |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1582 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1583 | REMOVAL_ERROR(5, "Can not remove pseudowire from network configuration"), |
| 1584 | |
| 1585 | /** |
| 1586 | * |
| 1587 | */ |
| 1588 | ADDITION_ERROR(6, "Can not add pseudowire in network configuration"), |
| 1589 | |
| 1590 | /** |
| 1591 | * |
| 1592 | */ |
| 1593 | CONFIG_NOT_FOUND(7, "Can not find configuration class for pseudowires"); |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1594 | |
| 1595 | private final int code; |
| 1596 | private final String description; |
| 1597 | private int nextId; |
| 1598 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1599 | Result(int code, String description) { |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1600 | this.code = code; |
| 1601 | this.description = description; |
| 1602 | } |
| 1603 | |
| 1604 | public String getDescription() { |
| 1605 | return description; |
| 1606 | } |
| 1607 | |
Pier Ventre | f3cf5b9 | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 1608 | @Override |
| 1609 | public String toString() { |
| 1610 | return code + ": " + description; |
| 1611 | } |
| 1612 | } |
| 1613 | |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1614 | /** |
| 1615 | * Enum helper for handling the direction of the pw. |
| 1616 | */ |
| 1617 | public enum Direction { |
| 1618 | /** |
| 1619 | * The forward direction of the pseudo wire. |
| 1620 | */ |
Andreas Pantelopoulos | 5e7be3d | 2017-10-23 12:18:25 -0700 | [diff] [blame] | 1621 | FWD, /** |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1622 | * The reverse direction of the pseudo wire. |
| 1623 | */ |
Jon Hall | 8f867f4 | 2017-02-22 13:37:33 -0800 | [diff] [blame] | 1624 | REV |
Pier Ventre | ac3e1d9 | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 1625 | } |
| 1626 | |
Pier Ventre | 6b19e48 | 2016-11-07 16:21:04 -0800 | [diff] [blame] | 1627 | } |