diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2017-02-27 09:54:18 +0100 |
---|---|---|
committer | Marek Gradzki <mgradzki@cisco.com> | 2017-02-27 12:07:31 +0100 |
commit | 691dfba001163ebad7d43f287bf9e6ffe6bf451d (patch) | |
tree | 1b6757c3b79742f7e8c9542d0db7b82623dc6ba8 /dhcp/dhcp-impl/src/main/java | |
parent | 06e02d8840109eaa8a97946529a37003dd9e6059 (diff) |
HC2VPP-85: DHCP relay initializing reader
Change-Id: Iab91aab6103b9d837a5a0c73e2836807f24d1f14
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'dhcp/dhcp-impl/src/main/java')
3 files changed, 215 insertions, 1 deletions
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<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 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> 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 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<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); + } +} |