HONEYCOMB-270 Remove read (presence) workarounds for nat 21/3721/2
authorMaros Marsalek <mmarsale@cisco.com>
Tue, 8 Nov 2016 11:34:36 +0000 (12:34 +0100)
committerMaros Marsalek <mmarsale@cisco.com>
Tue, 8 Nov 2016 12:17:57 +0000 (13:17 +0100)
Change-Id: Ia0986dbe173123a4dca4f7f3bf65e7ba4851b1b7
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/AbstractInterfaceNatCustomizer.java
nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceInboundNatCustomizer.java
nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java
nat/nat2vpp/src/test/java/io/fd/honeycomb/nat/read/ifc/InterfaceInboundNatCustomizerTest.java

index e48e7eb..f4d4590 100644 (file)
@@ -47,6 +47,11 @@ abstract class AbstractInterfaceNatCustomizer<C extends DataObject, B extends Bu
     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<C> id,
                                       @Nonnull final B builder,
                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
+        // NOOP
+    }
+
+    @Override
+    public boolean isPresent(final InstanceIdentifier<C> id, final C built, final ReadContext ctx) throws ReadFailedException {
         final String ifcName = id.firstKeyOf(Interface.class).getName();
         getLog().debug("Reading NAT features on interface: {}", ifcName);
         final int index = ifcContext.getIndex(ifcName, ctx.getMappingContext());
@@ -56,17 +61,15 @@ abstract class AbstractInterfaceNatCustomizer<C extends DataObject, B extends Bu
                 dumpMgr.getDump(id, ctx.getModificationCache(), null);
 
         // Find entries for current ifc and if is marked as inside set the builder to return presence container
-        dump.or(new SnatInterfaceDetailsReplyDump()).snatInterfaceDetails.stream()
+        return dump.or(new SnatInterfaceDetailsReplyDump()).snatInterfaceDetails.stream()
                 .filter(snatIfcDetail -> snatIfcDetail.swIfIndex == index)
                 .filter(this::isExpectedNatType)
                 .findFirst()
-                .ifPresent(snatIfcDetail -> setBuilderPresence(builder));
+                .isPresent();
         // Not setting data, just marking the builder to propagate empty container to indicate presence
     }
 
     protected abstract Logger getLog();
 
-    abstract void setBuilderPresence(@Nonnull final B builder);
-
     abstract boolean isExpectedNatType(final SnatInterfaceDetails snatInterfaceDetails);
 }
index 52467a1..3e4b60f 100644 (file)
@@ -17,6 +17,7 @@
 package io.fd.honeycomb.nat.read.ifc;
 
 import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
 import io.fd.honeycomb.translate.spi.read.Initialized;
 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
 import io.fd.honeycomb.translate.vpp.util.NamingContext;
@@ -32,8 +33,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interfa
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Inbound;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.InboundBuilder;
 import org.opendaylight.yangtools.concepts.Builder;
-import org.opendaylight.yangtools.yang.binding.Augmentation;
-import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -55,8 +54,10 @@ final class InterfaceInboundNatCustomizer extends AbstractInterfaceNatCustomizer
     }
 
     @Override
-    void setBuilderPresence(@Nonnull final InboundBuilder builder) {
-        ((PresenceInboundBuilder) builder).setPresent(true);
+    public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Inbound> id,
+                                      @Nonnull final InboundBuilder builder, @Nonnull final ReadContext ctx)
+            throws ReadFailedException {
+        super.readCurrentAttributes(id, builder, ctx);
     }
 
     @Override
@@ -68,7 +69,7 @@ final class InterfaceInboundNatCustomizer extends AbstractInterfaceNatCustomizer
     @Override
     public InboundBuilder getBuilder(@Nonnull final InstanceIdentifier<Inbound> id) {
         // Return not present value by default
-        return new PresenceInboundBuilder(false);
+        return new InboundBuilder();
     }
 
     @Override
@@ -91,53 +92,4 @@ final class InterfaceInboundNatCustomizer extends AbstractInterfaceNatCustomizer
                 .child(Inbound.class);
         return Initialized.create(cfgId, readValue);
     }
-
-    // TODO HONEYCOMB-270, make this better, having to fake a builder + value is just exploitation.
-
-    /**
-     * Special Builder to also propagate empty container into the resulting data.
-     */
-    private static final class PresenceInboundBuilder extends InboundBuilder {
-
-        private volatile boolean isPresent = false;
-
-        PresenceInboundBuilder(final boolean isPresent) {
-            this.isPresent = isPresent;
-        }
-
-        void setPresent(final boolean present) {
-            this.isPresent = present;
-        }
-
-        @Override
-        public Inbound build() {
-            return isPresent
-                    ? super.build()
-                    : NotPresentInbound.NOT_PRESENT_INBOUND;
-        }
-    }
-
-    /**
-     * Fake container that returns false on equals.
-     */
-    private static final class NotPresentInbound implements Inbound {
-
-        private static final NotPresentInbound NOT_PRESENT_INBOUND = new NotPresentInbound();
-
-        @Override
-        public <E extends Augmentation<Inbound>> E getAugmentation(final Class<E> augmentationType) {
-            throw new UnsupportedOperationException();
-        }
-
-        @Override
-        public Class<? extends DataContainer> getImplementedInterface() {
-            return Inbound.class;
-        }
-
-        @Override
-        public boolean equals(final Object obj) {
-            // This is necessary to fake this.equals(something)
-            return obj == NOT_PRESENT_INBOUND;
-        }
-    }
 }
