Merge "remove unnecessary @Override close() "
[netvirt.git] / vpnservice / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronvpnManager.java
index fcb91005eb8be734e48bba552d7f97b4bd2275ce..9a664dcc670f4b22925c8fa2e62dd1d1c45f00ed 100644 (file)
@@ -21,14 +21,12 @@ import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev14081
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargets;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.VpnTargetsBuilder;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTarget;
-import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets
-        .VpnTargetBuilder;
+import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTargetBuilder;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTargetKey;
 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.vpn.instances.VpnInstanceBuilder;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
-import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.vpn.instance
-        .Ipv4FamilyBuilder;
+import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.vpn.instance.Ipv4FamilyBuilder;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceBuilder;
 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
@@ -39,7 +37,39 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.Adj
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.AdjacenciesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.Adjacency;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.AssociateNetworksInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.AssociateNetworksOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.AssociateNetworksOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.AssociateRouterInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateL3VPNInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateL3VPNOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.CreateL3VPNOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DeleteL3VPNInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DeleteL3VPNOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DeleteL3VPNOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DissociateNetworksInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DissociateNetworksOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DissociateNetworksOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.DissociateRouterInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetFixedIPsForNeutronPortInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetFixedIPsForNeutronPortOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetFixedIPsForNeutronPortOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNOutput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.GetL3VPNOutputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.L3vpnInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NeutronvpnService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterAssociatedToVpn;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterAssociatedToVpnBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterDisassociatedFromVpn;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterDisassociatedFromVpnBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.RouterInterfacesMap;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetAddedToVpnBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetDeletedFromVpnBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.SubnetUpdatedInVpnBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.VpnMaps;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfacesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.RouterInterfacesKey;
@@ -47,6 +77,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev15060
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.InterfacesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.router.interfaces.map.router.interfaces.InterfacesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.adjacency.list.AdjacencyKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.ext.rev150712.NetworkL3Extension;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
@@ -54,17 +85,13 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.por
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.*;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.createl3vpn.input.L3vpn;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.getl3vpn.output.L3vpnInstances;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.getl3vpn.output
-        .L3vpnInstancesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.getl3vpn.output.L3vpnInstancesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.SubnetmapKey;
@@ -88,11 +115,12 @@ import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 
-public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , EventListener{
+public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, EventListener {
 
     private static final Logger logger = LoggerFactory.getLogger(NeutronvpnManager.class);
     private final DataBroker broker;
     private LockManagerService lockManager;
+    private NeutronvpnNatManager nvpnNatManager;
     IMdsalApiManager mdsalUtil;
     private NotificationPublishService notificationPublishService;
     private NotificationService notificationService;
@@ -103,9 +131,10 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
      * @param mdsalManager - MDSAL Util API access
      */
     public NeutronvpnManager(final DataBroker db, IMdsalApiManager mdsalManager,NotificationPublishService notiPublishService,
-                             NotificationService notiService) {
+                             NotificationService notiService, NeutronvpnNatManager vpnNatMgr) {
         broker = db;
         mdsalUtil = mdsalManager;
+        nvpnNatManager = vpnNatMgr;
         notificationPublishService = notiPublishService;
         notificationService = notiService;
     }
@@ -156,7 +185,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
             if (portId != null) {
                 List<Uuid> portList = builder.getPortList();
                 if (portList == null) {
-                    portList = new ArrayList<Uuid>();
+                    portList = new ArrayList<>();
                 }
                 portList.add(portId);
                 builder.setPortList(portList);
@@ -237,7 +266,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
     private void updateVpnInstanceNode(String vpnName, List<String> rd, List<String> irt, List<String> ert) {
 
         VpnInstanceBuilder builder = null;
-        List<VpnTarget> vpnTargetList = new ArrayList<VpnTarget>();
+        List<VpnTarget> vpnTargetList = new ArrayList<>();
         boolean isLockAcquired = false;
         InstanceIdentifier<VpnInstance> vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class).
                 child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
@@ -253,7 +282,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
             }
             if (irt != null && !irt.isEmpty()) {
                 if (ert != null && !ert.isEmpty()) {
-                    List<String> commonRT = new ArrayList<String>(irt);
+                    List<String> commonRT = new ArrayList<>(irt);
                     commonRT.retainAll(ert);
 
                     for (String common : commonRT) {
@@ -343,7 +372,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
             if (networks != null) {
                 List<Uuid> nwList = builder.getNetworkIds();
                 if (nwList == null) {
-                    nwList = new ArrayList<Uuid>();
+                    nwList = new ArrayList<>();
                 }
                 nwList.addAll(networks);
                 builder.setNetworkIds(nwList);
@@ -443,7 +472,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
             return;
         }
         String infName = port.getUuid().getValue();
-        List<Adjacency> adjList = new ArrayList<Adjacency>();
+        List<Adjacency> adjList = new ArrayList<>();
         InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName);
 
         // find router associated to vpn
@@ -460,7 +489,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
             StringBuilder IpPrefixBuild = new StringBuilder(ip.getIpAddress().getIpv4Address().getValue());
             String IpPrefix = IpPrefixBuild.append("/32").toString();
             Adjacency vmAdj = new AdjacencyBuilder().setKey(new AdjacencyKey(IpPrefix)).setIpAddress(IpPrefix)
-                    .setMacAddress(port.getMacAddress()).build();
+                    .setMacAddress(port.getMacAddress().getValue()).build();
             adjList.add(vmAdj);
             // create extra route adjacency
             if (rtr != null && rtr.getRoutes() != null) {
@@ -561,7 +590,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
 
         CreateL3VPNOutputBuilder opBuilder = new CreateL3VPNOutputBuilder();
         SettableFuture<RpcResult<CreateL3VPNOutput>> result = SettableFuture.create();
-        List<RpcError> errorList = new ArrayList<RpcError>();
+        List<RpcError> errorList = new ArrayList<>();
         int failurecount = 0;
         int warningcount = 0;
 
@@ -670,7 +699,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         GetL3VPNOutputBuilder opBuilder = new GetL3VPNOutputBuilder();
         SettableFuture<RpcResult<GetL3VPNOutput>> result = SettableFuture.create();
         Uuid inputVpnId = input.getId();
-        List<VpnInstance> vpns = new ArrayList<VpnInstance>();
+        List<VpnInstance> vpns = new ArrayList<>();
 
         try {
             if (inputVpnId == null) {
@@ -709,7 +738,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
                             .withWarning(ErrorType.PROTOCOL, "invalid-value", message).build());
                 }
             }
-            List<L3vpnInstances> l3vpnList = new ArrayList<L3vpnInstances>();
+            List<L3vpnInstances> l3vpnList = new ArrayList<>();
             for (VpnInstance vpnInstance : vpns) {
                 Uuid vpnId = new Uuid(vpnInstance.getVpnInstanceName());
                 // create VpnMaps id
@@ -720,8 +749,8 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
                 List<String> rd = Arrays.asList(vpnInstance.getIpv4Family().getRouteDistinguisher().split(","));
                 List<VpnTarget> vpnTargetList = vpnInstance.getIpv4Family().getVpnTargets().getVpnTarget();
 
-                List<String> ertList = new ArrayList<String>();
-                List<String> irtList = new ArrayList<String>();
+                List<String> ertList = new ArrayList<>();
+                List<String> irtList = new ArrayList<>();
 
                 for (VpnTarget vpnTarget : vpnTargetList) {
                     if (vpnTarget.getVrfRTType() == VpnTarget.VrfRTType.ExportExtcommunity) {
@@ -763,7 +792,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
 
         DeleteL3VPNOutputBuilder opBuilder = new DeleteL3VPNOutputBuilder();
         SettableFuture<RpcResult<DeleteL3VPNOutput>> result = SettableFuture.create();
-        List<RpcError> errorList = new ArrayList<RpcError>();
+        List<RpcError> errorList = new ArrayList<>();
 
         int failurecount = 0;
         int warningcount = 0;
@@ -821,8 +850,11 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         boolean isLockAcquired = false;
         String lockName = vpnId.getValue() + subnet.getValue();
         String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance>elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
+        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
+                (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
+        Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
+                elanIdentifierId);
+        //TODO: Cache it in add so can reuse it in update and delete. Best would be to cache in some ElanUtils
         long elanTag = elanInstance.get().getElanTag();
         Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
         if (vpnId.equals(routerId)) {
@@ -857,15 +889,18 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         boolean isLockAcquired = false;
         String lockName = vpnId.getValue() + subnet.getValue();
         String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance>elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
+        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
+                (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
+        Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
+                elanIdentifierId);
         long elanTag = elanInstance.get().getElanTag();
         try {
             isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName);
-            checkAndPublishSubnetUpdNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isBeingAssociated, elanTag);
+            checkAndPublishSubnetUpdNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isBeingAssociated,
+                    elanTag);
             logger.debug("Subnet updated in Vpn notification sent");
-        }catch (Exception e){
-            logger.error("Subnet updated in Vpn notification failed",e);
+        } catch (Exception e) {
+            logger.error("Subnet updated in Vpn notification failed", e);
         }finally {
             if (isLockAcquired) {
                 NeutronvpnUtils.unlock(lockManager, lockName);
@@ -881,18 +916,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         }
     }
 
-
-
-        // router-interfaces-map
-//    list router-interfaces {
-//        key router-id;
-//        leaf router-id { type yang:uuid; }
-//        list interfaces {
-//            key interface-id;
-//            leaf interface-id { type yang:uuid; }
-//        }
-//    }
-////}
     InstanceIdentifier<RouterInterfaces> getRouterInterfacesId(Uuid routerId) {
         return InstanceIdentifier.builder(RouterInterfacesMap.class)
                 .child(RouterInterfaces.class, new RouterInterfacesKey(routerId)).build();
@@ -929,7 +952,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
     }
 
     protected List<Adjacency> addAdjacencyforExtraRoute(List<Routes> routeList, boolean rtrUp, String vpnifname) {
-        List<Adjacency> adjList = new ArrayList<Adjacency>();
+        List<Adjacency> adjList = new ArrayList<>();
         for (Routes route : routeList) {
             if (route != null && route.getNexthop() != null && route.getDestination() != null) {
                 boolean isLockAcquired = false;
@@ -941,7 +964,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
                         destination, infName);
                 Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey
                         (new AdjacencyKey(destination)).build();
-                if (rtrUp == false) {
+                if (!rtrUp) {
                     if (infName.equals(vpnifname)) {
                         adjList.add(erAdj);
                     }
@@ -1032,8 +1055,10 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         boolean isLockAcquired = false;
         String lockName = vpnId.getValue() + subnet.getValue();
         String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance>elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId);
+        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child
+                (ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
+        Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
+                elanIdentifierId);
         long elanTag = elanInstance.get().getElanTag();
         Uuid routerId = NeutronvpnUtils.getVpnMap(broker, vpnId).getRouterId();
         if (vpnId.equals(routerId)) {
@@ -1078,9 +1103,17 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
             logger.debug("Updating association of subnets to external vpn {}", vpnId.getValue());
             if (routerSubnets != null) {
                 for (Uuid subnetId : routerSubnets) {
-                    updateVpnForSubnet(vpnId, subnetId,true);
+                    updateVpnForSubnet(vpnId, subnetId, true);
                 }
             }
+            try {
+                checkAndPublishRouterAssociatedtoVpnNotification(routerId, vpnId);
+                logger.debug("notification upon association of router {} to VPN {} published", routerId.getValue(),
+                        vpnId.getValue());
+            } catch (Exception e) {
+                logger.error("publishing of notification upon association of router {} to VPN {} failed : ", routerId
+                        .getValue(), vpnId.getValue(), e);
+            }
         } else {
             logger.debug("Adding subnets to internal vpn {}", vpnId.getValue());
             for (Uuid subnet : routerSubnets) {
@@ -1099,52 +1132,95 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
             }
         }
         clearFromVpnMaps(vpnId, routerId, null);
+        try {
+            checkAndPublishRouterDisassociatedFromVpnNotification(routerId, vpnId);
+            logger.debug("notification upon disassociation of router {} from VPN {} published", routerId.getValue(),
+                    vpnId.getValue());
+        } catch (Exception e) {
+            logger.error("publishing of notification upon disassociation of router {} from VPN {} failed : ", routerId
+                    .getValue(), vpnId.getValue(), e);
+        }
     }
 
     protected List<String> associateNetworksToVpn(Uuid vpn, List<Uuid> networks) {
-        List<String> failed = new ArrayList<String>();
+
+        List<String> failedNwList = new ArrayList<String>();
+        List<Uuid> passedNwList = new ArrayList<>();
         if (!networks.isEmpty()) {
-            // store in Data Base
-            updateVpnMaps(vpn, null, null, null, networks);
             // process corresponding subnets for VPN
             for (Uuid nw : networks) {
-                if (NeutronvpnUtils.getNeutronNetwork(broker, nw) == null) {
-                    failed.add(nw.getValue());
+                Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
+                Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
+                if (network == null) {
+                    failedNwList.add(String.format("network %s not found", nw.getValue()));
+                } else if (vpnId != null) {
+                    failedNwList.add(String.format("network %s already associated to another VPN %s", nw.getValue(),
+                            vpnId.getValue()));
                 } else {
                     List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
-                    logger.debug("Adding network subnets...");
+                    logger.debug("Adding network subnets...{}", networkSubnets);
                     if (networkSubnets != null) {
                         for (Uuid subnet : networkSubnets) {
-                            addSubnetToVpn(vpn, subnet);
+                            // check if subnet added as router interface to some router
+                            Uuid subnetVpnId = NeutronvpnUtils.getVpnForSubnet(broker, subnet);
+                            if (subnetVpnId == null) {
+                                addSubnetToVpn(vpn, subnet);
+                                passedNwList.add(nw);
+                            } else {
+                                failedNwList.add(String.format("subnet %s already added as router interface bound to " +
+                                        "internal/external VPN %s", subnet.getValue (), subnetVpnId.getValue()));
+                            }
                         }
                     }
+                    if (network.getAugmentation(NetworkL3Extension.class) != null && network.getAugmentation
+                            (NetworkL3Extension.class).isExternal()) {
+                        nvpnNatManager.addExternalNetworkToVpn(network, vpn);
+                    }
                 }
             }
+            updateVpnMaps(vpn, null, null, null, passedNwList);
         }
-        return failed;
+        return failedNwList;
     }
 
     protected List<String> dissociateNetworksFromVpn(Uuid vpn, List<Uuid> networks) {
-        List<String> failed = new ArrayList<String>();
+
+        List<String> failedNwList = new ArrayList<String>();
+        List<Uuid> passedNwList = new ArrayList<>();
         if (networks != null && !networks.isEmpty()) {
-            // store in Data Base
-            clearFromVpnMaps(vpn, null, networks);
             // process corresponding subnets for VPN
             for (Uuid nw : networks) {
-                if (NeutronvpnUtils.getNeutronNetwork(broker, nw) == null) {
-                    failed.add(nw.getValue());
+                Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw);
+                if (network == null) {
+                    failedNwList.add(String.format("network %s not found", nw.getValue()));
                 } else {
-                    List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
-                    logger.debug("Removing network subnets...");
-                    if (networkSubnets != null) {
-                        for (Uuid subnet : networkSubnets) {
-                            removeSubnetFromVpn(vpn, subnet);
+                    Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw);
+                    if (vpn.equals(vpnId)) {
+                        List<Uuid> networkSubnets = NeutronvpnUtils.getSubnetIdsFromNetworkId(broker, nw);
+                        logger.debug("Removing network subnets...");
+                        if (networkSubnets != null) {
+                            for (Uuid subnet : networkSubnets) {
+                                removeSubnetFromVpn(vpn, subnet);
+                                passedNwList.add(nw);
+                            }
                         }
+                    } else {
+                        if (vpnId == null) {
+                            failedNwList.add(String.format("input network %s not associated to any vpn yet", nw
+                                    .getValue()));
+                        } else {
+                            failedNwList.add(String.format("input network %s associated to a another vpn %s instead " +
+                                    "of the one given as input", nw.getValue(), vpnId.getValue()));
+                        }
+                    }
+                    if (network.getAugmentation(NetworkL3Extension.class).isExternal()) {
+                        nvpnNatManager.removeExternalNetworkFromVpn(network);
                     }
                 }
             }
+            clearFromVpnMaps(vpn, null, passedNwList);
         }
-        return failed;
+        return failedNwList;
     }
 
     @Override
@@ -1162,7 +1238,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
                 if (netIds != null && !netIds.isEmpty()) {
                     List<String> failed = associateNetworksToVpn(vpnId, netIds);
                     if (!failed.isEmpty()) {
-                        returnMsg.append("network(s) not found : ").append(failed);
+                        returnMsg.append(failed);
                     }
                 }
             } else {
@@ -1203,9 +1279,13 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
                 Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
                 VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, vpnId);
                 if (rtr != null && vpnMap != null) {
+                    Uuid extVpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
                     if (vpnMap.getRouterId() != null) {
                         returnMsg.append("vpn ").append(vpnId.getValue()).append(" already associated to router ")
                                 .append(vpnMap.getRouterId().getValue());
+                    } else if (extVpnId != null) {
+                        returnMsg.append("router ").append(routerId.getValue()).append(" already associated to " +
+                                "another VPN ").append(extVpnId.getValue());
                     } else {
                         associateRouterToVpn(vpnId, routerId);
                     }
@@ -1250,7 +1330,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
                 if (netIds != null && !netIds.isEmpty()) {
                     List<String> failed = dissociateNetworksFromVpn(vpnId, netIds);
                     if (!failed.isEmpty()) {
-                        returnMsg.append("netowrk(s) not found : ").append(failed);
+                        returnMsg.append(failed);
                     }
                 }
             } else {
@@ -1292,7 +1372,19 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
                 if (routerId != null) {
                     Router rtr = NeutronvpnUtils.getNeutronRouter(broker, routerId);
                     if (rtr != null) {
-                        dissociateRouterFromVpn(vpnId, routerId);
+                        Uuid routerVpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true);
+                        if (vpnId.equals(routerVpnId)) {
+                            dissociateRouterFromVpn(vpnId, routerId);
+                        } else {
+                            if (routerVpnId == null) {
+                                returnMsg.append("input router ").append(routerId.getValue()).append(" not associated" +
+                                        " to any vpn yet");
+                            } else {
+                                returnMsg.append("input router ").append(routerId.getValue()).append(" associated to " +
+                                        "vpn ").append(routerVpnId.getValue()).append("instead of the vpn given as " +
+                                        "input");
+                            }
+                        }
                     } else {
                         returnMsg.append("router not found : ").append(routerId.getValue());
                     }
@@ -1383,19 +1475,12 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         deleteVpnInstance(routerId);
     }
 
-    protected Subnet getNeutronSubnet(Uuid subnetId) {
-        InstanceIdentifier<Subnet> inst = InstanceIdentifier.create(Neutron.class).
-                child(Subnets.class).child(Subnet.class, new SubnetKey(subnetId));
-        Optional<Subnet> sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, inst);
-
-        if (sn.isPresent()) {
-            return sn.get();
-        }
-        return null;
+    protected Subnet getNeutronSubnet(Uuid subnetId){
+        return NeutronvpnUtils.getNeutronSubnet(broker, subnetId);
     }
 
     protected IpAddress getNeutronSubnetGateway(Uuid subnetId) {
-        Subnet sn = getNeutronSubnet(subnetId);
+        Subnet sn = NeutronvpnUtils.getNeutronSubnet(broker, subnetId);
         if (null != sn) {
             return sn.getGatewayIp();
         }
@@ -1411,7 +1496,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
     }
 
     protected List<Uuid> getSubnetsforVpn(Uuid vpnid) {
-        List<Uuid> subnets = new ArrayList<Uuid>();
+        List<Uuid> subnets = new ArrayList<>();
         //read subnetmaps
         InstanceIdentifier<Subnetmaps> subnetmapsid = InstanceIdentifier.builder(Subnetmaps.class).build();
         Optional<Subnetmaps> subnetmaps = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
@@ -1428,7 +1513,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
     }
 
     public List<String> showNeutronPortsCLI() {
-        List<String> result = new ArrayList<String>();
+        List<String> result = new ArrayList<>();
         result.add(String.format(" %-34s  %-22s  %-22s  %-6s ", "PortName", "Mac Address", "IP Address",
                 "Prefix Length"));
         result.add("---------------------------------------------------------------------------------------");
@@ -1436,11 +1521,12 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         try {
             Optional<Ports> ports = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, portidentifier);
             if (ports.isPresent() && ports.get().getPort() != null) {
-                List<Port> portList = ports.get().getPort();
-                for (Port port : portList) {
-                    result.add(String.format(" %-34s  %-22s  %-22s  %-6s ", port.getUuid().getValue(), port
-                            .getMacAddress(), port.getFixedIps().get(0).getIpAddress().getIpv4Address().getValue(),
-                            NeutronvpnUtils.getIPPrefixFromPort(broker, port)));
+                for (Port port : ports.get().getPort()) {
+                    if (port.getFixedIps() != null && !port.getFixedIps().isEmpty()) {
+                        result.add(String.format(" %-34s  %-22s  %-22s  %-6s ", port.getUuid().getValue(), port.
+                                getMacAddress(), port.getFixedIps().get(0).getIpAddress().getIpv4Address().getValue(),
+                                NeutronvpnUtils.getIPPrefixFromPort(broker, port)));
+                    }
                 }
             }
         } catch (Exception e) {
@@ -1451,7 +1537,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
     }
 
     public List<String> showVpnConfigCLI(Uuid vpnuuid) {
-        List<String> result = new ArrayList<String>();
+        List<String> result = new ArrayList<>();
         if (vpnuuid == null) {
             System.out.println("");
             System.out.println("Displaying VPN config for all VPNs");
@@ -1523,7 +1609,8 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         return help.toString();
     }
 
-    private void checkAndPublishSubnetAddNotification(Uuid subnetId, String subnetIp, String vpnName, Boolean isExternalvpn, Long elanTag)throws InterruptedException{
+    private void checkAndPublishSubnetAddNotification(Uuid subnetId, String subnetIp, String vpnName,
+                                                      Boolean isExternalvpn, Long elanTag)throws InterruptedException {
         SubnetAddedToVpnBuilder builder = new SubnetAddedToVpnBuilder();
 
         logger.info("publish notification called");
@@ -1537,7 +1624,8 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         notificationPublishService.putNotification(builder.build());
     }
 
-    private void checkAndPublishSubnetDelNotification(Uuid subnetId, String subnetIp, String vpnName, Boolean isExternalvpn, Long elanTag)throws InterruptedException{
+    private void checkAndPublishSubnetDelNotification(Uuid subnetId, String subnetIp, String vpnName,
+                                                      Boolean isExternalvpn, Long elanTag) throws InterruptedException {
         SubnetDeletedFromVpnBuilder builder = new SubnetDeletedFromVpnBuilder();
 
         logger.info("publish notification called");
@@ -1551,7 +1639,8 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         notificationPublishService.putNotification(builder.build());
     }
 
-    private void checkAndPublishSubnetUpdNotification(Uuid subnetId, String subnetIp, String vpnName, Boolean isExternalvpn, Long elanTag)throws InterruptedException{
+    private void checkAndPublishSubnetUpdNotification(Uuid subnetId, String subnetIp, String vpnName,
+                                                      Boolean isExternalvpn, Long elanTag) throws InterruptedException {
         SubnetUpdatedInVpnBuilder builder = new SubnetUpdatedInVpnBuilder();
 
         logger.info("publish notification called");
@@ -1565,4 +1654,19 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , Eve
         notificationPublishService.putNotification(builder.build());
     }
 
+    private void checkAndPublishRouterAssociatedtoVpnNotification(Uuid routerId, Uuid vpnId) throws
+            InterruptedException {
+        RouterAssociatedToVpn routerAssociatedToVpn = new RouterAssociatedToVpnBuilder().setRouterId(routerId)
+                .setVpnId(vpnId).build();
+        logger.info("publishing notification upon association of router to VPN");
+        notificationPublishService.putNotification(routerAssociatedToVpn);
+    }
+
+    private void checkAndPublishRouterDisassociatedFromVpnNotification(Uuid routerId, Uuid vpnId) throws
+            InterruptedException {
+        RouterDisassociatedFromVpn routerDisassociatedFromVpn = new RouterDisassociatedFromVpnBuilder().setRouterId
+                (routerId).setVpnId(vpnId).build();
+        logger.info("publishing notification upon disassociation of router from VPN");
+        notificationPublishService.putNotification(routerDisassociatedFromVpn);
+    }
 }