Convert vpnmanager-impl to use blueprint annotations
[netvirt.git] / vpnservice / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / netvirt / vpnmanager / VpnFootprintService.java
index 85b5f741ea0e1bb7fca87fd8753f842dd10ac8f1..0689809b2e885c57aaf80231be02ef042adb1201 100644 (file)
@@ -9,25 +9,23 @@
 package org.opendaylight.netvirt.vpnmanager;
 
 import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
-
+import javax.inject.Inject;
+import javax.inject.Singleton;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.netvirt.fibmanager.api.IFibManager;
 import org.opendaylight.netvirt.vpnmanager.api.IVpnFootprintService;
-import org.opendaylight.netvirt.vpnmanager.utilities.InterfaceUtils;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rpcs.rev160406.OdlInterfaceRpcService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AddDpnEvent;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AddDpnEventBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AddInterfaceToDpnOnVpnEvent;
@@ -57,6 +55,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Singleton
 public class VpnFootprintService implements IVpnFootprintService {
 
     private static final Logger LOG = LoggerFactory.getLogger(VpnFootprintService.class);
@@ -64,36 +63,31 @@ public class VpnFootprintService implements IVpnFootprintService {
     private final DataBroker dataBroker;
     private final IFibManager fibManager;
     private final VpnOpDataSyncer vpnOpDataSyncer;
-    private final OdlInterfaceRpcService ifaceMgrRpcService;
     private final NotificationPublishService notificationPublishService;
 
+    @Inject
     public VpnFootprintService(final DataBroker dataBroker, final IFibManager fibManager,
-        final OdlInterfaceRpcService ifaceRpcService, final NotificationPublishService notificationPublishService,
-        final VpnOpDataSyncer vpnOpDataSyncer) {
+            final NotificationPublishService notificationPublishService, final VpnOpDataSyncer vpnOpDataSyncer) {
         this.dataBroker = dataBroker;
         this.fibManager = fibManager;
         this.vpnOpDataSyncer = vpnOpDataSyncer;
-        this.ifaceMgrRpcService = ifaceRpcService;
         this.notificationPublishService = notificationPublishService;
     }
 
     @Override
     public void updateVpnToDpnMapping(BigInteger dpId, String vpnName, String primaryRd, String interfaceName,
-            ImmutablePair<IpAddresses.IpAddressSource, String> ipAddressSourceValuePair,
-                                      boolean add) {
+            ImmutablePair<IpAddresses.IpAddressSource, String> ipAddressSourceValuePair, boolean add) {
         long vpnId = VpnUtil.getVpnId(dataBroker, vpnName);
-        if (dpId == null) {
-            dpId = InterfaceUtils.getDpnForInterface(ifaceMgrRpcService, interfaceName);
-        }
         if (!dpId.equals(BigInteger.ZERO)) {
             if (add) {
-                // Considering the possibility of VpnInstanceOpData not being ready yet cause the VPN is
+                // Considering the possibility of VpnInstanceOpData not being ready yet cause
+                // the VPN is
                 // still in its creation process
                 if (vpnId == VpnConstants.INVALID_ID) {
                     LOG.error("updateVpnToDpnMapping: Operational data  for vpn not ready. Waiting to update vpn"
                             + " footprint for vpn {} on dpn {} interface {}", vpnName, dpId, interfaceName);
                     vpnOpDataSyncer.waitForVpnDataReady(VpnOpDataSyncer.VpnOpDataType.vpnInstanceToId, vpnName,
-                        VpnConstants.PER_VPN_INSTANCE_OPDATA_MAX_WAIT_TIME_IN_MILLISECONDS);
+                            VpnConstants.PER_VPN_INSTANCE_OPDATA_MAX_WAIT_TIME_IN_MILLISECONDS);
                     vpnId = VpnUtil.getVpnId(dataBroker, vpnName);
                 }
                 if (interfaceName != null) {
@@ -114,7 +108,7 @@ public class VpnFootprintService implements IVpnFootprintService {
     }
 
     private void createOrUpdateVpnToDpnListForInterfaceName(long vpnId, String primaryRd, BigInteger dpnId,
-                                                            String intfName, String vpnName) {
+            String intfName, String vpnName) {
         Boolean newDpnOnVpn = Boolean.FALSE;
 
         synchronized (vpnName.intern()) {
@@ -135,7 +129,8 @@ public class VpnFootprintService implements IVpnFootprintService {
 
                 writeTxn.put(LogicalDatastoreType.OPERATIONAL, id, vpnToDpnListBuilder.build(),
                         WriteTransaction.CREATE_MISSING_PARENTS);
-                /* If earlier state was inactive, it is considered new DPN coming back to the
+                /*
+                 * If earlier state was inactive, it is considered new DPN coming back to the
                  * same VPN
                  */
                 if (vpnToDpnList.getDpnState() == VpnToDpnList.DpnState.Inactive) {
@@ -155,9 +150,8 @@ public class VpnFootprintService implements IVpnFootprintService {
                 LOG.debug("createOrUpdateVpnToDpnList: Creating vpn footprint for vpn {} vpnId {} interface {}"
                         + " on dpn {}", vpnName, vpnId, intfName, dpnId);
             }
-            CheckedFuture<Void, TransactionCommitFailedException> futures = writeTxn.submit();
             try {
-                futures.get();
+                writeTxn.submit().get();
             } catch (InterruptedException | ExecutionException e) {
                 LOG.error("createOrUpdateVpnToDpnList: Error adding to dpnToVpnList for vpn {} vpnId {} interface {}"
                         + " dpn {}", vpnName, vpnId, intfName, dpnId, e);
@@ -167,11 +161,11 @@ public class VpnFootprintService implements IVpnFootprintService {
         LOG.info("createOrUpdateVpnToDpnList: Created/Updated vpn footprint for vpn {} vpnId {} interfacName{}"
                 + " on dpn {}", vpnName, vpnId, intfName, dpnId);
         /*
-         * Informing the Fib only after writeTxn is submitted successfuly.
+         * Informing the FIB only after writeTxn is submitted successfully.
          */
         if (newDpnOnVpn) {
-            fibManager.populateFibOnNewDpn(dpnId, vpnId, primaryRd, new DpnEnterExitVpnWorker(dpnId, vpnName, primaryRd,
-                true /* entered */));
+            fibManager.populateFibOnNewDpn(dpnId, vpnId, primaryRd,
+                    new DpnEnterExitVpnWorker(dpnId, vpnName, primaryRd, true /* entered */));
             LOG.info("createOrUpdateVpnToDpnList: Sent populateFib event for new dpn {} in VPN {} for interface {}",
                     dpnId, vpnName, intfName);
         }
@@ -201,7 +195,8 @@ public class VpnFootprintService implements IVpnFootprintService {
                 vpnToDpnListBuilder.setDpnState(VpnToDpnList.DpnState.Active).setIpAddresses(ipAddresses);
 
                 writeTxn.put(LogicalDatastoreType.OPERATIONAL, id, vpnToDpnListBuilder.build(), true);
-                /* If earlier state was inactive, it is considered new DPN coming back to the
+                /*
+                 * If earlier state was inactive, it is considered new DPN coming back to the
                  * same VPN
                  */
                 if (vpnToDpnList.getDpnState() == VpnToDpnList.DpnState.Inactive) {
@@ -216,9 +211,8 @@ public class VpnFootprintService implements IVpnFootprintService {
                 writeTxn.put(LogicalDatastoreType.OPERATIONAL, id, vpnToDpnListBuilder.build(), true);
                 newDpnOnVpn = Boolean.TRUE;
             }
-            CheckedFuture<Void, TransactionCommitFailedException> futures = writeTxn.submit();
             try {
-                futures.get();
+                writeTxn.submit().get();
             } catch (InterruptedException | ExecutionException e) {
                 LOG.error("Error adding to dpnToVpnList for vpn {} ipAddresses {} dpn {}", vpnName,
                         ipAddressSourceValuePair.getValue(), dpnId, e);
@@ -230,26 +224,26 @@ public class VpnFootprintService implements IVpnFootprintService {
          */
         if (newDpnOnVpn) {
             LOG.debug("Sending populateFib event for new dpn {} in VPN {}", dpnId, vpnName);
-            fibManager.populateFibOnNewDpn(dpnId, vpnId, primaryRd, new DpnEnterExitVpnWorker(dpnId, vpnName, primaryRd,
-                    true /* entered */));
+            fibManager.populateFibOnNewDpn(dpnId, vpnId, primaryRd,
+                    new DpnEnterExitVpnWorker(dpnId, vpnName, primaryRd, true /* entered */));
         }
     }
 
     private void removeOrUpdateVpnToDpnListForInterfaceName(long vpnId, String rd, BigInteger dpnId, String intfName,
-                                                            String vpnName) {
+            String vpnName) {
         Boolean lastDpnOnVpn = Boolean.FALSE;
         synchronized (vpnName.intern()) {
             InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
             VpnToDpnList dpnInVpn = VpnUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, id).orNull();
             if (dpnInVpn == null) {
                 LOG.error("removeOrUpdateVpnToDpnList: Could not find DpnToVpn map for VPN=[name={} rd={} id={}]"
-                    + " and dpnId={}", vpnName, rd, id, dpnId);
+                        + " and dpnId={}", vpnName, rd, id, dpnId);
                 return;
             }
             List<VpnInterfaces> vpnInterfaces = dpnInVpn.getVpnInterfaces();
             if (vpnInterfaces == null) {
                 LOG.error("Could not find vpnInterfaces for DpnInVpn map for VPN=[name={} rd={} id={}] and dpnId={}",
-                         vpnName, rd, id, dpnId);
+                        vpnName, rd, id, dpnId);
                 return;
             }
 
@@ -272,14 +266,13 @@ public class VpnFootprintService implements IVpnFootprintService {
                             WriteTransaction.CREATE_MISSING_PARENTS);
 
                 } else {
-                    writeTxn.delete(LogicalDatastoreType.OPERATIONAL, id.child(VpnInterfaces.class,
-                                                                               new VpnInterfacesKey(intfName)));
+                    writeTxn.delete(LogicalDatastoreType.OPERATIONAL,
+                            id.child(VpnInterfaces.class, new VpnInterfacesKey(intfName)));
                     LOG.debug("removeOrUpdateVpnToDpnList: Updating vpn footprint for vpn {} vpnId {} interface {},"
                             + " on dpn {}", vpnName, vpnName, intfName, dpnId);
                 }
-                CheckedFuture<Void, TransactionCommitFailedException> futures = writeTxn.submit();
                 try {
-                    futures.get();
+                    writeTxn.submit().get();
                 } catch (InterruptedException | ExecutionException e) {
                     LOG.error("removeOrUpdateVpnToDpnList: Error removing from dpnToVpnList for vpn {} vpnId {}"
                             + " interface {} dpn {}", vpnName, vpnId, intfName, dpnId, e);
@@ -291,10 +284,10 @@ public class VpnFootprintService implements IVpnFootprintService {
                 + " on dpn {}", vpnName, vpnName, intfName, dpnId);
 
         if (lastDpnOnVpn) {
-            fibManager.cleanUpDpnForVpn(dpnId, vpnId, rd, new DpnEnterExitVpnWorker(dpnId, vpnName, rd,
-                false /* exited */));
-            LOG.info("removeOrUpdateVpnToDpnList: Sent cleanup event for dpn {} in VPN {} vpnId {} interface {}",
-                    dpnId, vpnName, vpnId, intfName);
+            fibManager.cleanUpDpnForVpn(dpnId, vpnId, rd,
+                    new DpnEnterExitVpnWorker(dpnId, vpnName, rd, false /* exited */));
+            LOG.info("removeOrUpdateVpnToDpnList: Sent cleanup event for dpn {} in VPN {} vpnId {} interface {}", dpnId,
+                    vpnName, vpnId, intfName);
         }
     }
 
@@ -311,15 +304,14 @@ public class VpnFootprintService implements IVpnFootprintService {
             }
             List<IpAddresses> ipAddresses = dpnInVpn.getIpAddresses();
             if (ipAddresses == null) {
-                LOG.error("Could not find ipAddresses for DpnInVpn map for VPN=[name={} rd={} id={}] and dpnId={}",
+                LOG.info("Could not find ipAddresses for DpnInVpn map for VPN=[name={} rd={} id={}] and dpnId={}",
                         vpnName, rd, id, dpnId);
                 return;
             }
 
             IpAddresses currIpAddress = new IpAddressesBuilder()
                     .setKey(new IpAddressesKey(ipAddressSourceValuePair.getValue()))
-                    .setIpAddressSource(ipAddressSourceValuePair.getKey())
-                    .build();
+                    .setIpAddressSource(ipAddressSourceValuePair.getKey()).build();
             if (ipAddresses.remove(currIpAddress)) {
                 WriteTransaction writeTxn = dataBroker.newWriteOnlyTransaction();
                 if (ipAddresses.isEmpty()) {
@@ -335,15 +327,14 @@ public class VpnFootprintService implements IVpnFootprintService {
                     writeTxn.put(LogicalDatastoreType.OPERATIONAL, id, dpnInVpnBuilder.build(), true);
 
                 } else {
-                    writeTxn.delete(LogicalDatastoreType.OPERATIONAL, id.child(IpAddresses.class,
-                            new IpAddressesKey(ipAddressSourceValuePair.getValue())));
+                    writeTxn.delete(LogicalDatastoreType.OPERATIONAL,
+                            id.child(IpAddresses.class, new IpAddressesKey(ipAddressSourceValuePair.getValue())));
                 }
-                CheckedFuture<Void, TransactionCommitFailedException> futures = writeTxn.submit();
                 try {
-                    futures.get();
+                    writeTxn.submit().get();
                 } catch (InterruptedException | ExecutionException e) {
-                    LOG.error("Error removing from dpnToVpnList for vpn {} Ipaddress {} dpn {}",
-                            vpnName, ipAddressSourceValuePair.getValue(), dpnId, e);
+                    LOG.error("Error removing from dpnToVpnList for vpn {} Ipaddress {} dpn {}", vpnName,
+                            ipAddressSourceValuePair.getValue(), dpnId, e);
                     throw new RuntimeException(e.getMessage());
                 }
             }
@@ -351,8 +342,8 @@ public class VpnFootprintService implements IVpnFootprintService {
 
         if (lastDpnOnVpn) {
             LOG.debug("Sending cleanup event for dpn {} in VPN {}", dpnId, vpnName);
-            fibManager.cleanUpDpnForVpn(dpnId, vpnId, rd, new DpnEnterExitVpnWorker(dpnId, vpnName, rd,
-                    false /* exited */));
+            fibManager.cleanUpDpnForVpn(dpnId, vpnId, rd,
+                    new DpnEnterExitVpnWorker(dpnId, vpnName, rd, false /* exited */));
         }
     }
 
@@ -374,7 +365,7 @@ public class VpnFootprintService implements IVpnFootprintService {
                 LOG.info("publishAddNotification: Successful in notifying listeners for add dpn {} in vpn {} rd {}"
                         + " event ", dpnId, vpnName, rd);
             }
-        });
+        }, MoreExecutors.directExecutor());
     }
 
     private void publishRemoveNotification(final BigInteger dpnId, final String vpnName, final String rd) {
@@ -395,17 +386,17 @@ public class VpnFootprintService implements IVpnFootprintService {
                 LOG.info("publishRemoveNotification: Successful in notifying listeners for remove dpn {} in vpn {}"
                         + " rd {} event ", dpnId, vpnName, rd);
             }
-        });
+        }, MoreExecutors.directExecutor());
     }
 
     private void publishInterfaceAddedToVpnNotification(String interfaceName, BigInteger dpnId, String vpnName,
-                                                        Long vpnId) {
+            Long vpnId) {
         LOG.debug("publishInterfaceAddedToVpnNotification: Sending notification for addition of interface {} on dpn {}"
                 + " for vpn {}", interfaceName, dpnId, vpnName);
-        AddInterfaceEventData data = new AddInterfaceEventDataBuilder().setInterfaceName(interfaceName)
-                .setVpnId(vpnId).setDpnId(dpnId).build();
-        AddInterfaceToDpnOnVpnEvent event = new AddInterfaceToDpnOnVpnEventBuilder()
-                .setAddInterfaceEventData(data).build();
+        AddInterfaceEventData data = new AddInterfaceEventDataBuilder().setInterfaceName(interfaceName).setVpnId(vpnId)
+                .setDpnId(dpnId).build();
+        AddInterfaceToDpnOnVpnEvent event = new AddInterfaceToDpnOnVpnEventBuilder().setAddInterfaceEventData(data)
+                .build();
         final ListenableFuture<?> eventFuture = notificationPublishService.offerNotification(event);
         Futures.addCallback(eventFuture, new FutureCallback<Object>() {
             @Override
@@ -419,11 +410,11 @@ public class VpnFootprintService implements IVpnFootprintService {
                 LOG.trace("publishInterfaceAddedToVpnNotification: Successful in notifying listeners for add"
                         + " interface {} on dpn {} in vpn {} event ", interfaceName, dpnId, vpnName);
             }
-        });
+        }, MoreExecutors.directExecutor());
     }
 
     private void publishInterfaceRemovedFromVpnNotification(String interfaceName, BigInteger dpnId, String vpnName,
-                                                            Long vpnId) {
+            Long vpnId) {
         LOG.debug("publishInterfaceAddedToVpnNotification: Sending notification for removal of interface {}"
                 + " from dpn {} for vpn {}", interfaceName, dpnId, vpnName);
         RemoveInterfaceEventData data = new RemoveInterfaceEventDataBuilder().setInterfaceName(interfaceName)
@@ -434,9 +425,10 @@ public class VpnFootprintService implements IVpnFootprintService {
         Futures.addCallback(eventFuture, new FutureCallback<Object>() {
             @Override
             public void onFailure(Throwable error) {
-                LOG.warn("publishInterfaceAddedToVpnNotification: Error in notifying listeners"
-                        + " for removing interface {} from dpn {} in vpn {} event ", interfaceName, dpnId, vpnName,
-                        error);
+                LOG.warn(
+                        "publishInterfaceAddedToVpnNotification: Error in notifying listeners"
+                                + " for removing interface {} from dpn {} in vpn {} event ",
+                        interfaceName, dpnId, vpnName, error);
             }
 
             @Override
@@ -444,13 +436,12 @@ public class VpnFootprintService implements IVpnFootprintService {
                 LOG.trace("publishInterfaceAddedToVpnNotification: Successful in notifying listeners for removing"
                         + " interface {} from dpn {} in vpn {} event ", interfaceName, dpnId, vpnName);
             }
-        });
+        }, MoreExecutors.directExecutor());
     }
 
-
     /**
-     * JobCallback class is used as a future callback for
-     * main and rollback workers to handle success and failure.
+     * JobCallback class is used as a future callback for main and rollback workers
+     * to handle success and failure.
      */
     private class DpnEnterExitVpnWorker implements FutureCallback<List<Void>> {
         private final Logger log = LoggerFactory.getLogger(DpnEnterExitVpnWorker.class);
@@ -467,31 +458,30 @@ public class VpnFootprintService implements IVpnFootprintService {
         }
 
         /**
-         * This implies that all the future instances have returned success. -- TODO: Confirm this
+         * This implies that all the future instances have returned success. -- TODO:
+         * Confirm this
          */
         @Override
         public void onSuccess(List<Void> voids) {
             if (entered) {
                 publishAddNotification(dpnId, vpnName, rd);
-                log.info("onSuccess: FootPrint established for vpn {} rd {} on dpn {}", vpnName,
-                        rd, dpnId);
+                log.info("onSuccess: FootPrint established for vpn {} rd {} on dpn {}", vpnName, rd, dpnId);
             } else {
                 publishRemoveNotification(dpnId, vpnName, rd);
-                log.info("onSuccess: FootPrint cleared for vpn {} rd {} on dpn {}", vpnName,
-                        rd, dpnId);
+                log.info("onSuccess: FootPrint cleared for vpn {} rd {} on dpn {}", vpnName, rd, dpnId);
             }
         }
 
         /**
-         * This method is used to handle failure callbacks.
-         * If more retry needed, the retrycount is decremented and mainworker is executed again.
-         * After retries completed, rollbackworker is executed.
-         * If rollbackworker fails, this is a double-fault. Double fault is logged and ignored.
+         * This method is used to handle failure callbacks. If more retry needed, the
+         * retrycount is decremented and mainworker is executed again. After retries
+         * completed, rollbackworker is executed. If rollbackworker fails, this is a
+         * double-fault. Double fault is logged and ignored.
          */
         @Override
         public void onFailure(Throwable throwable) {
-            log.error("onFailure: Failed to establish/clear footprint for vpn {} rd {} on dpn {} ",
-                    vpnName, rd, dpnId, throwable);
+            log.info("onFailure: Failed to establish/clear footprint for vpn {} rd {} on dpn {} ", vpnName, rd, dpnId,
+                    throwable);
         }
     }