From 36d68584f46032b29eb7fc502a8c6fb11a7ee59b Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Mon, 24 Oct 2016 16:14:37 +0200 Subject: HONEYCOMB-248 Enable in/out NAT feature read Change-Id: I6fe57b955437d0b0024323bcbac268f0ed4799f6 Signed-off-by: Maros Marsalek --- .../read/ifc/InterfaceOutboundNatCustomizer.java | 111 +++++++++++++++++++-- 1 file changed, 100 insertions(+), 11 deletions(-) (limited to 'nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java') diff --git a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java index cb0103111..b08a636ca 100644 --- a/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java +++ b/nat/nat2vpp/src/main/java/io/fd/honeycomb/nat/read/ifc/InterfaceOutboundNatCustomizer.java @@ -17,38 +17,127 @@ 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.ReaderCustomizer; +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; +import io.fd.vpp.jvpp.snat.dto.SnatInterfaceDetails; +import io.fd.vpp.jvpp.snat.dto.SnatInterfaceDetailsReplyDump; import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214.NatInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.Nat; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.nat.rev161214._interface.nat.attributes.NatBuilder; 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; import org.slf4j.LoggerFactory; -final class InterfaceOutboundNatCustomizer implements ReaderCustomizer { +final class InterfaceOutboundNatCustomizer extends AbstractInterfaceNatCustomizer { private static final Logger LOG = LoggerFactory.getLogger(InterfaceOutboundNatCustomizer.class); - @Nonnull + InterfaceOutboundNatCustomizer( + @Nonnull final DumpCacheManager dumpMgr, + @Nonnull final NamingContext ifcContext) { + super(dumpMgr, ifcContext); + } + @Override - public OutboundBuilder getBuilder(@Nonnull final InstanceIdentifier id) { - return new OutboundBuilder(); + protected Logger getLog() { + return LOG; } @Override - public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, - @Nonnull final OutboundBuilder builder, @Nonnull final ReadContext ctx) - throws ReadFailedException { - // FIXME HONEYCOMB-248 VPP-459 Implement when read is available in VPP/Snat - LOG.debug("Unable to read Outbound NAT feature state for an interface"); + void setBuilderPresence(@Nonnull final OutboundBuilder builder) { + ((PresenceOutboundBuilder) builder).setPresent(true); + } + + @Override + boolean isExpectedNatType(final SnatInterfaceDetails snatInterfaceDetails) { + return snatInterfaceDetails.isInside == 0; + } + + @Nonnull + @Override + public OutboundBuilder getBuilder(@Nonnull final InstanceIdentifier id) { + return new PresenceOutboundBuilder(false); } @Override public void merge(@Nonnull final Builder parentBuilder, @Nonnull final Outbound readValue) { ((NatBuilder) parentBuilder).setOutbound(readValue); } + + @Nonnull + @Override + public Initialized init(@Nonnull final InstanceIdentifier id, + @Nonnull final Outbound readValue, + @Nonnull final ReadContext ctx) { + final InstanceIdentifier cfgId = + InstanceIdentifier.create(Interfaces.class) + .child(Interface.class, + new InterfaceKey(id.firstKeyOf( + org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class).getName())) + .augmentation(NatInterfaceAugmentation.class) + .child(Nat.class) + .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 getAugmentation(final Class augmentationType) { + throw new UnsupportedOperationException(); + } + + @Override + public Class getImplementedInterface() { + return Outbound.class; + } + + @Override + public boolean equals(final Object obj) { + // This is necessary to fake this.equals(something) + return obj == NOT_PRESENT_OUTBOUND; + } + } } -- cgit 1.2.3-korg