subnet-op-data and port-op-data is empty after cluster reboot
[netvirt.git] / vpnservice / neutronvpn / neutronvpn-impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronvpnManager.java
index f94e410f131f88c5c00df78946a940af08ccfdd4..c9433753c625d955a5b570d758b7a8e806039069 100644 (file)
@@ -44,9 +44,6 @@ 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.interfaces.VpnInterfaceKey;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
-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.l3vpn.rev130911.Adjacencies;
 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;
@@ -81,9 +78,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev15060
 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.createl3vpn.input.L3vpn;
@@ -194,21 +188,11 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
                     builder = new SubnetmapBuilder().setKey(new SubnetmapKey(subnetId)).setId(subnetId);
                     LOG.debug("WithRouterFixedIPs: creating new subnetmap node for subnet ID {}", subnetId.getValue());
                 }
-                if (routerId != null) {
-                    builder.setRouterId(routerId);
-                } else {
-                    builder.setRouterId(null);
-                }
-                if (routerInterfaceName != null) {
-                    builder.setRouterInterfaceName(routerInterfaceName);
-                } else {
-                    builder.setRouterInterfaceName(null);
-                }
-                if (routerIntfMacAddress != null) {
-                    builder.setRouterIntfMacAddress(routerIntfMacAddress);
-                } else {
-                    builder.setRouterIntfMacAddress(null);
-                }
+
+                builder.setRouterId(routerId);
+                builder.setRouterInterfaceName(routerInterfaceName);
+                builder.setRouterIntfMacAddress(routerIntfMacAddress);
+
                 if (fixedIp != null) {
                     List<String> fixedIps = builder.getRouterInterfaceFixedIps();
                     if (fixedIps == null) {
@@ -682,7 +666,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         }
     }
 
-    protected void updateVpnInterface(Uuid vpnId, Uuid oldVpnId, Port port, boolean  isBeingAssociated) {
+    protected void updateVpnInterface(Uuid vpnId, Uuid oldVpnId, Port port, boolean  isBeingAssociated, boolean isSubnetIp) {
         if (vpnId == null || port == null) {
             return;
         }
@@ -721,9 +705,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
                     Adjacencies adjacencies = new AdjacenciesBuilder().setAdjacency(adjacencyList).build();
                     vpnIfBuilder.addAugmentation(Adjacencies.class, adjacencies);
                 }
-                MDSALUtil.syncUpdate(dataBroker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIfBuilder
-                        .build());
-
                 List<FixedIps> ips = port.getFixedIps();
                 for (FixedIps ip : ips) {
                     String ipValue = String.valueOf(ip.getIpAddress().getValue());
@@ -731,8 +712,10 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
                         NeutronvpnUtils.removeVpnPortFixedIpToPort(dataBroker, oldVpnId.getValue(), ipValue);
                     }
                     NeutronvpnUtils.createVpnPortFixedIpToPort(dataBroker, vpnId.getValue(), ipValue, infName, port
-                            .getMacAddress().getValue(), false, true, false);
+                            .getMacAddress().getValue(), isSubnetIp, true, false);
                 }
+                MDSALUtil.syncUpdate(dataBroker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIfBuilder
+                        .build());
             } else {
                 LOG.error("VPN Interface {} not found", infName);
             }
@@ -827,6 +810,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
 
         List<L3vpn> vpns = input.getL3vpn();
         for (L3vpn vpn : vpns) {
+            List<String> existingRDs = NeutronvpnUtils.getExistingRDs(dataBroker);
             RpcError error = null;
             String msg;
             if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) {
@@ -847,6 +831,15 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
                 warningcount++;
                 continue;
             }
+            if (existingRDs.contains(vpn.getRouteDistinguisher().get(0))) {
+                msg = String.format("Creation of L3VPN failed for VPN %s as another VPN with the same RD %s is already configured",
+                        vpn.getId().getValue(), vpn.getRouteDistinguisher().get(0));
+                LOG.warn(msg);
+                error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg);
+                errorList.add(error);
+                warningcount++;
+                continue;
+            }
             if (vpn.getRouterId() != null) {
                 if (NeutronvpnUtils.getNeutronRouter(dataBroker, vpn.getRouterId()) == null) {
                     msg = String.format("Creation of L3VPN failed for VPN %s due to router not found %s",
@@ -1095,28 +1088,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         LOG.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue());
         Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId);
         final Uuid routerId = NeutronvpnUtils.getVpnMap(dataBroker, vpnId).getRouterId();
-        // send subnet added to vpn notification
-        isExternalVpn = vpnId.equals(routerId) ? false : true;
-        String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
-                .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        try {
-            Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
-                    .CONFIGURATION, elanIdentifierId);
-            if (elanInstance.isPresent()) {
-                long elanTag = elanInstance.get().getElanTag();
-                checkAndPublishSubnetAddNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn,
-                        elanTag);
-                LOG.debug("Subnet added to VPN notification sent for subnet {} on VPN {}", subnet.getValue(),
-                        vpnId.getValue());
-            } else {
-                LOG.error("Subnet added to VPN notification failed for subnet {}  on VPN {} because of failure in " +
-                        "reading ELANInstance {}", subnet.getValue(), vpnId.getValue(), elanInstanceName);
-            }
-        } catch (Exception e) {
-            LOG.error("Subnet added to VPN notification failed for subnet {} on VPN {}", subnet.getValue(), vpnId
-                    .getValue(), e);
-        }
         // Check if there are ports on this subnet and add corresponding
         // vpn-interfaces
         List<Uuid> portList = sn.getPortList();
