Added NASId, IP Address for a device and auto pick data from external or ONOS configuration
Change-Id: Ib3ecd911c43dd1cd0d03cef3916b1e513f9bbdaf
diff --git a/src/main/java/org/opencord/sadis/SubscriberAndDeviceInformationAdapter.java b/src/main/java/org/opencord/sadis/SubscriberAndDeviceInformationAdapter.java
index c131704..56e60c5 100644
--- a/src/main/java/org/opencord/sadis/SubscriberAndDeviceInformationAdapter.java
+++ b/src/main/java/org/opencord/sadis/SubscriberAndDeviceInformationAdapter.java
@@ -17,14 +17,24 @@
import java.io.IOException;
import java.io.InputStream;
+import java.net.MalformedURLException;
import java.net.URL;
+import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
+import com.google.common.collect.Maps;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService {
+
+ private final Logger log = LoggerFactory.getLogger(this.getClass());
+
private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0;
private static final long DEFAULT_TTL = 0;
@@ -33,17 +43,46 @@
private int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
private long cacheEntryTtl = DEFAULT_TTL;
+ private Map<String, SubscriberAndDeviceInformation> localCfgData = null;
+
public SubscriberAndDeviceInformationAdapter() {
cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize)
.expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
}
- public void configure(String url, int maximumCacheSeize, long cacheEntryTtl) {
+ /**
+ * Configures the Adapter for data source and cache parameters.
+ *
+ * @param cfg Configuration data.
+ */
+ public void configure(SadisConfig cfg) {
+ String url = null;
+ try {
+ // if the url is not present then assume data is in netcfg
+ if (cfg.getUrl() != null) {
+ url = cfg.getUrl().toString();
+ } else {
+ localCfgData = Maps.newConcurrentMap();
+
+ cfg.getEntries().forEach(entry -> {
+ localCfgData.put(entry.id(), entry);
+ });
+ log.info("url is null, data source is local netcfg data");
+ }
+ } catch (MalformedURLException mUrlEx) {
+ log.error("Invalid URL specified: {}", mUrlEx);
+ }
+
+ int maximumCacheSeize = cfg.getCacheMaxSize();
+ long cacheEntryTtl = cfg.getCacheTtl().getSeconds();
+
// Rebuild cache if needed
- if (url != this.url || maximumCacheSeize != this.maxiumCacheSize || cacheEntryTtl != this.cacheEntryTtl) {
- this.url = url;
+ if ((url != null && url != this.url) || maximumCacheSeize != this.maxiumCacheSize ||
+ cacheEntryTtl != this.cacheEntryTtl) {
this.maxiumCacheSize = maximumCacheSeize;
this.cacheEntryTtl = cacheEntryTtl;
+ this.url = url;
+
Cache<String, SubscriberAndDeviceInformation> newCache = CacheBuilder.newBuilder()
.maximumSize(maxiumCacheSize).expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
Cache<String, SubscriberAndDeviceInformation> oldCache = cache;
@@ -89,29 +128,35 @@
/*
* Not in cache, if we have a URL configured we can attempt to get it
- * from there.
+ * from there, else check for it in the locally configured data
*/
if (this.url == null) {
- return null;
- }
+ info = localCfgData.get(id);
- // Augment URL with query parameters
- StringBuilder buf = new StringBuilder(this.url);
- if (buf.charAt(buf.length() - 1) != '/') {
- buf.append('/');
- }
+ if (info != null) {
+ local.put(id, info);
+ return info;
+ }
+ } else {
+ // Augment URL with query parameters
+ StringBuilder buf = new StringBuilder(this.url);
+ if (buf.charAt(buf.length() - 1) != '/') {
+ buf.append('/');
+ }
- buf.append(id);
+ buf.append(id);
- try (InputStream io = new URL(buf.toString()).openStream()) {
- ObjectMapper mapper = new ObjectMapper();
- info = mapper.readValue(io, SubscriberAndDeviceInformation.class);
- local.put(id, info);
- return info;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ try (InputStream io = new URL(buf.toString()).openStream()) {
+ ObjectMapper mapper = new ObjectMapper();
+ info = mapper.readValue(io, SubscriberAndDeviceInformation.class);
+ local.put(id, info);
+ return info;
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
}
+ log.error("Data not found for id {}", id);
return null;
}
}