HC2VPP-85: DHCP relay initializing reader 33/5533/1
authorMarek Gradzki <[email protected]>
Mon, 27 Feb 2017 08:54:18 +0000 (09:54 +0100)
committerMarek Gradzki <[email protected]>
Mon, 27 Feb 2017 11:07:31 +0000 (12:07 +0100)
Change-Id: Iab91aab6103b9d837a5a0c73e2836807f24d1f14
Signed-off-by: Marek Gradzki <[email protected]>
dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/DhcpModule.java
dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpReaderFactory.java [new file with mode: 0644]
dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizer.java [new file with mode: 0644]
dhcp/dhcp-impl/src/test/java/io/fd/hc2vpp/dhcp/DhcpModuleTest.java
dhcp/dhcp-impl/src/test/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizerTest.java [new file with mode: 0644]

index 09514df..7063d0b 100644 (file)
@@ -18,7 +18,9 @@ package io.fd.hc2vpp.dhcp;
 
 import com.google.inject.AbstractModule;
 import com.google.inject.multibindings.Multibinder;
+import io.fd.hc2vpp.dhcp.read.DhcpReaderFactory;
 import io.fd.hc2vpp.dhcp.write.DhcpWriterFactory;
+import io.fd.honeycomb.translate.read.ReaderFactory;
 import io.fd.honeycomb.translate.write.WriterFactory;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -35,10 +37,13 @@ public final class DhcpModule extends AbstractModule {
         LOG.info("Installing DHCP module");
 
         LOG.info("Injecting writers factories");
-        // create writer factory binding
         final Multibinder<WriterFactory> writerFactoryBinder = Multibinder.newSetBinder(binder(), WriterFactory.class);
         writerFactoryBinder.addBinding().to(DhcpWriterFactory.class);
 
+        LOG.info("Injecting readers factories");
+        final Multibinder<ReaderFactory> readerFactoryBinder = Multibinder.newSetBinder(binder(), ReaderFactory.class);
+        readerFactoryBinder.addBinding().to(DhcpReaderFactory.class);
+
         LOG.info("Module DHCP successfully configured");
     }
 }
