blob: b5fe72e51f5b0dbaa0f670749140763ea796eb73 [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
Jonathan Harte846b9e2018-08-22 17:19:21 -070018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.module.SimpleModule;
20import com.google.common.cache.Cache;
21import com.google.common.cache.CacheBuilder;
22import com.google.common.collect.Maps;
23import org.onlab.packet.Ip4Address;
24import org.onlab.packet.VlanId;
25import org.opencord.sadis.SubscriberAndDeviceInformation;
26import org.opencord.sadis.SubscriberAndDeviceInformationService;
27import org.slf4j.Logger;
28import org.slf4j.LoggerFactory;
29
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070030import java.io.IOException;
31import java.io.InputStream;
Amit Ghosh38b232a2017-07-23 15:11:56 +010032import java.net.MalformedURLException;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070033import java.net.URL;
Amit Ghosh38b232a2017-07-23 15:11:56 +010034import java.util.Map;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070035import java.util.concurrent.TimeUnit;
36
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070037
38public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService {
Amit Ghosh38b232a2017-07-23 15:11:56 +010039
40 private final Logger log = LoggerFactory.getLogger(this.getClass());
41
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070042 private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0;
43 private static final long DEFAULT_TTL = 0;
44
45 private String url;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000046 private ObjectMapper mapper;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070047 private Cache<String, SubscriberAndDeviceInformation> cache;
48 private int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
49 private long cacheEntryTtl = DEFAULT_TTL;
50
Amit Ghosh38b232a2017-07-23 15:11:56 +010051 private Map<String, SubscriberAndDeviceInformation> localCfgData = null;
52
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070053 public SubscriberAndDeviceInformationAdapter() {
54 cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize)
55 .expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000056 mapper = new ObjectMapper();
57 SimpleModule module = new SimpleModule();
58 SadisConfig config = new SadisConfig();
59 SadisConfig.VlanIdDeserializer vlanID = config.new VlanIdDeserializer();
60 SadisConfig.Ip4AddressDeserializer ip4Address = config.new Ip4AddressDeserializer();
61 module.addDeserializer(VlanId.class, vlanID);
62 module.addDeserializer(Ip4Address.class, ip4Address);
63 mapper.registerModule(module);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070064 }
65
Amit Ghosh38b232a2017-07-23 15:11:56 +010066 /**
67 * Configures the Adapter for data source and cache parameters.
68 *
69 * @param cfg Configuration data.
70 */
71 public void configure(SadisConfig cfg) {
72 String url = null;
73 try {
74 // if the url is not present then assume data is in netcfg
75 if (cfg.getUrl() != null) {
76 url = cfg.getUrl().toString();
77 } else {
78 localCfgData = Maps.newConcurrentMap();
79
80 cfg.getEntries().forEach(entry -> {
81 localCfgData.put(entry.id(), entry);
82 });
83 log.info("url is null, data source is local netcfg data");
84 }
85 } catch (MalformedURLException mUrlEx) {
86 log.error("Invalid URL specified: {}", mUrlEx);
87 }
88
89 int maximumCacheSeize = cfg.getCacheMaxSize();
90 long cacheEntryTtl = cfg.getCacheTtl().getSeconds();
91
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070092 // Rebuild cache if needed
Deepa vaddireddyad295192017-08-10 13:14:02 +053093 if (isurlChanged(url) || maximumCacheSeize != this.maxiumCacheSize ||
Amit Ghosh38b232a2017-07-23 15:11:56 +010094 cacheEntryTtl != this.cacheEntryTtl) {
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070095 this.maxiumCacheSize = maximumCacheSeize;
96 this.cacheEntryTtl = cacheEntryTtl;
Amit Ghosh38b232a2017-07-23 15:11:56 +010097 this.url = url;
98
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070099 Cache<String, SubscriberAndDeviceInformation> newCache = CacheBuilder.newBuilder()
100 .maximumSize(maxiumCacheSize).expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
101 Cache<String, SubscriberAndDeviceInformation> oldCache = cache;
102
103 synchronized (this) {
104 cache = newCache;
105 }
106
107 oldCache.invalidateAll();
108 oldCache.cleanUp();
109 }
110 }
111
Deepa vaddireddyad295192017-08-10 13:14:02 +0530112 private boolean isurlChanged(String url) {
113 if (url == null && this.url == null) {
114 return false;
115 }
116 return !((url == this.url) || (url != null && url.equals(this.url)));
117 }
118
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700119 /*
120 * (non-Javadoc)
121 *
122 * @see
123 * org.opencord.sadis.SubscriberAndDeviceInformationService#clearCache()
124 */
125 @Override
126 public void invalidateAll() {
127 cache.invalidateAll();
128 }
129
130 /*
131 * (non-Javadoc)
132 *
133 * @see
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000134 * org.opencord.sadis.SubscriberAndDeviceInformationService#invalidateId()
135 */
136 @Override
137 public void invalidateId(String id) {
138 cache.invalidate(id);
139 }
140
141 /*
142 * (non-Javadoc)
143 *
144 * @see
145 * org.opencord.sadis.SubscriberAndDeviceInformationService#getfromCache(java.lang.
146 * String)
147 */
148 @Override
149 public SubscriberAndDeviceInformation getfromCache(String id) {
150 Cache<String, SubscriberAndDeviceInformation> local;
151 synchronized (this) {
152 local = cache;
153 }
154 SubscriberAndDeviceInformation info = local.getIfPresent(id);
155 if (info != null) {
156 return info;
157 }
158 return null;
159 }
160
161 /*
162 * (non-Javadoc)
163 *
164 * @see
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700165 * org.opencord.sadis.SubscriberAndDeviceInformationService#get(java.lang.
166 * String)
167 */
168 @Override
169 public SubscriberAndDeviceInformation get(String id) {
170 Cache<String, SubscriberAndDeviceInformation> local;
171 synchronized (this) {
172 local = cache;
173 }
174
175 SubscriberAndDeviceInformation info = local.getIfPresent(id);
176 if (info != null) {
177 return info;
178 }
179
180 /*
181 * Not in cache, if we have a URL configured we can attempt to get it
Amit Ghosh38b232a2017-07-23 15:11:56 +0100182 * from there, else check for it in the locally configured data
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700183 */
184 if (this.url == null) {
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000185 info = (localCfgData == null) ? null : localCfgData.get(id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700186
Amit Ghosh38b232a2017-07-23 15:11:56 +0100187 if (info != null) {
188 local.put(id, info);
189 return info;
190 }
191 } else {
192 // Augment URL with query parameters
Amit Ghosh2c3ff592017-11-13 07:04:41 +0000193 String urlWithSubId = this.url.replaceAll("%s", id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700194
Amit Ghosh2c3ff592017-11-13 07:04:41 +0000195 try (InputStream io = new URL(urlWithSubId).openStream()) {
Amit Ghosh38b232a2017-07-23 15:11:56 +0100196 info = mapper.readValue(io, SubscriberAndDeviceInformation.class);
197 local.put(id, info);
198 return info;
199 } catch (IOException e) {
Jonathan Harte846b9e2018-08-22 17:19:21 -0700200 // TODO use a better http library that allows us to read status code
201 log.debug("Exception while reading remote data " + e.getMessage());
Amit Ghosh38b232a2017-07-23 15:11:56 +0100202 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700203 }
Jonathan Harte846b9e2018-08-22 17:19:21 -0700204 log.debug("Data not found for id {}", id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700205 return null;
206 }
207}