HONEYCOMB-91: fix restoring BD from persisted config.
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / v3po / translate / v3po / vpp / BridgeDomainCustomizer.java
index a8f13c6..3bc7ba1 100644 (file)
 
 package io.fd.honeycomb.v3po.translate.v3po.vpp;
 
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils.booleanToByte;
+
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer;
@@ -24,6 +28,8 @@ import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
 import io.fd.honeycomb.v3po.translate.write.WriteContext;
 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
+import java.util.List;
+import javax.annotation.Nonnull;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomainKey;
@@ -36,16 +42,9 @@ import org.openvpp.jvpp.future.FutureJVpp;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-import java.util.List;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkNotNull;
-
 public class BridgeDomainCustomizer
-        extends FutureJVppCustomizer
-        implements ListWriterCustomizer<BridgeDomain, BridgeDomainKey> {
+    extends FutureJVppCustomizer
+    implements ListWriterCustomizer<BridgeDomain, BridgeDomainKey> {
 
     private static final Logger LOG = LoggerFactory.getLogger(BridgeDomainCustomizer.class);
 
@@ -60,12 +59,12 @@ public class BridgeDomainCustomizer
     @Nonnull
     @Override
     public Optional<List<BridgeDomain>> extract(@Nonnull final InstanceIdentifier<BridgeDomain> currentId,
-                                      @Nonnull final DataObject parentData) {
+                                                @Nonnull final DataObject parentData) {
         return Optional.fromNullable(((BridgeDomains) parentData).getBridgeDomain());
     }
 
     private BridgeDomainAddDelReply addOrUpdateBridgeDomain(final int bdId, @Nonnull final BridgeDomain bd)
-                throws VppBaseCallException {
+        throws VppBaseCallException {
         final BridgeDomainAddDelReply reply;
         final BridgeDomainAddDel request = new BridgeDomainAddDel();
         request.bdId = bdId;
@@ -84,16 +83,23 @@ public class BridgeDomainCustomizer
     @Override
     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
                                        @Nonnull final BridgeDomain dataBefore,
-                                       @Nonnull final WriteContext ctx) throws WriteFailedException.CreateFailedException {
+                                       @Nonnull final WriteContext ctx)
+        throws WriteFailedException.CreateFailedException {
         LOG.debug("writeCurrentAttributes: id={}, current={}, ctx={}", id, dataBefore, ctx);
         final String bdName = dataBefore.getName();
 
         try {
-            // FIXME we need the bd index to be returned by VPP or we should have a counter field (maybe in context similar to artificial name)
-            // Here we assign the next available ID from bdContext's perspective
-            int index = 1;
-            while(bdContext.containsName(index, ctx.getMappingContext())) {
-                index++;
+            int index;
+            if (bdContext.containsIndex(bdName, ctx.getMappingContext())) {
+                index = bdContext.getIndex(bdName, ctx.getMappingContext());
+            } else {
+                // FIXME we need the bd index to be returned by VPP or we should have a counter field
+                // (maybe in context similar to artificial name)
+                // Here we assign the next available ID from bdContext's perspective
+                index = 1;
+                while (bdContext.containsName(index, ctx.getMappingContext())) {
+                    index++;
+                }
             }
             addOrUpdateBridgeDomain(index, dataBefore);
             bdContext.addName(index, bdName, ctx.getMappingContext());
@@ -103,43 +109,38 @@ public class BridgeDomainCustomizer
         }
     }
 
-    private byte booleanToByte(@Nullable final Boolean aBoolean) {
-        return aBoolean != null && aBoolean
-                ? (byte) 1
-                : (byte) 0;
-    }
-
     @Override
     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
                                         @Nonnull final BridgeDomain dataBefore,
-                                        @Nonnull final WriteContext ctx) throws WriteFailedException.DeleteFailedException {
+                                        @Nonnull final WriteContext ctx)
+        throws WriteFailedException.DeleteFailedException {
         LOG.debug("deleteCurrentAttributes: id={}, dataBefore={}, ctx={}", id, dataBefore, ctx);
         final String bdName = id.firstKeyOf(BridgeDomain.class).getName();
-        int bdId = bdId = bdContext.getIndex(bdName, ctx.getMappingContext());
+        int bdId = bdContext.getIndex(bdName, ctx.getMappingContext());
         try {
 
             final BridgeDomainAddDel request = new BridgeDomainAddDel();
             request.bdId = bdId;
 
-            final BridgeDomainAddDelReply reply =
-                    TranslateUtils.getReply(getFutureJVpp().bridgeDomainAddDel(request).toCompletableFuture());
+            TranslateUtils.getReply(getFutureJVpp().bridgeDomainAddDel(request).toCompletableFuture());
             LOG.debug("Bridge domain {} (id={}) deleted successfully", bdName, bdId);
         } catch (VppBaseCallException e) {
             LOG.warn("Bridge domain {} (id={}) delete failed", bdName, bdId);
-            throw new WriteFailedException.DeleteFailedException(id,e);
+            throw new WriteFailedException.DeleteFailedException(id, e);
         }
     }
 
     @Override
     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<BridgeDomain> id,
                                         @Nonnull final BridgeDomain dataBefore, @Nonnull final BridgeDomain dataAfter,
-                                        @Nonnull final WriteContext ctx) throws WriteFailedException.UpdateFailedException {
+                                        @Nonnull final WriteContext ctx)
+        throws WriteFailedException.UpdateFailedException {
         LOG.debug("updateCurrentAttributes: id={}, dataBefore={}, dataAfter={}, ctx={}", id, dataBefore, dataAfter,
-                ctx);
+            ctx);
 
         final String bdName = checkNotNull(dataAfter.getName());
         checkArgument(bdName.equals(dataBefore.getName()),
-                "BridgeDomain name changed. It should be deleted and then created.");
+            "BridgeDomain name changed. It should be deleted and then created.");
 
         try {
             addOrUpdateBridgeDomain(bdContext.getIndex(bdName, ctx.getMappingContext()), dataAfter);