diff options
Diffstat (limited to 'dhcp/dhcp-impl/src/main')
4 files changed, 66 insertions, 16 deletions
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 index db719061b..2f71866d3 100644 --- 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 @@ -16,6 +16,7 @@ package io.fd.hc2vpp.dhcp.read; +import com.google.common.collect.ImmutableSet; import com.google.inject.Inject; import io.fd.honeycomb.translate.impl.read.GenericInitListReader; import io.fd.honeycomb.translate.read.ReaderFactory; @@ -27,6 +28,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp 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.relay.attributes.Server; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** @@ -45,6 +47,9 @@ public final class DhcpReaderFactory implements ReaderFactory { 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))); + registry.subtreeAdd( + ImmutableSet.of(InstanceIdentifier.create(Relay.class).child(Server.class)), + 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 index db7e7141c..875b93933 100644 --- 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 @@ -35,6 +35,7 @@ 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.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.CompletionStage; @@ -47,6 +48,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp 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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.relay.attributes.ServerBuilder; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; @@ -72,10 +74,10 @@ final class DhcpRelayCustomizer extends FutureJVppCustomizer final CompletionStage<DhcpProxyDetailsReplyDump> result = getFutureJVpp().dhcpProxyDump(new DhcpProxyDump()) .thenCombine(getFutureJVpp().dhcpProxyDump(request), - (ip4, ip6) -> { - ip4.dhcpProxyDetails.addAll(ip6.dhcpProxyDetails); - return ip4; - }); + (ip4, ip6) -> { + ip4.dhcpProxyDetails.addAll(ip6.dhcpProxyDetails); + return ip4; + }); return getReplyForRead(result.toCompletableFuture(), id); }; } @@ -136,8 +138,14 @@ final class DhcpRelayCustomizer extends FutureJVppCustomizer 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)); + if (detail.servers != null) { + builder.setServer(Arrays.stream(detail.servers).map( + server -> new ServerBuilder() + .setAddress(readAddress(server.dhcpServer, isIp6)) + .setVrfId(UnsignedInts.toLong(server.serverVrfId)) + .build() + ).collect(Collectors.toList())); + } } } diff --git a/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/write/DhcpRelayCustomizer.java b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/write/DhcpRelayCustomizer.java index 58e3d5811..cb9939228 100644 --- a/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/write/DhcpRelayCustomizer.java +++ b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/write/DhcpRelayCustomizer.java @@ -16,6 +16,8 @@ package io.fd.hc2vpp.dhcp.write; +import static com.google.common.base.Preconditions.checkArgument; + import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.Ipv4Translator; @@ -26,11 +28,13 @@ import io.fd.honeycomb.translate.write.WriteContext; import io.fd.honeycomb.translate.write.WriteFailedException; import io.fd.vpp.jvpp.core.dto.DhcpProxyConfig; import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import java.util.List; 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.Ipv6; 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.RelayKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.relay.attributes.Server; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +51,10 @@ final class DhcpRelayCustomizer extends FutureJVppCustomizer implements ListWrit public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Relay> id, @Nonnull final Relay dataAfter, @Nonnull final WriteContext writeContext) throws WriteFailedException { LOG.debug("Writing Relay {} dataAfter={}", id, dataAfter); - setRelay(id, dataAfter, writeContext, true); + checkArgument(dataAfter.getServer() != null && !dataAfter.getServer().isEmpty(), "At least one DHCP server needs to be configured"); + for (final Server server : dataAfter.getServer()) { + setRelay(id, dataAfter, server, true); + } } @Override @@ -55,26 +62,50 @@ final class DhcpRelayCustomizer extends FutureJVppCustomizer implements ListWrit @Nonnull final Relay dataAfter, @Nonnull final WriteContext writeContext) throws WriteFailedException { LOG.debug("Updating Relay {} before={} after={}", id, dataBefore, dataAfter); - setRelay(id, dataAfter, writeContext, true); + final List<Server> serversBefore = dataBefore.getServer(); + checkArgument(serversBefore != null && !serversBefore.isEmpty(), + "At least one DHCP server needs to be configured before update operation"); + final List<Server> serversAfter = dataAfter.getServer(); + checkArgument(serversAfter != null && !serversAfter.isEmpty(), + "At least one DHCP server needs to be configured after update operation"); + + // remove old servers (we do not expect many, so no need for efficient search): + for (final Server server : serversBefore) { + if (!serversAfter.contains(server)) { + setRelay(id, dataAfter, server, false); + } + } + // and add new ones: + for (final Server server : serversAfter) { + if (!serversBefore.contains(server)) { + setRelay(id, dataAfter, server, true); + } + } } @Override public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Relay> id, @Nonnull final Relay dataBefore, @Nonnull final WriteContext writeContext) throws WriteFailedException { LOG.debug("Removing Relay {} dataBefore={}", id, dataBefore); - setRelay(id, dataBefore, writeContext, false); + checkArgument(dataBefore.getServer() != null && !dataBefore.getServer().isEmpty(), + "At least one DHCP server needs to be configured"); + for (final Server server : dataBefore.getServer()) { + setRelay(id, dataBefore, server, false); + } } - private void setRelay(final InstanceIdentifier<Relay> id, final Relay relay, final WriteContext writeContext, - final boolean isAdd) throws WriteFailedException { + private void setRelay(final InstanceIdentifier<Relay> id, final Relay relay, final Server server, + final boolean isAdd) + throws WriteFailedException { final DhcpProxyConfig request = new DhcpProxyConfig(); request.rxVrfId = relay.getRxVrfId().byteValue(); final boolean isIpv6 = Ipv6.class == relay.getAddressType(); request.isIpv6 = booleanToByte(isIpv6); - request.serverVrfId = relay.getServerVrfId().intValue(); + request.serverVrfId = server.getVrfId().intValue(); request.isAdd = booleanToByte(isAdd); - request.dhcpServer = parseAddress(relay.getServerAddress(), isIpv6); + request.dhcpServer = parseAddress(server.getAddress(), isIpv6); request.dhcpSrcAddress = parseAddress(relay.getGatewayAddress(), isIpv6); + LOG.debug("DHCP config change id={} request={}", id, request); getReplyForWrite(getFutureJVpp().dhcpProxyConfig(request).toCompletableFuture(), id); } diff --git a/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/write/DhcpWriterFactory.java b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/write/DhcpWriterFactory.java index 93dc8048e..062826aaf 100644 --- a/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/write/DhcpWriterFactory.java +++ b/dhcp/dhcp-impl/src/main/java/io/fd/hc2vpp/dhcp/write/DhcpWriterFactory.java @@ -16,6 +16,7 @@ package io.fd.hc2vpp.dhcp.write; +import com.google.common.collect.ImmutableSet; import com.google.inject.Inject; import io.fd.honeycomb.translate.impl.write.GenericListWriter; import io.fd.honeycomb.translate.write.WriterFactory; @@ -25,6 +26,7 @@ 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.dhcp.attributes.Relays; 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.relay.attributes.Server; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /** @@ -32,13 +34,17 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; */ public final class DhcpWriterFactory implements WriterFactory { - private static final InstanceIdentifier<Relay> RELAY_ID = InstanceIdentifier.create(Dhcp.class).child(Relays.class).child(Relay.class); + private static final InstanceIdentifier<Relay> RELAY_ID = + InstanceIdentifier.create(Dhcp.class).child(Relays.class).child(Relay.class); @Inject private FutureJVppCore vppApi; @Override public void init(@Nonnull final ModifiableWriterRegistryBuilder registry) { - registry.add(new GenericListWriter<>(RELAY_ID, new DhcpRelayCustomizer(vppApi))); + registry.subtreeAdd( + ImmutableSet.of(InstanceIdentifier.create(Relay.class).child(Server.class)), + new GenericListWriter<>(RELAY_ID, new DhcpRelayCustomizer(vppApi)) + ); } } |