refactoring to support delete\update of servics.
[unimgr.git] / netvirt / src / main / java / org / opendaylight / unimgr / mef / netvirt / IpvcListener.java
index 181c4a40104a0a6578aada874d8ed629479aa105..de9d487cc7e7984344c45d1153287eb36d35690c 100644 (file)
@@ -9,43 +9,50 @@
 package org.opendaylight.unimgr.mef.netvirt;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-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.unimgr.api.UnimgrDataTreeChangeListener;
+import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.subnets.Subnet;
+import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.subnets.SubnetBuilder;
 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.uni.ip.unis.IpUni;
-import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.uni.ip.unis.ip.uni.subnets.Subnet;
+import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.IpvcVpn;
 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.mef.service.choice.ipvc.choice.Ipvc;
+import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.mef.service.choice.ipvc.choice.ipvc.VpnElans;
 import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.services.rev150526.mef.services.mef.service.mef.service.choice.ipvc.choice.ipvc.unis.Uni;
+import org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.types.rev150526.Identifier45;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.arputil.rev160406.OdlArputilService;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class IpvcListener extends UnimgrDataTreeChangeListener<Ipvc> {
     private static final Logger Log = LoggerFactory.getLogger(IpvcListener.class);
+    private final IUniPortManager uniPortManager;
+    private final ISubnetManager subnetManager;
     private ListenerRegistration<IpvcListener> ipvcListenerRegistration;
-    private final NotificationPublishService notificationPublishService;
-    private final OdlArputilService arpUtilService;
 
-    public IpvcListener(final DataBroker dataBroker, final NotificationPublishService notPublishService,
-            final OdlArputilService arputilService) {
+    public IpvcListener(final DataBroker dataBroker, final IUniPortManager uniPortManager,
+            final ISubnetManager subnetManager) {
         super(dataBroker);
-        this.notificationPublishService = notPublishService;
-        this.arpUtilService = arputilService;
+        this.uniPortManager = uniPortManager;
+        this.subnetManager = subnetManager;
+
         registerListener();
     }
 
     public void registerListener() {
         try {
             final DataTreeIdentifier<Ipvc> dataTreeIid = new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
-                    MefUtils.getIpvcInstanceIdentifier());
+                    MefServicesUtils.getIpvcsInstanceIdentifier());
             ipvcListenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIid, this);
             Log.info("IpvcDataTreeChangeListener created and registered");
         } catch (final Exception e) {
@@ -85,148 +92,262 @@ public class IpvcListener extends UnimgrDataTreeChangeListener<Ipvc> {
 
     private void addIpvc(DataTreeModification<Ipvc> newDataObject) {
         try {
-            Ipvc data = newDataObject.getRootNode().getDataAfter();
-            String instanceName = data.getIpvcId().getValue();
+            WriteTransaction tx = MdsalUtils.createTransaction(dataBroker);
+            Ipvc ipvc = newDataObject.getRootNode().getDataAfter();
+            String instanceName = ipvc.getIpvcId().getValue();
             final String vpnName = NetvirtVpnUtils.getUUidFromString(instanceName);
+            InstanceIdentifier<Ipvc> ipvcId = newDataObject.getRootPath().getRootIdentifier();
+            List<Uni> unis = new ArrayList<>();
+            synchronized (vpnName.intern()) {
+                Log.info("Adding vpn instance: " + instanceName);
+                NetvirtVpnUtils.createVpnInstance(vpnName, tx);
+                MefServicesUtils.addOperIpvcVpnElan(ipvcId, vpnName, tx);
+                if (ipvc.getUnis() != null && ipvc.getUnis() != null) {
+                    unis = ipvc.getUnis().getUni();
+                }
+                Log.info("Number of UNI's: " + unis.size());
 
-            Log.info("Adding vpn instance: " + instanceName);
-            NetvirtVpnUtils.createVpnInstance(dataBroker, vpnName);
-            Log.info("Number of UNI's: " + data.getUnis().getUni().size());
-
-
-            // Create elan interfaces
-            for (Uni uni : data.getUnis().getUni()) {
-                createInterfaces(vpnName, uni);
+                // Create elan/vpn interfaces
+                for (Uni uni : unis) {
+                    createInterfaces(vpnName, uni, ipvcId, tx);
+                }
+                MdsalUtils.commitTransaction(tx);
+            }
+            for (Uni uni : unis) {
+                IpUni ipUni = MefInterfaceUtils.getIpUni(dataBroker, uni.getUniId(), uni.getIpUniId(),
+                        LogicalDatastoreType.CONFIGURATION);
+                createDirectSubnet(uni, ipUni);
+                subnetManager.assignIpUniNetworks(uni.getUniId(), ipUni.getIpUniId(), ipvcId);
             }
         } catch (final Exception e) {
             Log.error("Add ipvc failed !", e);
         }
     }
 
-    private void updateIpvc(DataTreeModification<Ipvc> modifiedDataObject) {
+    private void removeIpvc(DataTreeModification<Ipvc> removedDataObject) {
         try {
-            Ipvc original = modifiedDataObject.getRootNode().getDataBefore();
-            Ipvc update = modifiedDataObject.getRootNode().getDataAfter();
+            Ipvc ipvc = removedDataObject.getRootNode().getDataBefore();
+            InstanceIdentifier<Ipvc> ipvcId = removedDataObject.getRootPath().getRootIdentifier();
+            IpvcVpn operIpvcVpn = MefServicesUtils.getOperIpvcVpn(dataBroker, ipvcId);
+            if (operIpvcVpn == null) {
+                Log.error("Ipvc {} hasn't been created as required", ipvc.getIpvcId());
+                return;
+            }
+            String vpnName = operIpvcVpn.getVpnId();
+
+            synchronized (vpnName.intern()) {
+                // remove elan/vpn interfaces
+                // must be in different transactios
+                WriteTransaction tx = MdsalUtils.createTransaction(dataBroker);
+                removeUni(ipvcId, operIpvcVpn, ipvc.getUnis().getUni(), tx);
+                MdsalUtils.commitTransaction(tx);
+                // Let to work for listeners
+                // TODO : change to listener
+                NetvirtUtils.safeSleep();
+
+                WriteTransaction txvpn = MdsalUtils.createTransaction(dataBroker);
+                NetvirtVpnUtils.removeVpnInstance(operIpvcVpn.getVpnId(), txvpn);
+                MefServicesUtils.removeOperIpvcVpn(ipvcId, txvpn);
+                MdsalUtils.commitTransaction(txvpn);
+            }
+        } catch (final Exception e) {
+            Log.error("Remove ipvc failed !", e);
+        }
+    }
 
-            String instanceName = original.getIpvcId().getValue();
-            final String vpnName = NetvirtVpnUtils.getUUidFromString(instanceName);
+    private void removeUni(InstanceIdentifier<Ipvc> ipvcId, IpvcVpn operIpvcVpn, List<Uni> uniToRemove,
+            WriteTransaction tx) {
+        if (uniToRemove == null) {
+            Log.trace("No UNI's to remove");
+        }
+        for (Uni uni : uniToRemove) {
+            Identifier45 uniId = uni.getUniId();
+            Identifier45 ipUniId = uni.getIpUniId();
+            IpUni ipUni = MefInterfaceUtils.getIpUni(dataBroker, uniId, ipUniId, LogicalDatastoreType.CONFIGURATION);
+            if (ipUni == null) {
+                String errorMessage = String.format("Couldn't find ipuni %s for uni %s", ipUniId, uniId);
+                Log.error(errorMessage);
+                throw new UnsupportedOperationException(errorMessage);
+            }
 
-            Log.info("Updating elan instance: " + instanceName);
+            removeDirectSubnet(uni, ipUni);
+            subnetManager.unAssignIpUniNetworks(uni.getUniId(), ipUni.getIpUniId(), ipvcId);
+            removeInterfaces(ipvcId, operIpvcVpn, uni, ipUni, tx);
+        }
+    }
 
-            if (original == null || original.getUnis() == null || original.getUnis().getUni() == null) {
-                addIpvc(modifiedDataObject);
+    private void updateIpvc(DataTreeModification<Ipvc> modifiedDataObject) {
+        try {
+            Ipvc origIpvc = modifiedDataObject.getRootNode().getDataBefore();
+            Ipvc updateIpvc = modifiedDataObject.getRootNode().getDataAfter();
+            InstanceIdentifier<Ipvc> ipvcId = modifiedDataObject.getRootPath().getRootIdentifier();
+            IpvcVpn operIpvcVpn = MefServicesUtils.getOperIpvcVpn(dataBroker, ipvcId);
+            if (operIpvcVpn == null) {
+                Log.error("Ipvc {} hasn't been created as required", origIpvc.getIpvcId());
                 return;
             }
-
-            List<Uni> originalUni = original.getUnis().getUni();
-            List<Uni> updateUni = update.getUnis().getUni();
-            if (updateUni != null && !updateUni.isEmpty()) {
-                List<Uni> existingClonedUni = new ArrayList<>();
-                if (originalUni != null && !originalUni.isEmpty()) {
-                    existingClonedUni.addAll(0, originalUni);
-                    originalUni.removeAll(updateUni);
-                    updateUni.removeAll(existingClonedUni);
-                    // removing the Uni which are not presented in the updated
-                    // List
-                    for (Uni uni : originalUni) {
-                        removeElanInterface(vpnName, uni);
-                    }
+            String vpnName = operIpvcVpn.getVpnId();
+
+            List<Uni> originalUni = origIpvc.getUnis() != null && origIpvc.getUnis().getUni() != null
+                    ? origIpvc.getUnis().getUni() : Collections.emptyList();
+            List<Uni> updateUni = updateIpvc.getUnis() != null && updateIpvc.getUnis().getUni() != null
+                    ? updateIpvc.getUnis().getUni() : Collections.emptyList();
+
+            synchronized (vpnName.intern()) {
+                WriteTransaction txRemove = MdsalUtils.createTransaction(dataBroker);
+                List<Uni> uniToRemove = new ArrayList<>(originalUni);
+                uniToRemove.removeAll(updateUni);
+                removeUni(ipvcId, operIpvcVpn, uniToRemove, txRemove);
+                MdsalUtils.commitTransaction(txRemove);
+
+                WriteTransaction tx = MdsalUtils.createTransaction(dataBroker);
+                List<Uni> uniToCreate = new ArrayList<>(updateUni);
+                uniToCreate.removeAll(originalUni);
+                for (Uni uni : uniToCreate) {
+                    createInterfaces(vpnName, uni, ipvcId, tx);
                 }
+                MdsalUtils.commitTransaction(tx);
 
-                // Adding the new Uni which are presented in the updated List
-                if (updateUni.size() > 0) {
-                    for (Uni uni : updateUni) {
-                        createInterfaces(vpnName, uni);
-                    }
-                }
-            } else if (originalUni != null && !originalUni.isEmpty()) {
-                for (Uni uni : originalUni) {
-                    removeElanInterface(vpnName, uni);
+                for (Uni uni : uniToCreate) {
+                    IpUni ipUni = MefInterfaceUtils.getIpUni(dataBroker, uni.getUniId(), uni.getIpUniId(),
+                            LogicalDatastoreType.CONFIGURATION);
+                    createDirectSubnet(uni, ipUni);
+                    subnetManager.assignIpUniNetworks(uni.getUniId(), ipUni.getIpUniId(), ipvcId);
                 }
             }
+
         } catch (final Exception e) {
             Log.error("Update ipvc failed !", e);
         }
     }
 
-    private void removeIpvc(DataTreeModification<Ipvc> removedDataObject) {
-        try {
-            Ipvc data = removedDataObject.getRootNode().getDataBefore();
+    private void createInterfaces(String vpnName, Uni uniInService, InstanceIdentifier<Ipvc> ipvcId,
+            WriteTransaction tx) {
+        String uniId = uniInService.getUniId().getValue();
+        String ipUniId = uniInService.getIpUniId().getValue();
+        org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni uni = MefInterfaceUtils
+                .getUni(dataBroker, uniId, LogicalDatastoreType.OPERATIONAL);
+        if (uni == null) {
+            String errorMessage = String.format("Couldn't find uni %s for ipvc", uniId);
+            Log.error(errorMessage);
+            throw new UnsupportedOperationException(errorMessage);
+        }
+        IpUni ipUni = MefInterfaceUtils.getIpUni(dataBroker, uniInService.getUniId(), uniInService.getIpUniId(),
+                LogicalDatastoreType.CONFIGURATION);
+        if (ipUni == null) {
+            String errorMessage = String.format("Couldn't find ipuni %s for uni %s", ipUniId, uniId);
+            Log.error(errorMessage);
+            throw new UnsupportedOperationException(errorMessage);
+        }
 
-            String instanceName = data.getIpvcId().getValue();
+        Long vlan = ipUni.getVlan() != null ? Long.valueOf(ipUni.getVlan().getValue()) : null;
+        String elanName = NetvirtVpnUtils.getElanNameForVpnPort(uniId);
+        String interfaceName = createElanInterface(vpnName, ipvcId, uniId, elanName, vlan, tx);
+        createVpnInterface(vpnName, uni, ipUni, interfaceName, tx);
+        MefServicesUtils.addOperIpvcVpnElan(ipvcId, vpnName, uniInService.getUniId(), uniInService.getIpUniId(),
+                elanName, interfaceName, null, tx);
+    }
 
-            for (Uni uni : data.getUnis().getUni()) {
-                removeElanInterface(instanceName, uni);
-            }
+    private String createElanInterface(String vpnName, InstanceIdentifier<Ipvc> ipvcId, String uniId, String elanName,
+            Long vlan, WriteTransaction tx) {
+        Log.info("Adding elan instance: " + elanName);
+        NetvirtUtils.updateElanInstance(elanName, tx);
 
-            Log.info("Removing elan instance: " + instanceName);
-            NetvirtUtils.deleteElanInstance(dataBroker, instanceName);
-        } catch (final Exception e) {
-            Log.error("Remove ipvc failed !", e);
+        Log.info("Added trunk interface for uni {} vlan: {}", uniId, vlan);
+        if (vlan != null) {
+            uniPortManager.addCeVlan(uniId, vlan);
         }
+        String interfaceName = uniPortManager.getUniVlanInterface(uniId, vlan);
+        if (interfaceName == null) {
+            String errorMessage = String.format("Couldn't create  uni %s vlan interface %s", uniId, vlan);
+            Log.error(errorMessage);
+            throw new UnsupportedOperationException(errorMessage);
+        }
+
+        Log.info("Adding elan interface: " + interfaceName);
+        NetvirtUtils.createElanInterface(elanName, interfaceName, EtreeInterfaceType.Root, false, tx);
+        return interfaceName;
+    }
+
+    private void createVpnInterface(String vpnName,
+            org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni uni,
+            IpUni ipUni, String interfaceName, WriteTransaction tx) {
+
+        Log.info("Adding vpn interface: " + interfaceName);
+        NetvirtVpnUtils.createUpdateVpnInterface(vpnName, interfaceName, ipUni.getIpAddress(),
+                uni.getMacAddress().getValue(), true, null, tx);
+        NetvirtVpnUtils.createVpnPortFixedIp(vpnName, interfaceName, ipUni.getIpAddress(), uni.getMacAddress(), tx);
+        Log.info("Finished working on vpn instance: " + vpnName);
     }
 
-    private void createInterfaces(Ipvc ipvc, Uni uni) {
-        String instanceName = ipvc.getIpvcId().getValue();
-        createInterfaces(instanceName, uni);
+    private void createDirectSubnet(Uni uni, IpUni ipUni) {
+        IpPrefix uniIpPrefix = ipUni.getIpAddress();
+        String subnetIp = NetvirtVpnUtils.getSubnetFromPrefix(uniIpPrefix);
+        IpPrefix subnetPrefix = new IpPrefix(new Ipv4Prefix(subnetIp));
+        InstanceIdentifier<Subnet> path = MefInterfaceUtils.getSubnetInstanceIdentifier(uni.getUniId(),
+                ipUni.getIpUniId(), subnetPrefix);
+        SubnetBuilder subnet = new SubnetBuilder();
+        subnet.setUniId(uni.getUniId());
+        subnet.setIpUniId(ipUni.getIpUniId());
+        subnet.setSubnet(subnetPrefix);
+        MdsalUtils.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, path, subnet.build());
     }
 
-    private void createInterfaces(String vpnName, Uni uniInService) {
+    private void removeInterfaces(InstanceIdentifier<Ipvc> ipvcId, IpvcVpn ipvcVpn, Uni uniInService, IpUni ipUni,
+            WriteTransaction tx) {
         String uniId = uniInService.getUniId().getValue();
-        String ipUniId = uniInService.getIpUniId().getValue();
+        org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni uni = MefInterfaceUtils
+                .getUni(dataBroker, uniId, LogicalDatastoreType.OPERATIONAL);
+        if (uni == null) {
+            String errorMessage = String.format("Couldn't find uni %s for ipvc", uniId);
+            Log.error(errorMessage);
+            throw new UnsupportedOperationException(errorMessage);
+        }
 
-        Log.info("Adding/updating elan instance: " + uniId);
+        String vpnName = ipvcVpn.getVpnId();
 
-        org.opendaylight.yang.gen.v1.http.metroethernetforum.org.ns.yang.mef.interfaces.rev150526.mef.interfaces.unis.Uni uni = IpvcUniUtils
-                .getUni(dataBroker, uniId);
-        if (uni == null) {
-            Log.error("Couldn't find uni {} for ipvc-uni", uniId);
-            throw new UnsupportedOperationException();
+        VpnElans vpnElans = MefServicesUtils.findVpnElanForNetwork(new Identifier45(uniId), ipUni.getIpUniId(),
+                ipvcVpn);
+        if (vpnElans == null) {
+            Log.error("Trying to remome non-operational vpn/elan for Uni {} Ip-UNi {}", uniId, ipUni.getIpUniId());
         }
-        IpUni ipUni = IpvcUniUtils.getIpUni(dataBroker, uniId, ipUniId);
-        Integer vlan = ipUni.getVlan() != null ? ipUni.getVlan().getValue() : null;
 
-        String elanName = NetvirtVpnUtils.getElanNameForVpnPort(uniId);
-        Log.info("Adding elan instance: " + elanName);
-        NetvirtUtils.updateElanInstance(dataBroker, elanName);
+        NetvirtVpnUtils.removeVpnInterfaceAdjacencies(dataBroker, vpnName, vpnElans.getElanPort());
+        // TODO : change to listener
+        NetvirtUtils.safeSleep();
+        removeElan(vpnElans, uniId, ipUni, tx);
+        removeVpnInterface(vpnName, vpnElans, uniId, ipUni, tx);
+        MefServicesUtils.removeOperIpvcElan(dataBroker, ipvcId, ipvcVpn.getVpnId(), uniInService.getUniId(),
+                uniInService.getIpUniId(), vpnElans.getElanId(), vpnElans.getElanPort());
+    }
 
-        String interfaceName = NetvirtVpnUtils.getInterfaceNameForVlan(ipUniId, vlan);
-        Log.info("Adding trunk interface: " + interfaceName);
-        IpvcUniUtils.addUni(dataBroker, uniInService, interfaceName, vlan);
+    private void removeElan(VpnElans vpnElans, String uniId, IpUni ipUni, WriteTransaction tx) {
+        Long vlan = ipUni.getVlan() != null ? Long.valueOf(ipUni.getVlan().getValue()) : 0;
+        Log.info("Removing trunk interface for uni {} vlan: {}", uniId, vlan);
+        uniPortManager.removeCeVlan(uniId, vlan);
 
-        Log.info("Adding elan interface: " + interfaceName);
-        NetvirtUtils.createInterface(dataBroker, elanName, interfaceName, EtreeInterfaceType.Root, false);
+        String elanName = vpnElans.getElanId();
+        String interfaceName = vpnElans.getElanPort();
 
-        Log.info("Adding vpn interface: " + interfaceName);
-        NetvirtVpnUtils.createUpdateVpnInterface(dataBroker, vpnName, interfaceName, ipUni.getIpAddress(),
-                uni.getMacAddress(), true, null);
-        NetvirtVpnUtils.createVpnPortFixedIp(dataBroker, vpnName, interfaceName, ipUni.getIpAddress(),
-                uni.getMacAddress());
-
-        Log.info("Adding connected network for interface : " + interfaceName);
-        NetvirtVpnUtils.addDirectSubnetToVpn(dataBroker, notificationPublishService, vpnName, elanName,
-                ipUni.getIpAddress(), interfaceName);
-
-        if (ipUni != null && ipUni.getSubnets() != null && ipUni.getSubnets().getSubnet() != null) {
-            for (Subnet subnet : ipUni.getSubnets().getSubnet()) {
-                Log.info("Resolving MAC address for gateway: " + subnet.getGateway());
-                MacAddress gwMacAddress = NetvirtVpnUtils.resolveGwMac(dataBroker, arpUtilService, vpnName,
-                        ipUni.getIpAddress(), subnet.getGateway(), interfaceName); // trunk
-                if (gwMacAddress == null) {
-                    continue;
-                }
-                Log.info("update vpn interface: " + interfaceName);
-                NetvirtVpnUtils.createUpdateVpnInterface(dataBroker, vpnName, interfaceName, subnet.getSubnet(),
-                        gwMacAddress, false, ipUni.getIpAddress());
-            }
-        }
-        Log.info("Finished working on elan instance: " + uniId);
+        Log.info("Removing elan instance {} and interface {}: ", elanName, interfaceName);
+        NetvirtUtils.deleteElanInterface(interfaceName, tx);
+        NetvirtUtils.deleteElanInstance(elanName, tx);
+    }
+
+    private void removeVpnInterface(String vpnName, VpnElans vpnElans, String uniId, IpUni ipUni, WriteTransaction tx) {
+        String interfaceName = vpnElans.getElanPort();
+        Log.info("Removing vpn interface: " + interfaceName);
+        NetvirtVpnUtils.removeVpnInterface(interfaceName, tx);
+        NetvirtVpnUtils.removeVpnPortFixedIp(vpnName, ipUni.getIpAddress(), tx);
+        Log.info("Finished working on vpn instance: " + vpnName);
     }
 
-    private void removeElanInterface(String instanceName, Uni uni) {
-        String uniId = uni.getIpUniId().getValue();
-        String interfaceName = uniId;
-        Log.info("Removing elan interface: " + uniId);
-        NetvirtUtils.deleteElanInterface(dataBroker, instanceName, interfaceName);
+    private void removeDirectSubnet(Uni uni, IpUni ipUni) {
+        IpPrefix uniIpPrefix = ipUni.getIpAddress();
+        String subnetIp = NetvirtVpnUtils.getSubnetFromPrefix(uniIpPrefix);
+        IpPrefix subnetPrefix = new IpPrefix(new Ipv4Prefix(subnetIp));
+        InstanceIdentifier<Subnet> path = MefInterfaceUtils.getSubnetInstanceIdentifier(uni.getUniId(),
+                ipUni.getIpUniId(), subnetPrefix);
+        MdsalUtils.delete(dataBroker, LogicalDatastoreType.CONFIGURATION, path);
     }
 }