David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 180c109 | 2017-08-03 22:46:14 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 3 | * |
| 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 Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 16 | package org.opencord.sadis.impl; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 17 | |
| 18 | import java.io.IOException; |
| 19 | import java.io.InputStream; |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 20 | import java.net.MalformedURLException; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 21 | import java.net.URL; |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 22 | import java.util.Map; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
Amit Ghosh | c29c7a9 | 2017-08-01 09:59:13 +0100 | [diff] [blame] | 25 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
| 26 | import org.opencord.sadis.SubscriberAndDeviceInformationService; |
| 27 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 28 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | import com.google.common.cache.Cache; |
| 30 | import com.google.common.cache.CacheBuilder; |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 31 | import com.google.common.collect.Maps; |
| 32 | |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 36 | |
| 37 | public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService { |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 38 | |
| 39 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 40 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 41 | 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 Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 49 | private Map<String, SubscriberAndDeviceInformation> localCfgData = null; |
| 50 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 51 | public SubscriberAndDeviceInformationAdapter() { |
| 52 | cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize) |
| 53 | .expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build(); |
| 54 | } |
| 55 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 56 | /** |
| 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 82 | // Rebuild cache if needed |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 83 | if ((url != null && url != this.url) || maximumCacheSeize != this.maxiumCacheSize || |
| 84 | cacheEntryTtl != this.cacheEntryTtl) { |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 85 | this.maxiumCacheSize = maximumCacheSeize; |
| 86 | this.cacheEntryTtl = cacheEntryTtl; |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 87 | this.url = url; |
| 88 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 89 | 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 Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 134 | * from there, else check for it in the locally configured data |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 135 | */ |
| 136 | if (this.url == null) { |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 137 | info = localCfgData.get(id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 138 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 139 | 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 149 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 150 | buf.append(id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 151 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 152 | 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 161 | } |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 162 | log.error("Data not found for id {}", id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 163 | return null; |
| 164 | } |
| 165 | } |