+
+ protected void delAdjFromVpnInterface(InstanceIdentifier<VpnInterface> identifier, Adjacency adj) {
+ Optional<VpnInterface> optVpnInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, identifier);
+
+ if (optVpnInterface.isPresent()) {
+ VpnInterface currVpnIntf = optVpnInterface.get();
+
+ InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
+ Optional<Adjacencies> optAdjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
+ if (optAdjacencies.isPresent()) {
+ List<Adjacency> adjacencies = optAdjacencies.get().getAdjacency();
+
+ if (!adjacencies.isEmpty()) {
+ String rd = getRouteDistinguisher(currVpnIntf.getVpnInstanceName());
+ LOG.trace("Adjacencies are " + adjacencies);
+ Iterator<Adjacency> adjIt = adjacencies.iterator();
+ while (adjIt.hasNext()) {
+ Adjacency adjElem = adjIt.next();
+ if (adjElem.getIpAddress().equals(adj.getIpAddress())) {
+ VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
+ VpnUtil.getNextHopLabelKey(rd, adj.getIpAddress()));
+ adjIt.remove();
+
+ Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(adjacencies);
+ VpnInterface newVpnIntf = VpnUtil.getVpnInterface(currVpnIntf.getName(), currVpnIntf.getVpnInstanceName(), aug);
+
+ VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, newVpnIntf);
+
+ delExtraRoute(adj.getIpAddress(), rd, currVpnIntf.getVpnInstanceName());
+ break;
+ }
+
+ }
+ }
+ }
+ }
+
+ }
+
+ protected void addExtraRoute(String destination, String nextHop, String rd, String routerID, int label, String intfName) {
+
+ //add extra route to vpn mapping; advertise with nexthop as tunnel ip
+ VpnUtil.syncUpdate(
+ broker,
+ LogicalDatastoreType.OPERATIONAL,
+ VpnUtil.getVpnToExtrarouteIdentifier(
+ (rd != null) ? rd : routerID, destination),
+ VpnUtil.getVpnToExtraroute(destination, nextHop));
+
+ if(intfName != null && !intfName.isEmpty()) {
+ BigInteger dpnId = InterfaceUtils.getDpnForInterface(interfaceManager, intfName);
+ String nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(broker, dpnId);
+ if (nextHopIp == null && !nextHopIp.isEmpty()) {
+ LOG.error("NextHop for interface {} is null. Failed adding extra route for prefix {}", intfName, destination);
+ return;
+ }
+ nextHop = nextHopIp;
+ }
+ if (rd != null) {
+ addPrefixToBGP(rd, destination, nextHop, label);
+ } else {
+ // ### add FIB route directly
+ addFibEntryToDS(routerID, destination, nextHop, label);
+ }
+ }
+
+ protected void delExtraRoute(String destination, String rd, String routerID) {
+ if (rd != null) {
+ removePrefixFromBGP(rd, destination);
+ } else {
+ // ### add FIB route directly
+ removeFibEntryFromDS(routerID, destination);
+ }
+ }
+
+ class VpnInterfaceOpListener extends org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener<VpnInterface> {
+
+ public VpnInterfaceOpListener() {
+ super(VpnInterface.class);
+ }
+
+ @Override
+ protected void remove(InstanceIdentifier<VpnInterface> identifier, VpnInterface del) {
+ final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
+ String interfaceName = key.getName();
+
+ //increment the vpn interface count in Vpn Instance Op Data
+ Long ifCnt = 0L;
+ String rd = getRouteDistinguisher(del.getVpnInstanceName());
+ if(rd.isEmpty()) rd = del.getVpnInstanceName();
+ VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(broker, rd);
+ if(vpnInstOp != null && vpnInstOp.getVpnInterfaceCount() != null) {
+ ifCnt = vpnInstOp.getVpnInterfaceCount();
+ }
+
+ LOG.trace("VpnInterfaceOpListener remove: interface name {} rd {} interface count in Vpn Op Instance {}", interfaceName, rd, ifCnt);
+
+ VpnUtil.asyncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
+ VpnUtil.getVpnInstanceOpDataIdentifier(rd),
+ VpnUtil.updateIntfCntInVpnInstOpData(ifCnt - 1, rd), VpnUtil.DEFAULT_CALLBACK);
+
+ //TODO: Clean up the DPN List in Vpn Instance Op if ifCnt is zero
+
+ notifyTaskIfRequired(interfaceName);
+ }
+
+ private void notifyTaskIfRequired(String intfName) {
+ Runnable notifyTask = vpnIntfMap.remove(intfName);
+ if (notifyTask == null) {
+ return;
+ }
+ executorService.execute(notifyTask);
+ }
+
+ @Override
+ protected void update(InstanceIdentifier<VpnInterface> identifier, VpnInterface original, VpnInterface update) {
+ }
+
+ @Override
+ protected void add(InstanceIdentifier<VpnInterface> identifier, VpnInterface add) {
+ final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
+ String interfaceName = key.getName();
+
+ //increment the vpn interface count in Vpn Instance Op Data
+ Long ifCnt = 0L;
+ String rd = getRouteDistinguisher(add.getVpnInstanceName());
+ if(rd.isEmpty()) rd = add.getVpnInstanceName();
+ VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(broker, rd);
+ if(vpnInstOp != null && vpnInstOp.getVpnInterfaceCount() != null) {
+ ifCnt = vpnInstOp.getVpnInterfaceCount();
+ }
+
+ LOG.trace("VpnInterfaceOpListener add: interface name {} rd {} interface count in Vpn Op Instance {}", interfaceName, rd, ifCnt);
+
+ VpnUtil.asyncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
+ VpnUtil.getVpnInstanceOpDataIdentifier(rd),
+ VpnUtil.updateIntfCntInVpnInstOpData(ifCnt + 1, rd), VpnUtil.DEFAULT_CALLBACK);
+
+
+ }
+ }
+
+ protected void updatePrefixesForDPN(BigInteger dpnId, UpdateRouteAction action) {
+
+ InstanceIdentifierBuilder<VpnInstances> idBuilder = InstanceIdentifier.builder(VpnInstances.class);
+ InstanceIdentifier<VpnInstances> vpnInstancesId = idBuilder.build();
+ Optional<VpnInstances> vpnInstances = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, vpnInstancesId);
+
+ if (vpnInstances.isPresent()) {
+ List<VpnInstance> vpnInstanceList = vpnInstances.get().getVpnInstance();
+ Iterator<VpnInstance> vpnInstIter = vpnInstanceList.iterator();
+ while (vpnInstIter.hasNext()) {
+ VpnInstance vpnInstance = vpnInstIter.next();
+ VpnAfConfig vpnConfig = vpnInstance.getIpv4Family();
+ String rd = vpnConfig.getRouteDistinguisher();
+
+ InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
+ Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
+ if (dpnInVpn.isPresent()) {
+ if (action == UpdateRouteAction.ADVERTISE_ROUTE) {
+ fibManager.populateFibOnNewDpn(dpnId, VpnUtil
+ .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
+ }
+ List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
+ .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces = dpnInVpn.get().getVpnInterfaces();
+ for(org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
+ .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces vpnInterface : vpnInterfaces) {
+ InstanceIdentifier<VpnInterface> vpnIntfId = VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getInterfaceName());
+ InstanceIdentifier<Adjacencies> path = vpnIntfId.augmentation(Adjacencies.class);
+ Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
+
+ if (adjacencies.isPresent()) {
+ List<Adjacency> adjacencyList = adjacencies.get().getAdjacency();
+ Iterator<Adjacency> adjacencyIterator = adjacencyList.iterator();
+
+ while (adjacencyIterator.hasNext()) {
+ Adjacency adjacency = adjacencyIterator.next();
+ try {
+ if(action == UpdateRouteAction.ADVERTISE_ROUTE)
+ bgpManager.addPrefix(rd, adjacency.getIpAddress(), adjacency.getNextHopIp(), adjacency.getLabel().intValue());
+ else if(action == UpdateRouteAction.WITHDRAW_ROUTE)
+ bgpManager.deletePrefix(rd, adjacency.getIpAddress());
+ } catch (Exception e) {
+ LOG.error("Exception when updating prefix {} in vrf {} to BGP", adjacency.getIpAddress(), rd);
+ }
+ }
+ }
+
+ }
+ if (action == UpdateRouteAction.WITHDRAW_ROUTE) {
+ fibManager.cleanUpDpnForVpn(dpnId, VpnUtil
+ .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
+ }
+ }
+ }
+ }
+ }
+