index b08a636..da940c8 100644 (file)
@@ -32,8 +32,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interfa
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.Outbound;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.nat.OutboundBuilder;
 import org.opendaylight.yangtools.concepts.Builder;
-import org.opendaylight.yangtools.yang.binding.Augmentation;
-import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
@@ -54,11 +52,6 @@ final class InterfaceOutboundNatCustomizer extends AbstractInterfaceNatCustomize
         return LOG;
     }
 
-    @Override
-    void setBuilderPresence(@Nonnull final OutboundBuilder builder) {
-        ((PresenceOutboundBuilder) builder).setPresent(true);
-    }
-
     @Override
     boolean isExpectedNatType(final SnatInterfaceDetails snatInterfaceDetails) {
         return snatInterfaceDetails.isInside == 0;
@@ -67,7 +60,7 @@ final class InterfaceOutboundNatCustomizer extends AbstractInterfaceNatCustomize
     @Nonnull
     @Override
     public OutboundBuilder getBuilder(@Nonnull final InstanceIdentifier<Outbound> id) {
-        return new PresenceOutboundBuilder(false);
+        return new OutboundBuilder();
     }
 
     @Override
@@ -90,54 +83,4 @@ final class InterfaceOutboundNatCustomizer extends AbstractInterfaceNatCustomize
                         .child(Outbound.class);
         return Initialized.create(cfgId, readValue);
     }
-
-    // TODO HONEYCOMB-270, make this better, having to fake a builder + value is just exploitation.
-
-    /**
-     * Special Builder to also propagate empty container into the resulting data.
-     */
-    private static final class PresenceOutboundBuilder extends OutboundBuilder {
-
-        private volatile boolean isPresent = false;
-
-        PresenceOutboundBuilder(final boolean isPresent) {
-            this.isPresent = isPresent;
-        }
-
-        void setPresent(final boolean present) {
-            this.isPresent = present;
-        }
-
-        @Override
-        public Outbound build() {
-            final Outbound build = super.build();
-            return isPresent
-                    ? build
-                    : NotPresentOutbound.NOT_PRESENT_OUTBOUND;
-        }
-    }
-
-    /**
-     * Fake container that returns false on equals.
-     */
-    private static final class NotPresentOutbound implements Outbound {
-
-        private static final NotPresentOutbound NOT_PRESENT_OUTBOUND = new NotPresentOutbound();
-
-        @Override
-        public <E extends Augmentation<Outbound>> E getAugmentation(final Class<E> augmentationType) {
-            throw new UnsupportedOperationException();
-        }
-
-        @Override
-        public Class<? extends DataContainer> getImplementedInterface() {
-            return Outbound.class;
-        }
-
-        @Override
-        public boolean equals(final Object obj) {
-            // This is necessary to fake this.equals(something)
-            return obj == NOT_PRESENT_OUTBOUND;
-        }
-    }
 }
index 140d35f..3cef188 100644 (file)
@@ -51,7 +51,7 @@ public class InterfaceInboundNatCustomizerTest
     private static final String CTX_NAME = "ifc";
 
     @Mock
-    private EntityDumpExecutor<SnatInterfaceDetailsReplyDump, Void> abc;
+    private EntityDumpExecutor<SnatInterfaceDetailsReplyDump, Void> natExecutor;
     private DumpCacheManager<SnatInterfaceDetailsReplyDump, Void> dumpMgr;
     private NamingContext ifcContext = new NamingContext(CTX_NAME, CTX_NAME);
     private InstanceIdentifier<Inbound> id;
@@ -65,9 +65,9 @@ public class InterfaceInboundNatCustomizerTest
         id = getId(Inbound.class);
         defineMapping(mappingContext, IFC_NAME, IFC_IDX, CTX_NAME);
         // empty dump
-        Mockito.doReturn(new SnatInterfaceDetailsReplyDump()).when(abc).executeDump(id, null);
+        Mockito.doReturn(new SnatInterfaceDetailsReplyDump()).when(natExecutor).executeDump(id, null);
         dumpMgr = new DumpCacheManager.DumpCacheManagerBuilder<SnatInterfaceDetailsReplyDump, Void>()
-                .withExecutor(abc)
+                .withExecutor(natExecutor)
                 .build();
     }
 
@@ -95,7 +95,7 @@ public class InterfaceInboundNatCustomizerTest
         detail.isInside = 1;
         detail.swIfIndex = IFC_IDX;
         details.snatInterfaceDetails = Lists.newArrayList(detail);
-        Mockito.doReturn(details).when(abc).executeDump(id, null);
+        Mockito.doReturn(details).when(natExecutor).executeDump(id, null);
 
         assertTrue(getReader().read(id, ctx).isPresent());
     }