From 2a810b8e6fe88ceef3ef781bfddef79829834b45 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Mon, 16 May 2016 14:51:53 +0200 Subject: Fix interface dump caching Change-Id: I18d29bebf754c34bbc05e5c9cfb78d8aba87c205 Signed-off-by: Marek Gradzki --- .../v3po/interfacesstate/InterfaceUtils.java | 63 +++++++++++++--------- 1 file changed, 38 insertions(+), 25 deletions(-) (limited to 'v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java') diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java index 036018e88..1ee472745 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java @@ -17,6 +17,7 @@ package io.fd.honeycomb.v3po.translate.v3po.interfacesstate; import static com.google.common.base.Preconditions.checkNotNull; +import static io.fd.honeycomb.v3po.translate.v3po.interfacesstate.InterfaceCustomizer.getCachedInterfaceDump; import com.google.common.base.Preconditions; import com.google.common.collect.Iterables; @@ -26,6 +27,7 @@ import java.math.BigInteger; import java.util.Map; import java.util.Objects; import java.util.concurrent.CompletionStage; +import java.util.stream.Collectors; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType; @@ -92,15 +94,15 @@ public final class InterfaceUtils { } // TODO rename and move to V3poUtils + /** - * Reads first 6 bytes of supplied byte array and converts to string as Yang dictates - *

Replace later with + * Reads first 6 bytes of supplied byte array and converts to string as Yang dictates

Replace later with * https://git.opendaylight.org/gerrit/#/c/34869/10/model/ietf/ietf-type- util/src/main/ * java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java * * @param vppPhysAddress byte array of bytes in big endian order, constructing the network IF physical address. * @return String like "aa:bb:cc:dd:ee:ff" - * @throws NullPointerException if vppPhysAddress is null + * @throws NullPointerException if vppPhysAddress is null * @throws IllegalArgumentException if vppPhysAddress.length < 6 */ public static String vppPhysAddrToYang(@Nonnull final byte[] vppPhysAddress) { @@ -110,7 +112,7 @@ public final class InterfaceUtils { StringBuilder physAddr = new StringBuilder(); appendHexByte(physAddr, vppPhysAddress[0]); - for (int i=1; i < PHYSICAL_ADDRESS_LENGTH; i++) { + for (int i = 1; i < PHYSICAL_ADDRESS_LENGTH; i++) { physAddr.append(":"); appendHexByte(physAddr, vppPhysAddress[i]); } @@ -144,34 +146,34 @@ public final class InterfaceUtils { /** * Queries VPP for interface description given interface key. * - * @param futureJvpp VPP Java Future API - * @param key interface key - * @param index VPP index of the interface - * @param allInterfaces cached interfaces dump with all the interfaces. If interface not present, another dump all - * will be performed - * + * @param futureJvpp VPP Java Future API + * @param key interface key + * @param index VPP index of the interface + * @param ctx per-tx scope context containing cached dump with all the interfaces. If the cache is not + * available or outdated, another dump will be performed. * @return SwInterfaceDetails DTO or null if interface was not found - * * @throws IllegalArgumentException If interface cannot be found */ @Nonnull public static SwInterfaceDetails getVppInterfaceDetails(@Nonnull final FutureJVpp futureJvpp, @Nonnull InterfaceKey key, final int index, - @Nonnull final Map allInterfaces) { + @Nonnull final Context ctx) { final SwInterfaceDump request = new SwInterfaceDump(); request.nameFilter = key.getName().getBytes(); request.nameFilterValid = 1; + final Map allInterfaces = getCachedInterfaceDump(ctx); + + // Returned cached if available + if (allInterfaces.containsKey(index)) { + return allInterfaces.get(index); + } + CompletionStage requestFuture = futureJvpp.swInterfaceDump(request); SwInterfaceDetailsReplyDump ifaces = V3poUtils.getReply(requestFuture.toCompletableFuture()); if (null == ifaces || null == ifaces.swInterfaceDetails || ifaces.swInterfaceDetails.isEmpty()) { request.nameFilterValid = 0; - // Returned cached if available - if(allInterfaces.containsKey(index)) { - return allInterfaces.get(index); - } - LOG.warn("VPP returned null instead of interface by key {} and its not cached", key.getName()); LOG.warn("Iterating through all the interfaces to find interface: {}", key.getName()); @@ -179,10 +181,20 @@ public final class InterfaceUtils { requestFuture = futureJvpp.swInterfaceDump(request); ifaces = V3poUtils.getReply(requestFuture.toCompletableFuture()); - return ifaces.swInterfaceDetails.stream().filter((swIfc) -> swIfc.swIfIndex == index) - .findFirst().orElseThrow(() -> new IllegalArgumentException("Unable to find interface " + key.getName())); + // Update the cache + allInterfaces.clear(); + allInterfaces + .putAll(ifaces.swInterfaceDetails.stream().collect(Collectors.toMap(d -> d.swIfIndex, d -> d))); + + if (allInterfaces.containsKey(index)) { + return allInterfaces.get(index); + } + throw new IllegalArgumentException("Unable to find interface " + key.getName()); } - return Iterables.getOnlyElement(ifaces.swInterfaceDetails); + + final SwInterfaceDetails iface = Iterables.getOnlyElement(ifaces.swInterfaceDetails); + allInterfaces.put(index, iface); // update the cache + return iface; } /** @@ -193,15 +205,15 @@ public final class InterfaceUtils { */ @Nonnull public static Class getInterfaceType(@Nonnull final String interfaceName) { - if(interfaceName.startsWith("tap")) { + if (interfaceName.startsWith("tap")) { return Tap.class; } - if(interfaceName.startsWith("vxlan")) { + if (interfaceName.startsWith("vxlan")) { return VxlanTunnel.class; } - if(interfaceName.startsWith("VirtualEthernet")) { + if (interfaceName.startsWith("VirtualEthernet")) { return VhostUser.class; } @@ -210,8 +222,9 @@ public final class InterfaceUtils { static boolean isInterfaceOfType(final Context ctx, final int index, final Class ifcType) { - final SwInterfaceDetails cachedDetails = checkNotNull(InterfaceCustomizer.getCachedInterfaceDump(ctx).get(index), - "Interface {} cannot be found in context", index); + final SwInterfaceDetails cachedDetails = + checkNotNull(getCachedInterfaceDump(ctx).get(index), + "Interface {} cannot be found in context", index); return ifcType.equals(getInterfaceType(V3poUtils.toString(cachedDetails.interfaceName))); } } -- cgit 1.2.3-korg