2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netvirt.vpnmanager;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.Objects;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
27 import org.opendaylight.netvirt.bgpmanager.api.IBgpManager;
28 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
29 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
30 import org.opendaylight.netvirt.vpnmanager.VpnOpDataSyncer.VpnOpDataType;
31 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
32 import org.opendaylight.netvirt.vpnmanager.populator.input.L3vpnInput;
33 import org.opendaylight.netvirt.vpnmanager.populator.intfc.VpnPopulator;
34 import org.opendaylight.netvirt.vpnmanager.populator.registry.L3vpnRegistry;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.PortOpData;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.SubnetOpData;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.TaskState;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.port.op.data.PortOpDataEntry;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.port.op.data.PortOpDataEntryKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.subnet.op.data.SubnetOpDataEntry;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.subnet.op.data.SubnetOpDataEntryBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.subnet.op.data.SubnetOpDataEntryKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.subnet.op.data.subnet.op.data.entry.SubnetToDpn;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ExternalNetworks;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.networks.Networks;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.external.networks.NetworksKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.opendaylight.yangtools.yang.common.Uint32;
58 import org.opendaylight.yangtools.yang.common.Uint64;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 public class VpnSubnetRouteHandler {
64 private static final Logger LOG = LoggerFactory.getLogger(VpnSubnetRouteHandler.class);
65 private static final String LOGGING_PREFIX = "SUBNETROUTE:";
66 private final DataBroker dataBroker;
67 private final SubnetOpDpnManager subOpDpnManager;
68 private final IBgpManager bgpManager;
69 private final VpnOpDataSyncer vpnOpDataSyncer;
70 private final VpnNodeListener vpnNodeListener;
71 private final IFibManager fibManager;
72 private final VpnUtil vpnUtil;
75 public VpnSubnetRouteHandler(final DataBroker dataBroker, final SubnetOpDpnManager subnetOpDpnManager,
76 final IBgpManager bgpManager, final VpnOpDataSyncer vpnOpDataSyncer, final VpnNodeListener vpnNodeListener,
77 final IFibManager fibManager, VpnUtil vpnUtil) {
78 this.dataBroker = dataBroker;
79 this.subOpDpnManager = subnetOpDpnManager;
80 this.bgpManager = bgpManager;
81 this.vpnOpDataSyncer = vpnOpDataSyncer;
82 this.vpnNodeListener = vpnNodeListener;
83 this.fibManager = fibManager;
84 this.vpnUtil = vpnUtil;
87 // TODO Clean up the exception handling
88 @SuppressWarnings("checkstyle:IllegalCatch")
89 public void onSubnetAddedToVpn(Subnetmap subnetmap, boolean isBgpVpn, Long elanTag) {
90 Uuid subnetId = subnetmap.getId();
91 String subnetIp = subnetmap.getSubnetIp();
92 Subnetmap subMap = null;
93 SubnetOpDataEntry subOpEntry = null;
94 SubnetOpDataEntryBuilder subOpBuilder = null;
95 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier = null;
96 Optional<SubnetOpDataEntry> optionalSubs = null;
98 Preconditions.checkNotNull(subnetId, LOGGING_PREFIX + " onSubnetAddedToVpn: SubnetId cannot be null or empty!");
99 Preconditions.checkNotNull(subnetIp,
100 LOGGING_PREFIX + " onSubnetAddedToVpn: SubnetPrefix cannot be null or empty!");
101 Preconditions.checkNotNull(elanTag, LOGGING_PREFIX + " onSubnetAddedToVpn: ElanTag cannot be null or empty!");
103 if (subnetmap.getVpnId() == null) {
104 LOG.error("onSubnetAddedToVpn: VpnId {} for subnet {} not found, bailing out", subnetmap.getVpnId(),
108 String vpnName = subnetmap.getVpnId().getValue();
109 Uint32 vpnId = waitAndGetVpnIdIfInvalid(vpnName);
110 if (VpnConstants.INVALID_ID.equals(vpnId)) {
112 "{} onSubnetAddedToVpn: VpnInstance to VPNId mapping not yet available for VpnName {} "
113 + "processing subnet {} with IP {}, bailing out now.",
114 LOGGING_PREFIX, vpnName, subnetId, subnetIp);
118 String primaryRd = vpnUtil.getPrimaryRd(vpnName);
119 VpnInstanceOpDataEntry vpnInstanceOpData = waitAndGetVpnInstanceOpDataIfNull(vpnName, primaryRd);
120 if (vpnInstanceOpData == null) {
122 "{} onSubnetAddedToVpn: VpnInstanceOpData not yet available for VpnName {} "
123 + "processing subnet {} with IP {}, bailing out now.",
124 LOGGING_PREFIX, vpnName, subnetId, subnetIp);
127 LOG.info("{} onSubnetAddedToVpn: Subnet {} with IP {} being added to vpn {}", LOGGING_PREFIX,
128 subnetId.getValue(), subnetIp, vpnName);
129 //TODO(vivek): Change this to use more granularized lock at subnetId level
131 vpnUtil.lockSubnet(subnetId.getValue());
132 // Please check if subnetId belongs to an External Network
133 InstanceIdentifier<Subnetmap> subMapid =
134 InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class,
135 new SubnetmapKey(subnetId)).build();
136 Optional<Subnetmap> sm = SingleTransactionDataBroker.syncReadOptional(dataBroker,
137 LogicalDatastoreType.CONFIGURATION, subMapid);
138 if (!sm.isPresent()) {
139 LOG.error("{} onSubnetAddedToVpn: Unable to retrieve subnetmap entry for subnet {} IP {}"
140 + " vpnName {}", LOGGING_PREFIX, subnetId, subnetIp, vpnName);
146 InstanceIdentifier<Networks> netsIdentifier = InstanceIdentifier.builder(ExternalNetworks.class)
147 .child(Networks.class, new NetworksKey(subMap.getNetworkId())).build();
148 Optional<Networks> optionalNets = SingleTransactionDataBroker.syncReadOptional(dataBroker,
149 LogicalDatastoreType.CONFIGURATION, netsIdentifier);
150 if (optionalNets.isPresent()) {
151 LOG.info("{} onSubnetAddedToVpn: subnet {} with IP {} is an external subnet on external "
152 + "network {}, so ignoring this for SubnetRoute on vpn {}", LOGGING_PREFIX,
153 subnetId.getValue(), subnetIp, subMap.getNetworkId().getValue(), vpnName);
157 //Create and add SubnetOpDataEntry object for this subnet to the SubnetOpData container
158 subOpIdentifier = InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
159 new SubnetOpDataEntryKey(subnetId)).build();
160 optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL,
162 if (optionalSubs.isPresent()) {
163 LOG.error("{} onSubnetAddedToVpn: SubnetOpDataEntry for subnet {} with ip {} and vpn {} already"
164 + " detected to be present", LOGGING_PREFIX, subnetId.getValue(), subnetIp, vpnName);
167 LOG.debug("{} onSubnetAddedToVpn: Creating new SubnetOpDataEntry node for subnet {} subnetIp {} "
168 + "vpn {}", LOGGING_PREFIX, subnetId.getValue(), subnetIp, vpnName);
169 subOpBuilder = new SubnetOpDataEntryBuilder().withKey(new SubnetOpDataEntryKey(subnetId));
170 subOpBuilder.setSubnetId(subnetId);
171 subOpBuilder.setSubnetCidr(subnetIp);
173 if (isBgpVpn && !VpnUtil.isBgpVpn(vpnName, primaryRd)) {
174 LOG.error("{} onSubnetAddedToVpn: The VPN Instance name {} does not have RD. Bailing out for"
175 + " subnet {} subnetIp {} ", LOGGING_PREFIX, vpnName, subnetId.getValue(), subnetIp);
178 subOpBuilder.setVrfId(primaryRd);
179 subOpBuilder.setVpnName(vpnName);
180 subOpBuilder.setSubnetToDpn(new ArrayList<>());
181 subOpBuilder.setRouteAdvState(TaskState.Idle);
182 subOpBuilder.setElanTag(elanTag);
183 Long l3Vni = vpnInstanceOpData.getL3vni() != null ? vpnInstanceOpData.getL3vni().toJava() : 0L;
184 subOpBuilder.setL3vni(l3Vni);
185 subOpEntry = subOpBuilder.build();
186 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, subOpIdentifier,
187 subOpEntry, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
188 LOG.info("{} onSubnetAddedToVpn: Added subnetopdataentry to OP Datastore for subnet {}", LOGGING_PREFIX,
189 subnetId.getValue());
190 } catch (TransactionCommitFailedException e) {
191 LOG.error("{} Creation of SubnetOpDataEntry for subnet {} failed ", LOGGING_PREFIX,
192 subnetId.getValue(), e);
193 // The second part of this method depends on subMap being non-null so fail fast here.
195 } catch (RuntimeException e) { //TODO: Avoid this
196 LOG.error("{} onSubnetAddedToVpn: Unable to handle subnet {} with ip {} added to vpn {}", LOGGING_PREFIX,
197 subnetId.getValue(), subnetIp, vpnName, e);
199 } catch (ReadFailedException e) {
200 LOG.error("{} onSubnetAddedToVpn: Failed to read data store for subnet {} ip {} vpn {}", LOGGING_PREFIX,
201 subnetId, subnetIp, vpnName);
204 vpnUtil.unlockSubnet(subnetId.getValue());
207 //In second critical section , Port-Op-Data will be updated.
208 vpnUtil.lockSubnet(subnetId.getValue());
210 SubnetToDpn subDpn = null;
211 Map<Uint64, SubnetToDpn> subDpnMap = new HashMap<>();
213 optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL,
215 subOpBuilder = new SubnetOpDataEntryBuilder(optionalSubs.get())
216 .withKey(new SubnetOpDataEntryKey(subnetId));
217 List<Uuid> portList = subMap.getPortList();
218 if (portList != null) {
219 for (Uuid port : portList) {
220 Interface intfState = InterfaceUtils.getInterfaceStateFromOperDS(dataBroker,port.getValue());
221 if (intfState != null) {
223 dpnId = InterfaceUtils.getDpIdFromInterface(intfState);
224 } catch (Exception e) {
225 LOG.error("{} onSubnetAddedToVpn: Unable to obtain dpnId for interface {},"
226 + " subnetroute inclusion for this interface for subnet {} subnetIp {} "
227 + "vpn {} failed with exception", LOGGING_PREFIX, port.getValue(),
228 subnetId.getValue(), subnetIp, vpnName, e);
231 if (dpnId.equals(Uint64.ZERO)) {
232 LOG.error("{} onSubnetAddedToVpn: Port {} is not assigned DPN yet,"
233 + " ignoring subnet {} subnetIP {} vpn {}", LOGGING_PREFIX, port.getValue(),
234 subnetId.getValue(), subnetIp, vpnName);
237 subOpDpnManager.addPortOpDataEntry(port.getValue(), subnetId, dpnId);
238 if (intfState.getOperStatus() != OperStatus.Up) {
239 LOG.error("{} onSubnetAddedToVpn: Port {} is not UP yet, ignoring subnet {}"
240 + " subnetIp {} vpn {}", LOGGING_PREFIX, port.getValue(),
241 subnetId.getValue(), subnetIp, vpnName);
244 subDpn = subOpDpnManager.addInterfaceToDpn(subnetId, dpnId, port.getValue());
245 if (intfState.getOperStatus() == OperStatus.Up) {
247 subDpnMap.put(dpnId, subDpn);
250 subOpDpnManager.addPortOpDataEntry(port.getValue(), subnetId, null);
253 if (subDpnMap.size() > 0) {
254 subOpBuilder.setSubnetToDpn(new ArrayList<>(subDpnMap.values()));
257 electNewDpnForSubnetRoute(subOpBuilder, null /* oldDpnId */, subnetId,
258 subMap.getNetworkId(), isBgpVpn);
259 subOpEntry = subOpBuilder.build();
260 SingleTransactionDataBroker.syncUpdate(dataBroker, LogicalDatastoreType.OPERATIONAL, subOpIdentifier,
261 subOpEntry, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
262 LOG.info("{} onSubnetAddedToVpn: Added PortOpDataEntry and VpnInterfaces to SubnetOpData"
263 + " for subnet {} subnetIp {} vpn {} TaskState {} lastTaskState {}", LOGGING_PREFIX,
264 subnetId.getValue(), subnetIp, vpnName, subOpEntry.getRouteAdvState(),
265 subOpEntry.getLastAdvState());
266 } catch (RuntimeException e) {
267 LOG.error("{} onSubnetAddedToVpn: Unable to handle subnet {} with ip {} added to vpn {}", LOGGING_PREFIX,
268 subnetId.getValue(), subnetIp, vpnName, e);
269 } catch (ReadFailedException e) {
270 LOG.error("{} onSubnetAddedToVpn: Failed to read data store for subnet {} ip {} vpn {}", LOGGING_PREFIX,
271 subnetId, subnetIp, vpnName);
272 } catch (TransactionCommitFailedException ex) {
273 LOG.error("{} onSubnetAddedToVpn: Creation of SubnetOpDataEntry for subnet {} subnetIp {} vpn {} failed",
274 LOGGING_PREFIX, subnetId.getValue(), subnetIp, vpnName, ex);
276 vpnUtil.unlockSubnet(subnetId.getValue());
280 private Uint32 waitAndGetVpnIdIfInvalid(String vpnName) {
281 Uint32 vpnId = vpnUtil.getVpnId(vpnName);
282 if (VpnConstants.INVALID_ID.equals(vpnId)) {
283 LOG.debug("VpnId is invalid, waiting to fetch again: vpnName={}, vpnId={}", vpnName, vpnId);
284 vpnOpDataSyncer.waitForVpnDataReady(VpnOpDataType.vpnInstanceToId, vpnName,
285 VpnConstants.PER_VPN_INSTANCE_MAX_WAIT_TIME_IN_MILLISECONDS);
286 vpnId = vpnUtil.getVpnId(vpnName);
291 private VpnInstanceOpDataEntry waitAndGetVpnInstanceOpDataIfNull(String vpnName, String primaryRd) {
292 VpnInstanceOpDataEntry vpnInstanceOpData = vpnUtil.getVpnInstanceOpData(primaryRd);
293 if (vpnInstanceOpData == null) {
294 LOG.debug("vpnInstanceOpData is null, waiting to fetch again: vpnName={}", vpnName);
295 vpnOpDataSyncer.waitForVpnDataReady(VpnOpDataType.vpnOpData, vpnName,
296 VpnConstants.PER_VPN_INSTANCE_OPDATA_MAX_WAIT_TIME_IN_MILLISECONDS);
297 vpnInstanceOpData = vpnUtil.getVpnInstanceOpData(primaryRd);
299 return vpnInstanceOpData;
302 // TODO Clean up the exception handling
303 @SuppressWarnings("checkstyle:IllegalCatch")
304 public void onSubnetDeletedFromVpn(Subnetmap subnetmap, boolean isBgpVpn) {
305 Uuid subnetId = subnetmap.getId();
306 LOG.info("{} onSubnetDeletedFromVpn: Subnet {} with ip {} being removed from vpnId {}", LOGGING_PREFIX,
307 subnetId, subnetmap.getSubnetIp(), subnetmap.getVpnId());
308 //TODO(vivek): Change this to use more granularized lock at subnetId level
310 vpnUtil.lockSubnet(subnetId.getValue());
311 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier =
312 InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
313 new SubnetOpDataEntryKey(subnetId)).build();
314 Optional<SubnetOpDataEntry> optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker,
315 LogicalDatastoreType.OPERATIONAL,
317 if (!optionalSubs.isPresent()) {
318 LOG.error("{} onSubnetDeletedFromVpn: SubnetOpDataEntry for subnet {} subnetIp {} vpn {}"
319 + " not available in datastore", LOGGING_PREFIX, subnetId.getValue(),
320 subnetId.getValue(), subnetmap.getVpnId());
323 LOG.trace("{} onSubnetDeletedFromVpn: Removing the SubnetOpDataEntry node for subnet {} subnetIp {}"
324 + " vpnName {} rd {} TaskState {}", LOGGING_PREFIX, subnetId.getValue(),
325 optionalSubs.get().getSubnetCidr(), optionalSubs.get().getVpnName(),
326 optionalSubs.get().getVrfId(), optionalSubs.get().getRouteAdvState());
327 /* If subnet is deleted (or if its removed from VPN), the ports that are DOWN on that subnet
328 * will continue to be stale in portOpData DS, as subDpnList used for portOpData removal will
329 * contain only ports that are UP. So here we explicitly cleanup the ports of the subnet by
330 * going through the list of ports on the subnet
332 InstanceIdentifier<Subnetmap> subMapid =
333 InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class,
334 new SubnetmapKey(subnetId)).build();
335 Optional<Subnetmap> sm = SingleTransactionDataBroker.syncReadOptional(dataBroker,
336 LogicalDatastoreType.CONFIGURATION, subMapid);
337 if (!sm.isPresent()) {
338 LOG.error("{} onSubnetDeletedFromVpn: Stale ports removal: Unable to retrieve subnetmap entry"
339 + " for subnet {} subnetIp {} vpnName {}", LOGGING_PREFIX, subnetId.getValue(),
340 optionalSubs.get().getSubnetCidr(), optionalSubs.get().getVpnName());
342 Subnetmap subMap = sm.get();
343 List<Uuid> portList = subMap.getPortList();
344 if (portList != null) {
345 for (Uuid port : portList) {
346 InstanceIdentifier<PortOpDataEntry> portOpIdentifier =
347 InstanceIdentifier.builder(PortOpData.class).child(PortOpDataEntry.class,
348 new PortOpDataEntryKey(port.getValue())).build();
349 LOG.trace("{} onSubnetDeletedFromVpn: Deleting portOpData entry for port {}"
350 + " from subnet {} subnetIp {} vpnName {} TaskState {}",
351 LOGGING_PREFIX, port.getValue(), subnetId.getValue(),
352 optionalSubs.get().getSubnetCidr(), optionalSubs.get().getVpnName(),
353 optionalSubs.get().getRouteAdvState());
354 SingleTransactionDataBroker.syncDelete(dataBroker, LogicalDatastoreType.OPERATIONAL,
355 portOpIdentifier, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
360 SubnetOpDataEntryBuilder subOpBuilder = new SubnetOpDataEntryBuilder(optionalSubs.get());
361 String rd = subOpBuilder.getVrfId();
362 String subnetIp = subOpBuilder.getSubnetCidr();
363 String vpnName = subOpBuilder.getVpnName();
364 //Withdraw the routes for all the interfaces on this subnet
365 //Remove subnet route entry from FIB
366 deleteSubnetRouteFromFib(rd, subnetIp, vpnName, isBgpVpn);
367 SingleTransactionDataBroker.syncDelete(dataBroker, LogicalDatastoreType.OPERATIONAL, subOpIdentifier,
368 VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
369 LOG.info("{} onSubnetDeletedFromVpn: Removed subnetopdataentry successfully from Datastore"
370 + " for subnet {} subnetIp {} vpnName {} rd {}", LOGGING_PREFIX, subnetId.getValue(),
371 subnetIp, vpnName, rd);
372 } catch (RuntimeException e) { //TODO: Avoid this
373 LOG.error("{} onSubnetDeletedFromVpn: Unable to handle subnet {} with Ip {} removed from vpn {}",
374 LOGGING_PREFIX, subnetId.getValue(), subnetmap.getSubnetIp(), subnetmap.getVpnId(), e);
375 } catch (TransactionCommitFailedException ex) {
376 LOG.error("{} onSubnetDeletedFromVpn: Removal of SubnetOpDataEntry for subnet {} subnetIp {}"
377 + " vpnId {} failed", LOGGING_PREFIX, subnetId.getValue(), subnetmap.getSubnetIp(),
378 subnetmap.getVpnId(), ex);
379 } catch (ReadFailedException e) {
380 LOG.error("{} onSubnetDeletedFromVpn: Failed to read data store for subnet {} ip {} vpn {}",
381 LOGGING_PREFIX, subnetId, subnetmap.getSubnetIp(), subnetmap.getVpnId());
383 vpnUtil.unlockSubnet(subnetId.getValue());
387 public void onSubnetUpdatedInVpn(Subnetmap subnetmap, Long elanTag) {
388 Uuid subnetId = subnetmap.getId();
389 String vpnName = subnetmap.getVpnId().getValue();
390 String subnetIp = subnetmap.getSubnetIp();
392 Preconditions.checkNotNull(subnetId,
393 LOGGING_PREFIX + " onSubnetUpdatedInVpn: SubnetId cannot be null or empty!");
394 Preconditions.checkNotNull(subnetIp,
395 LOGGING_PREFIX + " onSubnetUpdatedInVpn: SubnetPrefix cannot be null or empty!");
396 Preconditions.checkNotNull(vpnName, LOGGING_PREFIX + " onSubnetUpdatedInVpn: VpnName cannot be null or empty!");
397 Preconditions.checkNotNull(elanTag, LOGGING_PREFIX + " onSubnetUpdatedInVpn: ElanTag cannot be null or empty!");
399 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier =
400 InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
401 new SubnetOpDataEntryKey(subnetId)).build();
402 Optional<SubnetOpDataEntry> optionalSubs =
403 SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL,
405 if (optionalSubs.isPresent()) {
406 onSubnetDeletedFromVpn(subnetmap, true);
408 onSubnetAddedToVpn(subnetmap, true, elanTag);
410 LOG.info("{} onSubnetUpdatedInVpn: subnet {} with Ip {} updated successfully for vpn {}", LOGGING_PREFIX,
411 subnetId.getValue(), subnetIp, vpnName);
412 } catch (ReadFailedException e) {
413 LOG.error("onSubnetUpdatedInVpn: Failed to read data store for subnet{} ip {} elanTag {} vpn {}",subnetId,
414 subnetIp, elanTag, vpnName);
418 // TODO Clean up the exception handling
419 @SuppressWarnings("checkstyle:IllegalCatch")
420 public void onPortAddedToSubnet(Subnetmap subnetmap, Uuid portId) {
421 Uuid subnetId = subnetmap.getId();
422 LOG.info("{} onPortAddedToSubnet: Port {} being added to subnet {}", LOGGING_PREFIX, portId.getValue(),
423 subnetId.getValue());
424 //TODO(vivek): Change this to use more granularized lock at subnetId level
426 vpnUtil.lockSubnet(subnetId.getValue());
427 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier =
428 InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
429 new SubnetOpDataEntryKey(subnetId)).build();
431 Optional<SubnetOpDataEntry> optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker,
432 LogicalDatastoreType.OPERATIONAL, subOpIdentifier);
433 if (!optionalSubs.isPresent()) {
434 LOG.info("{} onPortAddedToSubnet: Port {} is part of a subnet {} that is not in VPN, ignoring",
435 LOGGING_PREFIX, portId.getValue(), subnetId.getValue());
438 String vpnName = optionalSubs.get().getVpnName();
439 String subnetIp = optionalSubs.get().getSubnetCidr();
440 String rd = optionalSubs.get().getVrfId();
441 String routeAdvState = optionalSubs.get().getRouteAdvState().toString();
442 LOG.info("{} onPortAddedToSubnet: Port {} being added to subnet {} subnetIp {} vpnName {} rd {} "
443 + "TaskState {}", LOGGING_PREFIX, portId.getValue(), subnetId.getValue(), subnetIp,
444 vpnName, rd, routeAdvState);
445 subOpDpnManager.addPortOpDataEntry(portId.getValue(), subnetId, null);
446 Interface intfState = InterfaceUtils.getInterfaceStateFromOperDS(dataBroker,portId.getValue());
447 if (intfState == null) {
448 // Interface State not yet available
453 dpnId = InterfaceUtils.getDpIdFromInterface(intfState);
454 } catch (Exception e) {
455 LOG.error("{} onPortAddedToSubnet: Unable to obtain dpnId for interface {}. subnetroute inclusion"
456 + " for this interface failed for subnet {} subnetIp {} vpn {} rd {}",
457 LOGGING_PREFIX, portId.getValue(), subnetId.getValue(), subnetIp, vpnName, rd, e);
460 if (dpnId.equals(Uint64.ZERO)) {
461 LOG.error("{} onPortAddedToSubnet: Port {} is not assigned DPN yet, ignoring subnetRoute "
462 + "inclusion for the interface into subnet {} subnetIp {} vpnName {} rd {}",
463 LOGGING_PREFIX, portId.getValue(), subnetId.getValue(), subnetIp, vpnName, rd);
466 subOpDpnManager.addPortOpDataEntry(portId.getValue(), subnetId, dpnId);
467 if (intfState.getOperStatus() != OperStatus.Up) {
468 LOG.error("{} onPortAddedToSubnet: Port {} is not UP yet, ignoring subnetRoute inclusion for "
469 + "the interface into subnet {} subnetIp {} vpnName {} rd {}", LOGGING_PREFIX,
470 portId.getValue(), subnetId.getValue(), subnetIp, vpnName, rd);
473 LOG.debug("{} onPortAddedToSubnet: Port {} added. Updating the SubnetOpDataEntry node for subnet {} "
474 + "subnetIp {} vpnName {} rd {} TaskState {}", LOGGING_PREFIX, portId.getValue(),
475 subnetId.getValue(), subnetIp, vpnName, rd, routeAdvState);
476 SubnetToDpn subDpn = subOpDpnManager.addInterfaceToDpn(subnetId, dpnId, portId.getValue());
477 if (subDpn == null) {
478 LOG.error("{} onPortAddedToSubnet: subnet-to-dpn list is null for subnetId {}. portId {}, "
479 + "vpnName {}, rd {}, subnetIp {}", LOGGING_PREFIX, subnetId.getValue(),
480 portId.getValue(), vpnName, rd, subnetIp);
483 SubnetOpDataEntry subnetOpDataEntry = optionalSubs.get();
484 SubnetOpDataEntryBuilder subOpBuilder = new SubnetOpDataEntryBuilder(subnetOpDataEntry);
485 List<SubnetToDpn> subDpnList = subOpBuilder.getSubnetToDpn();
486 subDpnList.add(subDpn);
487 subOpBuilder.setSubnetToDpn(subDpnList);
488 if (subOpBuilder.getRouteAdvState() != TaskState.Advertised) {
489 if (subOpBuilder.getNhDpnId() == null) {
490 // No nexthop selected yet, elect one now
491 electNewDpnForSubnetRoute(subOpBuilder, null /* oldDpnId */, subnetId,
492 subnetmap.getNetworkId(), true);
493 } else if (!VpnUtil.isExternalSubnetVpn(subnetOpDataEntry.getVpnName(), subnetId.getValue())) {
494 // Already nexthop has been selected, only publishing to bgp required, so publish to bgp
495 getNexthopTepAndPublishRoute(subOpBuilder, subnetId);
498 SubnetOpDataEntry subOpEntry = subOpBuilder.build();
499 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, subOpIdentifier,
500 subOpEntry, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
501 LOG.info("{} onPortAddedToSubnet: Updated subnetopdataentry to OP Datastore for port {} subnet {}"
502 + " subnetIp {} vpnName {} rd {} TaskState {} lastTaskState {}", LOGGING_PREFIX,
503 portId.getValue(), subnetId.getValue(), subOpEntry.getSubnetCidr(), subOpEntry.getVpnName(),
504 subOpBuilder.getVrfId(), subOpEntry.getRouteAdvState(), subOpEntry.getLastAdvState());
505 } catch (RuntimeException e) { //TODO: Avoid this
506 LOG.error("{} onPortAddedToSubnet: Unable to handle port {} added to subnet {}", LOGGING_PREFIX,
507 portId.getValue(), subnetId.getValue(), e);
508 } catch (ReadFailedException e) {
509 LOG.error("{} onPortAddedToSubnet: Failed to read data store for port {} subnet {}", LOGGING_PREFIX,
511 } catch (TransactionCommitFailedException e) {
512 LOG.error("{} onPortAddedToSubnet: Updation of subnetOpEntry for port {} subnet {} falied",
513 LOGGING_PREFIX, portId.getValue(), subnetId.getValue(), e);
515 vpnUtil.unlockSubnet(subnetId.getValue());
519 // TODO Clean up the exception handling
520 @SuppressWarnings("checkstyle:IllegalCatch")
521 public void onPortRemovedFromSubnet(Subnetmap subnetmap, Uuid portId) {
522 Uuid subnetId = subnetmap.getId();
523 //TODO(vivek): Change this to use more granularized lock at subnetId level
525 vpnUtil.lockSubnet(subnetId.getValue());
526 PortOpDataEntry portOpEntry = subOpDpnManager.removePortOpDataEntry(portId.getValue(),
528 if (portOpEntry == null) {
531 Uint64 dpnId = portOpEntry.getDpnId();
533 LOG.error("{} onPortRemovedFromSubnet: Port {} does not have a DPNId associated,"
534 + " ignoring removal from subnet {}", LOGGING_PREFIX, portId.getValue(),
535 subnetId.getValue());
538 boolean last = subOpDpnManager.removeInterfaceFromDpn(subnetId, dpnId, portId.getValue());
539 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier =
540 InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
541 new SubnetOpDataEntryKey(subnetId)).build();
542 Optional<SubnetOpDataEntry> optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker,
543 LogicalDatastoreType.OPERATIONAL, subOpIdentifier);
544 if (!optionalSubs.isPresent()) {
545 LOG.info("{} onPortRemovedFromSubnet: Port {} is part of a subnet {} that is not in VPN,"
546 + " ignoring", LOGGING_PREFIX, portId.getValue(), subnetId.getValue());
549 LOG.info("{} onPortRemovedFromSubnet: Port {} being removed. Updating the SubnetOpDataEntry"
550 + " for subnet {} subnetIp {} vpnName {} rd {} TaskState {} lastTaskState {}",
551 LOGGING_PREFIX, portId.getValue(), subnetId.getValue(), optionalSubs.get().getSubnetCidr(),
552 optionalSubs.get().getVpnName(), optionalSubs.get().getVrfId(),
553 optionalSubs.get().getRouteAdvState(), optionalSubs.get().getLastAdvState());
554 SubnetOpDataEntry subnetOpDataEntry = optionalSubs.get();
555 SubnetOpDataEntryBuilder subOpBuilder = new SubnetOpDataEntryBuilder(subnetOpDataEntry);
556 Uint64 nhDpnId = subOpBuilder.getNhDpnId();
557 if (nhDpnId != null && nhDpnId.equals(dpnId)) {
558 // select another NhDpnId
560 LOG.debug("{} onPortRemovedFromSubnet: Last port {} being removed from subnet {} subnetIp {}"
561 + " vpnName {} rd {}", LOGGING_PREFIX, portId.getValue(), subnetId.getValue(),
562 subOpBuilder.getSubnetCidr(), subOpBuilder.getVpnName(), subOpBuilder.getVrfId());
563 // last port on this DPN, so we need to elect the new NHDpnId
564 electNewDpnForSubnetRoute(subOpBuilder, nhDpnId, subnetId, subnetmap.getNetworkId(),
565 !VpnUtil.isExternalSubnetVpn(subnetOpDataEntry.getVpnName(), subnetId.getValue()));
566 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL,
567 subOpIdentifier, subOpBuilder.build(), VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
568 LOG.info("{} onPortRemovedFromSubnet: Updated subnetopdataentry to OP Datastore"
569 + " removing port {} from subnet {} subnetIp {} vpnName {} rd {}", LOGGING_PREFIX,
570 portId.getValue(), subnetId.getValue(), subOpBuilder.getSubnetCidr(),
571 subOpBuilder.getVpnName(), subOpBuilder.getVrfId());
574 } catch (RuntimeException e) {
575 LOG.error("{} onPortRemovedFromSubnet: Unable to handle port {} removed from subnet {}", LOGGING_PREFIX,
576 portId.getValue(), subnetId.getValue(), e);
577 } catch (ReadFailedException e) {
578 LOG.error("{} onPortRemovedFromSubnet: Failed to read data store for port {} subnet {}", LOGGING_PREFIX,
580 } catch (TransactionCommitFailedException e) {
581 LOG.error("{} onPortRemovedFromSubnet: Removal of portOp for {} from subnet {} failed", LOGGING_PREFIX,
582 portId.getValue(), subnetId.getValue(), e);
584 vpnUtil.unlockSubnet(subnetId.getValue());
588 // TODO Clean up the exception handling
589 @SuppressWarnings("checkstyle:IllegalCatch")
590 public void onInterfaceUp(Uint64 dpnId, String intfName, Uuid subnetId) {
591 //TODO(vivek): Change this to use more granularized lock at subnetId level
592 SubnetToDpn subDpn = null;
593 if (dpnId == null || Objects.equals(dpnId, Uint64.ZERO)) {
594 LOG.error("{} onInterfaceUp: Unable to determine the DPNID for port {} on subnet {}", LOGGING_PREFIX,
595 intfName, subnetId.getValue());
599 vpnUtil.lockSubnet(subnetId.getValue());
600 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier =
601 InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
602 new SubnetOpDataEntryKey(subnetId)).build();
603 Optional<SubnetOpDataEntry> optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker,
604 LogicalDatastoreType.OPERATIONAL, subOpIdentifier);
605 if (!optionalSubs.isPresent()) {
606 LOG.trace("{} onInterfaceUp: SubnetOpDataEntry for subnet {} is not available."
607 + " Ignoring interfaceUp for port{}", LOGGING_PREFIX, subnetId.getValue(), intfName);
610 subOpDpnManager.addPortOpDataEntry(intfName, subnetId, dpnId);
611 subDpn = subOpDpnManager.addInterfaceToDpn(subnetId, dpnId, intfName);
612 if (subDpn == null) {
615 SubnetOpDataEntry subnetOpDataEntry = optionalSubs.get();
616 SubnetOpDataEntryBuilder subOpBuilder = new SubnetOpDataEntryBuilder(subnetOpDataEntry);
617 LOG.info("{} onInterfaceUp: Updating the SubnetOpDataEntry node for subnet {} subnetIp {} vpn {}"
618 + " rd {} TaskState {} lastTaskState {}" , LOGGING_PREFIX, subnetId.getValue(),
619 subOpBuilder.getSubnetCidr(), subOpBuilder.getVpnName(), subOpBuilder.getVrfId(),
620 subOpBuilder.getRouteAdvState(), subOpBuilder.getLastAdvState());
621 boolean isExternalSubnetVpn = VpnUtil.isExternalSubnetVpn(subnetOpDataEntry.getVpnName(),
622 subnetId.getValue());
623 List<SubnetToDpn> subDpnList = new ArrayList<>();
624 subDpnList = subOpBuilder.getSubnetToDpn() != null ? new ArrayList<>(subOpBuilder.getSubnetToDpn())
626 subDpnList.add(subDpn);
627 subOpBuilder.setSubnetToDpn(subDpnList);
628 if (subOpBuilder.getRouteAdvState() != TaskState.Advertised) {
629 if (subOpBuilder.getNhDpnId() == null) {
630 // No nexthop selected yet, elect one now
631 electNewDpnForSubnetRoute(subOpBuilder, null /* oldDpnId */, subnetId,
632 null /*networkId*/, !isExternalSubnetVpn);
633 } else if (!isExternalSubnetVpn) {
634 // Already nexthop has been selected, only publishing to bgp required, so publish to bgp
635 getNexthopTepAndPublishRoute(subOpBuilder, subnetId);
638 SubnetOpDataEntry subOpEntry = subOpBuilder.build();
639 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, subOpIdentifier,
640 subOpEntry, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
641 LOG.info("{} onInterfaceUp: Updated subnetopdataentry to OP Datastore port {} up for subnet {}"
642 + " subnetIp {} vpnName {} rd {} TaskState {} lastTaskState {} ", LOGGING_PREFIX, intfName,
643 subnetId.getValue(), subOpEntry.getSubnetCidr(), subOpEntry.getVpnName(),
644 subOpEntry.getVrfId(), subOpEntry.getRouteAdvState(), subOpEntry.getLastAdvState());
645 } catch (RuntimeException e) {
646 LOG.error("{} onInterfaceUp: Unable to handle interface up event for port {} in subnet {}",
647 LOGGING_PREFIX, intfName, subnetId.getValue(), e);
648 } catch (ReadFailedException e) {
649 LOG.error("{} onInterfaceUp: Failed to read data store for interface {} dpn {} subnet {}", LOGGING_PREFIX,
650 intfName, dpnId, subnetId);
651 } catch (TransactionCommitFailedException e) {
652 LOG.error("{} onInterfaceUp: Updation of SubnetOpDataEntry for subnet {} on port {} up failed",
653 LOGGING_PREFIX, subnetId.getValue(), intfName, e);
655 vpnUtil.unlockSubnet(subnetId.getValue());
659 // TODO Clean up the exception handling
660 @SuppressWarnings("checkstyle:IllegalCatch")
661 public void onInterfaceDown(final Uint64 dpnId, final String interfaceName, Uuid subnetId) {
662 if (dpnId == null || Objects.equals(dpnId, Uint64.ZERO)) {
663 LOG.error("{} onInterfaceDown: Unable to determine the DPNID for port {} on subnet {}", LOGGING_PREFIX,
664 interfaceName, subnetId.getValue());
668 vpnUtil.lockSubnet(subnetId.getValue());
669 boolean last = subOpDpnManager.removeInterfaceFromDpn(subnetId, dpnId, interfaceName);
670 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier =
671 InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
672 new SubnetOpDataEntryKey(subnetId)).build();
673 Optional<SubnetOpDataEntry> optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker,
674 LogicalDatastoreType.OPERATIONAL,
676 if (!optionalSubs.isPresent()) {
677 LOG.info("{} onInterfaceDown: SubnetOpDataEntry for subnet {} is not available."
678 + " Ignoring port {} down event.", LOGGING_PREFIX, subnetId.getValue(), interfaceName);
681 SubnetOpDataEntry subnetOpDataEntry = optionalSubs.get();
682 SubnetOpDataEntryBuilder subOpBuilder = new SubnetOpDataEntryBuilder(subnetOpDataEntry);
683 LOG.info("{} onInterfaceDown: Updating the SubnetOpDataEntry node for subnet {} subnetIp {}"
684 + " vpnName {} rd {} TaskState {} lastTaskState {} on port {} down", LOGGING_PREFIX,
685 subnetId.getValue(), subOpBuilder.getSubnetCidr(), subOpBuilder.getVpnName(),
686 subOpBuilder.getVrfId(), subOpBuilder.getRouteAdvState(), subOpBuilder.getLastAdvState(),
688 Uint64 nhDpnId = subOpBuilder.getNhDpnId();
689 if (nhDpnId != null && nhDpnId.equals(dpnId)) {
690 // select another NhDpnId
692 LOG.debug("{} onInterfaceDown: Last active port {} on the subnet {} subnetIp {} vpn {}"
693 + " rd {}", LOGGING_PREFIX, interfaceName, subnetId.getValue(),
694 subOpBuilder.getSubnetCidr(), subOpBuilder.getVpnName(), subOpBuilder.getVrfId());
695 // last port on this DPN, so we need to elect the new NHDpnId
696 electNewDpnForSubnetRoute(subOpBuilder, dpnId, subnetId, null /*networkId*/,
697 !VpnUtil.isExternalSubnetVpn(subnetOpDataEntry.getVpnName(), subnetId.getValue()));
698 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL,
699 subOpIdentifier, subOpBuilder.build(), VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
700 LOG.info("{} onInterfaceDown: Updated subnetopdataentry for subnet {} subnetIp {} vpnName {}"
701 + " rd {} to OP Datastore on port {} down ", LOGGING_PREFIX, subnetId.getValue(),
702 subOpBuilder.getSubnetCidr(), subOpBuilder.getVpnName(), subOpBuilder.getVrfId(),
706 } catch (RuntimeException e) { //TODO: Remove RuntimeException
707 LOG.error("{} onInterfaceDown: Unable to handle interface down event for port {} in subnet {}",
708 LOGGING_PREFIX, interfaceName, subnetId.getValue(), e);
709 } catch (ReadFailedException e) {
710 LOG.error("{} onInterfaceDown: Failed to read data store for interface {} dpn {} subnet {}",
711 LOGGING_PREFIX, interfaceName, dpnId, subnetId.getValue(), e);
712 } catch (TransactionCommitFailedException ex) {
713 LOG.error("{} onInterfaceDown: SubnetOpDataEntry update on interface {} down event for subnet {} failed",
714 LOGGING_PREFIX, interfaceName, subnetId.getValue(), ex);
716 vpnUtil.unlockSubnet(subnetId.getValue());
720 // TODO Clean up the exception handling
721 @SuppressWarnings("checkstyle:IllegalCatch")
722 public void updateSubnetRouteOnTunnelUpEvent(Uuid subnetId, Uint64 dpnId) {
723 LOG.info("{} updateSubnetRouteOnTunnelUpEvent: Subnet {} Dpn {}", LOGGING_PREFIX, subnetId.getValue(),
726 vpnUtil.lockSubnet(subnetId.getValue());
727 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier =
728 InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
729 new SubnetOpDataEntryKey(subnetId)).build();
730 Optional<SubnetOpDataEntry> optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker,
731 LogicalDatastoreType.OPERATIONAL, subOpIdentifier);
732 if (!optionalSubs.isPresent()) {
733 LOG.error("{} updateSubnetRouteOnTunnelUpEvent: SubnetOpDataEntry for subnet {} is not available",
734 LOGGING_PREFIX, subnetId.getValue());
737 LOG.info("{} updateSubnetRouteOnTunnelUpEvent: Subnet {} subnetIp {} vpnName {} rd {} TaskState {}"
738 + " lastTaskState {} Dpn {}", LOGGING_PREFIX, subnetId.getValue(),
739 optionalSubs.get().getSubnetCidr(), optionalSubs.get().getVpnName(),
740 optionalSubs.get().getVrfId(), optionalSubs.get().getRouteAdvState(),
741 optionalSubs.get().getLastAdvState(), dpnId.toString());
742 SubnetOpDataEntry subOpEntry = optionalSubs.get();
743 SubnetOpDataEntryBuilder subOpBuilder = new SubnetOpDataEntryBuilder(subOpEntry);
744 boolean isExternalSubnetVpn = VpnUtil.isExternalSubnetVpn(subOpEntry.getVpnName(), subnetId.getValue());
745 if (subOpBuilder.getRouteAdvState() != TaskState.Advertised) {
746 if (subOpBuilder.getNhDpnId() == null) {
747 // No nexthop selected yet, elect one now
748 electNewDpnForSubnetRoute(subOpBuilder, null /* oldDpnId */, subnetId,
749 null /*networkId*/, !isExternalSubnetVpn);
750 } else if (!isExternalSubnetVpn) {
751 // Already nexthop has been selected, only publishing to bgp required, so publish to bgp
752 getNexthopTepAndPublishRoute(subOpBuilder, subnetId);
755 subOpEntry = subOpBuilder.build();
756 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, subOpIdentifier,
757 subOpEntry, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
758 LOG.info("{} updateSubnetRouteOnTunnelUpEvent: Updated subnetopdataentry to OP Datastore tunnel up"
759 + " on dpn {} for subnet {} subnetIp {} vpnName {} rd {} TaskState {} lastTaskState {}",
760 LOGGING_PREFIX, dpnId.toString(), subnetId.getValue(), subOpEntry.getSubnetCidr(),
761 subOpEntry.getVpnName(), subOpEntry.getVrfId(), subOpEntry.getRouteAdvState(),
762 subOpEntry.getLastAdvState());
763 } catch (RuntimeException e) { //TODO: lockSubnet() throws this exception. Rectify lockSubnet()
764 LOG.error("{} updateSubnetRouteOnTunnelUpEvent: Unable to handle tunnel up event for subnetId {} dpnId {}",
765 LOGGING_PREFIX, subnetId.getValue(), dpnId.toString(), e);
766 } catch (TransactionCommitFailedException ex) {
767 LOG.error("{} updateSubnetRouteOnTunnelUpEvent: Failed to update subnetRoute for subnet {} on dpn {}",
768 LOGGING_PREFIX, subnetId.getValue(), dpnId.toString(), ex);
769 } catch (ReadFailedException e) {
770 LOG.error("{} updateSubnetRouteOnTunnelUpEvent: Failed to read data store for subnet {} on dpn {}",
771 LOGGING_PREFIX, subnetId.getValue(), dpnId.toString(), e);
774 vpnUtil.unlockSubnet(subnetId.getValue());
778 // TODO Clean up the exception handling
779 @SuppressWarnings("checkstyle:IllegalCatch")
780 public void updateSubnetRouteOnTunnelDownEvent(Uuid subnetId, Uint64 dpnId) {
781 LOG.info("updateSubnetRouteOnTunnelDownEvent: Subnet {} Dpn {}", subnetId.getValue(), dpnId.toString());
782 //TODO(vivek): Change this to use more granularized lock at subnetId level
784 vpnUtil.lockSubnet(subnetId.getValue());
785 InstanceIdentifier<SubnetOpDataEntry> subOpIdentifier =
786 InstanceIdentifier.builder(SubnetOpData.class).child(SubnetOpDataEntry.class,
787 new SubnetOpDataEntryKey(subnetId)).build();
788 Optional<SubnetOpDataEntry> optionalSubs = SingleTransactionDataBroker.syncReadOptional(dataBroker,
789 LogicalDatastoreType.OPERATIONAL, subOpIdentifier);
790 if (!optionalSubs.isPresent()) {
791 LOG.error("{} updateSubnetRouteOnTunnelDownEvent: SubnetOpDataEntry for subnet {}"
792 + " is not available", LOGGING_PREFIX, subnetId.getValue());
795 LOG.debug("{} updateSubnetRouteOnTunnelDownEvent: Dpn {} Subnet {} subnetIp {} vpnName {} rd {}"
796 + " TaskState {} lastTaskState {}", LOGGING_PREFIX, dpnId.toString(), subnetId.getValue(),
797 optionalSubs.get().getSubnetCidr(), optionalSubs.get().getVpnName(),
798 optionalSubs.get().getVrfId(), optionalSubs.get().getRouteAdvState(),
799 optionalSubs.get().getLastAdvState());
800 SubnetOpDataEntry subOpEntry = null;
801 SubnetOpDataEntryBuilder subOpBuilder = new SubnetOpDataEntryBuilder(optionalSubs.get());
802 Uint64 nhDpnId = subOpBuilder.getNhDpnId();
803 if (nhDpnId != null && nhDpnId.equals(dpnId)) {
804 electNewDpnForSubnetRoute(subOpBuilder, dpnId, subnetId, null /*networkId*/, true);
805 subOpEntry = subOpBuilder.build();
806 SingleTransactionDataBroker.syncWrite(dataBroker, LogicalDatastoreType.OPERATIONAL, subOpIdentifier,
807 subOpEntry, VpnUtil.SINGLE_TRANSACTION_BROKER_NO_RETRY);
808 LOG.info("{} updateSubnetRouteOnTunnelDownEvent: Subnet {} Dpn {} subnetIp {} vpnName {} rd {}"
809 + " TaskState {} lastTaskState {}", LOGGING_PREFIX, subnetId.getValue(),
810 dpnId.toString(), optionalSubs.get().getSubnetCidr(), optionalSubs.get().getVpnName(),
811 optionalSubs.get().getVrfId(), optionalSubs.get().getRouteAdvState(),
812 optionalSubs.get().getLastAdvState());
814 } catch (RuntimeException e) {
815 LOG.error("{} updateSubnetRouteOnTunnelDownEvent: Unable to handle tunnel down event for subnetId {}"
816 + " dpnId {}", LOGGING_PREFIX, subnetId.getValue(), dpnId.toString(), e);
817 } catch (ReadFailedException e) {
818 LOG.error("{} Failed to read data store for subnet {} dpn {}", LOGGING_PREFIX, subnetId, dpnId);
819 } catch (TransactionCommitFailedException e) {
820 LOG.error("{} updateSubnetRouteOnTunnelDownEvent: Updation of SubnetOpDataEntry for subnet {}"
821 + " on dpn {} failed", LOGGING_PREFIX, subnetId.getValue(), dpnId, e);
823 vpnUtil.unlockSubnet(subnetId.getValue());
827 @SuppressWarnings("checkstyle:IllegalCatch")
828 private void publishSubnetRouteToBgp(SubnetOpDataEntryBuilder subOpBuilder, String nextHopIp) {
830 //BGP manager will handle withdraw and advertise internally if prefix
832 Uint32 label = Uint32.ZERO;
833 Uint32 l3vni = Uint32.ZERO;
835 VrfEntry.EncapType encapType = VpnUtil.getEncapType(VpnUtil.isL3VpnOverVxLan(l3vni));
836 if (encapType.equals(VrfEntry.EncapType.Vxlan)) {
837 l3vni = subOpBuilder.getL3vni();
839 label = subOpBuilder.getLabel();
841 bgpManager.advertisePrefix(subOpBuilder.getVrfId(), null /*macAddress*/, subOpBuilder.getSubnetCidr(),
842 Arrays.asList(nextHopIp), encapType, label, l3vni,
843 Uint32.ZERO /*l2vni*/, null /*gatewayMacAddress*/);
844 subOpBuilder.setLastAdvState(subOpBuilder.getRouteAdvState()).setRouteAdvState(TaskState.Advertised);
845 } catch (Exception e) {
846 LOG.error("{} publishSubnetRouteToBgp: Subnet route not advertised for subnet {} subnetIp {} vpn {} rd {}"
847 + " with dpnid {}", LOGGING_PREFIX, subOpBuilder.getSubnetId().getValue(),
848 subOpBuilder.getSubnetCidr(), subOpBuilder.getVpnName(), subOpBuilder.getVrfId(), nextHopIp, e);
852 private void getNexthopTepAndPublishRoute(SubnetOpDataEntryBuilder subOpBuilder, Uuid subnetId) {
853 String nhTepIp = InterfaceUtils.getEndpointIpAddressForDPN(dataBroker,
854 subOpBuilder.getNhDpnId());
855 if (nhTepIp != null) {
856 publishSubnetRouteToBgp(subOpBuilder, nhTepIp);
858 LOG.warn("Unable to find nexthopip for rd {} subnetroute subnetip {} for dpnid {}",
859 subOpBuilder.getVrfId(), subOpBuilder.getSubnetCidr(),
860 subOpBuilder.getNhDpnId().toString());
861 electNewDpnForSubnetRoute(subOpBuilder, null /* oldDpnId */, subnetId, null /*networkId*/, true);
865 // TODO Clean up the exception handling
866 @SuppressWarnings("checkstyle:IllegalCatch")
867 private boolean addSubnetRouteToFib(String rd, String subnetIp, Uint64 nhDpnId, String nextHopIp,
868 String vpnName, Long elanTag, Uint32 label, Uint32 l3vni,
869 Uuid subnetId, boolean isBgpVpn, String networkName) {
871 Preconditions.checkNotNull(rd,
872 LOGGING_PREFIX + " addSubnetRouteToFib: RouteDistinguisher cannot be null or empty!");
873 Preconditions.checkNotNull(subnetIp,
874 LOGGING_PREFIX + " addSubnetRouteToFib: SubnetRouteIp cannot be null or empty!");
875 Preconditions.checkNotNull(vpnName, LOGGING_PREFIX + " addSubnetRouteToFib: vpnName cannot be null or empty!");
876 Preconditions.checkNotNull(elanTag, LOGGING_PREFIX + " addSubnetRouteToFib: elanTag cannot be null or empty!");
877 Preconditions.checkNotNull(label, LOGGING_PREFIX + " addSubnetRouteToFib: label cannot be null or empty!");
878 VrfEntry.EncapType encapType = VpnUtil.getEncapType(VpnUtil.isL3VpnOverVxLan(l3vni));
879 VpnPopulator vpnPopulator = L3vpnRegistry.getRegisteredPopulator(encapType);
880 LOG.info("{} addSubnetRouteToFib: Adding SubnetRoute fib entry for vpnName {}, subnetIP {}, elanTag {}",
881 LOGGING_PREFIX, vpnName, subnetIp, elanTag);
882 L3vpnInput input = new L3vpnInput().setRouteOrigin(RouteOrigin.CONNECTED).setRd(rd).setVpnName(vpnName)
883 .setSubnetIp(subnetIp).setNextHopIp(nextHopIp).setL3vni(l3vni.longValue())
884 .setLabel(label.longValue()).setElanTag(elanTag)
885 .setDpnId(nhDpnId).setEncapType(encapType).setNetworkName(networkName).setPrimaryRd(rd);
887 vpnPopulator.populateFib(input, null /*writeCfgTxn*/);
890 Preconditions.checkNotNull(nextHopIp, LOGGING_PREFIX + "NextHopIp cannot be null or empty!");
891 vpnUtil.syncWrite(LogicalDatastoreType.OPERATIONAL, VpnUtil
892 .getPrefixToInterfaceIdentifier(vpnUtil.getVpnId(vpnName), subnetIp), VpnUtil
893 .getPrefixToInterface(nhDpnId, subnetId.getValue(), subnetIp, Prefixes.PrefixCue.SubnetRoute));
894 vpnPopulator.populateFib(input, null /*writeCfgTxn*/);
896 // BGP manager will handle withdraw and advertise internally if prefix
898 bgpManager.advertisePrefix(rd, null /*macAddress*/, subnetIp, Collections.singletonList(nextHopIp),
899 encapType, label, l3vni, Uint32.ZERO /*l2vni*/, null /*gatewayMacAddress*/);
900 } catch (Exception e) {
901 LOG.error("{} addSubnetRouteToFib: Subnet route not advertised for subnet {} subnetIp {} vpnName {} rd {} "
902 + "with dpnid {}", LOGGING_PREFIX, subnetId.getValue(), subnetIp, vpnName, rd, nhDpnId, e);
908 private Uint32 getLabel(String rd, String subnetIp) {
909 Uint32 label = vpnUtil.getUniqueId(VpnConstants.VPN_IDPOOL_NAME, VpnUtil.getNextHopLabelKey(rd, subnetIp));
910 LOG.trace("{} getLabel: Allocated subnetroute label {} for rd {} prefix {}", LOGGING_PREFIX, label, rd,
915 // TODO Clean up the exception handling
916 @SuppressWarnings("checkstyle:IllegalCatch")
917 private boolean deleteSubnetRouteFromFib(String rd, String subnetIp, String vpnName, boolean isBgpVpn) {
918 Preconditions.checkNotNull(rd,
919 LOGGING_PREFIX + " deleteSubnetRouteFromFib: RouteDistinguisher cannot be null or empty!");
920 Preconditions.checkNotNull(subnetIp,
921 LOGGING_PREFIX + " deleteSubnetRouteFromFib: SubnetRouteIp cannot be null or empty!");
922 deleteSubnetRouteFibEntryFromDS(rd, subnetIp, vpnName);
925 bgpManager.withdrawPrefix(rd, subnetIp);
926 } catch (Exception e) {
927 LOG.error("{} deleteSubnetRouteFromFib: Subnet route not withdrawn for subnetIp {} vpn {} rd {}",
928 LOGGING_PREFIX, subnetIp, vpnName, rd, e);
935 public void deleteSubnetRouteFibEntryFromDS(String rd, String prefix, String vpnName) {
936 fibManager.removeFibEntry(rd, prefix, null);
937 List<VpnInstanceOpDataEntry> vpnsToImportRoute = vpnUtil.getVpnsImportingMyRoute(vpnName);
938 for (VpnInstanceOpDataEntry vpnInstance : vpnsToImportRoute) {
939 String importingRd = vpnInstance.getVrfId();
940 fibManager.removeFibEntry(importingRd, prefix, null);
941 LOG.info("SUBNETROUTE: deleteSubnetRouteFibEntryFromDS: Deleted imported subnet route rd {} prefix {}"
942 + " from vpn {} importingRd {}", rd, prefix, vpnInstance.getVpnInstanceName(), importingRd);
944 LOG.info("SUBNETROUTE: deleteSubnetRouteFibEntryFromDS: Removed subnetroute FIB for prefix {} rd {}"
945 + " vpnName {}", prefix, rd, vpnName);
948 // TODO Clean up the exception handling
949 @SuppressWarnings("checkstyle:IllegalCatch")
950 private void electNewDpnForSubnetRoute(SubnetOpDataEntryBuilder subOpBuilder, @Nullable Uint64 oldDpnId,
951 Uuid subnetId, Uuid networkId, boolean isBgpVpn) {
952 List<SubnetToDpn> subDpnList = null;
953 boolean isRouteAdvertised = false;
954 subDpnList = new ArrayList<>();
955 subDpnList = subOpBuilder.getSubnetToDpn() != null ? new ArrayList<>(subOpBuilder.getSubnetToDpn())
957 String rd = subOpBuilder.getVrfId();
958 String subnetIp = subOpBuilder.getSubnetCidr();
959 String vpnName = subOpBuilder.getVpnName();
960 long elanTag = subOpBuilder.getElanTag().toJava();
961 boolean isAlternateDpnSelected = false;
962 Uint32 l3vni = Uint32.ZERO;
963 Uint32 label = Uint32.ZERO;
964 String networkName = networkId != null ? networkId.getValue() : null;
966 LOG.info("{} electNewDpnForSubnetRoute: Handling subnet {} subnetIp {} vpn {} rd {} TaskState {}"
967 + " lastTaskState {}", LOGGING_PREFIX, subnetId.getValue(), subnetIp, subOpBuilder.getVpnName(),
968 subOpBuilder.getVrfId(), subOpBuilder.getRouteAdvState(), subOpBuilder.getLastAdvState());
970 // Non-BGPVPN as it stands here represents use-case of External Subnets of VLAN-Provider-Network
971 // TODO(Tomer): Pulling in both external and internal VLAN-Provider-Network need to be
972 // blended more better into this design.
973 if (VpnUtil.isL3VpnOverVxLan(subOpBuilder.getL3vni())) {
974 l3vni = subOpBuilder.getL3vni();
976 label = getLabel(rd, subnetIp);
977 subOpBuilder.setLabel(label);
979 isRouteAdvertised = addSubnetRouteToFib(rd, subnetIp, null /* nhDpnId */, null /* nhTepIp */,
980 vpnName, elanTag, label, l3vni, subnetId, isBgpVpn, networkName);
981 if (isRouteAdvertised) {
982 subOpBuilder.setRouteAdvState(TaskState.Advertised);
984 LOG.error("{} electNewDpnForSubnetRoute: Unable to find TepIp for subnet {} subnetip {} vpnName {}"
985 + " rd {}, attempt next dpn", LOGGING_PREFIX, subnetId.getValue(), subnetIp,
987 subOpBuilder.setRouteAdvState(TaskState.PendingAdvertise);
992 String nhTepIp = null;
993 Uint64 nhDpnId = null;
994 for (SubnetToDpn subnetToDpn : subDpnList) {
995 if (subnetToDpn.getDpnId().equals(oldDpnId)) {
996 // Is this same is as input dpnId, then ignore it
999 nhDpnId = subnetToDpn.getDpnId();
1000 if (vpnNodeListener.isConnectedNode(nhDpnId)) {
1001 // selected dpnId is connected to ODL
1002 // but does it have a TEP configured at all?
1004 nhTepIp = InterfaceUtils.getEndpointIpAddressForDPN(dataBroker, nhDpnId);
1005 if (nhTepIp != null) {
1006 isAlternateDpnSelected = true;
1009 } catch (Exception e) {
1010 LOG.warn("{} electNewDpnForSubnetRoute: Unable to find TepIp for rd {} subnetroute subnetip {}"
1011 + " for dpnid {}, attempt next", LOGGING_PREFIX, rd, subnetIp, nhDpnId.toString(), e);
1015 if (!isAlternateDpnSelected) {
1016 //If no alternate Dpn is selected as nextHopDpn, withdraw the subnetroute if it had a nextHop already.
1017 if (isRouteAdvertised(subOpBuilder) && oldDpnId != null) {
1018 LOG.info("{} electNewDpnForSubnetRoute: No alternate DPN available for subnet {} subnetIp {} vpn {}"
1019 + " rd {} Prefix withdrawn from BGP", LOGGING_PREFIX, subnetId.getValue(), subnetIp, vpnName,
1021 // Withdraw route from BGP for this subnet
1022 boolean routeWithdrawn = deleteSubnetRouteFromFib(rd, subnetIp, vpnName, isBgpVpn);
1023 //subOpBuilder.setNhDpnId(Uint64.valueOf(null));
1024 subOpBuilder.setLastAdvState(subOpBuilder.getRouteAdvState());
1025 if (routeWithdrawn) {
1026 subOpBuilder.setRouteAdvState(TaskState.Withdrawn);
1028 LOG.error("{} electNewDpnForSubnetRoute: Withdrawing NextHopDPN {} for subnet {} subnetIp {}"
1029 + " vpn {} rd {} from BGP failed", LOGGING_PREFIX, oldDpnId, subnetId.getValue(),
1030 subnetIp, vpnName, rd);
1031 subOpBuilder.setRouteAdvState(TaskState.PendingWithdraw);
1035 //If alternate Dpn is selected as nextHopDpn, use that for subnetroute.
1036 subOpBuilder.setNhDpnId(nhDpnId);
1037 if (VpnUtil.isL3VpnOverVxLan(subOpBuilder.getL3vni())) {
1038 l3vni = subOpBuilder.getL3vni();
1040 label = getLabel(rd, subnetIp);
1041 subOpBuilder.setLabel(label);
1043 //update the VRF entry for the subnetroute.
1044 isRouteAdvertised = addSubnetRouteToFib(rd, subnetIp, nhDpnId, nhTepIp,
1045 vpnName, elanTag, label, l3vni, subnetId, isBgpVpn, networkName);
1046 subOpBuilder.setLastAdvState(subOpBuilder.getRouteAdvState());
1047 if (isRouteAdvertised) {
1048 subOpBuilder.setRouteAdvState(TaskState.Advertised);
1050 LOG.error("{} electNewDpnForSubnetRoute: Swapping to add new NextHopDpn {} for subnet {} subnetIp {}"
1051 + " vpn {} rd {} failed", LOGGING_PREFIX, nhDpnId, subnetId.getValue(), subnetIp, vpnName, rd);
1052 subOpBuilder.setRouteAdvState(TaskState.PendingAdvertise);
1057 private boolean isRouteAdvertised(SubnetOpDataEntryBuilder subOpBuilder) {
1058 return subOpBuilder.getRouteAdvState() == TaskState.Advertised
1059 || subOpBuilder.getRouteAdvState() == TaskState.PendingAdvertise;