diff --git a/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpReaderFactory.java b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpReaderFactory.java
new file mode 100644 (file)
index 0000000..db71906
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.hc2vpp.dhcp.read;
+
+import com.google.inject.Inject;
+import io.fd.honeycomb.translate.impl.read.GenericInitListReader;
+import io.fd.honeycomb.translate.read.ReaderFactory;
+import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
+import io.fd.vpp.jvpp.core.future.FutureJVppCore;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.Dhcp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.DhcpBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.Relays;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.RelaysBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.Relay;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Factory producing writers for DHCP plugin's data.
+ */
+public final class DhcpReaderFactory implements ReaderFactory {
+
+    private static final InstanceIdentifier<Dhcp> DHCP_ID = InstanceIdentifier.create(Dhcp.class);
+    private static final InstanceIdentifier<Relays> RELAYS_ID = DHCP_ID.child(Relays.class);
+    private static final InstanceIdentifier<Relay> RELAY_ID = RELAYS_ID.child(Relay.class);
+
+    @Inject
+    private FutureJVppCore vppApi;
+
+    @Override
+    public void init(@Nonnull final ModifiableReaderRegistryBuilder registry) {
+        registry.addStructuralReader(DHCP_ID, DhcpBuilder.class);
+        registry.addStructuralReader(RELAYS_ID, RelaysBuilder.class);
+        registry.add(new GenericInitListReader<>(RELAY_ID, new io.fd.hc2vpp.dhcp.read.DhcpRelayCustomizer(vppApi)));
+    }
+}
diff --git a/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizer.java b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizer.java
new file mode 100644 (file)
index 0000000..db7e714
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.hc2vpp.dhcp.read;
+
+import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;
+
+import com.google.common.base.Optional;
+import com.google.common.primitives.UnsignedInts;
+import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
+import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
+import io.fd.hc2vpp.common.translate.util.Ipv4Translator;
+import io.fd.hc2vpp.common.translate.util.Ipv6Translator;
+import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
+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.spi.read.InitializingListReaderCustomizer;
+import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
+import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
+import io.fd.vpp.jvpp.core.dto.DhcpProxyDetails;
+import io.fd.vpp.jvpp.core.dto.DhcpProxyDetailsReplyDump;
+import io.fd.vpp.jvpp.core.dto.DhcpProxyDump;
+import io.fd.vpp.jvpp.core.future.FutureJVppCore;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CompletionStage;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.Ipv4;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.Ipv6;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.RelaysBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.Relay;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.RelayBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.RelayKey;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+final class DhcpRelayCustomizer extends FutureJVppCustomizer
+    implements InitializingListReaderCustomizer<Relay, RelayKey, RelayBuilder>,
+    JvppReplyConsumer, ByteDataTranslator, Ipv6Translator, Ipv4Translator {
+
+    private final DumpCacheManager<DhcpProxyDetailsReplyDump, Void> dumpManager;
+
+    DhcpRelayCustomizer(final FutureJVppCore vppApi) {
+        super(vppApi);
+        dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<DhcpProxyDetailsReplyDump, Void>()
+            .withExecutor(executor())
+            .acceptOnly(DhcpProxyDetailsReplyDump.class)
+            .build();
+    }
+
+    private EntityDumpExecutor<DhcpProxyDetailsReplyDump, Void> executor() {
+        return (id, param) -> {
+            DhcpProxyDump request = new DhcpProxyDump();
+            request.isIp6 = 1;
+
+            final CompletionStage<DhcpProxyDetailsReplyDump> result = getFutureJVpp().dhcpProxyDump(new DhcpProxyDump())
+                .thenCombine(getFutureJVpp().dhcpProxyDump(request),
+                (ip4, ip6) -> {
+                    ip4.dhcpProxyDetails.addAll(ip6.dhcpProxyDetails);
+                    return ip4;
+                });
+            return getReplyForRead(result.toCompletableFuture(), id);
+        };
+    }
+
+    @Nonnull
+    @Override
+    public List<RelayKey> getAllIds(@Nonnull final InstanceIdentifier<Relay> id, @Nonnull final ReadContext context)
+        throws ReadFailedException {
+        Collections.emptyList();
+
+        final Optional<DhcpProxyDetailsReplyDump> dump =
+            dumpManager.getDump(id, context.getModificationCache(), NO_PARAMS);
+
+        if (!dump.isPresent() || dump.get().dhcpProxyDetails.isEmpty()) {
+            return Collections.emptyList();
+        }
+
+        return dump.get().dhcpProxyDetails.stream().map(detail -> new RelayKey(detail.isIpv6 == 1
+            ? Ipv6.class
+            : Ipv4.class,
+            UnsignedInts.toLong(detail.rxVrfId))).collect(Collectors.toList());
+    }
+
+    @Override
+    public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<Relay> readData) {
+        ((RelaysBuilder) builder).setRelay(readData);
+    }
+
+    @Nonnull
+    @Override
+    public RelayBuilder getBuilder(@Nonnull final InstanceIdentifier<Relay> id) {
+        return new RelayBuilder();
+    }
+
+    @Override
+    public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Relay> id, @Nonnull final RelayBuilder builder,
+                                      @Nonnull final ReadContext ctx) throws ReadFailedException {
+        final Optional<DhcpProxyDetailsReplyDump> dump =
+            dumpManager.getDump(id, ctx.getModificationCache(), NO_PARAMS);
+
+        if (!dump.isPresent() || dump.get().dhcpProxyDetails.isEmpty()) {
+            return;
+        }
+
+        final RelayKey key = id.firstKeyOf(Relay.class);
+
+        final byte isIpv6 = (byte) (Ipv6.class == key.getAddressType()
+            ? 1
+            : 0);
+        final int rxVrfId = key.getRxVrfId().intValue();
+
+        final java.util.Optional<DhcpProxyDetails> result =
+            dump.get().dhcpProxyDetails.stream().filter(d -> d.isIpv6 == isIpv6 && d.rxVrfId == rxVrfId).findFirst();
+
+        if (result.isPresent()) {
+            final DhcpProxyDetails detail = result.get();
+            builder.setAddressType(key.getAddressType());
+            builder.setRxVrfId(key.getRxVrfId());
+            final boolean isIp6 = byteToBoolean(detail.isIpv6);
+            builder.setGatewayAddress(readAddress(detail.dhcpSrcAddress, isIp6));
+            builder.setServerAddress(readAddress(detail.dhcpServer, isIp6));
+            builder.setServerVrfId(UnsignedInts.toLong(detail.serverVrfId));
+        }
+    }
+
+    private IpAddress readAddress(final byte[] ip, final boolean isIp6) {
+        if (isIp6) {
+            return new IpAddress(arrayToIpv6AddressNoZone(ip));
+        } else {
+            return new IpAddress(arrayToIpv4AddressNoZone(ip));
+        }
+    }
+
+    @Nonnull
+    @Override
+    public Initialized<? extends DataObject> init(@Nonnull final InstanceIdentifier<Relay> id,
+                                                  @Nonnull final Relay readValue,
+                                                  @Nonnull final ReadContext ctx) {
+        return Initialized.create(id, readValue);
+    }
+}
index 998f75f..ad955a2 100644 (file)
@@ -30,8 +30,11 @@ import com.google.inject.Inject;
 import com.google.inject.name.Named;
 import com.google.inject.testing.fieldbinder.Bind;
 import com.google.inject.testing.fieldbinder.BoundFieldModule;
