JUnits for Interface Manager
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / VpnManager.java
index ee0c56eaefaccb00bb449e3136dc43ef4f57f3e6..8e44ce2bbccd4b4298fa61a47d171893df7921e1 100644 (file)
@@ -26,9 +26,11 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnAfConfig;
+import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceBuilder;
+import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstance1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstance1Builder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
@@ -52,6 +54,7 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
     private final DataBroker broker;
     private final IBgpManager bgpManager;
     private IdManagerService idManager;
+    private VpnInterfaceManager vpnInterfaceManager;
     private final FibEntriesListener fibListener;
 
     private static final FutureCallback<Void> DEFAULT_CALLBACK =
@@ -83,10 +86,10 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
         try {
             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
                     getWildCardPath(), VpnManager.this, DataChangeScope.SUBTREE);
-            fibListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
+            fibListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
                     getFibEntryListenerPath(), fibListener, DataChangeScope.BASE);
         } catch (final Exception e) {
-            LOG.error("VPN Service DataChange listener registration fail!", e);
+            LOG.error("VPN Service DataChange listener registration fail !", e);
             throw new IllegalStateException("VPN Service registration Listener failed.", e);
         }
     }
@@ -95,11 +98,30 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
         this.idManager = idManager;
     }
 
+    public void setVpnInterfaceManager(VpnInterfaceManager vpnInterfaceManager) {
+        this.vpnInterfaceManager = vpnInterfaceManager;
+    }
+
     @Override
     protected void remove(InstanceIdentifier<VpnInstance> identifier, VpnInstance del) {
-        LOG.info("Remove event - Key: {}, value: {}", identifier, del);
+        LOG.trace("Remove VPN event - Key: {}, value: {}", identifier, del);
         String vpnName = del.getVpnInstanceName();
         InstanceIdentifier<VpnInstance> vpnIdentifier = VpnUtil.getVpnInstanceIdentifier(vpnName);
+        //Clean up vpn Interface
+        InstanceIdentifier<VpnInterfaces> vpnInterfacesId = InstanceIdentifier.builder(VpnInterfaces.class).build();
+        Optional<VpnInterfaces> optionalVpnInterfaces = read(LogicalDatastoreType.OPERATIONAL, vpnInterfacesId);
+
+        if(optionalVpnInterfaces.isPresent()) {
+            List<VpnInterface> vpnInterfaces = optionalVpnInterfaces.get().getVpnInterface();
+            for(VpnInterface vpnInterface : vpnInterfaces) {
+                if(vpnInterface.getVpnInstanceName().equals(vpnName)) {
+                    LOG.debug("VpnInterface {} will be removed from VPN {}", vpnInterface.getName(), vpnName);
+                    vpnInterfaceManager.remove(
+                            VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface);
+                }
+            }
+        }
+
         delete(LogicalDatastoreType.OPERATIONAL, vpnIdentifier);
 
         String rd = del.getIpv4Family().getRouteDistinguisher();
@@ -113,13 +135,13 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
     @Override
     protected void update(InstanceIdentifier<VpnInstance> identifier,
             VpnInstance original, VpnInstance update) {
-        LOG.info("Update event - Key: {}, value: {}", identifier, update);
+        LOG.trace("Update event - Key: {}, value: {}", identifier, update);
     }
 
     @Override
     protected void add(InstanceIdentifier<VpnInstance> identifier,
             VpnInstance value) {
-        LOG.info("key: {}, value: {}" +identifier, value);
+        LOG.trace("key: {}, value: {}", identifier, value);
 
         long vpnId = getUniqueId(value.getVpnInstanceName());
 
@@ -219,7 +241,7 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
             } else {
                 LOG.warn("RPC Call to Get Unique Id returned with Errors {}", rpcResult.getErrors());
             }
-        } catch (NullPointerException | InterruptedException | ExecutionException e) {
+        } catch (InterruptedException | ExecutionException e) {
             LOG.warn("Exception when getting Unique Id",e);
         }
         return 0;
@@ -240,7 +262,7 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
         @Override
         protected void remove(InstanceIdentifier<VrfEntry> identifier,
                 VrfEntry del) {
-            LOG.info("Remove Fib event - Key : {}, value : {} ",identifier, del);
+            LOG.trace("Remove Fib event - Key : {}, value : {} ", identifier, del);
             final VrfTablesKey key = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
             String rd = key.getRouteDistinguisher();
             Long label = del.getLabel();
@@ -256,12 +278,12 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
                         LOG.debug("Fib Route entry is empty.");
                         return;
                     }
-                    LOG.info("Removing label from vpn info - {}", label);
+                    LOG.debug("Removing label from vpn info - {}", label);
                     routeIds.remove(label);
                     asyncWrite(LogicalDatastoreType.OPERATIONAL, augId,
                             new VpnInstance1Builder(vpnAug).setRouteEntryId(routeIds).build(), DEFAULT_CALLBACK);
                 } else {
-                    LOG.info("VPN Augmentation not found");
+                    LOG.warn("VPN Augmentation not found for vpn instance {}", vpn.getVpnInstanceName());
                 }
             } else {
                 LOG.warn("No VPN Instance found for RD: {}", rd);
@@ -278,7 +300,7 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
         @Override
         protected void add(InstanceIdentifier<VrfEntry> identifier,
                 VrfEntry add) {
-            LOG.info("Add Vrf Entry event - Key : {}, value : {}",identifier, add);
+            LOG.trace("Add Vrf Entry event - Key : {}, value : {}", identifier, add);
             final VrfTablesKey key = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
             String rd = key.getRouteDistinguisher();
             Long label = add.getLabel();
@@ -293,12 +315,12 @@ public class VpnManager extends AbstractDataChangeListener<VpnInstance> implemen
                     if(routeIds == null) {
                         routeIds = new ArrayList<>();
                     }
-                    LOG.info("Adding label to vpn info - {}", label);
+                    LOG.debug("Adding label to vpn info - {}", label);
                     routeIds.add(label);
                     asyncWrite(LogicalDatastoreType.OPERATIONAL, augId,
                             new VpnInstance1Builder(vpnAug).setRouteEntryId(routeIds).build(), DEFAULT_CALLBACK);
                 } else {
-                    LOG.info("VPN Augmentation not found");
+                    LOG.warn("VPN Augmentation not found for vpn instance {}", vpn.getVpnInstanceName());
                 }
             } else {
                 LOG.warn("No VPN Instance found for RD: {}", rd);