|
| 1 | +/******************************************************************************* |
| 2 | + * This file is part of OpenNMS(R). |
| 3 | + * |
| 4 | + * Copyright (C) 2019 The OpenNMS Group, Inc. |
| 5 | + * OpenNMS(R) is Copyright (C) 1999-2019 The OpenNMS Group, Inc. |
| 6 | + * |
| 7 | + * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. |
| 8 | + * |
| 9 | + * OpenNMS(R) is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License as published |
| 11 | + * by the Free Software Foundation, either version 3 of the License, |
| 12 | + * or (at your option) any later version. |
| 13 | + * |
| 14 | + * OpenNMS(R) is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License |
| 20 | + * along with OpenNMS(R). If not, see: |
| 21 | + * http://www.gnu.org/licenses/ |
| 22 | + * |
| 23 | + * For more information contact: |
| 24 | + * OpenNMS(R) Licensing <license@opennms.org> |
| 25 | + * http://www.opennms.org/ |
| 26 | + * http://www.opennms.com/ |
| 27 | + *******************************************************************************/ |
| 28 | + |
| 29 | +package org.opennms.core.mate.model; |
| 30 | + |
| 31 | +import com.google.common.cache.CacheBuilder; |
| 32 | +import com.google.common.cache.CacheLoader; |
| 33 | +import com.google.common.cache.LoadingCache; |
| 34 | +import org.opennms.core.mate.api.EntityScopeProvider; |
| 35 | +import org.opennms.core.mate.api.Scope; |
| 36 | + |
| 37 | +import java.net.InetAddress; |
| 38 | +import java.util.Objects; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +public class CachedEntityScopeProviderImpl implements EntityScopeProvider { |
| 42 | + |
| 43 | + private final static class Tuple<A, B> { |
| 44 | + final A a; |
| 45 | + final B b; |
| 46 | + |
| 47 | + public Tuple(A a, B b) { |
| 48 | + this.a = a; |
| 49 | + this.b = b; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean equals(Object o) { |
| 54 | + if (o == null || getClass() != o.getClass()) return false; |
| 55 | + Tuple<?, ?> tuple = (Tuple<?, ?>) o; |
| 56 | + return Objects.equals(a, tuple.a) && Objects.equals(b, tuple.b); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int hashCode() { |
| 61 | + return Objects.hash(a, b); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private final static class Triple<A, B, C> { |
| 66 | + final A a; |
| 67 | + final B b; |
| 68 | + final C c; |
| 69 | + |
| 70 | + public Triple(A a, B b, C c) { |
| 71 | + this.a = a; |
| 72 | + this.b = b; |
| 73 | + this.c = c; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean equals(Object o) { |
| 78 | + if (o == null || getClass() != o.getClass()) return false; |
| 79 | + Triple<?, ?, ?> triple = (Triple<?, ?, ?>) o; |
| 80 | + return Objects.equals(a, triple.a) && Objects.equals(b, triple.b) && Objects.equals(c, triple.c); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public int hashCode() { |
| 85 | + return Objects.hash(a, b, c); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private final LoadingCache<Integer, Scope> nodeScopes; |
| 90 | + private final LoadingCache<Tuple<Integer, String>, Scope> interfaceScopes; |
| 91 | + private final LoadingCache<Tuple<Integer, Integer>, Scope> interfaceScopesByIfIndex; |
| 92 | + private final LoadingCache<Triple<Integer, InetAddress, String>, Scope> serviceScopes; |
| 93 | + private final EntityScopeProvider entityScopeProvider; |
| 94 | + |
| 95 | + public CachedEntityScopeProviderImpl(final EntityScopeProvider entityScopeProvider, final long expireAfterWrite, final long expireAfterAccess, final long refreshAfterWrite, final long maximumSize) { |
| 96 | + this.entityScopeProvider = Objects.requireNonNull(entityScopeProvider); |
| 97 | + |
| 98 | + this.nodeScopes = createCache(expireAfterWrite, expireAfterAccess, refreshAfterWrite, maximumSize, new CacheLoader<>() { |
| 99 | + @Override |
| 100 | + public Scope load(final Integer integer) { |
| 101 | + return CachedEntityScopeProviderImpl.this.entityScopeProvider.getScopeForNode(integer); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + this.interfaceScopes = createCache(expireAfterWrite, expireAfterAccess, refreshAfterWrite, maximumSize, new CacheLoader<>() { |
| 106 | + @Override |
| 107 | + public Scope load(final Tuple<Integer, String> tuple) { |
| 108 | + return CachedEntityScopeProviderImpl.this.entityScopeProvider.getScopeForInterface(tuple.a, tuple.b); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + this.interfaceScopesByIfIndex = createCache(expireAfterWrite, expireAfterAccess, refreshAfterWrite, maximumSize, new CacheLoader<>() { |
| 113 | + @Override |
| 114 | + public Scope load(final Tuple<Integer, Integer> tuple) { |
| 115 | + return CachedEntityScopeProviderImpl.this.entityScopeProvider.getScopeForInterfaceByIfIndex(tuple.a, tuple.b); |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + this.serviceScopes = createCache(expireAfterWrite, expireAfterAccess, refreshAfterWrite, maximumSize, new CacheLoader<>() { |
| 120 | + @Override |
| 121 | + public Scope load(final Triple<Integer, InetAddress, String> triple) { |
| 122 | + return CachedEntityScopeProviderImpl.this.entityScopeProvider.getScopeForService(triple.a, triple.b, triple.c); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + private <K, V> LoadingCache<K, V> createCache(final long expireAfterWrite, final long expireAfterAccess, final long refreshAfterWrite, final long maximumSize, CacheLoader<K, V> loader) { |
| 128 | + final CacheBuilder<Object, Object> cacheBuilder = CacheBuilder.newBuilder(); |
| 129 | + |
| 130 | + if (expireAfterWrite >= 0) { |
| 131 | + cacheBuilder.expireAfterWrite(expireAfterWrite, TimeUnit.SECONDS); |
| 132 | + } |
| 133 | + |
| 134 | + if (expireAfterAccess >= 0) { |
| 135 | + cacheBuilder.expireAfterAccess(expireAfterAccess, TimeUnit.SECONDS); |
| 136 | + } |
| 137 | + |
| 138 | + if (refreshAfterWrite >= 0) { |
| 139 | + cacheBuilder.refreshAfterWrite(refreshAfterWrite, TimeUnit.SECONDS); |
| 140 | + } |
| 141 | + |
| 142 | + if (maximumSize > 0) { |
| 143 | + cacheBuilder.maximumSize(maximumSize); |
| 144 | + } |
| 145 | + |
| 146 | + return cacheBuilder.build(loader); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public Scope getScopeForScv() { |
| 151 | + return entityScopeProvider.getScopeForScv(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public Scope getScopeForNode(final Integer nodeId) { |
| 156 | + return nodeScopes.getUnchecked(nodeId); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public Scope getScopeForInterface(final Integer nodeId, final String ipAddress) { |
| 161 | + return interfaceScopes.getUnchecked(new Tuple<>(nodeId, ipAddress)); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public Scope getScopeForInterfaceByIfIndex(final Integer nodeId, final int ifIndex) { |
| 166 | + return interfaceScopesByIfIndex.getUnchecked(new Tuple<>(nodeId, ifIndex)); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Scope getScopeForService(final Integer nodeId, final InetAddress ipAddress, final String serviceName) { |
| 171 | + return serviceScopes.getUnchecked(new Triple<>(nodeId, ipAddress, serviceName)); |
| 172 | + } |
| 173 | +} |
0 commit comments