X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=mappingservice%2Finmemorydb%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Flispflowmapping%2Finmemorydb%2FHashMapDb.java;h=3e41dbf16ad3665e47090528fd2fa443c47b4710;hb=refs%2Fchanges%2F04%2F48104%2F2;hp=d826b7b8fc4aadc8b92975e5a009c23184125b0e;hpb=ec1865ee3d56e9ed403324dfd87ad79c16cf614b;p=lispflowmapping.git diff --git a/mappingservice/inmemorydb/src/main/java/org/opendaylight/lispflowmapping/inmemorydb/HashMapDb.java b/mappingservice/inmemorydb/src/main/java/org/opendaylight/lispflowmapping/inmemorydb/HashMapDb.java index d826b7b8f..3e41dbf16 100644 --- a/mappingservice/inmemorydb/src/main/java/org/opendaylight/lispflowmapping/inmemorydb/HashMapDb.java +++ b/mappingservice/inmemorydb/src/main/java/org/opendaylight/lispflowmapping/inmemorydb/HashMapDb.java @@ -8,23 +8,18 @@ package org.opendaylight.lispflowmapping.inmemorydb; -import java.util.Date; -import java.util.Map; import java.util.AbstractMap.SimpleImmutableEntry; +import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.TimeUnit; - import org.opendaylight.lispflowmapping.inmemorydb.radixtrie.RadixTrie; import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO; import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor; import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry; -import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys; import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinary; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinary; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid; -import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.lfm.mappingservice.dao.inmemorydb.config.rev151007.InmemorydbConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -32,27 +27,11 @@ public class HashMapDb implements ILispDAO, AutoCloseable { protected static final Logger LOG = LoggerFactory.getLogger(HashMapDb.class); private static final Object TABLES = (Object) "tables"; - private static final int DEFAULT_RECORD_TIMEOUT = 240; // SB registered mapping entries time out after 4 min - private ConcurrentMap> data = new ConcurrentHashMap>(); - private TimeUnit timeUnit = TimeUnit.SECONDS; - private int recordTimeOut; - - public HashMapDb() { - this.recordTimeOut = DEFAULT_RECORD_TIMEOUT; - } - - public HashMapDb(final InmemorydbConfig inmemoryConfig) { - if (inmemoryConfig.getRecordTimeout() <= 0) { - this.recordTimeOut = DEFAULT_RECORD_TIMEOUT; - } else { - this.recordTimeOut = inmemoryConfig.getRecordTimeout(); - } - } + private ConcurrentMap> data = new ConcurrentHashMap<>(); // IPv4 and IPv6 radix tries used for longest prefix matching - private RadixTrie ip4Trie = new RadixTrie(32, true); - private RadixTrie ip6Trie = new RadixTrie(128, true); + private RadixTrie ip4Trie = new RadixTrie<>(32, true); + private RadixTrie ip6Trie = new RadixTrie<>(128, true); public void tryAddToIpTrie(Object key) { if (key instanceof Eid) { @@ -92,19 +71,22 @@ public class HashMapDb implements ILispDAO, AutoCloseable { return data.get(key); } + private RadixTrie.TrieNode lookupBestNode(Eid eid) { + if (eid.getAddress() instanceof Ipv4PrefixBinary) { + Ipv4PrefixBinary prefix = (Ipv4PrefixBinary) eid.getAddress(); + return ip4Trie.lookupBest(prefix.getIpv4AddressBinary().getValue(), prefix.getIpv4MaskLength()); + } else if (eid.getAddress() instanceof Ipv6PrefixBinary) { + Ipv6PrefixBinary prefix = (Ipv6PrefixBinary) eid.getAddress(); + return ip6Trie.lookupBest(prefix.getIpv6AddressBinary().getValue(), prefix.getIpv6MaskLength()); + } + return null; + } + @Override public Map getBest(Object key) { if (key instanceof Eid) { Eid eid = (Eid) key; - RadixTrie.TrieNode node = null; - - if (eid.getAddress() instanceof Ipv4PrefixBinary) { - Ipv4PrefixBinary prefix = (Ipv4PrefixBinary) eid.getAddress(); - node = ip4Trie.lookupBest(prefix.getIpv4AddressBinary().getValue(), prefix.getIpv4MaskLength()); - } else if (eid.getAddress() instanceof Ipv6PrefixBinary) { - Ipv6PrefixBinary prefix = (Ipv6PrefixBinary) eid.getAddress(); - node = ip6Trie.lookupBest(prefix.getIpv6AddressBinary().getValue(), prefix.getIpv6MaskLength()); - } + RadixTrie.TrieNode node = lookupBestNode(eid); if (node == null) { return get(key); } @@ -119,21 +101,13 @@ public class HashMapDb implements ILispDAO, AutoCloseable { if (key instanceof Eid) { Eid eid = (Eid) key; - RadixTrie.TrieNode node = null; - - if (eid.getAddress() instanceof Ipv4PrefixBinary) { - Ipv4PrefixBinary prefix = (Ipv4PrefixBinary) eid.getAddress(); - node = ip4Trie.lookupBest(prefix.getIpv4AddressBinary().getValue(), prefix.getIpv4MaskLength()); - } else if (eid.getAddress() instanceof Ipv6PrefixBinary) { - Ipv6PrefixBinary prefix = (Ipv6PrefixBinary) eid.getAddress(); - node = ip6Trie.lookupBest(prefix.getIpv6AddressBinary().getValue(), prefix.getIpv6MaskLength()); - } + RadixTrie.TrieNode node = lookupBestNode(eid); if (node == null) { data = get(key); - return (data == null) ? null : new SimpleImmutableEntry>((Eid)key, data); + return (data == null) ? null : new SimpleImmutableEntry<>((Eid)key, data); } data = get(node.data()); - return (data == null) ? null : new SimpleImmutableEntry>((Eid)node.data(), data); + return (data == null) ? null : new SimpleImmutableEntry<>((Eid)node.data(), data); } return null; } @@ -146,6 +120,7 @@ public class HashMapDb implements ILispDAO, AutoCloseable { } } } + @Override public Eid getWidestNegativePrefix(Eid key) { RadixTrie.TrieNode node = null; @@ -199,41 +174,6 @@ public class HashMapDb implements ILispDAO, AutoCloseable { data.clear(); } - // TODO: this should be moved outside of DAO implementation - public void cleanOld() { - getAll(new IRowVisitor() { - public void visitRow(Object keyId, String valueKey, Object value) { - if (value != null && valueKey instanceof String && ((String) valueKey).equals(SubKeys.REGDATE)) { - Date date = (Date) value; - if (isExpired(date)) { - removeSpecific(keyId, SubKeys.RECORD); - } - } - } - - private boolean isExpired(Date date) { - return System.currentTimeMillis() - date.getTime() - > TimeUnit.MILLISECONDS.convert(recordTimeOut, timeUnit); - } - }); - } - - public TimeUnit getTimeUnit() { - return timeUnit; - } - - public void setRecordTimeOut(int recordTimeOut) { - this.recordTimeOut = recordTimeOut; - } - - public int getRecordTimeOut() { - return recordTimeOut; - } - - public void setTimeUnit(TimeUnit timeUnit) { - this.timeUnit = timeUnit; - } - public void close() throws Exception { data.clear(); }