@@ -1153,43 +1124,21 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
             oldVpnId = sn.getVpnId();
             List<String> ips = sn.getRouterInterfaceFixedIps();
             for (String ipValue : ips) {
-                if (oldVpnId != null) {
-                    NeutronvpnUtils.removeVpnPortFixedIpToPort(dataBroker, oldVpnId.getValue(), ipValue);
-                }
-                NeutronvpnUtils.createVpnPortFixedIpToPort(dataBroker, vpnId.getValue(), ipValue, sn
-                        .getRouterInterfaceName().getValue(), sn.getRouterIntfMacAddress(), true, true, false);
+                // Update the association of router-interface to external vpn
+                String PortName = NeutronvpnUtils.getNeutronPortNameFromVpnPortFixedIp(dataBroker, oldVpnId.getValue(), ipValue);
+                updateVpnInterface(vpnId, oldVpnId, NeutronvpnUtils.getNeutronPort(dataBroker, new Uuid(PortName)),
+                        isBeingAssociated, true);
             }
         }
         sn = updateSubnetNode(subnet, null, null, null, null, vpnId);
-        // send vpn updated for subnet notification
-        String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
-                .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        try {
-            Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
-                    .CONFIGURATION, elanIdentifierId);
-            if (elanInstance.isPresent()) {
-                long elanTag = elanInstance.get().getElanTag();
-                checkAndPublishSubnetUpdNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isBeingAssociated,
-                        elanTag);
-                LOG.debug("VPN updated for subnet notification sent for subnet {} on VPN {}", subnet.getValue(),
-                        vpnId.getValue());
-            } else {
-                LOG.error("VPN updated for subnet notification failed for subnet {} on VPN {} because of failure " +
-                        "in reading ELANInstance {}", subnet.getValue(), vpnId.getValue(), elanInstanceName);
-            }
-        } catch (Exception e) {
-            LOG.error("VPN updated for subnet notification failed for subnet {} on VPN {}", subnet.getValue(),
-                    vpnId.getValue(), e);
-        }
         // Check for ports on this subnet and update association of
         // corresponding vpn-interfaces to external vpn
         List<Uuid> portList = sn.getPortList();
         if (portList != null) {
             for (Uuid port : sn.getPortList()) {
-                LOG.debug("Updating vpn-interface for port {}", port.getValue());
+                LOG.debug("Updating vpn-interface for port {} isBeingAssociated {}", port.getValue(), isBeingAssociated);
                 updateVpnInterface(vpnId, oldVpnId, NeutronvpnUtils.getNeutronPort(dataBroker, port),
-                        isBeingAssociated);
+                        isBeingAssociated, false);
             }
         }
     }
@@ -1495,28 +1444,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         LOG.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue());
         final Uuid routerId = NeutronvpnUtils.getVpnMap(dataBroker, vpnId).getRouterId();
         Subnetmap sn = NeutronvpnUtils.getSubnetmap(dataBroker, subnet);
-        // send subnet removed from vpn notification
-        isExternalVpn = vpnId.equals(routerId) ? false : true;
-        String elanInstanceName = sn.getNetworkId().getValue();
-        InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
-                .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
-        try {
-            Optional<ElanInstance> elanInstance = NeutronvpnUtils.read(dataBroker, LogicalDatastoreType
-                    .CONFIGURATION, elanIdentifierId);
-            if (elanInstance.isPresent()) {
-                long elanTag = elanInstance.get().getElanTag();
-                checkAndPublishSubnetDelNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn,
-                        elanTag);
-                LOG.debug("Subnet removed from VPN notification sent for subnet {} on VPN {}", subnet.getValue(),
-                        vpnId.getValue());
-            } else {
-                LOG.error("Subnet removed from VPN notification failed for subnet {} on VPN {} because of failure " +
-                        "in reading ELANInstance {}", subnet.getValue(), vpnId.getValue(), elanInstanceName);
-            }
-        } catch (Exception e) {
-            LOG.error("Subnet removed from VPN notification failed for subnet {} on VPN {}", subnet.getValue(),
-                    vpnId.getValue(), e);
-        }
         if (sn != null) {
             // Check if there are ports on this subnet; remove corresponding vpn-interfaces
             List<Uuid> portList = sn.getPortList();
@@ -2124,9 +2051,11 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
             return;
         }
 
