blob: fbd77aad8fe2c002296ca07d18c29259c5c85266 [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
Amit Ghosh38b232a2017-07-23 15:11:56 +010095 if ((url != null && url != this.url) || maximumCacheSeize != this.maxiumCacheSize ||
96 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
114 /*
115 * (non-Javadoc)
116 *
117 * @see
118 * org.opencord.sadis.SubscriberAndDeviceInformationService#clearCache()
119 */
120 @Override
121 public void invalidateAll() {
122 cache.invalidateAll();
123 }
124
125 /*
126 * (non-Javadoc)
127 *
128 * @see
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000129 * org.opencord.sadis.SubscriberAndDeviceInformationService#invalidateId()
130 */
131 @Override
132 public void invalidateId(String id) {
133 cache.invalidate(id);
134 }
135
136 /*
137 * (non-Javadoc)
138 *
139 * @see
140 * org.opencord.sadis.SubscriberAndDeviceInformationService#getfromCache(java.lang.
141 * String)
142 */
143 @Override
144 public SubscriberAndDeviceInformation getfromCache(String id) {
145 Cache<String, SubscriberAndDeviceInformation> local;
146 synchronized (this) {
147 local = cache;
148 }
149 SubscriberAndDeviceInformation info = local.getIfPresent(id);
150 if (info != null) {
151 return info;
152 }
153 return null;
154 }
155
156 /*
157 * (non-Javadoc)
158 *
159 * @see
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700160 * org.opencord.sadis.SubscriberAndDeviceInformationService#get(java.lang.
161 * String)
162 */
163 @Override
164 public SubscriberAndDeviceInformation get(String id) {
165 Cache<String, SubscriberAndDeviceInformation> local;
166 synchronized (this) {
167 local = cache;
168 }
169
170 SubscriberAndDeviceInformation info = local.getIfPresent(id);
171 if (info != null) {
172 return info;
173 }
174
175 /*
176 * Not in cache, if we have a URL configured we can attempt to get it
Amit Ghosh38b232a2017-07-23 15:11:56 +0100177 * from there, else check for it in the locally configured data
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700178 */
179 if (this.url == null) {
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000180 info = (localCfgData == null) ? null : localCfgData.get(id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700181
Amit Ghosh38b232a2017-07-23 15:11:56 +0100182 if (info != null) {
183 local.put(id, info);
184 return info;
185 }
186 } else {
187 // Augment URL with query parameters
188 StringBuilder buf = new StringBuilder(this.url);
189 if (buf.charAt(buf.length() - 1) != '/') {
190 buf.append('/');
191 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700192
Amit Ghosh38b232a2017-07-23 15:11:56 +0100193 buf.append(id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700194
Amit Ghosh38b232a2017-07-23 15:11:56 +0100195 try (InputStream io = new URL(buf.toString()).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) {
200 // TODO Auto-generated catch block
Deepa vaddireddy386f38b2017-08-02 06:24:01 +0000201 log.warn("Exception while reading remote data " + e.getMessage());
Amit Ghosh38b232a2017-07-23 15:11:56 +0100202 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700203 }
Amit Ghosh38b232a2017-07-23 15:11:56 +0100204 log.error("Data not found for id {}", id);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700205 return null;
206 }
207}