blob: 5026e66944a6913e447b2676cae4290a0b5ea74f [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;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000029import com.fasterxml.jackson.databind.module.SimpleModule;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070030import com.google.common.cache.Cache;
31import com.google.common.cache.CacheBuilder;
Amit Ghosh38b232a2017-07-23 15:11:56 +010032import com.google.common.collect.Maps;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000033import org.onlab.packet.VlanId;
34import org.onlab.packet.Ip4Address;
Amit Ghosh38b232a2017-07-23 15:11:56 +010035
36import org.slf4j.Logger;
37import org.slf4j.LoggerFactory;
38
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070039
40public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService {
Amit Ghosh38b232a2017-07-23 15:11:56 +010041
42 private final Logger log = LoggerFactory.getLogger(this.getClass());
43
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070044 private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0;
45 private static final long DEFAULT_TTL = 0;
46
47 private String url;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000048 private ObjectMapper mapper;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070049 private Cache<String, SubscriberAndDeviceInformation> cache;
50 private int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
51 private long cacheEntryTtl = DEFAULT_TTL;
52
Amit Ghosh38b232a2017-07-23 15:11:56 +010053 private Map<String, SubscriberAndDeviceInformation> localCfgData = null;
54
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070055 public SubscriberAndDeviceInformationAdapter() {
56 cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize)
57 .expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000058 mapper = new ObjectMapper();
59 SimpleModule module = new SimpleModule();
60 SadisConfig config = new SadisConfig();
61 SadisConfig.VlanIdDeserializer vlanID = config.new VlanIdDeserializer();
62 SadisConfig.Ip4AddressDeserializer ip4Address = config.new Ip4AddressDeserializer();
63 module.addDeserializer(VlanId.class, vlanID);
64 module.addDeserializer(Ip4Address.class, ip4Address);
65 mapper.registerModule(module);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070066 }
67
Amit Ghosh38b232a2017-07-23 15:11:56 +010068 /**
69 * Configures the Adapter for data source and cache parameters.
70 *
71 * @param cfg Configuration data.
72 */
73 public void configure(SadisConfig cfg) {
74 String url = null;
75 try {
76 // if the url is not present then assume data is in netcfg
77 if (cfg.getUrl() != null) {
78 url = cfg.getUrl().toString();
79 } else {
80 localCfgData = Maps.newConcurrentMap();
81
82 cfg.getEntries().forEach(entry -> {
83 localCfgData.put(entry.id(), entry);
84 });
85 log.info("url is null, data source is local netcfg data");
86 }
87 } catch (MalformedURLException mUrlEx) {
88 log.error("Invalid URL specified: {}", mUrlEx);
89 }
90
91 int maximumCacheSeize = cfg.getCacheMaxSize();
92 long cacheEntryTtl = cfg.getCacheTtl().getSeconds();
93
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070094 // Rebuild cache if needed
Deepa vaddireddyad295192017-08-10 13:14:02 +053095 if (isurlChanged(url) || maximumCacheSeize != this.maxiumCacheSize ||
Amit Ghosh38b232a2017-07-23 15:11:56 +010096 cacheEntryTtl != this.cacheEntryTtl) {
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070097 this.maxiumCacheSize = maximumCacheSeize;
98 this.cacheEntryTtl = cacheEntryTtl;
Amit Ghosh38b232a2017-07-23 15:11:56 +010099 this.url = url;
100
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700101 Cache<String, SubscriberAndDeviceInformation> newCache = CacheBuilder.newBuilder()
102 .maximumSize(maxiumCacheSize).expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
103 Cache<String, SubscriberAndDeviceInformation> oldCache = cache;
104
105 synchronized (this) {
106 cache = newCache;
107 }
108
109 oldCache.invalidateAll();
110 oldCache.cleanUp();
111 }
112 }
113
Deepa vaddireddyad295192017-08-10 13:14:02 +0530114 private boolean isurlChanged(String url) {
115 if (url == null && this.url == null) {
116 return false;
117 }
118 return !((url == this.url) || (url != null && url.equals(this.url)));
119 }
120
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700121 /*
122 * (non-Javadoc)
123 *
124 * @see
125 * org.opencord.sadis.SubscriberAndDeviceInformationService#clearCache()
126 */
127 @Override
128 public void invalidateAll() {
129 cache.invalidateAll();
130 }
131
132 /*
133 * (non-Javadoc)
134 *
135 * @see
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000136 * org.opencord.sadis.SubscriberAndDeviceInformationService#invalidateId()
137 */
138 @Override
139 public void invalidateId(String id) {
140 cache.invalidate(id);
141 }
142
143 /*
144 * (non-Javadoc)
145 *
146 * @see
147 * org.opencord.sadis.SubscriberAndDeviceInformationService#getfromCache(java.lang.
148 * String)
149 */
150 @Override
151 public SubscriberAndDeviceInformation getfromCache(String id) {
152 Cache<String, SubscriberAndDeviceInformation> local;
153 synchronized (this) {
154 local = cache;
155 }
156 SubscriberAndDeviceInformation info = local.getIfPresent(id);
157 if (info != null) {
158 return info;
159 }
160 return null;
161 }
162
163 /*
164 * (non-Javadoc)
165 *
166 * @see
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700167 * org.opencord.sadis.SubscriberAndDeviceInformationService#get(java.lang.
168 * String)
169 */
170 @Override
171 public SubscriberAndDeviceInformation get(String id) {
172 Cache<String, SubscriberAndDeviceInformation> local;
173 synchronized (this) {
174 local = cache;
175 }
176
177 SubscriberAndDeviceInformation info = local.getIfPresent(id);
178 if (info != null) {
179 return info;
180 }
181
182 /*
183 * Not in cache, if we have a URL configured we can attempt to get it
Amit Ghosh38b232a2017-07-23 15:11:56 +0100184 * from there, else check for it in the locally configured data
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700185 */
186 if (this.url == null) {
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000187 info = (localCfgData == null) ? null : localCfgData.get(id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700188
Amit Ghosh38b232a2017-07-23 15:11:56 +0100189 if (info != null) {
190 local.put(id, info);
191 return info;
192 }
193 } else {
194 // Augment URL with query parameters
Amit Ghosh2c3ff592017-11-13 07:04:41 +0000195 String urlWithSubId = this.url.replaceAll("%s", id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700196
Amit Ghosh2c3ff592017-11-13 07:04:41 +0000197 try (InputStream io = new URL(urlWithSubId).openStream()) {
Amit Ghosh38b232a2017-07-23 15:11:56 +0100198 info = mapper.readValue(io, SubscriberAndDeviceInformation.class);
199 local.put(id, info);
200 return info;
201 } catch (IOException e) {
202 // TODO Auto-generated catch block
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000203 log.warn("Exception while reading remote data " + e.getMessage());
Amit Ghosh38b232a2017-07-23 15:11:56 +0100204 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700205 }
Amit Ghosh38b232a2017-07-23 15:11:56 +0100206 log.error("Data not found for id {}", id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700207 return null;
208 }
209}