introducing loopback port for VPP
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / iface / InterfaceManager.java
index 5fac50bd88e80e3067a2351d81d352530d5ef9c7..d3222249886752288cd9a304c44a1dd4d3d99bf9 100644 (file)
@@ -8,51 +8,54 @@
 
 package org.opendaylight.groupbasedpolicy.renderer.vpp.iface;
 
-import java.util.concurrent.ExecutorService;
-
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
-
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import com.google.common.eventbus.Subscribe;
+import com.google.common.util.concurrent.AsyncFunction;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.ConfigCommand;
+import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.LoopbackCommand;
+import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.TapPortCommand;
 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand;
 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.VhostUserCommand.VhostUserCommandBuilder;
 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.NodeOperEvent;
 import org.opendaylight.groupbasedpolicy.renderer.vpp.event.VppEndpointConfEvent;
 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General.Operations;
 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
+import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.LocationType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint.InterfaceTypeChoice;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.LoopbackCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.TapCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.vpp.endpoint._interface.type.choice.VhostUserCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VhostUserRole;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceAugmentation;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.L2Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.Interconnection;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBased;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.l2.base.attributes.interconnection.BridgeBasedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VhostUserRole;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.L2Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.Interconnection;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBased;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.l2.base.attributes.interconnection.BridgeBasedBuilder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
-import com.google.common.eventbus.Subscribe;
-import com.google.common.util.concurrent.AsyncFunction;
-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;
-
 public class InterfaceManager implements AutoCloseable {
 
     private static final Logger LOG = LoggerFactory.getLogger(InterfaceManager.class);
@@ -60,98 +63,133 @@ public class InterfaceManager implements AutoCloseable {
     private final VppEndpointLocationProvider vppEndpointLocationProvider;
     private final ExecutorService netconfWorker;
 
-    public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider, @Nonnull ExecutorService netconfWorker) {
+    public InterfaceManager(@Nonnull MountedDataBrokerProvider mountDataProvider, @Nonnull DataBroker dataProvider,
+            @Nonnull ExecutorService netconfWorker) {
         this.mountDataProvider = Preconditions.checkNotNull(mountDataProvider);
         this.netconfWorker = Preconditions.checkNotNull(netconfWorker);
         this.vppEndpointLocationProvider = new VppEndpointLocationProvider(dataProvider);
     }
 
     @Subscribe
+    @SuppressWarnings("OptionalGetWithoutIsPresent")
     public synchronized void vppEndpointChanged(VppEndpointConfEvent event) {
-        switch (event.getDtoModificationType()) {
-            case CREATED:
-                vppEndpointCreated(event.getAfter().get());
-                break;
-            case UPDATED:
-                vppEndpointDeleted(event.getBefore().get());
-                vppEndpointCreated(event.getAfter().get());
-                break;
-            case DELETED:
-                vppEndpointDeleted(event.getBefore().get());
+        try {
+            switch (event.getDtoModificationType()) {
+                case CREATED: {
+                    vppEndpointCreated(event.getAfter().get()).get();
+                }
                 break;
+                case UPDATED:
+                    vppEndpointUpdated(event.getBefore().get(), event.getAfter().get()).get();
+                    break;
+                case DELETED:
+                    vppEndpointDeleted(event.getBefore().get()).get();
+                    break;
+            }
+        } catch (InterruptedException | ExecutionException e) {
+            LOG.error("Failed to update Vpp Endpoint. {}", event, e);
         }
     }
 
-    private void vppEndpointCreated(VppEndpoint vppEndpoint) {
-        Optional<ConfigCommand> potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
+    private ListenableFuture<Void> vppEndpointCreated(VppEndpoint vppEndpoint) {
+        InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
+        LOG.trace("Creating VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice);
+        Optional<ConfigCommand> potentialIfaceCommand = Optional.absent();
+        if (interfaceTypeChoice instanceof VhostUserCase) {
+            potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
+        } else if (interfaceTypeChoice instanceof TapCase) {
+            potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.PUT);
+        } else if (interfaceTypeChoice instanceof LoopbackCase){
+            potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.PUT);
+        }
+
         if (!potentialIfaceCommand.isPresent()) {
-            return;
+            LOG.debug("Interface/PUT command was not created for VppEndpoint point {}", vppEndpoint);
+            return Futures.immediateFuture(null);
         }
         ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
         InstanceIdentifier<?> vppNodeIid = vppEndpoint.getVppNodePath();
         Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
         if (!potentialVppDataProvider.isPresent()) {
             LOG.debug("Cannot get data broker for mount point {}", vppNodeIid);
-            return;
+            return Futures.immediateFuture(null);
         }
         DataBroker vppDataBroker = potentialVppDataProvider.get();
-        createIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
+        return createInterfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
     }
 
-    private void createIfaceOnVpp(ConfigCommand createIfaceWithoutBdCommand, DataBroker vppDataBroker,
-            VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
-        ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction();
+    private ListenableFuture<Void> createInterfaceOnVpp(ConfigCommand createIfaceWithoutBdCommand, DataBroker vppDataBroker,
+                                                        VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
+        final ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction();
         createIfaceWithoutBdCommand.execute(rwTx);
-        Futures.addCallback(rwTx.submit(), new FutureCallback<Void>() {
+        LOG.trace("Creating Interface on VPP: {}", createIfaceWithoutBdCommand);
+        return Futures.transform(rwTx.submit(), new AsyncFunction<Void, Void>() {
 
             @Override
-            public void onSuccess(Void result) {
-                LOG.debug("Create interface on VPP command was successful:\nVPP: {}\nCommand: {}", vppNodeIid,
+            public ListenableFuture<Void> apply(@Nonnull Void input) {
+                LOG.debug("Create interface on VPP command was successful. VPP: {} Command: {}", vppNodeIid,
                         createIfaceWithoutBdCommand);
-                vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint);
-            }
-
-            @Override
-            public void onFailure(Throwable t) {
-                LOG.error("Create interface on VPP command was NOT successful:\nVPP: {}\nCommand: {}", vppNodeIid,
-                        createIfaceWithoutBdCommand, t);
+                return vppEndpointLocationProvider.createLocationForVppEndpoint(vppEndpoint);
             }
         }, netconfWorker);
     }
 
-    private void vppEndpointDeleted(VppEndpoint vppEndpoint) {
-        Optional<ConfigCommand> potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
+    private ListenableFuture<Void> vppEndpointUpdated(@Nonnull final VppEndpoint oldVppEndpoint,
+                                                      @Nonnull final VppEndpoint newVppEndpoint)
+            throws ExecutionException, InterruptedException {
+        if(!oldVppEndpoint.equals(newVppEndpoint)) {
+            LOG.debug("Updating vpp endpoint, old EP: {} new EP: {}", oldVppEndpoint, newVppEndpoint);
+            return Futures.transform(vppEndpointDeleted(oldVppEndpoint), new AsyncFunction<Void, Void>() {
+                @Override
+                public ListenableFuture<Void> apply(@Nonnull Void input) throws Exception {
+                    return vppEndpointCreated(newVppEndpoint);
+                }
+            });
+        }
+        LOG.debug("Update skipped, provided before/after vpp endpoints are equal");
+        return Futures.immediateFuture(null);
+    }
+
+    private ListenableFuture<Void> vppEndpointDeleted(@Nonnull VppEndpoint vppEndpoint) {
+        InterfaceTypeChoice interfaceTypeChoice = vppEndpoint.getInterfaceTypeChoice();
+        LOG.trace("Deleting VPP endpoint {}, type of {}", vppEndpoint, interfaceTypeChoice.toString());
+        Optional<ConfigCommand> potentialIfaceCommand = Optional.absent();
+        if (interfaceTypeChoice instanceof VhostUserCase) {
+            potentialIfaceCommand = createInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
+        } else if (interfaceTypeChoice instanceof TapCase) {
+            potentialIfaceCommand = createTapInterfaceWithoutBdCommand(vppEndpoint, Operations.DELETE);
+        } else if (interfaceTypeChoice instanceof LoopbackCase){
+            potentialIfaceCommand = createLoopbackWithoutBdCommand(vppEndpoint, Operations.DELETE);
+        }
+
         if (!potentialIfaceCommand.isPresent()) {
-            return;
+            LOG.debug("Interface/DELETE command was not created for VppEndpoint point {}", vppEndpoint);
+            return Futures.immediateFuture(null);
         }
         ConfigCommand ifaceWithoutBdCommand = potentialIfaceCommand.get();
         InstanceIdentifier<?> vppNodeIid = vppEndpoint.getVppNodePath();
         Optional<DataBroker> potentialVppDataProvider = mountDataProvider.getDataBrokerForMountPoint(vppNodeIid);
         if (!potentialVppDataProvider.isPresent()) {
             LOG.debug("Cannot get data broker for mount point {}", vppNodeIid);
-            return;
+            return Futures.immediateFuture(null);
         }
         DataBroker vppDataBroker = potentialVppDataProvider.get();
-        deleteIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
+        return deleteIfaceOnVpp(ifaceWithoutBdCommand, vppDataBroker, vppEndpoint, vppNodeIid);
     }
 
-    private void deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand, DataBroker vppDataBroker,
-            VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
+    private ListenableFuture<Void> deleteIfaceOnVpp(ConfigCommand deleteIfaceWithoutBdCommand,
+            DataBroker vppDataBroker, VppEndpoint vppEndpoint, InstanceIdentifier<?> vppNodeIid) {
         ReadWriteTransaction rwTx = vppDataBroker.newReadWriteTransaction();
         deleteIfaceWithoutBdCommand.execute(rwTx);
-        Futures.addCallback(rwTx.submit(), new FutureCallback<Void>() {
+        LOG.trace("Deleting Interface on VPP: {}", deleteIfaceWithoutBdCommand);
 
-            @Override
-            public void onSuccess(Void result) {
-                LOG.debug("Delete interface on VPP command was successful:\nVPP: {}\nCommand: {}", vppNodeIid,
-                        deleteIfaceWithoutBdCommand);
-                vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint);
-            }
+        return Futures.transform(rwTx.submit(), new AsyncFunction<Void, Void>() {
 
             @Override
-            public void onFailure(Throwable t) {
-                LOG.error("Delete interface on VPP command was NOT successful:\nVPP: {}\nCommand: {}", vppNodeIid,
-                        deleteIfaceWithoutBdCommand, t);
+            public ListenableFuture<Void> apply(Void input) {
+                LOG.debug("Delete interface on VPP command was successful: VPP: {} Command: {}", vppNodeIid,
+                        deleteIfaceWithoutBdCommand);
+                return vppEndpointLocationProvider.deleteLocationForVppEndpoint(vppEndpoint);
             }
         }, netconfWorker);
     }
@@ -171,7 +209,7 @@ public class InterfaceManager implements AutoCloseable {
                 break;
             case DELETED:
                 if (event.isBeforeConnected()) {
-                    // TODO we could do snapshot of VppEndpoints 
+                    // TODO we could do snapshot of VppEndpoints
                     // which can be used for reconciliation
                 }
                 break;
@@ -202,6 +240,55 @@ public class InterfaceManager implements AutoCloseable {
         return Optional.of(vhostUserCommand);
     }
 
+    private static Optional<ConfigCommand> createTapInterfaceWithoutBdCommand(@Nonnull VppEndpoint vppEp,
+            @Nonnull Operations operation) {
+        if (!hasNodeAndInterface(vppEp)) {
+            LOG.debug("Interface command is not created for {}", vppEp);
+            return Optional.absent();
+        }
+        TapPortCommand.TapPortCommandBuilder builder = TapPortCommand.builder();
+        InterfaceTypeChoice interfaceTypeChoice = vppEp.getInterfaceTypeChoice();
+        if (interfaceTypeChoice instanceof TapCase) {
+            TapCase tapIface = (TapCase) interfaceTypeChoice;
+            String name = tapIface.getName();
+            if (Strings.isNullOrEmpty(name)) {
+                LOG.debug("Tap interface command is not created because name is missing. {}", vppEp);
+                return Optional.absent();
+            }
+            builder.setTapName(name);
+            builder.setPhysAddress(tapIface.getPhysicalAddress());
+        }
+        TapPortCommand tapPortCommand = builder
+                .setOperation(operation)
+                .setDescription(vppEp.getDescription())
+                .setInterfaceName(vppEp.getVppInterfaceName())
+                .build();
+        return Optional.of(tapPortCommand);
+    }
+
+    private static Optional<ConfigCommand> createLoopbackWithoutBdCommand(@Nonnull VppEndpoint vppEp,
+        @Nonnull Operations operation) {
+        if (!hasNodeAndInterface(vppEp)) {
+            LOG.debug("Interface command is not created for {}", vppEp);
+            return Optional.absent();
+        }
+        LoopbackCommand.LoopbackCommandBuilder builder = LoopbackCommand.builder();
+        LoopbackCase loopIface = (LoopbackCase) vppEp.getInterfaceTypeChoice();
+
+        builder.setPhysAddress(loopIface.getPhysAddress());
+        builder.setBvi(loopIface.isBvi());
+        builder.setIpAddress(loopIface.getIpAddress());
+        builder.setIpPrefix(loopIface.getIpPrefix());
+
+        LoopbackCommand loopbackCommand = builder
+            .setOperation(operation)
+            .setDescription(vppEp.getDescription())
+            .setInterfaceName(vppEp.getVppInterfaceName())
+            .build();
+
+        return Optional.of(loopbackCommand);
+    }
+
     /**
      * Adds bridge domain to an interface if the interface exist.<br>
      * It rewrites bridge domain in case it already exist.<br>
@@ -210,7 +297,7 @@ public class InterfaceManager implements AutoCloseable {
      * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
      * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
      * containing message in {@link Exception#getMessage()}
-     * 
+     *
      * @param bridgeDomainName bridge domain
      * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
      *        {@link ExternalLocationCase} where
@@ -219,7 +306,7 @@ public class InterfaceManager implements AutoCloseable {
      * @return {@link ListenableFuture}
      */
     public synchronized @Nonnull ListenableFuture<Void> addBridgeDomainToInterface(@Nonnull String bridgeDomainName,
-            @Nonnull AddressEndpointWithLocation addrEpWithLoc) {
+            @Nonnull AddressEndpointWithLocation addrEpWithLoc, boolean enableBvi) {
         ExternalLocationCase epLoc = resolveAndValidateLocation(addrEpWithLoc);
         InstanceIdentifier<?> vppNodeIid = epLoc.getExternalNodeMountPoint();
         String interfacePath = epLoc.getExternalNodeConnector();
@@ -236,8 +323,8 @@ public class InterfaceManager implements AutoCloseable {
         if (!potentialVppDataProvider.isPresent()) {
             return Futures.immediateFailedFuture(new Exception("Cannot get data broker for mount point " + vppNodeIid));
         }
-
-        ReadWriteTransaction rwTx = potentialVppDataProvider.get().newReadWriteTransaction();
+        final DataBroker mountpoint = potentialVppDataProvider.get();
+        final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
         CheckedFuture<Optional<Interface>, ReadFailedException> futureIface =
                 rwTx.read(LogicalDatastoreType.CONFIGURATION, interfaceIid);
         return Futures.transform(futureIface, new AsyncFunction<Optional<Interface>, Void>() {
@@ -245,33 +332,44 @@ public class InterfaceManager implements AutoCloseable {
             @Override
             public ListenableFuture<Void> apply(Optional<Interface> optIface) throws Exception {
                 if (!optIface.isPresent()) {
-                    return Futures.immediateFailedFuture(new Exception("Iterface "
+                    return Futures.immediateFailedFuture(new Exception("Interface "
                             + interfaceIid.firstKeyOf(Interface.class) + " does not exist on node " + vppNodeIid));
                 }
 
                 String existingBridgeDomain = resolveBridgeDomain(optIface.get());
                 if (bridgeDomainName.equals(existingBridgeDomain)) {
-                    String nodePath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
-                    if (!nodePath.equals(epLoc.getExternalNode())) {
-                        vppEndpointLocationProvider.updateExternalNodeLocationForEndpoint(nodePath,
-                                epLoc.getExternalNodeMountPoint(), addrEpWithLoc.getKey());
+                    LOG.debug("Bridge domain {} already exists on interface {}", bridgeDomainName, interfacePath);
+                    String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
+                    if (!bridgeDomainPath.equals(epLoc.getExternalNode())) {
+                        return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
+                            .setExternalNode(bridgeDomainPath)
+                            .setExternalNodeMountPoint(vppNodeIid)
+                            .setExternalNodeConnector(interfacePath)
+                            .build(), addrEpWithLoc.getKey());
                     }
                     return Futures.immediateFuture(null);
                 }
-
                 InstanceIdentifier<L2> l2Iid =
                         interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
-                L2 l2 = new L2Builder()
-                    .setInterconnection(new BridgeBasedBuilder().setBridgeDomain(bridgeDomainName).build()).build();
-                rwTx.merge(LogicalDatastoreType.CONFIGURATION, l2Iid, l2);
-                return Futures.transform(rwTx.submit(), new Function<Void, Void>() {
+                final ReadWriteTransaction rwTxRead = mountpoint.newReadWriteTransaction();
+                Optional<L2> optL2 = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION, l2Iid, rwTxRead);
+                L2Builder l2Builder = (optL2.isPresent()) ? new L2Builder(optL2.get()) : new L2Builder();
+                L2 l2 = l2Builder.setInterconnection(new BridgeBasedBuilder()
+                    .setBridgeDomain(bridgeDomainName)
+                    .setBridgedVirtualInterface(enableBvi)
+                    .build()).build();
+                final ReadWriteTransaction rwTxPut = prepareTransactionAndPutData(mountpoint, l2, l2Iid);
+                LOG.debug("Adding bridge domain {} to interface {}", bridgeDomainName, interfacePath);
+                return Futures.transform(rwTxPut.submit(), new AsyncFunction<Void, Void>() {
 
                     @Override
-                    public Void apply(Void input) {
-                        String nodePath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
-                        vppEndpointLocationProvider.updateExternalNodeLocationForEndpoint(nodePath,
-                                epLoc.getExternalNodeMountPoint(), addrEpWithLoc.getKey());
-                        return null;
+                    public ListenableFuture<Void> apply(@Nonnull Void input) {
+                        String bridgeDomainPath = VppPathMapper.bridgeDomainToRestPath(bridgeDomainName);
+                        return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
+                                .setExternalNode(bridgeDomainPath)
+                                .setExternalNodeMountPoint(vppNodeIid)
+                                .setExternalNodeConnector(interfacePath)
+                                .build(), addrEpWithLoc.getKey());
                     }
                 }, netconfWorker);
             }
@@ -279,7 +377,6 @@ public class InterfaceManager implements AutoCloseable {
     }
 
     /**
-     * <p>
      * Removes bridge domain (if exist) from an interface (if exist).<br>
      * {@link VppEndpointLocationProvider#VPP_ENDPOINT_LOCATION_PROVIDER} will update endpoint
      * location.
@@ -287,7 +384,7 @@ public class InterfaceManager implements AutoCloseable {
      * If the interface does not exist or other problems occur {@link ListenableFuture} will fail
      * as {@link Futures#immediateFailedFuture(Throwable)} with {@link Exception}
      * containing message in {@link Exception#getMessage()}
-     * 
+     *
      * @param addrEpWithLoc {@link AddressEndpointWithLocation} containing
      *        {@link ExternalLocationCase} where
      *        {@link ExternalLocationCase#getExternalNodeMountPoint()} MUST NOT be {@code null}
@@ -324,25 +421,30 @@ public class InterfaceManager implements AutoCloseable {
                     // interface does not exist so we consider job done
                     return Futures.immediateFuture(null);
                 }
-
                 String existingBridgeDomain = resolveBridgeDomain(optIface.get());
                 if (Strings.isNullOrEmpty(existingBridgeDomain)) {
+                    LOG.debug("Bridge domain does not exist therefore it is considered as deleted for interface {}",
+                            interfacePath);
                     // bridge domain does not exist on interface so we consider job done
-                    vppEndpointLocationProvider.updateExternalNodeLocationForEndpoint(null,
-                            epLoc.getExternalNodeMountPoint(), addrEpWithLoc.getKey());
-                    return Futures.immediateFuture(null);
+                    return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
+                            .setExternalNode(null)
+                            .setExternalNodeMountPoint(vppNodeIid)
+                            .setExternalNodeConnector(interfacePath)
+                            .build(), addrEpWithLoc.getKey());
                 }
-
                 InstanceIdentifier<L2> l2Iid =
                         interfaceIid.builder().augmentation(VppInterfaceAugmentation.class).child(L2.class).build();
                 rwTx.delete(LogicalDatastoreType.CONFIGURATION, l2Iid);
-                return Futures.transform(rwTx.submit(), new Function<Void, Void>() {
+                LOG.debug("Deleting bridge domain from interface {}", interfacePath);
+                return Futures.transform(rwTx.submit(), new AsyncFunction<Void, Void>() {
 
                     @Override
-                    public Void apply(Void input) {
-                        vppEndpointLocationProvider.updateExternalNodeLocationForEndpoint(null,
-                                epLoc.getExternalNodeMountPoint(), addrEpWithLoc.getKey());
-                        return null;
+                    public ListenableFuture<Void> apply(@Nonnull Void input) {
+                        return vppEndpointLocationProvider.replaceLocationForEndpoint(new ExternalLocationCaseBuilder()
+                                .setExternalNode(null)
+                                .setExternalNodeMountPoint(vppNodeIid)
+                                .setExternalNodeConnector(interfacePath)
+                                .build(), addrEpWithLoc.getKey());
                     }
                 }, netconfWorker);
             }
@@ -376,11 +478,11 @@ public class InterfaceManager implements AutoCloseable {
 
     private static boolean hasNodeAndInterface(VppEndpoint vppEp) {
         if (vppEp.getVppNodePath() == null) {
-            LOG.trace("vpp-node is missing. {}", vppEp);
+            LOG.debug("vpp-node is missing. {}", vppEp);
             return false;
         }
         if (Strings.isNullOrEmpty(vppEp.getVppInterfaceName())) {
-            LOG.trace("vpp-interface-name is missing. {}", vppEp);
+            LOG.debug("vpp-interface-name is missing. {}", vppEp);
             return false;
         }
         return true;
@@ -391,4 +493,19 @@ public class InterfaceManager implements AutoCloseable {
         vppEndpointLocationProvider.close();
     }
 
+    // TODO workaround for netconf, remove when fixed
+    private synchronized <T extends DataObject> ReadWriteTransaction prepareTransactionAndPutData(final DataBroker mountpoint,
+                                                                                                  final T data,
+                                                                                                  InstanceIdentifier<T> iid) {
+        final ReadWriteTransaction rwTx = mountpoint.newReadWriteTransaction();
+        try {
+            rwTx.put(LogicalDatastoreType.CONFIGURATION, iid, data);
+        }
+        catch (IllegalStateException e) {
+            LOG.error("Assuming netconf transaction failed, restarting ...", e.getMessage());
+            return prepareTransactionAndPutData(mountpoint, data, iid);
+        }
+        return rwTx;
+    }
+
 }