blob: 4a65e565ca16bab608baef2ce7bb4ffba56b81c5 [file] [log] [blame]
Gamze Abaka1e5ccf52018-07-02 11:59:03 +00001/*
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 */
16package org.opencord.sadis.impl;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.google.common.cache.Cache;
20import com.google.common.cache.CacheBuilder;
21import com.google.common.collect.Maps;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import java.io.IOException;
26import java.io.InputStream;
27import java.net.MalformedURLException;
28import java.net.URL;
29import java.util.Map;
30import java.util.concurrent.TimeUnit;
31
32import org.onosproject.codec.JsonCodec;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.net.config.ConfigFactory;
35import org.onosproject.net.config.NetworkConfigRegistry;
36import org.opencord.sadis.BaseInformation;
37import org.opencord.sadis.BaseConfig;
38import org.opencord.sadis.BaseInformationService;
Matteo Scandoloc3e53722020-12-08 15:14:53 -080039
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000040import java.util.Set;
41
42
43public 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 Abaka1e5ccf52018-07-02 11:59:03 +000084 if (cfg.getUrl() != null) {
85 url = cfg.getUrl().toString();
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000086 }
87 } catch (MalformedURLException mUrlEx) {
88 log.error("Invalid URL specified: {}", mUrlEx);
89 }
90
Matteo Scandoloc3e53722020-12-08 15:14:53 -080091 // always load local data
92 localCfgData = Maps.newConcurrentMap();
93 cfg.getEntries().forEach(entry ->
94 localCfgData.put(entry.id(), entry));
95
Gamze Abaka1e5ccf52018-07-02 11:59:03 +000096 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 Scandoloc3e53722020-12-08 15:14:53 -0800130 * 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 Abaka1e5ccf52018-07-02 11:59:03 +0000141 * 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 */
Andrea Campanella092df202021-11-29 10:38:45 -0800166 //TODO remove
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000167 @Override
168 public T getfromCache(String id) {
169 Cache<String, T> local;
170 synchronized (this) {
171 local = cache;
172 }
173 T info = local.getIfPresent(id);
174 if (info != null) {
175 return info;
176 }
177 return null;
178 }
179
180 /*
181 * (non-Javadoc)
182 *
183 * @see
184 * org.opencord.sadis.BaseInformationService#get(java.lang.
185 * String)
186 */
187 @Override
188 public T get(String id) {
189 Cache<String, T> local;
190 synchronized (this) {
191 local = cache;
192 }
193
194 T info = local.getIfPresent(id);
195 if (info != null) {
196 return info;
197 }
198
199 /*
Matteo Scandoloc3e53722020-12-08 15:14:53 -0800200 * Not in cache, check for it in the locally configured data,
201 * if it's not there and we have a URL configured
202 * we can attempt to get it from there
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000203 */
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000204
Matteo Scandoloc3e53722020-12-08 15:14:53 -0800205 log.debug("Getting data from local config");
206 info = (localCfgData == null) ? null : localCfgData.get(id);
207
208 if (log.isTraceEnabled()) {
209 if (info == null) {
210 log.trace("Data not found in local config.");
211 } else {
212 log.trace("Found data in local config.");
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000213 }
Matteo Scandoloc3e53722020-12-08 15:14:53 -0800214 }
215
216 if (info == null && this.url != null) {
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000217 // Augment URL with query parameters
218 String urlWithSubId = this.url.replaceAll("%s", id);
Matteo Scandolofd4d68d2020-10-08 17:37:56 -0700219 log.debug("Getting data from the remote URL {}", urlWithSubId);
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000220
221 try (InputStream io = new URL(urlWithSubId).openStream()) {
222 info = mapper.readValue(io, getInformationClass());
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000223 } catch (IOException e) {
224 // TODO use a better http library that allows us to read status code
Andrea Campanella30041b92022-01-28 16:09:08 +0100225 log.debug("Exception while reading remote data {} ", e.getMessage(), e);
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000226 }
227 }
Matteo Scandoloc3e53722020-12-08 15:14:53 -0800228
229 if (info != null) {
230 local.put(id, info);
yasin sapli2c93ddb2021-06-11 17:46:26 +0300231 log.debug("Returning Info {}", info);
Matteo Scandoloc3e53722020-12-08 15:14:53 -0800232 return info;
233 } else {
234 log.warn("Data not found for id {}", id);
235 return null;
236 }
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000237 }
238
239 public abstract void registerModule();
240
241 public abstract Set<ConfigFactory> getConfigFactories();
242
243 public abstract JsonCodec<T> getCodec();
244
245 public abstract Class<T> getInformationClass();
246
247 public abstract Class<K> getConfigClass();
248
249 public abstract ApplicationId getAppId();
250}