blob: 884249044943fb6295335241cda87cd511692400 [file] [log] [blame]
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07001/*
Brian O'Connor180c1092017-08-03 22:46:14 -07002 * Copyright 2017-present Open Networking Foundation
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07003 *
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 */
Amit Ghoshc29c7a92017-08-01 09:59:13 +010016package org.opencord.sadis.impl;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070017
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
Amit Ghoshc29c7a92017-08-01 09:59:13 +010025import org.opencord.sadis.SubscriberAndDeviceInformation;
26import org.opencord.sadis.SubscriberAndDeviceInformationService;
27
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070028import com.fasterxml.jackson.databind.ObjectMapper;
29import com.google.common.cache.Cache;
30import com.google.common.cache.CacheBuilder;
Amit Ghosh38b232a2017-07-23 15:11:56 +010031import com.google.common.collect.Maps;
32
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070036
37public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService {
Amit Ghosh38b232a2017-07-23 15:11:56 +010038
39 private final Logger log = LoggerFactory.getLogger(this.getClass());
40
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070041 private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0;
42 private static final long DEFAULT_TTL = 0;
43
44 private String url;
45 private Cache<String, SubscriberAndDeviceInformation> cache;
46 private int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
47 private long cacheEntryTtl = DEFAULT_TTL;
48
Amit Ghosh38b232a2017-07-23 15:11:56 +010049 private Map<String, SubscriberAndDeviceInformation> localCfgData = null;
50
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070051 public SubscriberAndDeviceInformationAdapter() {
52 cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize)
53 .expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
54 }
55
Amit Ghosh38b232a2017-07-23 15:11:56 +010056 /**
57 * Configures the Adapter for data source and cache parameters.
58 *
59 * @param cfg Configuration data.
60 */
61 public void configure(SadisConfig cfg) {
62 String url = null;
63 try {
64 // if the url is not present then assume data is in netcfg
65 if (cfg.getUrl() != null) {
66 url = cfg.getUrl().toString();
67 } else {
68 localCfgData = Maps.newConcurrentMap();
69
70 cfg.getEntries().forEach(entry -> {
71 localCfgData.put(entry.id(), entry);
72 });
73 log.info("url is null, data source is local netcfg data");
74 }
75 } catch (MalformedURLException mUrlEx) {
76 log.error("Invalid URL specified: {}", mUrlEx);
77 }
78
79 int maximumCacheSeize = cfg.getCacheMaxSize();
80 long cacheEntryTtl = cfg.getCacheTtl().getSeconds();
81
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070082 // Rebuild cache if needed
Amit Ghosh38b232a2017-07-23 15:11:56 +010083 if ((url != null && url != this.url) || maximumCacheSeize != this.maxiumCacheSize ||
84 cacheEntryTtl != this.cacheEntryTtl) {
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070085 this.maxiumCacheSize = maximumCacheSeize;
86 this.cacheEntryTtl = cacheEntryTtl;
Amit Ghosh38b232a2017-07-23 15:11:56 +010087 this.url = url;
88
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070089 Cache<String, SubscriberAndDeviceInformation> newCache = CacheBuilder.newBuilder()
90 .maximumSize(maxiumCacheSize).expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
91 Cache<String, SubscriberAndDeviceInformation> oldCache = cache;
92
93 synchronized (this) {
94 cache = newCache;
95 }
96
97 oldCache.invalidateAll();
98 oldCache.cleanUp();
99 }
100 }
101
102 /*
103 * (non-Javadoc)
104 *
105 * @see
106 * org.opencord.sadis.SubscriberAndDeviceInformationService#clearCache()
107 */
108 @Override
109 public void invalidateAll() {
110 cache.invalidateAll();
111 }
112
113 /*
114 * (non-Javadoc)
115 *
116 * @see
117 * org.opencord.sadis.SubscriberAndDeviceInformationService#get(java.lang.
118 * String)
119 */
120 @Override
121 public SubscriberAndDeviceInformation get(String id) {
122 Cache<String, SubscriberAndDeviceInformation> local;
123 synchronized (this) {
124 local = cache;
125 }
126
127 SubscriberAndDeviceInformation info = local.getIfPresent(id);
128 if (info != null) {
129 return info;
130 }
131
132 /*
133 * Not in cache, if we have a URL configured we can attempt to get it
Amit Ghosh38b232a2017-07-23 15:11:56 +0100134 * from there, else check for it in the locally configured data
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700135 */
136 if (this.url == null) {
Amit Ghosh38b232a2017-07-23 15:11:56 +0100137 info = localCfgData.get(id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700138
Amit Ghosh38b232a2017-07-23 15:11:56 +0100139 if (info != null) {
140 local.put(id, info);
141 return info;
142 }
143 } else {
144 // Augment URL with query parameters
145 StringBuilder buf = new StringBuilder(this.url);
146 if (buf.charAt(buf.length() - 1) != '/') {
147 buf.append('/');
148 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700149
Amit Ghosh38b232a2017-07-23 15:11:56 +0100150 buf.append(id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700151
Amit Ghosh38b232a2017-07-23 15:11:56 +0100152 try (InputStream io = new URL(buf.toString()).openStream()) {
153 ObjectMapper mapper = new ObjectMapper();
154 info = mapper.readValue(io, SubscriberAndDeviceInformation.class);
155 local.put(id, info);
156 return info;
157 } catch (IOException e) {
158 // TODO Auto-generated catch block
159 e.printStackTrace();
160 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700161 }
Amit Ghosh38b232a2017-07-23 15:11:56 +0100162 log.error("Data not found for id {}", id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700163 return null;
164 }
165}