+import io.fd.hc2vpp.dhcp.read.DhcpReaderFactory;
 import io.fd.hc2vpp.dhcp.write.DhcpWriterFactory;
+import io.fd.honeycomb.translate.impl.read.registry.CompositeReaderRegistryBuilder;
 import io.fd.honeycomb.translate.impl.write.registry.FlatWriterRegistryBuilder;
+import io.fd.honeycomb.translate.read.ReaderFactory;
 import io.fd.honeycomb.translate.write.WriterFactory;
 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
 import java.util.HashSet;
@@ -60,6 +63,9 @@ public class DhcpModuleTest {
     @Inject
     private Set<WriterFactory> writerFactories = new HashSet<>();
 
+    @Inject
+    private Set<ReaderFactory> readerFactories = new HashSet<>();
+
     @Before
     public void setUp() {
         initMocks(this);
@@ -77,4 +83,16 @@ public class DhcpModuleTest {
         assertEquals(1, writerFactories.size());
         assertTrue(writerFactories.iterator().next() instanceof DhcpWriterFactory);
     }
+
+    @Test
+    public void testReaderFactories() throws Exception {
+        assertThat(readerFactories, is(not(empty())));
+
+        // Test registration process (all dependencies present, topological order of readers does exist, etc.)
+        final CompositeReaderRegistryBuilder registryBuilder = new CompositeReaderRegistryBuilder();
+        readerFactories.stream().forEach(factory -> factory.init(registryBuilder));
+        assertNotNull(registryBuilder.build());
+        assertEquals(1, readerFactories.size());
+        assertTrue(readerFactories.iterator().next() instanceof DhcpReaderFactory);
+    }
 }
diff --git a/dhcp/dhcp-impl/src/test/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizerTest.java b/dhcp/dhcp-impl/src/test/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizerTest.java
new file mode 100644 (file)
index 0000000..79a1f44
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.hc2vpp.dhcp.read;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+import io.fd.hc2vpp.common.test.read.InitializingListReaderCustomizerTest;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
+import io.fd.vpp.jvpp.core.dto.DhcpProxyDetails;
+import io.fd.vpp.jvpp.core.dto.DhcpProxyDetailsReplyDump;
+import io.fd.vpp.jvpp.core.dto.DhcpProxyDump;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.Dhcp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.Ipv4;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.Ipv6;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.Relays;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.RelaysBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.Relay;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.RelayBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.RelayKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
+
+public class DhcpRelayCustomizerTest extends InitializingListReaderCustomizerTest<Relay, RelayKey, RelayBuilder> {
+    public DhcpRelayCustomizerTest() {
+        super(Relay.class, RelaysBuilder.class);
+    }
+
+    private static InstanceIdentifier<Relays> RELAYS = InstanceIdentifier.create(Dhcp.class).child(Relays.class);
+
+    private KeyedInstanceIdentifier<Relay, RelayKey> IP4_IID = RELAYS.child(Relay.class, new RelayKey(Ipv4.class, 123L));
+    private KeyedInstanceIdentifier<Relay, RelayKey> IP6_IID = RELAYS.child(Relay.class, new RelayKey(Ipv6.class, 321L));
+
+    @Override
+    protected ReaderCustomizer<Relay, RelayBuilder> initCustomizer() {
+        return new DhcpRelayCustomizer(api);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        final DhcpProxyDetailsReplyDump ip4 = new DhcpProxyDetailsReplyDump();
+        final DhcpProxyDetails ip4Proxy = new DhcpProxyDetails();
+        ip4Proxy.rxVrfId = 123;
+        ip4Proxy.dhcpSrcAddress = new byte[]{1,2,3,4};
+        ip4Proxy.serverVrfId = 11;
+        ip4Proxy.dhcpServer = new byte[]{8,8,8,8};
+        ip4.dhcpProxyDetails.add(ip4Proxy);
+        when(api.dhcpProxyDump(new DhcpProxyDump())).thenReturn(future(ip4));
+
+        final DhcpProxyDetailsReplyDump ip6 = new DhcpProxyDetailsReplyDump();
+        final DhcpProxyDetails ip6Proxy = new DhcpProxyDetails();
+        ip6Proxy.rxVrfId = 321;
+        // 2001:0db8:0a0b:12f0:0000:0000:0000:0001
+        ip6Proxy.dhcpSrcAddress = new byte[] {0x20, 0x01, 0x0d, (byte) 0xb8, 0x0a, 0x0b, 0x12, (byte) 0xf0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0x01};
+        ip6Proxy.serverVrfId = 22;
+        ip6Proxy.isIpv6  = 1;
+        // 2001:0db8:0a0b:12f0:0000:0000:0000:0002
+        ip6Proxy.dhcpServer = new byte[] {0x20, 0x01, 0x0d, (byte) 0xb8, 0x0a, 0x0b, 0x12, (byte) 0xf0, 0 ,0 ,0 ,0 ,0 ,0 ,0 ,0x02};
+
+        final DhcpProxyDump ip6Dump = new DhcpProxyDump();
+        ip6Dump.isIp6 = 1;
+        ip6.dhcpProxyDetails.add(ip6Proxy);
+        when(api.dhcpProxyDump(ip6Dump)).thenReturn(future(ip6));
+    }
+
+    @Test
+    public void testGetAllIds() throws ReadFailedException {
+        final List<RelayKey> allIds = getCustomizer().getAllIds(RELAYS.child(Relay.class), ctx);
+        assertEquals(2, allIds.size());
+        assertThat(allIds, containsInAnyOrder(IP4_IID.getKey(), IP6_IID.getKey()));
+    }
+
+    @Test
+    public void testReadIp4() throws ReadFailedException {
+        final RelayBuilder builder = new RelayBuilder();
+        getCustomizer().readCurrentAttributes(IP4_IID, builder, ctx);
+        assertEquals(IP4_IID.getKey().getAddressType(), builder.getAddressType());
+        assertEquals(IP4_IID.getKey().getRxVrfId(), builder.getRxVrfId());
+        assertEquals(11L, builder.getServerVrfId().longValue());
+        assertArrayEquals("1.2.3.4".toCharArray(), builder.getGatewayAddress().getValue());
+        assertArrayEquals("8.8.8.8".toCharArray(), builder.getServerAddress().getValue());
+    }
+
+    @Test
+    public void testReadIp6() throws ReadFailedException {
+        final RelayBuilder builder = new RelayBuilder();
+        getCustomizer().readCurrentAttributes(IP6_IID, builder, ctx);
+        assertEquals(IP6_IID.getKey().getAddressType(), builder.getAddressType());
+        assertEquals(IP6_IID.getKey().getRxVrfId(), builder.getRxVrfId());
+        assertEquals(22L, builder.getServerVrfId().longValue());
+        assertArrayEquals("2001:db8:a0b:12f0::1".toCharArray(), builder.getGatewayAddress().getValue());
+        assertArrayEquals("2001:db8:a0b:12f0::2".toCharArray(), builder.getServerAddress().getValue());
+    }
+
+    @Test
+    public void testInit() {
+        final Relay data = new RelayBuilder().build();
+        invokeInitTest(IP4_IID, data, IP4_IID, data);
+    }
+}
\ No newline at end of file