From 3cecf2cd177bbcb87ad0d5eb19e8e0c80a016a5d Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Tue, 7 Feb 2017 13:50:59 +0100 Subject: HC2VPP-11/HC2VPP-12 - refactored ipv4/6 logic/tests - all dumping logic moved under common abstract classes - added tests Change-Id: Ifdee84795bd8cf6f0d29349dd2cfcf9b2bbec1c9 Signed-off-by: Jan Srnicek --- .../hc2vpp/v3po/interfacesstate/ip/IpReader.java | 152 ------------ .../ip/dump/params/IfaceDumpFilter.java | 18 ++ .../ip/readers/IpAddressReader.java | 148 +++++++++++ .../ip/readers/IpNeighbourReader.java | 103 ++++++++ .../v3po/interfacesstate/ip/readers/IpReader.java | 60 +++++ .../ip/v4/Ipv4AddressCustomizer.java | 61 ++--- .../ip/v4/Ipv4NeighbourCustomizer.java | 50 ++-- .../SubInterfaceIpv4AddressCustomizer.java | 45 +--- .../SubInterfaceIpv4NeighbourCustomizer.java | 42 +--- .../ip/v6/Ipv6AddressCustomizer.java | 56 ++--- .../ip/v6/Ipv6NeighbourCustomizer.java | 47 +--- .../SubInterfaceIpv6AddressCustomizer.java | 48 ++-- .../SubInterfaceIpv6NeighbourCustomizer.java | 42 +--- .../interfacesstate/ip/InterfaceChildNodeTest.java | 195 +++++++++++++++ .../ip/Ipv4AddressCustomizerTest.java | 275 --------------------- .../interfacesstate/ip/Ipv4CustomizerTest.java | 45 ---- .../ip/SubInterfaceIpv4AddressCustomizerTest.java | 245 ------------------ .../ip/v4/Ipv4AddressCustomizerTest.java | 269 ++++++++++++++++++++ .../interfacesstate/ip/v4/Ipv4CustomizerTest.java | 44 ++++ .../ip/v4/Ipv4NeighbourCustomizerTest.java | 77 ++++++ .../SubInterfaceIpv4AddressCustomizerTest.java | 235 ++++++++++++++++++ .../SubInterfaceIpv4NeighbourCustomizerTest.java | 84 +++++++ .../ip/v6/Ipv6AddressCustomizerTest.java | 78 ++++++ .../ip/v6/Ipv6NeighbourCustomizerTest.java | 80 ++++++ .../SubInterfaceIpv6AddressCustomizerTest.java | 74 ++++++ .../SubInterfaceIpv6NeighbourCustomizerTest.java | 85 +++++++ 26 files changed, 1663 insertions(+), 995 deletions(-) delete mode 100644 v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/IpReader.java create mode 100644 v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpAddressReader.java create mode 100644 v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpNeighbourReader.java create mode 100644 v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpReader.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/InterfaceChildNodeTest.java delete mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java delete mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/Ipv4CustomizerTest.java delete mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/SubInterfaceIpv4AddressCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4AddressCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4CustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4NeighbourCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4AddressCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4NeighbourCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6AddressCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6NeighbourCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6AddressCustomizerTest.java create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6NeighbourCustomizerTest.java (limited to 'v3po') diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/IpReader.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/IpReader.java deleted file mode 100644 index c16bde1ca..000000000 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/IpReader.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * 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.v3po.interfacesstate.ip; - -import static com.google.common.base.Preconditions.checkNotNull; - -import com.google.common.base.Optional; -import io.fd.hc2vpp.common.translate.util.AddressTranslator; -import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; -import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; -import io.fd.honeycomb.translate.read.ReadFailedException; -import io.fd.honeycomb.translate.util.RWUtils; -import io.fd.honeycomb.translate.util.read.cache.DumpSupplier; -import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor; -import io.fd.vpp.jvpp.core.dto.IpAddressDetails; -import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; -import io.fd.vpp.jvpp.core.dto.IpAddressDump; -import io.fd.vpp.jvpp.core.dto.IpNeighborDetails; -import io.fd.vpp.jvpp.core.dto.IpNeighborDetailsReplyDump; -import io.fd.vpp.jvpp.core.dto.IpNeighborDump; -import io.fd.vpp.jvpp.core.future.FutureJVppCore; -import java.util.Collections; -import java.util.List; -import java.util.function.Function; -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.Ipv4AddressNoZone; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; -import org.opendaylight.yangtools.yang.binding.Identifier; - -/** - * Utility class providing Ipv4/6 read support. - */ -public interface IpReader extends AddressTranslator, JvppReplyConsumer { - - @Nonnull - default List getAllIpv4AddressIds( - final Optional dumpOptional, - @Nonnull final Function keyConstructor) { - if (dumpOptional.isPresent() && dumpOptional.get().ipAddressDetails != null) { - return dumpOptional.get().ipAddressDetails.stream() - .map(detail -> keyConstructor.apply(arrayToIpv4AddressNoZone(detail.ip))) - .collect(Collectors.toList()); - } else { - return Collections.emptyList(); - } - } - - @Nonnull - default List getAllIpv6AddressIds( - final Optional dumpOptional, - @Nonnull final Function keyConstructor) { - if (dumpOptional.isPresent() && dumpOptional.get().ipAddressDetails != null) { - return dumpOptional.get().ipAddressDetails.stream() - .map(detail -> keyConstructor.apply(arrayToIpv6AddressNoZone(detail.ip))) - .collect(Collectors.toList()); - } else { - return Collections.emptyList(); - } - } - - default Optional findIpv4AddressDetailsByIp( - final Optional dump, - @Nonnull final Ipv4AddressNoZone ip) { - checkNotNull(ip, "ip address should not be null"); - - if (dump.isPresent() && dump.get().ipAddressDetails != null) { - final List details = dump.get().ipAddressDetails; - - return Optional.of(details.stream() - .filter(singleDetail -> ip.equals(arrayToIpv4AddressNoZone(singleDetail.ip))) - .collect(RWUtils.singleItemCollector())); - } - return Optional.absent(); - } - - default Optional findIpv6AddressDetailsByIp( - final Optional dump, - @Nonnull final Ipv6AddressNoZone ip) { - checkNotNull(ip, "ip address should not be null"); - - if (dump.isPresent() && dump.get().ipAddressDetails != null) { - final List details = dump.get().ipAddressDetails; - - return Optional.of(details.stream() - .filter(singleDetail -> ip.equals(arrayToIpv6AddressNoZone(singleDetail.ip))) - .collect(RWUtils.singleItemCollector())); - } - return Optional.absent(); - } - - default EntityDumpExecutor createAddressDumpExecutor( - @Nonnull final FutureJVppCore vppApi) { - return (identifier, params) -> { - checkNotNull(params, "Address dump params cannot be null"); - - final IpAddressDump dumpRequest = new IpAddressDump(); - dumpRequest.isIpv6 = booleanToByte(params.isIpv6()); - dumpRequest.swIfIndex = params.getInterfaceIndex(); - - return getReplyForRead(vppApi.ipAddressDump(dumpRequest).toCompletableFuture(), identifier); - }; - } - - default EntityDumpExecutor createNeighbourDumpExecutor( - @Nonnull final FutureJVppCore vppApi) { - return (identifier, params) -> { - checkNotNull(params, "Address dump params cannot be null"); - - final IpNeighborDump dumpRequest = new IpNeighborDump(); - dumpRequest.isIpv6 = booleanToByte(params.isIpv6()); - dumpRequest.swIfIndex = params.getInterfaceIndex(); - - return getReplyForRead(vppApi.ipNeighborDump(dumpRequest).toCompletableFuture(), identifier); - }; - } - - @Nonnull - default List getNeighborKeys( - final DumpSupplier> dumpSupplier, - final Function detailToKey) - throws ReadFailedException { - final Optional neighbourDumpOpt = dumpSupplier.get(); - - if (neighbourDumpOpt.isPresent()) { - final IpNeighborDetailsReplyDump neighbourDump = neighbourDumpOpt.get(); - - return neighbourDump.ipNeighborDetails.stream() - .map(detailToKey) - .collect(Collectors.toList()); - } else { - return Collections.emptyList(); - } - } - - - -} diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/dump/params/IfaceDumpFilter.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/dump/params/IfaceDumpFilter.java index 19a3ed7f9..ce57e34be 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/dump/params/IfaceDumpFilter.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/dump/params/IfaceDumpFilter.java @@ -41,4 +41,22 @@ public class IfaceDumpFilter { ", isIpv6=" + isIpv6 + '}'; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + IfaceDumpFilter that = (IfaceDumpFilter) o; + + if (interfaceIndex != that.interfaceIndex) return false; + return isIpv6 == that.isIpv6; + } + + @Override + public int hashCode() { + int result = interfaceIndex; + result = 31 * result + (isIpv6 ? 1 : 0); + return result; + } } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpAddressReader.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpAddressReader.java new file mode 100644 index 000000000..cb6228b33 --- /dev/null +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpAddressReader.java @@ -0,0 +1,148 @@ +/* + * 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.v3po.interfacesstate.ip.readers; + +import com.google.common.base.Optional; +import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; +import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.util.SubInterfaceUtils; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.util.RWUtils; +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.IpAddressDetails; +import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; +import io.fd.vpp.jvpp.core.dto.IpAddressDump; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import javax.annotation.Nonnull; +import java.util.Collections; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Provides logic for reading of ip addresses + */ +public abstract class IpAddressReader extends IpReader { + + private final DumpCacheManager dumpCacheManager; + + protected IpAddressReader(@Nonnull final NamingContext interfaceContext, final boolean isIpv6, + @Nonnull final DumpCacheManager dumpCacheManager) { + super(interfaceContext, isIpv6); + this.dumpCacheManager = dumpCacheManager; + } + + @Nonnull + protected Optional interfaceAddressDumpSupplier(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext context) throws ReadFailedException { + return dumpCacheManager.getDump(id, context.getModificationCache(), new IfaceDumpFilter(getInterfaceContext() + .getIndex(id.firstKeyOf(Interface.class).getName(), context.getMappingContext()), isIpv6())); + } + + @Nonnull + protected Optional subInterfaceAddressDumpSupplier(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext context) throws ReadFailedException { + final String subInterfaceName = SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(), + id.firstKeyOf(SubInterface.class).getIdentifier().intValue()); + return dumpCacheManager.getDump(id, context.getModificationCache(), new IfaceDumpFilter(getInterfaceContext() + .getIndex(subInterfaceName, context.getMappingContext()), isIpv6())); + } + + @Nonnull + protected List getAllIpv4AddressIds( + final Optional dumpOptional, + @Nonnull final Function keyConstructor) { + if (dumpOptional.isPresent() && dumpOptional.get().ipAddressDetails != null) { + return dumpOptional.get().ipAddressDetails.stream() + .map(detail -> keyConstructor.apply(arrayToIpv4AddressNoZone(detail.ip))) + .collect(Collectors.toList()); + } else { + return Collections.emptyList(); + } + } + + @Nonnull + protected List getAllIpv6AddressIds( + final Optional dumpOptional, + @Nonnull final Function keyConstructor) { + if (dumpOptional.isPresent() && dumpOptional.get().ipAddressDetails != null) { + return dumpOptional.get().ipAddressDetails.stream() + .map(detail -> keyConstructor.apply(arrayToIpv6AddressNoZone(detail.ip))) + .collect(Collectors.toList()); + } else { + return Collections.emptyList(); + } + } + + @Nonnull + protected Optional findIpv4AddressDetailsByIp( + final Optional dump, + @Nonnull final Ipv4AddressNoZone ip) { + checkNotNull(ip, "ip address should not be null"); + + if (dump.isPresent() && dump.get().ipAddressDetails != null) { + final List details = dump.get().ipAddressDetails; + + return Optional.of(details.stream() + .filter(singleDetail -> ip.equals(arrayToIpv4AddressNoZone(singleDetail.ip))) + .collect(RWUtils.singleItemCollector())); + } + return Optional.absent(); + } + + @Nonnull + protected Optional findIpv6AddressDetailsByIp( + final Optional dump, + @Nonnull final Ipv6AddressNoZone ip) { + checkNotNull(ip, "ip address should not be null"); + + if (dump.isPresent() && dump.get().ipAddressDetails != null) { + final List details = dump.get().ipAddressDetails; + + return Optional.of(details.stream() + .filter(singleDetail -> ip.equals(arrayToIpv6AddressNoZone(singleDetail.ip))) + .collect(RWUtils.singleItemCollector())); + } + return Optional.absent(); + } + + @Nonnull + protected static EntityDumpExecutor createAddressDumpExecutor( + @Nonnull final FutureJVppCore vppApi) { + return (identifier, params) -> { + checkNotNull(params, "Address dump params cannot be null"); + + final IpAddressDump dumpRequest = new IpAddressDump(); + dumpRequest.isIpv6 = ByteDataTranslator.INSTANCE.booleanToByte(params.isIpv6()); + dumpRequest.swIfIndex = params.getInterfaceIndex(); + + return JvppReplyConsumer.INSTANCE.getReplyForRead(vppApi.ipAddressDump(dumpRequest).toCompletableFuture(), identifier); + }; + } +} diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpNeighbourReader.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpNeighbourReader.java new file mode 100644 index 000000000..0acfb5b81 --- /dev/null +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpNeighbourReader.java @@ -0,0 +1,103 @@ +/* + * 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.v3po.interfacesstate.ip.readers; + +import com.google.common.base.Optional; +import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; +import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.util.SubInterfaceUtils; +import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +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.IpNeighborDetails; +import io.fd.vpp.jvpp.core.dto.IpNeighborDetailsReplyDump; +import io.fd.vpp.jvpp.core.dto.IpNeighborDump; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import javax.annotation.Nonnull; +import java.util.Collections; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Provides logic for reading ip neighbours + */ +public abstract class IpNeighbourReader extends IpReader { + + private final DumpCacheManager dumpCacheManager; + + protected IpNeighbourReader(@Nonnull final NamingContext interfaceContext, boolean isIpv6, + @Nonnull final DumpCacheManager dumpCacheManager) { + super(interfaceContext, isIpv6); + this.dumpCacheManager = dumpCacheManager; + } + + @Nonnull + protected Optional interfaceNeighboursDump(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext context) throws ReadFailedException { + return dumpCacheManager.getDump(id, context.getModificationCache(), new IfaceDumpFilter(getInterfaceContext() + .getIndex(id.firstKeyOf(Interface.class).getName(), context.getMappingContext()), isIpv6())); + } + + @Nonnull + protected Optional subInterfaceNeighboursDump(@Nonnull final InstanceIdentifier id, + @Nonnull final ReadContext context) throws ReadFailedException { + final String subInterfaceName = SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(), + id.firstKeyOf(SubInterface.class).getIdentifier().intValue()); + return dumpCacheManager.getDump(id, context.getModificationCache(), new IfaceDumpFilter(getInterfaceContext() + .getIndex(subInterfaceName, context.getMappingContext()), isIpv6())); + } + + @Nonnull + protected static EntityDumpExecutor createNeighbourDumpExecutor( + @Nonnull final FutureJVppCore vppApi) { + return (identifier, params) -> { + checkNotNull(params, "Address dump params cannot be null"); + + final IpNeighborDump dumpRequest = new IpNeighborDump(); + dumpRequest.isIpv6 = ByteDataTranslator.INSTANCE.booleanToByte(params.isIpv6()); + dumpRequest.swIfIndex = params.getInterfaceIndex(); + + return JvppReplyConsumer.INSTANCE.getReplyForRead(vppApi.ipNeighborDump(dumpRequest).toCompletableFuture(), identifier); + }; + } + + @Nonnull + protected List getNeighborKeys( + final Optional neighbourDumpOpt, + final Function detailToKey) + throws ReadFailedException { + if (neighbourDumpOpt.isPresent()) { + final IpNeighborDetailsReplyDump neighbourDump = neighbourDumpOpt.get(); + + return neighbourDump.ipNeighborDetails.stream() + .map(detailToKey) + .collect(Collectors.toList()); + } else { + return Collections.emptyList(); + } + } +} diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpReader.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpReader.java new file mode 100644 index 000000000..9003dbab7 --- /dev/null +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/readers/IpReader.java @@ -0,0 +1,60 @@ +/* + * 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.v3po.interfacesstate.ip.readers; + +import com.google.common.collect.ImmutableSet; +import io.fd.hc2vpp.common.translate.util.AddressTranslator; +import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory; +import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; + +import javax.annotation.Nonnull; + +/** + * Utility class providing Ipv4/6 read support. + */ +abstract class IpReader implements AddressTranslator, JvppReplyConsumer { + + private final NamingContext interfaceContext; + private final boolean isIpv6; + + IpReader(@Nonnull final NamingContext interfaceContext, final boolean isIpv6) { + this.interfaceContext = interfaceContext; + this.isIpv6 = isIpv6; + } + + @Nonnull + protected static CacheKeyFactory interfaceScopedCacheKeyFactory(@Nonnull final Class dumpReplyClass) { + return new TypeAwareIdentifierCacheKeyFactory(dumpReplyClass, ImmutableSet.of(Interface.class)); + } + + @Nonnull + protected static CacheKeyFactory subInterfaceScopedCacheKeyFactory(@Nonnull final Class dumpReplyClass) { + return new TypeAwareIdentifierCacheKeyFactory(dumpReplyClass, ImmutableSet.of(SubInterface.class)); + } + + protected NamingContext getInterfaceContext() { + return interfaceContext; + } + + protected boolean isIpv6() { + return isIpv6; + } +} diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4AddressCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4AddressCustomizer.java index 8b84cfa24..aeaae5bdf 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4AddressCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4AddressCustomizer.java @@ -16,28 +16,21 @@ package io.fd.hc2vpp.v3po.interfacesstate.ip.v4; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.NamingContext; import io.fd.hc2vpp.v3po.interfacesstate.InterfaceCustomizer; -import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader; import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpAddressReader; 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.RWUtils; -import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; +import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder; import io.fd.vpp.jvpp.core.dto.IpAddressDetails; import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; 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.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4; @@ -53,28 +46,24 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nonnull; +import java.util.List; + /** * Read customizer for interface Ipv4 addresses. */ -public class Ipv4AddressCustomizer extends FutureJVppCustomizer - implements InitializingListReaderCustomizer, IpReader { +public class Ipv4AddressCustomizer extends IpAddressReader + implements InitializingListReaderCustomizer { private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressCustomizer.class); - private final NamingContext interfaceContext; - private final DumpCacheManager dumpManager; - public Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext) { - super(futureJVppCore); - this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null"); - this.dumpManager = - new DumpCacheManager.DumpCacheManagerBuilder() - .withExecutor(createAddressDumpExecutor(futureJVppCore)) - // Key needs to contain interface ID to distinguish dumps between interfaces - .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class, - ImmutableSet.of(Interface.class))) - .build(); + super(interfaceContext, false, new DumpCacheManagerBuilder() + .withExecutor(createAddressDumpExecutor(futureJVppCore)) + // Key needs to contain interface ID to distinguish dumps between interfaces + .withCacheKeyFactory(interfaceScopedCacheKeyFactory(IpAddressDetailsReplyDump.class)) + .build()); } @Override @@ -85,20 +74,14 @@ public class Ipv4AddressCustomizer extends FutureJVppCustomizer @Override public void readCurrentAttributes(@Nonnull InstanceIdentifier
id, @Nonnull AddressBuilder builder, - @Nonnull ReadContext ctx) - throws ReadFailedException { + @Nonnull ReadContext ctx) throws ReadFailedException { LOG.debug("Reading attributes for interface address: {}", id); - - final String interfaceName = id.firstKeyOf(Interface.class).getName(); - final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext()); - final Optional dumpOptional = - dumpManager.getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(interfaceIndex, false)); + final Optional dumpOptional = interfaceAddressDumpSupplier(id, ctx); if (!dumpOptional.isPresent() || dumpOptional.get().ipAddressDetails.isEmpty()) { return; } - final Optional ipAddressDetails = - findIpv4AddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp()); + final Optional ipAddressDetails = findIpv4AddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp()); if (ipAddressDetails.isPresent()) { final IpAddressDetails detail = ipAddressDetails.get(); @@ -106,6 +89,8 @@ public class Ipv4AddressCustomizer extends FutureJVppCustomizer .setSubnet(new PrefixLengthBuilder().setPrefixLength(Short.valueOf(detail.prefixLength)).build()); if (LOG.isDebugEnabled()) { + final String interfaceName = id.firstKeyOf(Interface.class).getName(); + final int interfaceIndex = getInterfaceContext().getIndex(interfaceName, ctx.getMappingContext()); LOG.debug("Attributes for {} interface (id={}) address {} successfully read: {}", interfaceName, interfaceIndex, id, builder.build()); } @@ -116,13 +101,7 @@ public class Ipv4AddressCustomizer extends FutureJVppCustomizer public List getAllIds(@Nonnull InstanceIdentifier
id, @Nonnull ReadContext ctx) throws ReadFailedException { LOG.debug("Reading list of keys for interface addresses: {}", id); - - final String interfaceName = id.firstKeyOf(Interface.class).getName(); - final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext()); - final Optional dumpOptional = - dumpManager.getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(interfaceIndex, false)); - - return getAllIpv4AddressIds(dumpOptional, AddressKey::new); + return getAllIpv4AddressIds(interfaceAddressDumpSupplier(id, ctx), AddressKey::new); } @Override @@ -151,8 +130,8 @@ public class Ipv4AddressCustomizer extends FutureJVppCustomizer return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLengthBuilder() .setPrefixLength( - ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLength) subnet) - .getPrefixLength()).build(); + ((org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLength) subnet) + .getPrefixLength()).build(); } static InstanceIdentifier getCfgId( diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4NeighbourCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4NeighbourCustomizer.java index 0e5fb310f..c4eb7fd30 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4NeighbourCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4NeighbourCustomizer.java @@ -16,30 +16,18 @@ package io.fd.hc2vpp.v3po.interfacesstate.ip.v4; -import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.NeighborOrigin.Dynamic; -import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.NeighborOrigin.Static; - import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.NamingContext; -import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader; import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpNeighbourReader; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; -import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager; import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder; -import io.fd.honeycomb.translate.util.read.cache.DumpSupplier; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; import io.fd.vpp.jvpp.core.dto.IpNeighborDetails; import io.fd.vpp.jvpp.core.dto.IpNeighborDetailsReplyDump; import io.fd.vpp.jvpp.core.future.FutureJVppCore; -import java.util.List; -import java.util.function.Function; -import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Neighbor; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.NeighborBuilder; @@ -48,26 +36,27 @@ import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import javax.annotation.Nonnull; +import java.util.List; +import java.util.function.Function; + +import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.NeighborOrigin.Dynamic; +import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.NeighborOrigin.Static; + /** * Operational data read operation customizer for {@link Neighbor}
* Currently not supported in jvpp, so this is only dummy implementation
*/ -public class Ipv4NeighbourCustomizer extends FutureJVppCustomizer - implements ListReaderCustomizer, IpReader { - - private final DumpCacheManager dumpManager; - private final NamingContext interfaceContext; +public class Ipv4NeighbourCustomizer extends IpNeighbourReader + implements ListReaderCustomizer { public Ipv4NeighbourCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext) { - super(futureJVppCore); - dumpManager = new DumpCacheManagerBuilder() + super(interfaceContext, false, new DumpCacheManagerBuilder() .withExecutor(createNeighbourDumpExecutor(futureJVppCore)) // cached with parent interface scope - .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpNeighborDetailsReplyDump.class, - ImmutableSet.of(Interface.class))) - .build(); - this.interfaceContext = interfaceContext; + .withCacheKeyFactory(interfaceScopedCacheKeyFactory(IpNeighborDetailsReplyDump.class)) + .build()); } @Override @@ -81,7 +70,7 @@ public class Ipv4NeighbourCustomizer extends FutureJVppCustomizer final Ipv4AddressNoZone ip = id.firstKeyOf(Neighbor.class).getIp(); - final Optional dumpOpt = dumpSupplier(id, ctx).get(); + final Optional dumpOpt = interfaceNeighboursDump(id, ctx); if (dumpOpt.isPresent()) { dumpOpt.get().ipNeighborDetails @@ -100,7 +89,7 @@ public class Ipv4NeighbourCustomizer extends FutureJVppCustomizer @Override public List getAllIds(InstanceIdentifier id, ReadContext context) throws ReadFailedException { - return getNeighborKeys(dumpSupplier(id, context), keyMapper()); + return getNeighborKeys(interfaceNeighboursDump(id, context), keyMapper()); } @Override @@ -111,13 +100,4 @@ public class Ipv4NeighbourCustomizer extends FutureJVppCustomizer private Function keyMapper() { return ipNeighborDetails -> new NeighborKey(arrayToIpv4AddressNoZone(ipNeighborDetails.ipAddress)); } - - private DumpSupplier> dumpSupplier(final InstanceIdentifier id, - final ReadContext context) { - return () -> dumpManager - .getDump(id, context.getModificationCache(), new IfaceDumpFilter(interfaceContext - .getIndex(id.firstKeyOf(Interface.class).getName(), context.getMappingContext()), - false)); - } - } \ No newline at end of file diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4AddressCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4AddressCustomizer.java index f27229acd..55fd6ca3a 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4AddressCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4AddressCustomizer.java @@ -16,15 +16,11 @@ package io.fd.hc2vpp.v3po.interfacesstate.ip.v4.subinterface; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.NamingContext; import io.fd.hc2vpp.v3po.interfacesstate.SubInterfaceCustomizer; -import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader; import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpAddressReader; import io.fd.hc2vpp.v3po.util.SubInterfaceUtils; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; @@ -32,12 +28,9 @@ import io.fd.honeycomb.translate.spi.read.Initialized; import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer; import io.fd.honeycomb.translate.util.RWUtils; import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; import io.fd.vpp.jvpp.core.dto.IpAddressDetails; import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; 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.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4; @@ -52,27 +45,24 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nonnull; +import java.util.List; + /** * Read customizer for sub-interface Ipv4 addresses. */ -public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer - implements InitializingListReaderCustomizer, IpReader { +public class SubInterfaceIpv4AddressCustomizer extends IpAddressReader + implements InitializingListReaderCustomizer { private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class); - private final NamingContext interfaceContext; - private final DumpCacheManager dumpManager; - public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext) { - super(futureJVppCore); - this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null"); - this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder() + super(interfaceContext, false, new DumpCacheManager.DumpCacheManagerBuilder() .withExecutor(createAddressDumpExecutor(futureJVppCore)) //same as with ipv4 addresses for interfaces, these must have cache scope of their parent sub-interface - .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class, - ImmutableSet.of(SubInterface.class))) - .build(); + .withCacheKeyFactory(subInterfaceScopedCacheKeyFactory(IpAddressDetailsReplyDump.class)) + .build()); } private static String getSubInterfaceName(@Nonnull final InstanceIdentifier
id) { @@ -92,13 +82,8 @@ public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer throws ReadFailedException { LOG.debug("Reading attributes for sub-interface address: {}", id); - final String subInterfaceName = getSubInterfaceName(id); - final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()); - final Optional dumpOptional = dumpManager - .getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(subInterfaceIndex, false)); - final Optional ipAddressDetails = - findIpv4AddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp()); + findIpv4AddressDetailsByIp(subInterfaceAddressDumpSupplier(id, ctx), id.firstKeyOf(Address.class).getIp()); if (ipAddressDetails.isPresent()) { final IpAddressDetails detail = ipAddressDetails.get(); @@ -106,6 +91,8 @@ public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer builder.setSubnet(new PrefixLengthBuilder().setPrefixLength((short) detail.prefixLength).build()); if (LOG.isDebugEnabled()) { + final String subInterfaceName = getSubInterfaceName(id); + final int subInterfaceIndex = getInterfaceContext().getIndex(subInterfaceName, ctx.getMappingContext()); LOG.debug("Attributes for {} sub-interface (id={}) address {} successfully read: {}", subInterfaceName, subInterfaceIndex, id, builder.build()); } @@ -117,13 +104,7 @@ public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer public List getAllIds(@Nonnull InstanceIdentifier
id, @Nonnull ReadContext ctx) throws ReadFailedException { LOG.debug("Reading list of keys for sub-interface addresses: {}", id); - - final String subInterfaceName = getSubInterfaceName(id); - final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()); - final Optional dumpOptional = dumpManager - .getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(subInterfaceIndex, false)); - - return getAllIpv4AddressIds(dumpOptional, AddressKey::new); + return getAllIpv4AddressIds(subInterfaceAddressDumpSupplier(id, ctx), AddressKey::new); } @Override diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4NeighbourCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4NeighbourCustomizer.java index 71833db0d..aaedabd9f 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4NeighbourCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4NeighbourCustomizer.java @@ -17,26 +17,17 @@ package io.fd.hc2vpp.v3po.interfacesstate.ip.v4.subinterface; import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.NamingContext; -import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader; import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpNeighbourReader; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; -import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager; import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder; -import io.fd.honeycomb.translate.util.read.cache.DumpSupplier; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; import io.fd.vpp.jvpp.core.dto.IpNeighborDetails; import io.fd.vpp.jvpp.core.dto.IpNeighborDetailsReplyDump; import io.fd.vpp.jvpp.core.future.FutureJVppCore; -import java.util.List; -import java.util.function.Function; -import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Neighbor; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.NeighborBuilder; @@ -45,22 +36,20 @@ import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -public class SubInterfaceIpv4NeighbourCustomizer extends FutureJVppCustomizer - implements ListReaderCustomizer, IpReader { +import javax.annotation.Nonnull; +import java.util.List; +import java.util.function.Function; - private final DumpCacheManager dumpManager; - private final NamingContext interfaceContext; +public class SubInterfaceIpv4NeighbourCustomizer extends IpNeighbourReader + implements ListReaderCustomizer { public SubInterfaceIpv4NeighbourCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext) { - super(futureJVppCore); - dumpManager = new DumpCacheManagerBuilder() + super(interfaceContext, false, new DumpCacheManagerBuilder() .withExecutor(createNeighbourDumpExecutor(futureJVppCore)) // cached with parent interface scope - .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpNeighborDetailsReplyDump.class, - ImmutableSet.of(Interface.class))) - .build(); - this.interfaceContext = interfaceContext; + .withCacheKeyFactory(subInterfaceScopedCacheKeyFactory(IpNeighborDetailsReplyDump.class)) + .build()); } @Override @@ -74,7 +63,7 @@ public class SubInterfaceIpv4NeighbourCustomizer extends FutureJVppCustomizer final Ipv4AddressNoZone ip = id.firstKeyOf(Neighbor.class).getIp(); - final Optional dumpOpt = dumpSupplier(id, ctx).get(); + final Optional dumpOpt = subInterfaceNeighboursDump(id, ctx); if (dumpOpt.isPresent()) { dumpOpt.get().ipNeighborDetails @@ -90,7 +79,7 @@ public class SubInterfaceIpv4NeighbourCustomizer extends FutureJVppCustomizer @Override public List getAllIds(InstanceIdentifier id, ReadContext context) throws ReadFailedException { - return getNeighborKeys(dumpSupplier(id, context), keyMapper()); + return getNeighborKeys(subInterfaceNeighboursDump(id, context), keyMapper()); } @Override @@ -101,13 +90,4 @@ public class SubInterfaceIpv4NeighbourCustomizer extends FutureJVppCustomizer private Function keyMapper() { return ipNeighborDetails -> new NeighborKey(arrayToIpv4AddressNoZone(ipNeighborDetails.ipAddress)); } - - private DumpSupplier> dumpSupplier(final InstanceIdentifier id, - final ReadContext context) { - return () -> dumpManager - .getDump(id, context.getModificationCache(), new IfaceDumpFilter(interfaceContext - .getIndex(id.firstKeyOf(Interface.class).getName(), context.getMappingContext()), - false)); - } - } \ No newline at end of file diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6AddressCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6AddressCustomizer.java index a5166525e..55ca35250 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6AddressCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6AddressCustomizer.java @@ -17,27 +17,20 @@ package io.fd.hc2vpp.v3po.interfacesstate.ip.v6; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.NamingContext; import io.fd.hc2vpp.v3po.interfacesstate.InterfaceCustomizer; -import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader; import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpAddressReader; 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.RWUtils; -import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; +import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder; import io.fd.vpp.jvpp.core.dto.IpAddressDetails; import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; 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.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv6; @@ -51,25 +44,21 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class Ipv6AddressCustomizer extends FutureJVppCustomizer - implements InitializingListReaderCustomizer, IpReader { +import javax.annotation.Nonnull; +import java.util.List; - private static final Logger LOG = LoggerFactory.getLogger(Ipv6AddressCustomizer.class); +public class Ipv6AddressCustomizer extends IpAddressReader + implements InitializingListReaderCustomizer { - private final NamingContext interfaceContext; - private final DumpCacheManager dumpManager; + private static final Logger LOG = LoggerFactory.getLogger(Ipv6AddressCustomizer.class); public Ipv6AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext) { - super(futureJVppCore); - this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null"); - this.dumpManager = - new DumpCacheManager.DumpCacheManagerBuilder() - .withExecutor(createAddressDumpExecutor(futureJVppCore)) - // Key needs to contain interface ID to distinguish dumps between interfaces - .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class, - ImmutableSet.of(Interface.class))) - .build(); + super(interfaceContext, true, new DumpCacheManagerBuilder() + .withExecutor(createAddressDumpExecutor(futureJVppCore)) + // Key needs to contain interface ID to distinguish dumps between interfaces + .withCacheKeyFactory(interfaceScopedCacheKeyFactory(IpAddressDetailsReplyDump.class)) + .build()); } @Override @@ -83,17 +72,8 @@ public class Ipv6AddressCustomizer extends FutureJVppCustomizer @Nonnull ReadContext ctx) throws ReadFailedException { LOG.debug("Reading attributes for interface address: {}", id); - - final String interfaceName = id.firstKeyOf(Interface.class).getName(); - final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext()); - final Optional dumpOptional = - dumpManager.getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(interfaceIndex, true)); - - if (!dumpOptional.isPresent() || dumpOptional.get().ipAddressDetails.isEmpty()) { - return; - } final Optional ipAddressDetails = - findIpv6AddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp()); + findIpv6AddressDetailsByIp(interfaceAddressDumpSupplier(id, ctx), id.firstKeyOf(Address.class).getIp()); if (ipAddressDetails.isPresent()) { final IpAddressDetails detail = ipAddressDetails.get(); @@ -102,6 +82,8 @@ public class Ipv6AddressCustomizer extends FutureJVppCustomizer .build(); if (LOG.isDebugEnabled()) { + final String interfaceName = id.firstKeyOf(Interface.class).getName(); + final int interfaceIndex = getInterfaceContext().getIndex(interfaceName, ctx.getMappingContext()); LOG.debug("Attributes for {} interface (id={}) address {} successfully read: {}", interfaceName, interfaceIndex, id, builder.build()); } @@ -112,13 +94,7 @@ public class Ipv6AddressCustomizer extends FutureJVppCustomizer public List getAllIds(@Nonnull InstanceIdentifier
id, @Nonnull ReadContext ctx) throws ReadFailedException { LOG.debug("Reading list of keys for interface addresses: {}", id); - - final String interfaceName = id.firstKeyOf(Interface.class).getName(); - final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext()); - final Optional dumpOptional = - dumpManager.getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(interfaceIndex, true)); - - return getAllIpv6AddressIds(dumpOptional, AddressKey::new); + return getAllIpv6AddressIds(interfaceAddressDumpSupplier(id, ctx), AddressKey::new); } @Override diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6NeighbourCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6NeighbourCustomizer.java index 923795f50..de1b0ab5e 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6NeighbourCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6NeighbourCustomizer.java @@ -17,29 +17,18 @@ package io.fd.hc2vpp.v3po.interfacesstate.ip.v6; -import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.NeighborOrigin.Dynamic; -import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.NeighborOrigin.Static; - import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.NamingContext; -import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader; import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpNeighbourReader; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager; -import io.fd.honeycomb.translate.util.read.cache.DumpSupplier; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; import io.fd.vpp.jvpp.core.dto.IpNeighborDetails; import io.fd.vpp.jvpp.core.dto.IpNeighborDetailsReplyDump; import io.fd.vpp.jvpp.core.future.FutureJVppCore; -import java.util.List; -import java.util.function.Function; -import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv6Builder; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv6.Neighbor; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv6.NeighborBuilder; @@ -48,23 +37,23 @@ import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -public class Ipv6NeighbourCustomizer extends FutureJVppCustomizer - implements ListReaderCustomizer, IpReader { +import javax.annotation.Nonnull; +import java.util.List; +import java.util.function.Function; +import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.NeighborOrigin.Dynamic; +import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.NeighborOrigin.Static; - private final DumpCacheManager dumpManager; - private final NamingContext interfaceContext; +public class Ipv6NeighbourCustomizer extends IpNeighbourReader + implements ListReaderCustomizer { public Ipv6NeighbourCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext) { - super(futureJVppCore); - dumpManager = new DumpCacheManager.DumpCacheManagerBuilder() + super(interfaceContext, true, new DumpCacheManager.DumpCacheManagerBuilder() .withExecutor(createNeighbourDumpExecutor(futureJVppCore)) // cached with parent interface scope - .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpNeighborDetailsReplyDump.class, - ImmutableSet.of(Interface.class))) - .build(); - this.interfaceContext = interfaceContext; + .withCacheKeyFactory(interfaceScopedCacheKeyFactory(IpNeighborDetailsReplyDump.class)) + .build()); } @Override @@ -75,10 +64,8 @@ public class Ipv6NeighbourCustomizer extends FutureJVppCustomizer @Override public void readCurrentAttributes(InstanceIdentifier id, NeighborBuilder builder, ReadContext ctx) throws ReadFailedException { - final Ipv6AddressNoZone ip = id.firstKeyOf(Neighbor.class).getIp(); - - final Optional dumpOpt = dumpSupplier(id, ctx).get(); + final Optional dumpOpt = interfaceNeighboursDump(id, ctx); if (dumpOpt.isPresent()) { dumpOpt.get().ipNeighborDetails @@ -97,7 +84,7 @@ public class Ipv6NeighbourCustomizer extends FutureJVppCustomizer @Override public List getAllIds(InstanceIdentifier id, ReadContext context) throws ReadFailedException { - return getNeighborKeys(dumpSupplier(id, context), keyMapper()); + return getNeighborKeys(interfaceNeighboursDump(id, context), keyMapper()); } @Override @@ -108,12 +95,4 @@ public class Ipv6NeighbourCustomizer extends FutureJVppCustomizer private Function keyMapper() { return ipNeighborDetails -> new NeighborKey(arrayToIpv6AddressNoZone(ipNeighborDetails.ipAddress)); } - - private DumpSupplier> dumpSupplier(final InstanceIdentifier id, - final ReadContext context) { - return () -> dumpManager - .getDump(id, context.getModificationCache(), new IfaceDumpFilter(interfaceContext - .getIndex(id.firstKeyOf(Interface.class).getName(), context.getMappingContext()), - true)); - } } \ No newline at end of file diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6AddressCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6AddressCustomizer.java index 4aea4c00e..1e4b62c87 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6AddressCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6AddressCustomizer.java @@ -17,15 +17,11 @@ package io.fd.hc2vpp.v3po.interfacesstate.ip.v6.subinterface; -import static com.google.common.base.Preconditions.checkNotNull; - import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.NamingContext; import io.fd.hc2vpp.v3po.interfacesstate.SubInterfaceCustomizer; -import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader; import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpAddressReader; import io.fd.hc2vpp.v3po.util.SubInterfaceUtils; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; @@ -33,12 +29,9 @@ import io.fd.honeycomb.translate.spi.read.Initialized; import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer; import io.fd.honeycomb.translate.util.RWUtils; import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; import io.fd.vpp.jvpp.core.dto.IpAddressDetails; import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; 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.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.Ipv6; @@ -52,23 +45,20 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SubInterfaceIpv6AddressCustomizer extends FutureJVppCustomizer - implements InitializingListReaderCustomizer, IpReader { +import javax.annotation.Nonnull; +import java.util.List; - private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv6AddressCustomizer.class); +public class SubInterfaceIpv6AddressCustomizer extends IpAddressReader + implements InitializingListReaderCustomizer { - private final NamingContext interfaceContext; - private final DumpCacheManager dumpManager; + private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv6AddressCustomizer.class); public SubInterfaceIpv6AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext) { - super(futureJVppCore); - this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null"); - this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder() + super(interfaceContext, true, new DumpCacheManager.DumpCacheManagerBuilder() .withExecutor(createAddressDumpExecutor(futureJVppCore)) - .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class, - ImmutableSet.of(SubInterface.class))) - .build(); + .withCacheKeyFactory(subInterfaceScopedCacheKeyFactory(IpAddressDetailsReplyDump.class)) + .build()); } private static String getSubInterfaceName(@Nonnull final InstanceIdentifier
id) { @@ -87,21 +77,17 @@ public class SubInterfaceIpv6AddressCustomizer extends FutureJVppCustomizer @Nonnull ReadContext ctx) throws ReadFailedException { LOG.debug("Reading attributes for sub-interface address: {}", id); - - final String subInterfaceName = getSubInterfaceName(id); - final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()); - final Optional dumpOptional = dumpManager - .getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(subInterfaceIndex, false)); - final Optional ipAddressDetails = - findIpv6AddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp()); + findIpv6AddressDetailsByIp(subInterfaceAddressDumpSupplier(id, ctx), id.firstKeyOf(Address.class).getIp()); if (ipAddressDetails.isPresent()) { final IpAddressDetails detail = ipAddressDetails.get(); builder.setIp(arrayToIpv6AddressNoZone(detail.ip)); - builder.setPrefixLength((short) detail.prefixLength); + builder.setPrefixLength((short) Byte.toUnsignedInt(detail.prefixLength)); if (LOG.isDebugEnabled()) { + final String subInterfaceName = getSubInterfaceName(id); + final int subInterfaceIndex = getInterfaceContext().getIndex(subInterfaceName, ctx.getMappingContext()); LOG.debug("Attributes for {} sub-interface (id={}) address {} successfully read: {}", subInterfaceName, subInterfaceIndex, id, builder.build()); } @@ -113,13 +99,7 @@ public class SubInterfaceIpv6AddressCustomizer extends FutureJVppCustomizer public List getAllIds(@Nonnull InstanceIdentifier
id, @Nonnull ReadContext ctx) throws ReadFailedException { LOG.debug("Reading list of keys for sub-interface addresses: {}", id); - - final String subInterfaceName = getSubInterfaceName(id); - final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext()); - final Optional dumpOptional = dumpManager - .getDump(id, ctx.getModificationCache(), new IfaceDumpFilter(subInterfaceIndex, false)); - - return getAllIpv6AddressIds(dumpOptional, AddressKey::new); + return getAllIpv6AddressIds(subInterfaceAddressDumpSupplier(id, ctx), AddressKey::new); } @Override diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6NeighbourCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6NeighbourCustomizer.java index ac7882729..a1b50680d 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6NeighbourCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6NeighbourCustomizer.java @@ -17,26 +17,17 @@ package io.fd.hc2vpp.v3po.interfacesstate.ip.v6.subinterface; import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; import io.fd.hc2vpp.common.translate.util.NamingContext; -import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader; import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter; +import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpNeighbourReader; import io.fd.honeycomb.translate.read.ReadContext; import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; -import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager; import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder; -import io.fd.honeycomb.translate.util.read.cache.DumpSupplier; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; import io.fd.vpp.jvpp.core.dto.IpNeighborDetails; import io.fd.vpp.jvpp.core.dto.IpNeighborDetailsReplyDump; import io.fd.vpp.jvpp.core.future.FutureJVppCore; -import java.util.List; -import java.util.function.Function; -import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.Ipv6Builder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.Neighbor; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.NeighborBuilder; @@ -45,22 +36,20 @@ import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -public class SubInterfaceIpv6NeighbourCustomizer extends FutureJVppCustomizer - implements ListReaderCustomizer, IpReader { +import javax.annotation.Nonnull; +import java.util.List; +import java.util.function.Function; - private final DumpCacheManager dumpManager; - private final NamingContext interfaceContext; +public class SubInterfaceIpv6NeighbourCustomizer extends IpNeighbourReader + implements ListReaderCustomizer { public SubInterfaceIpv6NeighbourCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext) { - super(futureJVppCore); - dumpManager = new DumpCacheManagerBuilder() + super(interfaceContext, true, new DumpCacheManagerBuilder() .withExecutor(createNeighbourDumpExecutor(futureJVppCore)) // cached with parent interface scope - .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpNeighborDetailsReplyDump.class, - ImmutableSet.of(Interface.class))) - .build(); - this.interfaceContext = interfaceContext; + .withCacheKeyFactory(subInterfaceScopedCacheKeyFactory(IpNeighborDetailsReplyDump.class)) + .build()); } @Override @@ -74,7 +63,7 @@ public class SubInterfaceIpv6NeighbourCustomizer extends FutureJVppCustomizer final Ipv6AddressNoZone ip = id.firstKeyOf(Neighbor.class).getIp(); - final Optional dumpOpt = dumpSupplier(id, ctx).get(); + final Optional dumpOpt = subInterfaceNeighboursDump(id, ctx); if (dumpOpt.isPresent()) { dumpOpt.get().ipNeighborDetails @@ -90,7 +79,7 @@ public class SubInterfaceIpv6NeighbourCustomizer extends FutureJVppCustomizer @Override public List getAllIds(InstanceIdentifier id, ReadContext context) throws ReadFailedException { - return getNeighborKeys(dumpSupplier(id, context), keyMapper()); + return getNeighborKeys(subInterfaceNeighboursDump(id, context), keyMapper()); } @Override @@ -101,13 +90,4 @@ public class SubInterfaceIpv6NeighbourCustomizer extends FutureJVppCustomizer private Function keyMapper() { return ipNeighborDetails -> new NeighborKey(arrayToIpv6AddressNoZone(ipNeighborDetails.ipAddress)); } - - private DumpSupplier> dumpSupplier(final InstanceIdentifier id, - final ReadContext context) { - return () -> dumpManager - .getDump(id, context.getModificationCache(), new IfaceDumpFilter(interfaceContext - .getIndex(id.firstKeyOf(Interface.class).getName(), context.getMappingContext()), - true)); - } - } \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/InterfaceChildNodeTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/InterfaceChildNodeTest.java new file mode 100644 index 000000000..4e323e45d --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/InterfaceChildNodeTest.java @@ -0,0 +1,195 @@ +/* + * 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.v3po.interfacesstate.ip; + +import io.fd.hc2vpp.common.test.util.FutureProducer; +import io.fd.hc2vpp.common.test.util.NamingContextHelper; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.vpp.jvpp.core.dto.*; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress; + +import javax.annotation.Nonnull; +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +public interface InterfaceChildNodeTest extends NamingContextHelper, FutureProducer { + + String INTERFACE_CONTEXT_NAME = "iface-context"; + NamingContext INTERFACE_CONTEXT = new NamingContext("prefix", INTERFACE_CONTEXT_NAME); + + String IFACE_NAME = "eth0"; + int IFACE_ID = 1; + + String SUB_IFACE_NAME = "eth0.4"; + int SUB_IFACE_ID = 4; + + String IFACE_2_NAME = "eth1"; + int IFACE_2_ID = 2; + + String SUB_IFACE_2_NAME = "eth1.7"; + int SUB_IFACE_2_ID = 7; + + String IPV6_ONE_COMPRESSED = "2001:db8:a0b:12f0::1"; + byte[] IPV6_ONE_BYTES = {32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 1}; + byte IPV6_ONE_PREFIX = 64; + Ipv6AddressNoZone IPV6_ONE_ADDRESS_COMPRESSED = new Ipv6AddressNoZone(IPV6_ONE_COMPRESSED); + + String IPV6_TWO_COMPRESSED = "2001:db8:a0b:12f0::2"; + byte[] IPV6_TWO_BYTES = {32, 1, 13, -72, 10, 11, 18, -16, 0, 0, 0, 0, 0, 0, 0, 2}; + byte IPV6_TWO_PREFIX = 48; + Ipv6AddressNoZone IPV6_TWO_ADDRESS_COMPRESSED = new Ipv6AddressNoZone(IPV6_TWO_COMPRESSED); + + String IPV4_ONE = "192.168.2.1"; + byte[] IPV4_ONE_BYTES = {-64, -88, 2, 1}; + byte IPV4_ONE_PREFIX = 24; + Ipv4AddressNoZone IPV4_ONE_ADDRESS = new Ipv4AddressNoZone(IPV4_ONE); + + String IPV4_TWO = "192.168.2.2"; + byte[] IPV4_TWO_BYTES = {-64, -88, 2, 2}; + byte IPV4_TWO_PREFIX = 24; + Ipv4AddressNoZone IPV4_TWO_ADDRESS = new Ipv4AddressNoZone(IPV4_TWO); + + String MAC_ONE = "00:00:00:00:00:00"; + byte[] MAC_ONE_BYTES = {0, 0, 0, 0, 0, 0}; + PhysAddress MAC_ONE_ADDRESS = new PhysAddress(MAC_ONE); + + String MAC_TWO = "00:00:00:00:00:01"; + byte[] MAC_TWO_BYTES = {0, 0, 0, 0, 0, 1}; + PhysAddress MAC_TWO_ADDRESS = new PhysAddress(MAC_TWO); + + String MAC_THREE = "00:00:00:00:00:02"; + byte[] MAC_THREE_BYTES = {0, 0, 0, 0, 0, 2}; + PhysAddress MAC_THREE_ADDRESS = new PhysAddress(MAC_THREE); + + String MAC_FOUR = "00:00:00:00:00:03"; + byte[] MAC_FOUR_BYTES = {0, 0, 0, 0, 0, 3}; + PhysAddress MAC_FOUR_ADDRESS = new PhysAddress(MAC_FOUR); + + default void mockNeighborDump(@Nonnull final FutureJVppCore api, + @Nonnull final IpNeighborDump request, + @Nonnull final IpNeighborDetailsReplyDump reply) { + when(api.ipNeighborDump(request)).thenReturn(future(reply)); + } + + default void mockAddressDump(@Nonnull final FutureJVppCore api, + @Nonnull final IpAddressDump request, + @Nonnull final IpAddressDetailsReplyDump reply) { + when(api.ipAddressDump(request)).thenReturn(future(reply)); + } + + default IpAddressDump dumpV6AddressesIfaceTwo() { + IpAddressDump request = new IpAddressDump(); + request.isIpv6 = 1; + request.swIfIndex = IFACE_2_ID; + return request; + } + + default IpAddressDump dumpV6AddressesSubIfaceOne() { + IpAddressDump request = new IpAddressDump(); + request.swIfIndex = SUB_IFACE_ID; + request.isIpv6 = 1; + return request; + } + + + default IpNeighborDump dumpV4NeighborIfaceOne() { + IpNeighborDump request = new IpNeighborDump(); + request.swIfIndex = IFACE_ID; + request.isIpv6 = 0; + return request; + } + + default IpNeighborDump dumpV6NeighborsIfaceOne() { + IpNeighborDump request = new IpNeighborDump(); + request.swIfIndex = IFACE_ID; + request.isIpv6 = 1; + return request; + } + + default IpNeighborDump dumpV6NeighborsSubIfaceTwo() { + IpNeighborDump request = new IpNeighborDump(); + request.swIfIndex = SUB_IFACE_2_ID; + request.isIpv6 = 1; + return request; + } + + default IpNeighborDump dumpV4NeighborsSubIfaceOne() { + IpNeighborDump request = new IpNeighborDump(); + request.swIfIndex = SUB_IFACE_ID; + request.isIpv6 = 0; + return request; + } + + default void verifyList(@Nonnull final List expected, @Nonnull final List current) { + assertThat(current, hasSize(expected.size())); + assertTrue(expected.containsAll(current)); + } + + default IpNeighborDetailsReplyDump v4Neighbors() { + IpNeighborDetailsReplyDump fullDump = new IpNeighborDetailsReplyDump(); + fullDump.ipNeighborDetails = Arrays.asList( + neighborDump(IPV4_ONE_BYTES, 0, MAC_ONE_BYTES), + neighborDump(IPV4_TWO_BYTES, 0, MAC_TWO_BYTES)); + return fullDump; + } + + default IpNeighborDetailsReplyDump v6Neighbors() { + IpNeighborDetailsReplyDump fullDump = new IpNeighborDetailsReplyDump(); + fullDump.ipNeighborDetails = Arrays.asList( + neighborDump(IPV6_ONE_BYTES, 1, MAC_THREE_BYTES), + neighborDump(IPV6_TWO_BYTES, 1, MAC_FOUR_BYTES)); + return fullDump; + } + + default IpAddressDetailsReplyDump v4Addresses() { + IpAddressDetailsReplyDump fullDump = new IpAddressDetailsReplyDump(); + fullDump.ipAddressDetails = Arrays.asList( + addressDump(IPV4_ONE_BYTES, IPV4_ONE_PREFIX), + addressDump(IPV4_TWO_BYTES, IPV4_TWO_PREFIX)); + return fullDump; + } + + default IpAddressDetailsReplyDump v6Addresses() { + IpAddressDetailsReplyDump fullDump = new IpAddressDetailsReplyDump(); + fullDump.ipAddressDetails = Arrays.asList( + addressDump(IPV6_ONE_BYTES, IPV6_ONE_PREFIX), + addressDump(IPV6_TWO_BYTES, IPV6_TWO_PREFIX)); + return fullDump; + } + + static IpNeighborDetails neighborDump(byte[] address, int isIpv6, byte[] mac) { + IpNeighborDetails detail = new IpNeighborDetails(); + detail.ipAddress = address; + detail.isIpv6 = (byte) isIpv6; + detail.macAddress = mac; + return detail; + } + + static IpAddressDetails addressDump(byte[] address, byte prefix) { + IpAddressDetails details = new IpAddressDetails(); + details.ip = address; + details.prefixLength = prefix; + return details; + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java deleted file mode 100644 index e1b199ecc..000000000 --- a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/Ipv4AddressCustomizerTest.java +++ /dev/null @@ -1,275 +0,0 @@ -/* - * Copyright (c) 2016 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.v3po.interfacesstate.ip; - - -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; -import io.fd.hc2vpp.common.translate.util.Ipv4Translator; -import io.fd.hc2vpp.common.translate.util.NamingContext; -import io.fd.hc2vpp.v3po.interfacesstate.ip.v4.Ipv4AddressCustomizer; -import io.fd.honeycomb.translate.read.ReadFailedException; -import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; -import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; -import io.fd.vpp.jvpp.core.dto.IpAddressDetails; -import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; -import io.fd.vpp.jvpp.core.dto.IpAddressDump; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.CompletableFuture; -import java.util.stream.Collectors; -import org.hamcrest.Matchers; -import org.junit.Test; -import org.mockito.Mockito; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLength; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest implements - Ipv4Translator { - - private static final String IFACE_NAME = "eth0"; - private static final String IFACE_2_NAME = "eth1"; - private static final int IFACE_ID = 1; - private static final int IFACE_2_ID = 2; - private static final String IFC_CTX_NAME = "ifc-test-instance"; - - private NamingContext interfacesContext; - private InstanceIdentifier
ifaceOneAddressOneIdentifier; - private InstanceIdentifier
ifaceTwoAddressOneIdentifier; - private CacheKeyFactory cacheKeyFactory; - - public Ipv4AddressCustomizerTest() { - super(Address.class, Ipv4Builder.class); - } - - private static InstanceIdentifier
getId(final String address, final String ifaceName) { - return InstanceIdentifier.builder(InterfacesState.class) - .child(Interface.class, new InterfaceKey(ifaceName)) - .augmentation(Interface2.class) - .child(Ipv4.class) - .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address)))) - .build(); - } - - @Override - public void setUp() { - interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); - defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME); - defineMapping(mappingContext, IFACE_2_NAME, IFACE_2_ID, IFC_CTX_NAME); - - ifaceOneAddressOneIdentifier = - InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class, new InterfaceKey(IFACE_NAME)) - .augmentation(Interface2.class) - .child(Ipv4.class) - .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))); - ifaceTwoAddressOneIdentifier = - InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class, new InterfaceKey(IFACE_2_NAME)) - .augmentation(Interface2.class) - .child(Ipv4.class) - .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))); - - // to simulate complex key - cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class, - ImmutableSet.of(Interface.class)); - } - - @Override - protected ReaderCustomizer initCustomizer() { - return new Ipv4AddressCustomizer(api, interfacesContext); - } - - @Test - public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException { - //changed to mock to not store first dumped data(otherwise that double thenReturn on line 118 is not gonna work) - - IpAddressDetails detail1 = new IpAddressDetails(); - IpAddressDetails detail2 = new IpAddressDetails(); - - detail1.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detail2.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))); - - IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); - reply.ipAddressDetails = ImmutableList.of(detail1); - IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump(); - reply2.ipAddressDetails = ImmutableList.of(detail2); - - CompletableFuture future = new CompletableFuture<>(); - future.complete(reply); - CompletableFuture future2 = new CompletableFuture<>(); - future2.complete(reply2); - - when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2) - .thenReturn(future).thenReturn(future2); - when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2)) - .thenReturn(future(reply)).thenReturn(future(reply2)); - when(ctx.getModificationCache()).thenReturn(cache); - - - final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); - final InstanceIdentifier
id2 = getId("192.168.2.2", IFACE_2_NAME); - - final List ifc1Ids = getCustomizer().getAllIds(id, ctx); - assertThat(ifc1Ids.size(), is(1)); - assertThat(ifc1Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1")))); - final List ifc2Ids = getCustomizer().getAllIds(id2, ctx); - assertThat(ifc2Ids.size(), is(1)); - assertThat(ifc2Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2")))); - - AddressBuilder builder = new AddressBuilder(); - getCustomizer().readCurrentAttributes(id, builder, ctx); - assertEquals(builder.getIp().getValue(), "192.168.2.1"); - builder = new AddressBuilder(); - getCustomizer().readCurrentAttributes(id2, builder, ctx); - assertEquals(builder.getIp().getValue(), "192.168.2.2"); - } - - @Test - public void testReadCurrentAttributesSuccessfull() throws ReadFailedException { - IpAddressDetails detail1 = new IpAddressDetails(); - IpAddressDetails detail2 = new IpAddressDetails(); - IpAddressDetails detail3 = new IpAddressDetails(); - - detail1.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detail2.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))); - detail3.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))); - - IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); - reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3); - when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)); - when(ctx.getModificationCache()).thenReturn(cache); - - final AddressBuilder builder = new AddressBuilder(); - final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); - - getCustomizer().readCurrentAttributes(id, builder, ctx); - - assertEquals("192.168.2.1", builder.getIp().getValue()); - } - - @Test - public void testGetAllIdsFromSuccessfull() throws ReadFailedException { - IpAddressDetails detail1 = new IpAddressDetails(); - IpAddressDetails detail2 = new IpAddressDetails(); - IpAddressDetails detail3 = new IpAddressDetails(); - - detail1.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detail2.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))); - detail3.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))); - - IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); - reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3); - when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)); - when(ctx.getModificationCache()).thenReturn(cache); - - final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); - - List ids = getCustomizer().getAllIds(id, ctx).stream() - .map(key -> key.getIp()) - .collect(Collectors.toList()); - - assertEquals(3, ids.size()); - assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue())); - assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue())); - assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue())); - } - - @Test - public void testCachingScopeSpecificRequest() throws ReadFailedException { - fillCacheForTwoIfaces(); - final AddressBuilder ifaceOneAddressBuilder = new AddressBuilder(); - final AddressBuilder ifaceTwoAddressBuilder = new AddressBuilder(); - - getCustomizer().readCurrentAttributes(ifaceOneAddressOneIdentifier, ifaceOneAddressBuilder, ctx); - getCustomizer().readCurrentAttributes(ifaceTwoAddressOneIdentifier, ifaceTwoAddressBuilder, ctx); - - // addresses have caching scope of parent interface, so returned address should have respective prefix lengths - assertEquals("192.168.2.1", ifaceOneAddressBuilder.getIp().getValue()); - assertTrue(ifaceOneAddressBuilder.getSubnet() instanceof PrefixLength); - assertEquals(22, PrefixLength.class.cast(ifaceOneAddressBuilder.getSubnet()).getPrefixLength().intValue()); - - assertEquals("192.168.2.1", ifaceTwoAddressBuilder.getIp().getValue()); - assertTrue(ifaceTwoAddressBuilder.getSubnet() instanceof PrefixLength); - assertEquals(23, PrefixLength.class.cast(ifaceTwoAddressBuilder.getSubnet()).getPrefixLength().intValue()); - } - - @Test - public void testCachingScopeGetAll() throws ReadFailedException { - fillCacheForFirstIfaceSecondEmpty(); - - final List keysForIfaceOne = getCustomizer().getAllIds(ifaceOneAddressOneIdentifier, ctx); - assertThat(keysForIfaceOne, hasSize(1)); - final AddressKey keyIfaceOne = keysForIfaceOne.get(0); - assertEquals("192.168.2.1", keyIfaceOne.getIp().getValue()); - - final List keysForIfaceTwo = getCustomizer().getAllIds(ifaceTwoAddressOneIdentifier, ctx); - assertThat(keysForIfaceTwo, is(empty())); - } - - private void fillCacheForTwoIfaces() { - IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails(); - IpAddressDetails detailIfaceTwoAddressOne = new IpAddressDetails(); - IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump(); - IpAddressDetailsReplyDump replyIfaceTwo = new IpAddressDetailsReplyDump(); - - replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne); - replyIfaceTwo.ipAddressDetails = Arrays.asList(detailIfaceTwoAddressOne); - - detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detailIfaceOneAddressOne.prefixLength = 22; - - detailIfaceTwoAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detailIfaceTwoAddressOne.prefixLength = 23; - - cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne); - cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo); - } - - private void fillCacheForFirstIfaceSecondEmpty() { - IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails(); - IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump(); - replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne); - - detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detailIfaceOneAddressOne.prefixLength = 22; - - cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne); - cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump()); - } -} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/Ipv4CustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/Ipv4CustomizerTest.java deleted file mode 100644 index 9475d4664..000000000 --- a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/Ipv4CustomizerTest.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2016 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.v3po.interfacesstate.ip; - -import static org.mockito.Mockito.verifyZeroInteractions; - -import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; -import io.fd.hc2vpp.v3po.interfacesstate.ip.v4.Ipv4Customizer; -import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; -import org.junit.Test; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2Builder; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder; - -public class Ipv4CustomizerTest extends ReaderCustomizerTest { - - public Ipv4CustomizerTest() { - super(Ipv4.class, Interface2Builder.class); - } - - @Test - public void testReadCurrentAttributes() throws Exception { - customizer.readCurrentAttributes(null, null, ctx); - verifyZeroInteractions(api); - } - - @Override - protected ReaderCustomizer initCustomizer() { - return new Ipv4Customizer(api); - } -} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/SubInterfaceIpv4AddressCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/SubInterfaceIpv4AddressCustomizerTest.java deleted file mode 100644 index cfc7bc6dd..000000000 --- a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/SubInterfaceIpv4AddressCustomizerTest.java +++ /dev/null @@ -1,245 +0,0 @@ -/* - * Copyright (c) 2016 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.v3po.interfacesstate.ip; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import com.google.common.collect.ImmutableSet; -import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; -import io.fd.hc2vpp.common.translate.util.Ipv4Translator; -import io.fd.hc2vpp.common.translate.util.NamingContext; -import io.fd.hc2vpp.v3po.interfacesstate.ip.v4.subinterface.SubInterfaceIpv4AddressCustomizer; -import io.fd.honeycomb.translate.read.ReadFailedException; -import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; -import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory; -import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; -import io.fd.vpp.jvpp.core.dto.IpAddressDetails; -import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; -import java.util.Arrays; -import java.util.List; -import org.junit.Assert; -import org.junit.Test; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Address; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressKey; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class SubInterfaceIpv4AddressCustomizerTest extends ListReaderCustomizerTest - implements Ipv4Translator { - - private static final String IFC_CTX_NAME = "ifc-test-instance"; - private static final String IF_NAME = "local0"; - private static final int IF_INDEX = 1; - private static final String SUB_IF_NAME = "local0.1"; - private static final String SUB_IF_2_NAME = "local0.2"; - private static final long SUB_IF_ID = 1; - private static final int SUB_IF_INDEX = 11; - private static final int SUB_IF_2_INDEX = 12; - private static final InstanceIdentifier IP4_IID = - InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME)) - .augmentation(SubinterfaceStateAugmentation.class) - .child(SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(SUB_IF_ID)) - .child(Ipv4.class); - private static final Ipv4AddressNoZone IP1 = new Ipv4AddressNoZone("10.1.1.1"); - private static final Ipv4AddressNoZone IP2 = new Ipv4AddressNoZone("10.1.1.2"); - private static final short PREFIX_LENGTH = 16; - private InstanceIdentifier
ifaceOneAddressOneIdentifier; - private InstanceIdentifier
ifaceTwoAddressOneIdentifier; - private CacheKeyFactory cacheKeyFactory; - private NamingContext interfaceContext; - - public SubInterfaceIpv4AddressCustomizerTest() { - super(Address.class, Ipv4Builder.class); - } - - private static InstanceIdentifier
getId() { - return IP4_IID.child(Address.class); - } - - private static InstanceIdentifier
getId(final Ipv4AddressNoZone ip) { - return IP4_IID.child(Address.class, new AddressKey(ip)); - } - - @Override - protected void setUp() throws Exception { - interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); - - ifaceOneAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class, new InterfaceKey(IF_NAME)) - .augmentation(SubinterfaceStateAugmentation.class) - .child(SubInterfaces.class) - .child(SubInterface.class, new SubInterfaceKey(1L)) - .child(Ipv4.class) - .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))); - ifaceTwoAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class) - .child(Interface.class, new InterfaceKey(IF_NAME)) - .augmentation(SubinterfaceStateAugmentation.class) - .child(SubInterfaces.class) - .child(SubInterface.class, new SubInterfaceKey(2L)) - .child(Ipv4.class) - .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))); - - // to simulate complex key - cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class, - ImmutableSet.of(SubInterface.class)); - - defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); - defineMapping(mappingContext, SUB_IF_NAME, SUB_IF_INDEX, IFC_CTX_NAME); - defineMapping(mappingContext, SUB_IF_2_NAME, SUB_IF_2_INDEX, IFC_CTX_NAME); - } - - @Override - protected ListReaderCustomizer initCustomizer() { - return new SubInterfaceIpv4AddressCustomizer(api, interfaceContext); - } - - @Test - public void testRead() throws ReadFailedException { - final AddressBuilder builder = mock(AddressBuilder.class); - when(api.ipAddressDump(any())).thenReturn(future(dump())); - getCustomizer().readCurrentAttributes(getId(IP2), builder, ctx); - verify(builder).setIp(IP2); - verify(builder).setSubnet(new PrefixLengthBuilder().setPrefixLength(PREFIX_LENGTH).build()); - } - - @Test(expected = ReadFailedException.class) - public void testReadFailed() throws ReadFailedException { - when(api.ipAddressDump(any())).thenReturn(failedFuture()); - getCustomizer().readCurrentAttributes(getId(IP1), mock(AddressBuilder.class), ctx); - } - - @Test - public void testGetAllIds() throws ReadFailedException { - when(api.ipAddressDump(any())).thenReturn(future(dump())); - final List allIds = getCustomizer().getAllIds(getId(), ctx); - assertThat(allIds, hasSize(2)); - assertThat(allIds, containsInAnyOrder(new AddressKey(IP1), new AddressKey(IP2))); - } - - @Test(expected = ReadFailedException.class) - public void testGetAllIdsFailed() throws ReadFailedException { - when(api.ipAddressDump(any())).thenReturn(failedFuture()); - getCustomizer().getAllIds(getId(), ctx); - } - - @Test - public void testCacheScope() { - - } - - @Test - public void testCachingScopeSpecificRequest() throws ReadFailedException { - fillCacheForTwoIfaces(); - final AddressBuilder ifaceOneAddressBuilder = new AddressBuilder(); - final AddressBuilder ifaceTwoAddressBuilder = new AddressBuilder(); - - getCustomizer().readCurrentAttributes(ifaceOneAddressOneIdentifier, ifaceOneAddressBuilder, ctx); - getCustomizer().readCurrentAttributes(ifaceTwoAddressOneIdentifier, ifaceTwoAddressBuilder, ctx); - - // addresses have caching scope of parent interface, so returned address should have respective prefix lengths - assertEquals("192.168.2.1", ifaceOneAddressBuilder.getIp().getValue()); - assertTrue(ifaceOneAddressBuilder.getSubnet() instanceof PrefixLength); - assertEquals(22, PrefixLength.class.cast(ifaceOneAddressBuilder.getSubnet()).getPrefixLength().intValue()); - - assertEquals("192.168.2.1", ifaceTwoAddressBuilder.getIp().getValue()); - assertTrue(ifaceTwoAddressBuilder.getSubnet() instanceof PrefixLength); - assertEquals(23, PrefixLength.class.cast(ifaceTwoAddressBuilder.getSubnet()).getPrefixLength().intValue()); - } - - @Test - public void testCachingScopeGetAll() throws ReadFailedException { - fillCacheForFirstIfaceSecondEmpty(); - - final List keysForIfaceOne = getCustomizer().getAllIds(ifaceOneAddressOneIdentifier, ctx); - Assert.assertThat(keysForIfaceOne, hasSize(1)); - final AddressKey keyIfaceOne = keysForIfaceOne.get(0); - assertEquals("192.168.2.1", keyIfaceOne.getIp().getValue()); - - final List keysForIfaceTwo = getCustomizer().getAllIds(ifaceTwoAddressOneIdentifier, ctx); - Assert.assertThat(keysForIfaceTwo, is(empty())); - } - - private void fillCacheForTwoIfaces() { - IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails(); - IpAddressDetails detailIfaceTwoAddressOne = new IpAddressDetails(); - IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump(); - IpAddressDetailsReplyDump replyIfaceTwo = new IpAddressDetailsReplyDump(); - - replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne); - replyIfaceTwo.ipAddressDetails = Arrays.asList(detailIfaceTwoAddressOne); - - detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detailIfaceOneAddressOne.prefixLength = 22; - - detailIfaceTwoAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detailIfaceTwoAddressOne.prefixLength = 23; - - cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne); - cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo); - } - - private void fillCacheForFirstIfaceSecondEmpty() { - IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails(); - IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump(); - replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne); - - detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); - detailIfaceOneAddressOne.prefixLength = 22; - - cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne); - cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump()); - } - - private IpAddressDetailsReplyDump dump() { - final IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); - - final IpAddressDetails details1 = new IpAddressDetails(); - details1.ip = new byte[]{10, 1, 1, 1}; - details1.prefixLength = (byte) PREFIX_LENGTH; - reply.ipAddressDetails.add(details1); - - final IpAddressDetails details2 = new IpAddressDetails(); - details2.ip = new byte[]{10, 1, 1, 2}; - details2.prefixLength = (byte) PREFIX_LENGTH; - reply.ipAddressDetails.add(details2); - - return reply; - } -} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4AddressCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4AddressCustomizerTest.java new file mode 100644 index 000000000..58077d6e9 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4AddressCustomizerTest.java @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2016 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.v3po.interfacesstate.ip.v4; + + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.Ipv4Translator; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.v3po.interfacesstate.ip.InterfaceChildNodeTest; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory; +import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; +import io.fd.vpp.jvpp.core.dto.IpAddressDetails; +import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; +import io.fd.vpp.jvpp.core.dto.IpAddressDump; +import org.hamcrest.Matchers; +import org.junit.Test; +import org.mockito.Mockito; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLength; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.when; + +public class Ipv4AddressCustomizerTest extends ListReaderCustomizerTest implements + Ipv4Translator,InterfaceChildNodeTest { + + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + + private NamingContext interfacesContext; + private InstanceIdentifier
ifaceOneAddressOneIdentifier; + private InstanceIdentifier
ifaceTwoAddressOneIdentifier; + private CacheKeyFactory cacheKeyFactory; + + public Ipv4AddressCustomizerTest() { + super(Address.class, Ipv4Builder.class); + } + + private static InstanceIdentifier
getId(final String address, final String ifaceName) { + return InstanceIdentifier.builder(InterfacesState.class) + .child(Interface.class, new InterfaceKey(ifaceName)) + .augmentation(Interface2.class) + .child(Ipv4.class) + .child(Address.class, new AddressKey(new Ipv4AddressNoZone(new Ipv4Address(address)))) + .build(); + } + + @Override + public void setUp() { + interfacesContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + defineMapping(mappingContext, IFACE_NAME, IFACE_ID, IFC_CTX_NAME); + defineMapping(mappingContext, IFACE_2_NAME, IFACE_2_ID, IFC_CTX_NAME); + + ifaceOneAddressOneIdentifier = + InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IFACE_NAME)) + .augmentation(Interface2.class) + .child(Ipv4.class) + .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))); + ifaceTwoAddressOneIdentifier = + InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IFACE_2_NAME)) + .augmentation(Interface2.class) + .child(Ipv4.class) + .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))); + + // to simulate complex key + cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class, + ImmutableSet.of(Interface.class)); + } + + @Override + protected ReaderCustomizer initCustomizer() { + return new Ipv4AddressCustomizer(api, interfacesContext); + } + + @Test + public void testReadCurrentAttributesFor2Ifcs() throws ReadFailedException { + //changed to mock to not store first dumped data(otherwise that double thenReturn on line 118 is not gonna work) + + IpAddressDetails detail1 = new IpAddressDetails(); + IpAddressDetails detail2 = new IpAddressDetails(); + + detail1.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detail2.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))); + + IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); + reply.ipAddressDetails = ImmutableList.of(detail1); + IpAddressDetailsReplyDump reply2 = new IpAddressDetailsReplyDump(); + reply2.ipAddressDetails = ImmutableList.of(detail2); + + CompletableFuture future = new CompletableFuture<>(); + future.complete(reply); + CompletableFuture future2 = new CompletableFuture<>(); + future2.complete(reply2); + + when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future).thenReturn(future2) + .thenReturn(future).thenReturn(future2); + when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)).thenReturn(future(reply2)) + .thenReturn(future(reply)).thenReturn(future(reply2)); + when(ctx.getModificationCache()).thenReturn(cache); + + + final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); + final InstanceIdentifier
id2 = getId("192.168.2.2", IFACE_2_NAME); + + final List ifc1Ids = getCustomizer().getAllIds(id, ctx); + assertThat(ifc1Ids.size(), is(1)); + assertThat(ifc1Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.1")))); + final List ifc2Ids = getCustomizer().getAllIds(id2, ctx); + assertThat(ifc2Ids.size(), is(1)); + assertThat(ifc2Ids, Matchers.hasItem(new AddressKey(new Ipv4AddressNoZone("192.168.2.2")))); + + AddressBuilder builder = new AddressBuilder(); + getCustomizer().readCurrentAttributes(id, builder, ctx); + assertEquals(builder.getIp().getValue(), "192.168.2.1"); + builder = new AddressBuilder(); + getCustomizer().readCurrentAttributes(id2, builder, ctx); + assertEquals(builder.getIp().getValue(), "192.168.2.2"); + } + + @Test + public void testReadCurrentAttributesSuccessfull() throws ReadFailedException { + IpAddressDetails detail1 = new IpAddressDetails(); + IpAddressDetails detail2 = new IpAddressDetails(); + IpAddressDetails detail3 = new IpAddressDetails(); + + detail1.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detail2.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))); + detail3.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))); + + IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); + reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3); + when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)); + when(ctx.getModificationCache()).thenReturn(cache); + + final AddressBuilder builder = new AddressBuilder(); + final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); + + getCustomizer().readCurrentAttributes(id, builder, ctx); + + assertEquals("192.168.2.1", builder.getIp().getValue()); + } + + @Test + public void testGetAllIdsFromSuccessfull() throws ReadFailedException { + IpAddressDetails detail1 = new IpAddressDetails(); + IpAddressDetails detail2 = new IpAddressDetails(); + IpAddressDetails detail3 = new IpAddressDetails(); + + detail1.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detail2.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.2"))); + detail3.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.3"))); + + IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); + reply.ipAddressDetails = ImmutableList.of(detail1, detail2, detail3); + when(api.ipAddressDump(Mockito.any(IpAddressDump.class))).thenReturn(future(reply)); + when(ctx.getModificationCache()).thenReturn(cache); + + final InstanceIdentifier
id = getId("192.168.2.1", IFACE_NAME); + + List ids = getCustomizer().getAllIds(id, ctx).stream() + .map(key -> key.getIp()) + .collect(Collectors.toList()); + + assertEquals(3, ids.size()); + assertEquals(true, "192.168.2.1".equals(ids.get(0).getValue())); + assertEquals(true, "192.168.2.2".equals(ids.get(1).getValue())); + assertEquals(true, "192.168.2.3".equals(ids.get(2).getValue())); + } + + @Test + public void testCachingScopeSpecificRequest() throws ReadFailedException { + fillCacheForTwoIfaces(); + final AddressBuilder ifaceOneAddressBuilder = new AddressBuilder(); + final AddressBuilder ifaceTwoAddressBuilder = new AddressBuilder(); + + getCustomizer().readCurrentAttributes(ifaceOneAddressOneIdentifier, ifaceOneAddressBuilder, ctx); + getCustomizer().readCurrentAttributes(ifaceTwoAddressOneIdentifier, ifaceTwoAddressBuilder, ctx); + + // addresses have caching scope of parent interface, so returned address should have respective prefix lengths + assertEquals("192.168.2.1", ifaceOneAddressBuilder.getIp().getValue()); + assertTrue(ifaceOneAddressBuilder.getSubnet() instanceof PrefixLength); + assertEquals(22, PrefixLength.class.cast(ifaceOneAddressBuilder.getSubnet()).getPrefixLength().intValue()); + + assertEquals("192.168.2.1", ifaceTwoAddressBuilder.getIp().getValue()); + assertTrue(ifaceTwoAddressBuilder.getSubnet() instanceof PrefixLength); + assertEquals(23, PrefixLength.class.cast(ifaceTwoAddressBuilder.getSubnet()).getPrefixLength().intValue()); + } + + @Test + public void testCachingScopeGetAll() throws ReadFailedException { + fillCacheForFirstIfaceSecondEmpty(); + + final List keysForIfaceOne = getCustomizer().getAllIds(ifaceOneAddressOneIdentifier, ctx); + assertThat(keysForIfaceOne, hasSize(1)); + final AddressKey keyIfaceOne = keysForIfaceOne.get(0); + assertEquals("192.168.2.1", keyIfaceOne.getIp().getValue()); + + final List keysForIfaceTwo = getCustomizer().getAllIds(ifaceTwoAddressOneIdentifier, ctx); + assertThat(keysForIfaceTwo, is(empty())); + } + + private void fillCacheForTwoIfaces() { + IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails(); + IpAddressDetails detailIfaceTwoAddressOne = new IpAddressDetails(); + IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump(); + IpAddressDetailsReplyDump replyIfaceTwo = new IpAddressDetailsReplyDump(); + + replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne); + replyIfaceTwo.ipAddressDetails = Arrays.asList(detailIfaceTwoAddressOne); + + detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detailIfaceOneAddressOne.prefixLength = 22; + + detailIfaceTwoAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detailIfaceTwoAddressOne.prefixLength = 23; + + cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne); + cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo); + } + + private void fillCacheForFirstIfaceSecondEmpty() { + IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails(); + IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump(); + replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne); + + detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detailIfaceOneAddressOne.prefixLength = 22; + + cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne); + cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump()); + } +} diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4CustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4CustomizerTest.java new file mode 100644 index 000000000..da2213027 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4CustomizerTest.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2016 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.v3po.interfacesstate.ip.v4; + +import io.fd.hc2vpp.common.test.read.ReaderCustomizerTest; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2Builder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder; + +import static org.mockito.Mockito.verifyZeroInteractions; + +public class Ipv4CustomizerTest extends ReaderCustomizerTest { + + public Ipv4CustomizerTest() { + super(Ipv4.class, Interface2Builder.class); + } + + @Test + public void testReadCurrentAttributes() throws Exception { + customizer.readCurrentAttributes(null, null, ctx); + verifyZeroInteractions(api); + } + + @Override + protected ReaderCustomizer initCustomizer() { + return new Ipv4Customizer(api); + } +} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4NeighbourCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4NeighbourCustomizerTest.java new file mode 100644 index 000000000..d540dfa3b --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/Ipv4NeighbourCustomizerTest.java @@ -0,0 +1,77 @@ +/* + * 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.v3po.interfacesstate.ip.v4; + +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.v3po.interfacesstate.ip.InterfaceChildNodeTest; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Neighbor; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.NeighborBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.NeighborKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +public class Ipv4NeighbourCustomizerTest extends ListReaderCustomizerTest + implements InterfaceChildNodeTest { + + private InstanceIdentifier instanceIdentifier; + + public Ipv4NeighbourCustomizerTest() { + super(Neighbor.class, Ipv4Builder.class); + } + + @Override + protected void setUp() throws Exception { + instanceIdentifier = InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IFACE_NAME)) + .augmentation(Interface2.class) + .child(Ipv4.class) + .child(Neighbor.class, new NeighborKey(IPV4_ONE_ADDRESS)); + defineMapping(mappingContext, IFACE_NAME, IFACE_ID, INTERFACE_CONTEXT_NAME); + mockNeighborDump(api, dumpV4NeighborIfaceOne(), v4Neighbors()); + } + + @Test + public void testGetAll() throws ReadFailedException { + verifyList(Arrays.asList(new NeighborKey(IPV4_ONE_ADDRESS), new NeighborKey(IPV4_TWO_ADDRESS)), + getCustomizer().getAllIds(instanceIdentifier, ctx)); + } + + @Test + public void readCurrent() throws ReadFailedException { + final NeighborBuilder builder = new NeighborBuilder(); + getCustomizer().readCurrentAttributes(instanceIdentifier, builder, ctx); + + assertEquals(IPV4_ONE_ADDRESS, builder.getIp()); + assertEquals(MAC_ONE_ADDRESS, builder.getLinkLayerAddress()); + } + + @Override + protected ReaderCustomizer initCustomizer() { + return new Ipv4NeighbourCustomizer(api, INTERFACE_CONTEXT); + } +} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4AddressCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4AddressCustomizerTest.java new file mode 100644 index 000000000..736ff0031 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4AddressCustomizerTest.java @@ -0,0 +1,235 @@ +/* + * Copyright (c) 2016 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.v3po.interfacesstate.ip.v4.subinterface; + +import com.google.common.collect.ImmutableSet; +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.common.translate.util.Ipv4Translator; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; +import io.fd.honeycomb.translate.util.read.cache.CacheKeyFactory; +import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory; +import io.fd.vpp.jvpp.core.dto.IpAddressDetails; +import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump; +import org.junit.Assert; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Address; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class SubInterfaceIpv4AddressCustomizerTest extends ListReaderCustomizerTest + implements Ipv4Translator { + + private static final String IFC_CTX_NAME = "ifc-test-instance"; + private static final String IF_NAME = "local0"; + private static final int IF_INDEX = 1; + private static final String SUB_IF_NAME = "local0.1"; + private static final String SUB_IF_2_NAME = "local0.2"; + private static final long SUB_IF_ID = 1; + private static final int SUB_IF_INDEX = 11; + private static final int SUB_IF_2_INDEX = 12; + private static final InstanceIdentifier IP4_IID = + InstanceIdentifier.create(InterfacesState.class).child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(SubinterfaceStateAugmentation.class) + .child(SubInterfaces.class).child(SubInterface.class, new SubInterfaceKey(SUB_IF_ID)) + .child(Ipv4.class); + private static final Ipv4AddressNoZone IP1 = new Ipv4AddressNoZone("10.1.1.1"); + private static final Ipv4AddressNoZone IP2 = new Ipv4AddressNoZone("10.1.1.2"); + private static final short PREFIX_LENGTH = 16; + private InstanceIdentifier
ifaceOneAddressOneIdentifier; + private InstanceIdentifier
ifaceTwoAddressOneIdentifier; + private CacheKeyFactory cacheKeyFactory; + private NamingContext interfaceContext; + + public SubInterfaceIpv4AddressCustomizerTest() { + super(Address.class, Ipv4Builder.class); + } + + private static InstanceIdentifier
getId() { + return IP4_IID.child(Address.class); + } + + private static InstanceIdentifier
getId(final Ipv4AddressNoZone ip) { + return IP4_IID.child(Address.class, new AddressKey(ip)); + } + + @Override + protected void setUp() throws Exception { + interfaceContext = new NamingContext("generatedIfaceName", IFC_CTX_NAME); + + ifaceOneAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(SubinterfaceStateAugmentation.class) + .child(SubInterfaces.class) + .child(SubInterface.class, new SubInterfaceKey(1L)) + .child(Ipv4.class) + .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))); + ifaceTwoAddressOneIdentifier = InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IF_NAME)) + .augmentation(SubinterfaceStateAugmentation.class) + .child(SubInterfaces.class) + .child(SubInterface.class, new SubInterfaceKey(2L)) + .child(Ipv4.class) + .child(Address.class, new AddressKey(new Ipv4AddressNoZone("192.168.2.1"))); + + // to simulate complex key + cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(IpAddressDetailsReplyDump.class, + ImmutableSet.of(SubInterface.class)); + + defineMapping(mappingContext, IF_NAME, IF_INDEX, IFC_CTX_NAME); + defineMapping(mappingContext, SUB_IF_NAME, SUB_IF_INDEX, IFC_CTX_NAME); + defineMapping(mappingContext, SUB_IF_2_NAME, SUB_IF_2_INDEX, IFC_CTX_NAME); + } + + @Override + protected ListReaderCustomizer initCustomizer() { + return new SubInterfaceIpv4AddressCustomizer(api, interfaceContext); + } + + @Test + public void testRead() throws ReadFailedException { + final AddressBuilder builder = mock(AddressBuilder.class); + when(api.ipAddressDump(any())).thenReturn(future(dump())); + getCustomizer().readCurrentAttributes(getId(IP2), builder, ctx); + verify(builder).setIp(IP2); + verify(builder).setSubnet(new PrefixLengthBuilder().setPrefixLength(PREFIX_LENGTH).build()); + } + + @Test(expected = ReadFailedException.class) + public void testReadFailed() throws ReadFailedException { + when(api.ipAddressDump(any())).thenReturn(failedFuture()); + getCustomizer().readCurrentAttributes(getId(IP1), mock(AddressBuilder.class), ctx); + } + + @Test + public void testGetAllIds() throws ReadFailedException { + when(api.ipAddressDump(any())).thenReturn(future(dump())); + final List allIds = getCustomizer().getAllIds(getId(), ctx); + assertThat(allIds, hasSize(2)); + assertThat(allIds, containsInAnyOrder(new AddressKey(IP1), new AddressKey(IP2))); + } + + @Test(expected = ReadFailedException.class) + public void testGetAllIdsFailed() throws ReadFailedException { + when(api.ipAddressDump(any())).thenReturn(failedFuture()); + getCustomizer().getAllIds(getId(), ctx); + } + + @Test + public void testCachingScopeSpecificRequest() throws ReadFailedException { + fillCacheForTwoIfaces(); + final AddressBuilder ifaceOneAddressBuilder = new AddressBuilder(); + final AddressBuilder ifaceTwoAddressBuilder = new AddressBuilder(); + + getCustomizer().readCurrentAttributes(ifaceOneAddressOneIdentifier, ifaceOneAddressBuilder, ctx); + getCustomizer().readCurrentAttributes(ifaceTwoAddressOneIdentifier, ifaceTwoAddressBuilder, ctx); + + // addresses have caching scope of parent interface, so returned address should have respective prefix lengths + assertEquals("192.168.2.1", ifaceOneAddressBuilder.getIp().getValue()); + assertTrue(ifaceOneAddressBuilder.getSubnet() instanceof PrefixLength); + assertEquals(22, PrefixLength.class.cast(ifaceOneAddressBuilder.getSubnet()).getPrefixLength().intValue()); + + assertEquals("192.168.2.1", ifaceTwoAddressBuilder.getIp().getValue()); + assertTrue(ifaceTwoAddressBuilder.getSubnet() instanceof PrefixLength); + assertEquals(23, PrefixLength.class.cast(ifaceTwoAddressBuilder.getSubnet()).getPrefixLength().intValue()); + } + + @Test + public void testCachingScopeGetAll() throws ReadFailedException { + fillCacheForFirstIfaceSecondEmpty(); + + final List keysForIfaceOne = getCustomizer().getAllIds(ifaceOneAddressOneIdentifier, ctx); + Assert.assertThat(keysForIfaceOne, hasSize(1)); + final AddressKey keyIfaceOne = keysForIfaceOne.get(0); + assertEquals("192.168.2.1", keyIfaceOne.getIp().getValue()); + + final List keysForIfaceTwo = getCustomizer().getAllIds(ifaceTwoAddressOneIdentifier, ctx); + Assert.assertThat(keysForIfaceTwo, is(empty())); + } + + private void fillCacheForTwoIfaces() { + IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails(); + IpAddressDetails detailIfaceTwoAddressOne = new IpAddressDetails(); + IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump(); + IpAddressDetailsReplyDump replyIfaceTwo = new IpAddressDetailsReplyDump(); + + replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne); + replyIfaceTwo.ipAddressDetails = Arrays.asList(detailIfaceTwoAddressOne); + + detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detailIfaceOneAddressOne.prefixLength = 22; + + detailIfaceTwoAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detailIfaceTwoAddressOne.prefixLength = 23; + + cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne); + cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), replyIfaceTwo); + } + + private void fillCacheForFirstIfaceSecondEmpty() { + IpAddressDetails detailIfaceOneAddressOne = new IpAddressDetails(); + IpAddressDetailsReplyDump replyIfaceOne = new IpAddressDetailsReplyDump(); + replyIfaceOne.ipAddressDetails = Arrays.asList(detailIfaceOneAddressOne); + + detailIfaceOneAddressOne.ip = ipv4AddressNoZoneToArray(new Ipv4AddressNoZone(new Ipv4Address("192.168.2.1"))); + detailIfaceOneAddressOne.prefixLength = 22; + + cache.put(cacheKeyFactory.createKey(ifaceOneAddressOneIdentifier), replyIfaceOne); + cache.put(cacheKeyFactory.createKey(ifaceTwoAddressOneIdentifier), new IpAddressDetailsReplyDump()); + } + + private IpAddressDetailsReplyDump dump() { + final IpAddressDetailsReplyDump reply = new IpAddressDetailsReplyDump(); + + final IpAddressDetails details1 = new IpAddressDetails(); + details1.ip = new byte[]{10, 1, 1, 1}; + details1.prefixLength = (byte) PREFIX_LENGTH; + reply.ipAddressDetails.add(details1); + + final IpAddressDetails details2 = new IpAddressDetails(); + details2.ip = new byte[]{10, 1, 1, 2}; + details2.prefixLength = (byte) PREFIX_LENGTH; + reply.ipAddressDetails.add(details2); + + return reply; + } +} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4NeighbourCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4NeighbourCustomizerTest.java new file mode 100644 index 000000000..02fe52457 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v4/subinterface/SubInterfaceIpv4NeighbourCustomizerTest.java @@ -0,0 +1,84 @@ +/* + * 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.v3po.interfacesstate.ip.v4.subinterface; + +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.v3po.interfacesstate.ip.InterfaceChildNodeTest; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Neighbor; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.NeighborBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.NeighborKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +public class SubInterfaceIpv4NeighbourCustomizerTest extends ListReaderCustomizerTest + implements InterfaceChildNodeTest { + + private InstanceIdentifier instanceIdentifier; + + public SubInterfaceIpv4NeighbourCustomizerTest() { + super(Neighbor.class, Ipv4Builder.class); + } + + @Override + protected void setUp() throws Exception { + instanceIdentifier = InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IFACE_NAME)) + .augmentation(SubinterfaceStateAugmentation.class) + .child(SubInterfaces.class) + .child(SubInterface.class, new SubInterfaceKey((long) SUB_IFACE_ID)) + .child(Ipv4.class) + .child(Neighbor.class, new NeighborKey(IPV4_TWO_ADDRESS)); + + defineMapping(mappingContext, IFACE_NAME, IFACE_ID, INTERFACE_CONTEXT_NAME); + defineMapping(mappingContext, SUB_IFACE_NAME, SUB_IFACE_ID, INTERFACE_CONTEXT_NAME); + mockNeighborDump(api, dumpV4NeighborsSubIfaceOne(), v4Neighbors()); + } + + @Test + public void testGetAll() throws ReadFailedException { + verifyList(Arrays.asList(new NeighborKey(IPV4_ONE_ADDRESS), new NeighborKey(IPV4_TWO_ADDRESS)), + getCustomizer().getAllIds(instanceIdentifier, ctx)); + } + + @Test + public void testReadCurrent() throws ReadFailedException { + final NeighborBuilder neighborBuilder = new NeighborBuilder(); + getCustomizer().readCurrentAttributes(instanceIdentifier, neighborBuilder, ctx); + + assertEquals(MAC_TWO_ADDRESS, neighborBuilder.getLinkLayerAddress()); + assertEquals(IPV4_TWO_ADDRESS, neighborBuilder.getIp()); + } + + @Override + protected ReaderCustomizer initCustomizer() { + return new SubInterfaceIpv4NeighbourCustomizer(api, INTERFACE_CONTEXT); + } +} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6AddressCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6AddressCustomizerTest.java new file mode 100644 index 000000000..487f56918 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6AddressCustomizerTest.java @@ -0,0 +1,78 @@ +/* + * 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.v3po.interfacesstate.ip.v6; + +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.v3po.interfacesstate.ip.InterfaceChildNodeTest; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv6; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv6Builder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv6.Address; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv6.AddressBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv6.AddressKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +public class Ipv6AddressCustomizerTest extends ListReaderCustomizerTest + implements InterfaceChildNodeTest { + + private InstanceIdentifier
instanceIdentifier; + + public Ipv6AddressCustomizerTest() { + super(Address.class, Ipv6Builder.class); + } + + @Override + protected void setUp() throws Exception { + instanceIdentifier = InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IFACE_2_NAME)) + .augmentation(Interface2.class) + .child(Ipv6.class) + .child(Address.class, new AddressKey(IPV6_TWO_ADDRESS_COMPRESSED)); + defineMapping(mappingContext, IFACE_2_NAME, IFACE_2_ID, INTERFACE_CONTEXT_NAME); + mockAddressDump(api, dumpV6AddressesIfaceTwo(), v6Addresses()); + } + + @Override + protected ReaderCustomizer initCustomizer() { + return new Ipv6AddressCustomizer(api, INTERFACE_CONTEXT); + } + + @Test + public void testGetAll() throws ReadFailedException { + verifyList(Arrays.asList( + new AddressKey(IPV6_ONE_ADDRESS_COMPRESSED), new AddressKey(IPV6_TWO_ADDRESS_COMPRESSED)), + getCustomizer().getAllIds(instanceIdentifier, ctx)); + } + + @Test + public void testReadCurrent() throws ReadFailedException { + AddressBuilder builder = new AddressBuilder(); + getCustomizer().readCurrentAttributes(instanceIdentifier, builder, ctx); + + assertEquals(IPV6_TWO_ADDRESS_COMPRESSED, builder.getIp()); + assertEquals(IPV6_TWO_PREFIX, builder.getPrefixLength().longValue()); + } +} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6NeighbourCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6NeighbourCustomizerTest.java new file mode 100644 index 000000000..6f5a08abb --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/Ipv6NeighbourCustomizerTest.java @@ -0,0 +1,80 @@ +/* + * 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.v3po.interfacesstate.ip.v6; + +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.v3po.interfacesstate.ip.InterfaceChildNodeTest; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv6; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv6Builder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv6.Neighbor; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv6.NeighborBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv6.NeighborKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + + +public class Ipv6NeighbourCustomizerTest extends ListReaderCustomizerTest + implements InterfaceChildNodeTest { + + private InstanceIdentifier instanceIdentifier; + + public Ipv6NeighbourCustomizerTest() { + super(Neighbor.class, Ipv6Builder.class); + } + + @Override + protected void setUp() throws Exception { + instanceIdentifier = InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IFACE_NAME)) + .augmentation(Interface2.class) + .child(Ipv6.class) + .child(Neighbor.class, new NeighborKey(IPV6_ONE_ADDRESS_COMPRESSED)); + defineMapping(mappingContext, IFACE_NAME, IFACE_ID, INTERFACE_CONTEXT_NAME); + mockNeighborDump(api, dumpV6NeighborsIfaceOne(), v6Neighbors()); + } + + @Test + public void testGetAll() throws ReadFailedException { + verifyList(Arrays.asList( + new NeighborKey(IPV6_ONE_ADDRESS_COMPRESSED), + new NeighborKey(IPV6_TWO_ADDRESS_COMPRESSED)), + getCustomizer().getAllIds(instanceIdentifier, ctx)); + } + + @Test + public void readCurrent() throws ReadFailedException { + final NeighborBuilder builder = new NeighborBuilder(); + getCustomizer().readCurrentAttributes(instanceIdentifier, builder, ctx); + + assertEquals(IPV6_ONE_ADDRESS_COMPRESSED, builder.getIp()); + assertEquals(MAC_THREE_ADDRESS, builder.getLinkLayerAddress()); + } + + @Override + protected ReaderCustomizer initCustomizer() { + return new Ipv6NeighbourCustomizer(api, INTERFACE_CONTEXT); + } +} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6AddressCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6AddressCustomizerTest.java new file mode 100644 index 000000000..51d9f603f --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6AddressCustomizerTest.java @@ -0,0 +1,74 @@ +/* + * 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.v3po.interfacesstate.ip.v6.subinterface; + +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.v3po.interfacesstate.ip.InterfaceChildNodeTest; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.Ipv6; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.Ipv6Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.Address; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.AddressBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.AddressKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.Arrays; + +public class SubInterfaceIpv6AddressCustomizerTest extends ListReaderCustomizerTest + implements InterfaceChildNodeTest { + + private InstanceIdentifier
instanceIdentifier; + + public SubInterfaceIpv6AddressCustomizerTest() { + super(Address.class, Ipv6Builder.class); + } + + @Override + protected void setUp() throws Exception { + instanceIdentifier = InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IFACE_NAME)) + .augmentation(SubinterfaceStateAugmentation.class) + .child(SubInterfaces.class) + .child(SubInterface.class, new SubInterfaceKey((long) SUB_IFACE_ID)) + .child(Ipv6.class) + .child(Address.class, new AddressKey(IPV6_TWO_ADDRESS_COMPRESSED)); + + defineMapping(mappingContext, IFACE_NAME, IFACE_ID, INTERFACE_CONTEXT_NAME); + defineMapping(mappingContext, SUB_IFACE_NAME, SUB_IFACE_ID, INTERFACE_CONTEXT_NAME); + mockAddressDump(api, dumpV6AddressesSubIfaceOne(), v6Addresses()); + } + + @Test + public void testGetAll() throws ReadFailedException { + verifyList(Arrays.asList( + new AddressKey(IPV6_ONE_ADDRESS_COMPRESSED), new AddressKey(IPV6_TWO_ADDRESS_COMPRESSED)), + getCustomizer().getAllIds(instanceIdentifier, ctx)); + } + + @Override + protected ReaderCustomizer initCustomizer() { + return new SubInterfaceIpv6AddressCustomizer(api, INTERFACE_CONTEXT); + } +} \ No newline at end of file diff --git a/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6NeighbourCustomizerTest.java b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6NeighbourCustomizerTest.java new file mode 100644 index 000000000..24fd17eb8 --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/hc2vpp/v3po/interfacesstate/ip/v6/subinterface/SubInterfaceIpv6NeighbourCustomizerTest.java @@ -0,0 +1,85 @@ +/* + * 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.v3po.interfacesstate.ip.v6.subinterface; + +import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest; +import io.fd.hc2vpp.v3po.interfacesstate.ip.InterfaceChildNodeTest; +import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ReaderCustomizer; +import org.junit.Test; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.SubinterfaceStateAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.SubInterfaces; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterfaceKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.Ipv6; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.Ipv6Builder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.Neighbor; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.NeighborBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip6.attributes.ipv6.NeighborKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +public class SubInterfaceIpv6NeighbourCustomizerTest extends ListReaderCustomizerTest + implements InterfaceChildNodeTest { + + private InstanceIdentifier instanceIdentifier; + + public SubInterfaceIpv6NeighbourCustomizerTest() { + super(Neighbor.class, Ipv6Builder.class); + } + + @Override + protected void setUp() throws Exception { + instanceIdentifier = InstanceIdentifier.create(InterfacesState.class) + .child(Interface.class, new InterfaceKey(IFACE_2_NAME)) + .augmentation(SubinterfaceStateAugmentation.class) + .child(SubInterfaces.class) + .child(SubInterface.class, new SubInterfaceKey((long) SUB_IFACE_2_ID)) + .child(Ipv6.class) + .child(Neighbor.class, new NeighborKey(IPV6_TWO_ADDRESS_COMPRESSED)); + defineMapping(mappingContext, IFACE_2_NAME, IFACE_2_ID, INTERFACE_CONTEXT_NAME); + defineMapping(mappingContext, SUB_IFACE_2_NAME, SUB_IFACE_2_ID, INTERFACE_CONTEXT_NAME); + mockNeighborDump(api, dumpV6NeighborsSubIfaceTwo(), v6Neighbors()); + } + + @Test + public void testGetAll() throws ReadFailedException { + verifyList(Arrays.asList( + new NeighborKey(IPV6_ONE_ADDRESS_COMPRESSED), + new NeighborKey(IPV6_TWO_ADDRESS_COMPRESSED)), + getCustomizer().getAllIds(instanceIdentifier, ctx)); + } + + @Test + public void readCurrent() throws ReadFailedException { + NeighborBuilder builder = new NeighborBuilder(); + getCustomizer().readCurrentAttributes(instanceIdentifier, builder, ctx); + + assertEquals(IPV6_TWO_ADDRESS_COMPRESSED, builder.getIp()); + assertEquals(MAC_FOUR_ADDRESS, builder.getLinkLayerAddress()); + } + + @Override + protected ReaderCustomizer initCustomizer() { + return new SubInterfaceIpv6NeighbourCustomizer(api, INTERFACE_CONTEXT); + } +} \ No newline at end of file -- cgit 1.2.3-korg