+        WriteTransaction wrtConfigTxn = dataBroker.newWriteOnlyTransaction();
         for (String elanInterface : extElanInterfaces) {
-            createExternalVpnInterface(extNetId, elanInterface);
+            createExternalVpnInterface(extNetId, elanInterface, wrtConfigTxn);
         }
+        wrtConfigTxn.submit();
     }
 
     protected void removeExternalVpnInterfaces(Uuid extNetId) {
@@ -2135,27 +2064,24 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
             LOG.trace("No external ports attached for external network {}", extNetId);
             return;
         }
+        try {
 
-        for (String elanInterface : extElanInterfaces) {
-            boolean isLockAcquired = false;
-            InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils
-                    .buildVpnInterfaceIdentifier(elanInterface);
-            try {
-                isLockAcquired = NeutronvpnUtils.lock(elanInterface);
-                LOG.debug("removing vpn interface {}, vpnIfIdentifier", elanInterface, vpnIfIdentifier);
-                MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier);
-            } catch (Exception ex) {
-                LOG.error("Removal of vpninterface {} failed due to {}", elanInterface, ex);
-            } finally {
-                if (isLockAcquired) {
-                    NeutronvpnUtils.unlock(elanInterface);
-                }
+            WriteTransaction wrtConfigTxn = dataBroker.newWriteOnlyTransaction();
+            for (String elanInterface : extElanInterfaces) {
+                InstanceIdentifier<VpnInterface> vpnIfIdentifier = NeutronvpnUtils
+                        .buildVpnInterfaceIdentifier(elanInterface);
+                LOG.info("Removing vpn interface {}", elanInterface);
+                wrtConfigTxn.delete(LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier);
             }
+            wrtConfigTxn.submit();
+
+        } catch (Exception ex) {
+            LOG.error("Removal of vpninterfaces {} failed due to {}", extElanInterfaces, ex);
         }
     }
 
-    private void createExternalVpnInterface(Uuid vpnId, String infName) {
-        writeVpnInterfaceToDs(vpnId, infName, null, false /* not a router iface */, null);
+    private void createExternalVpnInterface(Uuid vpnId, String infName, WriteTransaction wrtConfigTxn) {
+        writeVpnInterfaceToDs(vpnId, infName, null, false /* not a router iface */, wrtConfigTxn);
     }
 
     private void writeVpnInterfaceToDs(Uuid vpnId, String infName, Adjacencies adjacencies,
@@ -2198,51 +2124,6 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable, Even
         return help.toString();
     }
 
-    private void checkAndPublishSubnetAddNotification(Uuid subnetId, String subnetIp, String vpnName,
-                                                      Boolean isExternalvpn, Long elanTag) throws InterruptedException {
-        SubnetAddedToVpnBuilder builder = new SubnetAddedToVpnBuilder();
-
-        LOG.info("publish notification called");
-
-        builder.setSubnetId(subnetId);
-        builder.setSubnetIp(subnetIp);
-        builder.setVpnName(vpnName);
-        builder.setExternalVpn(isExternalvpn);
-        builder.setElanTag(elanTag);
-
-        notificationPublishService.putNotification(builder.build());
-    }
-
-    private void checkAndPublishSubnetDelNotification(Uuid subnetId, String subnetIp, String vpnName,
-                                                      Boolean isExternalvpn, Long elanTag) throws InterruptedException {
-        SubnetDeletedFromVpnBuilder builder = new SubnetDeletedFromVpnBuilder();
-
-        LOG.info("publish notification called");
-
-        builder.setSubnetId(subnetId);
-        builder.setSubnetIp(subnetIp);
-        builder.setVpnName(vpnName);
-        builder.setExternalVpn(isExternalvpn);
-        builder.setElanTag(elanTag);
-
-        notificationPublishService.putNotification(builder.build());
-    }
-
-    private void checkAndPublishSubnetUpdNotification(Uuid subnetId, String subnetIp, String vpnName,
-                                                      Boolean isExternalvpn, Long elanTag) throws InterruptedException {
-        SubnetUpdatedInVpnBuilder builder = new SubnetUpdatedInVpnBuilder();
-
-        LOG.info("publish notification called");
-
-        builder.setSubnetId(subnetId);
-        builder.setSubnetIp(subnetIp);
-        builder.setVpnName(vpnName);
-        builder.setExternalVpn(isExternalvpn);
-        builder.setElanTag(elanTag);
-
-        notificationPublishService.putNotification(builder.build());
-    }
-
     private void checkAndPublishRouterAssociatedtoVpnNotification(Uuid routerId, Uuid vpnId) throws
             InterruptedException {
         RouterAssociatedToVpn routerAssociatedToVpn = new RouterAssociatedToVpnBuilder().setRouterId(routerId)