Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.opencord.sadis.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.google.common.cache.Cache; |
| 20 | import com.google.common.cache.CacheBuilder; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
| 27 | import java.net.MalformedURLException; |
| 28 | import java.net.URL; |
| 29 | import java.util.Map; |
| 30 | import java.util.concurrent.TimeUnit; |
| 31 | |
| 32 | import org.onosproject.codec.JsonCodec; |
| 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.net.config.ConfigFactory; |
| 35 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 36 | import org.opencord.sadis.BaseInformation; |
| 37 | import org.opencord.sadis.BaseConfig; |
| 38 | import org.opencord.sadis.BaseInformationService; |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 39 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 40 | import java.util.Set; |
| 41 | |
| 42 | |
| 43 | public abstract class InformationAdapter<T extends BaseInformation, K extends BaseConfig<T>> |
| 44 | implements BaseInformationService<T> { |
| 45 | |
| 46 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 47 | |
| 48 | protected static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0; |
| 49 | protected static final long DEFAULT_TTL = 0; |
| 50 | protected String url; |
| 51 | protected ObjectMapper mapper; |
| 52 | protected Cache<String, T> cache; |
| 53 | protected int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE; |
| 54 | protected long cacheEntryTtl = DEFAULT_TTL; |
| 55 | |
| 56 | protected Map<String, T> localCfgData = null; |
| 57 | |
| 58 | public final void updateConfig(NetworkConfigRegistry cfgService) { |
| 59 | K cfg = getConfig(cfgService); |
| 60 | if (cfg == null) { |
| 61 | this.log.warn("Configuration not available"); |
| 62 | return; |
| 63 | } |
| 64 | this.log.info("Cache Max Size: {}", cfg.getCacheMaxSize()); |
| 65 | this.log.info("Cache TTL: {}", cfg.getCacheTtl().getSeconds()); |
| 66 | this.log.info("Entries: {}", cfg.getEntries()); |
| 67 | |
| 68 | configure(cfg); |
| 69 | } |
| 70 | |
| 71 | public K getConfig(NetworkConfigRegistry cfgService) { |
| 72 | return cfgService.getConfig(getAppId(), getConfigClass()); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Configures the Adapter for data source and cache parameters. |
| 77 | * |
| 78 | * @param cfg Configuration data. |
| 79 | */ |
| 80 | public void configure(K cfg) { |
| 81 | |
| 82 | String url = null; |
| 83 | try { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 84 | if (cfg.getUrl() != null) { |
| 85 | url = cfg.getUrl().toString(); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 86 | } |
| 87 | } catch (MalformedURLException mUrlEx) { |
| 88 | log.error("Invalid URL specified: {}", mUrlEx); |
| 89 | } |
| 90 | |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 91 | // always load local data |
| 92 | localCfgData = Maps.newConcurrentMap(); |
| 93 | cfg.getEntries().forEach(entry -> |
| 94 | localCfgData.put(entry.id(), entry)); |
| 95 | |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 96 | int maximumCacheSeize = cfg.getCacheMaxSize(); |
| 97 | long cacheEntryTtl = cfg.getCacheTtl().getSeconds(); |
| 98 | |
| 99 | // Rebuild cache if needed |
| 100 | if (isUrlChanged(url) || maximumCacheSeize != this.maxiumCacheSize || |
| 101 | cacheEntryTtl != this.cacheEntryTtl) { |
| 102 | this.maxiumCacheSize = maximumCacheSeize; |
| 103 | this.cacheEntryTtl = cacheEntryTtl; |
| 104 | this.url = url; |
| 105 | |
| 106 | Cache<String, T> newCache = CacheBuilder.newBuilder() |
| 107 | .maximumSize(maxiumCacheSize).expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build(); |
| 108 | Cache<String, T> oldCache = cache; |
| 109 | |
| 110 | synchronized (this) { |
| 111 | cache = newCache; |
| 112 | } |
| 113 | |
| 114 | oldCache.invalidateAll(); |
| 115 | oldCache.cleanUp(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | protected boolean isUrlChanged(String url) { |
| 120 | if (url == null && this.url == null) { |
| 121 | return false; |
| 122 | } |
| 123 | return !((url == this.url) || (url != null && url.equals(this.url))); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * (non-Javadoc) |
| 128 | * |
| 129 | * @see |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 130 | * org.opencord.sadis.SadisService#clearLocalData() |
| 131 | */ |
| 132 | @Override |
| 133 | public void clearLocalData() { |
| 134 | localCfgData.clear(); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * (non-Javadoc) |
| 139 | * |
| 140 | * @see |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 141 | * org.opencord.sadis.SadisService#clearCache() |
| 142 | */ |
| 143 | @Override |
| 144 | public void invalidateAll() { |
| 145 | cache.invalidateAll(); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * (non-Javadoc) |
| 150 | * |
| 151 | * @see |
| 152 | * org.opencord.sadis.BaseInformationService#invalidateId() |
| 153 | */ |
| 154 | @Override |
| 155 | public void invalidateId(String id) { |
| 156 | cache.invalidate(id); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * (non-Javadoc) |
| 161 | * |
| 162 | * @see |
| 163 | * org.opencord.sadis.BaseInformationService#getfromCache(java.lang. |
| 164 | * String) |
| 165 | */ |
| 166 | @Override |
| 167 | public T getfromCache(String id) { |
| 168 | Cache<String, T> local; |
| 169 | synchronized (this) { |
| 170 | local = cache; |
| 171 | } |
| 172 | T info = local.getIfPresent(id); |
| 173 | if (info != null) { |
| 174 | return info; |
| 175 | } |
| 176 | return null; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * (non-Javadoc) |
| 181 | * |
| 182 | * @see |
| 183 | * org.opencord.sadis.BaseInformationService#get(java.lang. |
| 184 | * String) |
| 185 | */ |
| 186 | @Override |
| 187 | public T get(String id) { |
| 188 | Cache<String, T> local; |
| 189 | synchronized (this) { |
| 190 | local = cache; |
| 191 | } |
| 192 | |
| 193 | T info = local.getIfPresent(id); |
| 194 | if (info != null) { |
| 195 | return info; |
| 196 | } |
| 197 | |
| 198 | /* |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 199 | * Not in cache, check for it in the locally configured data, |
| 200 | * if it's not there and we have a URL configured |
| 201 | * we can attempt to get it from there |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 202 | */ |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 203 | |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 204 | log.debug("Getting data from local config"); |
| 205 | info = (localCfgData == null) ? null : localCfgData.get(id); |
| 206 | |
| 207 | if (log.isTraceEnabled()) { |
| 208 | if (info == null) { |
| 209 | log.trace("Data not found in local config."); |
| 210 | } else { |
| 211 | log.trace("Found data in local config."); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 212 | } |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | if (info == null && this.url != null) { |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 216 | // Augment URL with query parameters |
| 217 | String urlWithSubId = this.url.replaceAll("%s", id); |
Matteo Scandolo | fd4d68d | 2020-10-08 17:37:56 -0700 | [diff] [blame] | 218 | log.debug("Getting data from the remote URL {}", urlWithSubId); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 219 | |
| 220 | try (InputStream io = new URL(urlWithSubId).openStream()) { |
| 221 | info = mapper.readValue(io, getInformationClass()); |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 222 | } catch (IOException e) { |
| 223 | // TODO use a better http library that allows us to read status code |
| 224 | log.debug("Exception while reading remote data {} ", e.getMessage()); |
| 225 | } |
| 226 | } |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 227 | |
| 228 | if (info != null) { |
| 229 | local.put(id, info); |
yasin sapli | 2c93ddb | 2021-06-11 17:46:26 +0300 | [diff] [blame] | 230 | log.debug("Returning Info {}", info); |
Matteo Scandolo | c3e5372 | 2020-12-08 15:14:53 -0800 | [diff] [blame] | 231 | return info; |
| 232 | } else { |
| 233 | log.warn("Data not found for id {}", id); |
| 234 | return null; |
| 235 | } |
Gamze Abaka | 1e5ccf5 | 2018-07-02 11:59:03 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | public abstract void registerModule(); |
| 239 | |
| 240 | public abstract Set<ConfigFactory> getConfigFactories(); |
| 241 | |
| 242 | public abstract JsonCodec<T> getCodec(); |
| 243 | |
| 244 | public abstract Class<T> getInformationClass(); |
| 245 | |
| 246 | public abstract Class<K> getConfigClass(); |
| 247 | |
| 248 | public abstract ApplicationId getAppId(); |
| 249 | } |