blob: 56e60c5523651c0dca166e17d8a03ea3ef304fb8 [file] [log] [blame]
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
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;
17
18import java.io.IOException;
19import java.io.InputStream;
Amit Ghosh38b232a2017-07-23 15:11:56 +010020import java.net.MalformedURLException;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070021import java.net.URL;
Amit Ghosh38b232a2017-07-23 15:11:56 +010022import java.util.Map;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070023import java.util.concurrent.TimeUnit;
24
25import com.fasterxml.jackson.databind.ObjectMapper;
26import com.google.common.cache.Cache;
27import com.google.common.cache.CacheBuilder;
Amit Ghosh38b232a2017-07-23 15:11:56 +010028import com.google.common.collect.Maps;
29
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070033
34public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService {
Amit Ghosh38b232a2017-07-23 15:11:56 +010035
36 private final Logger log = LoggerFactory.getLogger(this.getClass());
37
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070038 private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0;
39 private static final long DEFAULT_TTL = 0;
40
41 private String url;
42 private Cache<String, SubscriberAndDeviceInformation> cache;
43 private int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
44 private long cacheEntryTtl = DEFAULT_TTL;
45
Amit Ghosh38b232a2017-07-23 15:11:56 +010046 private Map<String, SubscriberAndDeviceInformation> localCfgData = null;
47
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070048 public SubscriberAndDeviceInformationAdapter() {
49 cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize)
50 .expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
51 }
52
Amit Ghosh38b232a2017-07-23 15:11:56 +010053 /**
54 * Configures the Adapter for data source and cache parameters.
55 *
56 * @param cfg Configuration data.
57 */
58 public void configure(SadisConfig cfg) {
59 String url = null;
60 try {
61 // if the url is not present then assume data is in netcfg
62 if (cfg.getUrl() != null) {
63 url = cfg.getUrl().toString();
64 } else {
65 localCfgData = Maps.newConcurrentMap();
66
67 cfg.getEntries().forEach(entry -> {
68 localCfgData.put(entry.id(), entry);
69 });
70 log.info("url is null, data source is local netcfg data");
71 }
72 } catch (MalformedURLException mUrlEx) {
73 log.error("Invalid URL specified: {}", mUrlEx);
74 }
75
76 int maximumCacheSeize = cfg.getCacheMaxSize();
77 long cacheEntryTtl = cfg.getCacheTtl().getSeconds();
78
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070079 // Rebuild cache if needed
Amit Ghosh38b232a2017-07-23 15:11:56 +010080 if ((url != null && url != this.url) || maximumCacheSeize != this.maxiumCacheSize ||
81 cacheEntryTtl != this.cacheEntryTtl) {
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070082 this.maxiumCacheSize = maximumCacheSeize;
83 this.cacheEntryTtl = cacheEntryTtl;
Amit Ghosh38b232a2017-07-23 15:11:56 +010084 this.url = url;
85
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070086 Cache<String, SubscriberAndDeviceInformation> newCache = CacheBuilder.newBuilder()
87 .maximumSize(maxiumCacheSize).expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
88 Cache<String, SubscriberAndDeviceInformation> oldCache = cache;
89
90 synchronized (this) {
91 cache = newCache;
92 }
93
94 oldCache.invalidateAll();
95 oldCache.cleanUp();
96 }
97 }
98
99 /*
100 * (non-Javadoc)
101 *
102 * @see
103 * org.opencord.sadis.SubscriberAndDeviceInformationService#clearCache()
104 */
105 @Override
106 public void invalidateAll() {
107 cache.invalidateAll();
108 }
109
110 /*
111 * (non-Javadoc)
112 *
113 * @see
114 * org.opencord.sadis.SubscriberAndDeviceInformationService#get(java.lang.
115 * String)
116 */
117 @Override
118 public SubscriberAndDeviceInformation get(String id) {
119 Cache<String, SubscriberAndDeviceInformation> local;
120 synchronized (this) {
121 local = cache;
122 }
123
124 SubscriberAndDeviceInformation info = local.getIfPresent(id);
125 if (info != null) {
126 return info;
127 }
128
129 /*
130 * Not in cache, if we have a URL configured we can attempt to get it
Amit Ghosh38b232a2017-07-23 15:11:56 +0100131 * from there, else check for it in the locally configured data
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700132 */
133 if (this.url == null) {
Amit Ghosh38b232a2017-07-23 15:11:56 +0100134 info = localCfgData.get(id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700135
Amit Ghosh38b232a2017-07-23 15:11:56 +0100136 if (info != null) {
137 local.put(id, info);
138 return info;
139 }
140 } else {
141 // Augment URL with query parameters
142 StringBuilder buf = new StringBuilder(this.url);
143 if (buf.charAt(buf.length() - 1) != '/') {
144 buf.append('/');
145 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700146
Amit Ghosh38b232a2017-07-23 15:11:56 +0100147 buf.append(id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700148
Amit Ghosh38b232a2017-07-23 15:11:56 +0100149 try (InputStream io = new URL(buf.toString()).openStream()) {
150 ObjectMapper mapper = new ObjectMapper();
151 info = mapper.readValue(io, SubscriberAndDeviceInformation.class);
152 local.put(id, info);
153 return info;
154 } catch (IOException e) {
155 // TODO Auto-generated catch block
156 e.printStackTrace();
157 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700158 }
Amit Ghosh38b232a2017-07-23 15:11:56 +0100159 log.error("Data not found for id {}", id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700160 return null;
161 }
162}