blob: 216b5f85170fa08cecf64f58698b3552a9e26774 [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 */
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 Scandoloc3e53722020-12-08 15:14:53 -0800199 * 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 Abaka1e5ccf52018-07-02 11:59:03 +0000202 */
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000203
Matteo Scandoloc3e53722020-12-08 15:14:53 -0800204 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 Abaka1e5ccf52018-07-02 11:59:03 +0000212 }
Matteo Scandoloc3e53722020-12-08 15:14:53 -0800213 }
214
215 if (info == null && this.url != null) {
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000216 // Augment URL with query parameters
217 String urlWithSubId = this.url.replaceAll("%s", id);
Matteo Scandolofd4d68d2020-10-08 17:37:56 -0700218 log.debug("Getting data from the remote URL {}", urlWithSubId);
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000219
220 try (InputStream io = new URL(urlWithSubId).openStream()) {
221 info = mapper.readValue(io, getInformationClass());
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000222 } 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 Scandoloc3e53722020-12-08 15:14:53 -0800227
228 if (info != null) {
229 local.put(id, info);
yasin sapli2c93ddb2021-06-11 17:46:26 +0300230 log.debug("Returning Info {}", info);
Matteo Scandoloc3e53722020-12-08 15:14:53 -0800231 return info;
232 } else {
233 log.warn("Data not found for id {}", id);
234 return null;
235 }
Gamze Abaka1e5ccf52018-07-02 11:59:03 +0000236 }
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}