From 691dfba001163ebad7d43f287bf9e6ffe6bf451d Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Mon, 27 Feb 2017 09:54:18 +0100 Subject: HC2VPP-85: DHCP relay initializing reader Change-Id: Iab91aab6103b9d837a5a0c73e2836807f24d1f14 Signed-off-by: Marek Gradzki --- .../main/java/io/fd/hc2vpp/dhcp/DhcpModule.java | 7 +- .../io/fd/hc2vpp/dhcp/read/DhcpReaderFactory.java | 50 +++++++ .../fd/hc2vpp/dhcp/read/DhcpRelayCustomizer.java | 159 +++++++++++++++++++++ 3 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpReaderFactory.java create mode 100644 dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizer.java (limited to 'dhcp/dhcp-impl/src/main/java/io/fd') diff --git a/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/DhcpModule.java b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/DhcpModule.java index 09514df9b..7063d0b4e 100644 --- a/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/DhcpModule.java +++ b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/DhcpModule.java @@ -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 writerFactoryBinder = Multibinder.newSetBinder(binder(), WriterFactory.class); writerFactoryBinder.addBinding().to(DhcpWriterFactory.class); + LOG.info("Injecting readers factories"); + final Multibinder 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 index 000000000..db719061b --- /dev/null +++ b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpReaderFactory.java @@ -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_ID = InstanceIdentifier.create(Dhcp.class); + private static final InstanceIdentifier RELAYS_ID = DHCP_ID.child(Relays.class); + private static final InstanceIdentifier 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 index 000000000..db7e7141c --- /dev/null +++ b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/read/DhcpRelayCustomizer.java @@ -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, + JvppReplyConsumer, ByteDataTranslator, Ipv6Translator, Ipv4Translator { + + private final DumpCacheManager dumpManager; + + DhcpRelayCustomizer(final FutureJVppCore vppApi) { + super(vppApi); + dumpManager = new DumpCacheManager.DumpCacheManagerBuilder() + .withExecutor(executor()) + .acceptOnly(DhcpProxyDetailsReplyDump.class) + .build(); + } + + private EntityDumpExecutor executor() { + return (id, param) -> { + DhcpProxyDump request = new DhcpProxyDump(); + request.isIp6 = 1; + + final CompletionStage 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 getAllIds(@Nonnull final InstanceIdentifier id, @Nonnull final ReadContext context) + throws ReadFailedException { + Collections.emptyList(); + + final Optional 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 builder, @Nonnull final List readData) { + ((RelaysBuilder) builder).setRelay(readData); + } + + @Nonnull + @Override + public RelayBuilder getBuilder(@Nonnull final InstanceIdentifier id) { + return new RelayBuilder(); + } + + @Override + public void readCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final RelayBuilder builder, + @Nonnull final ReadContext ctx) throws ReadFailedException { + final Optional 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 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 init(@Nonnull final InstanceIdentifier id, + @Nonnull final Relay readValue, + @Nonnull final ReadContext ctx) { + return Initialized.create(id, readValue); + } +} -- cgit 1.2.3-korg