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; |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 29 | import com.fasterxml.jackson.databind.module.SimpleModule; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 30 | import com.google.common.cache.Cache; |
| 31 | import com.google.common.cache.CacheBuilder; |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 32 | import com.google.common.collect.Maps; |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 33 | import org.onlab.packet.VlanId; |
| 34 | import org.onlab.packet.Ip4Address; |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 35 | |
| 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 39 | |
| 40 | public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService { |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 41 | |
| 42 | private final Logger log = LoggerFactory.getLogger(this.getClass()); |
| 43 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 44 | private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0; |
| 45 | private static final long DEFAULT_TTL = 0; |
| 46 | |
| 47 | private String url; |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 48 | private ObjectMapper mapper; |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 49 | private Cache<String, SubscriberAndDeviceInformation> cache; |
| 50 | private int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE; |
| 51 | private long cacheEntryTtl = DEFAULT_TTL; |
| 52 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 53 | private Map<String, SubscriberAndDeviceInformation> localCfgData = null; |
| 54 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 55 | public SubscriberAndDeviceInformationAdapter() { |
| 56 | cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize) |
| 57 | .expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build(); |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 58 | 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 68 | /** |
| 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 94 | // Rebuild cache if needed |
Deepa vaddireddy | ad29519 | 2017-08-10 13:14:02 +0530 | [diff] [blame] | 95 | if (isurlChanged(url) || maximumCacheSeize != this.maxiumCacheSize || |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 96 | cacheEntryTtl != this.cacheEntryTtl) { |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 97 | this.maxiumCacheSize = maximumCacheSeize; |
| 98 | this.cacheEntryTtl = cacheEntryTtl; |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 99 | this.url = url; |
| 100 | |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 101 | 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 vaddireddy | ad29519 | 2017-08-10 13:14:02 +0530 | [diff] [blame] | 114 | 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 121 | /* |
| 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 vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 136 | * 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 167 | * 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 Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 184 | * from there, else check for it in the locally configured data |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 185 | */ |
| 186 | if (this.url == null) { |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 187 | info = (localCfgData == null) ? null : localCfgData.get(id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 188 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 189 | if (info != null) { |
| 190 | local.put(id, info); |
| 191 | return info; |
| 192 | } |
| 193 | } else { |
| 194 | // Augment URL with query parameters |
| 195 | StringBuilder buf = new StringBuilder(this.url); |
| 196 | if (buf.charAt(buf.length() - 1) != '/') { |
| 197 | buf.append('/'); |
| 198 | } |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 199 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 200 | buf.append(id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 201 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 202 | try (InputStream io = new URL(buf.toString()).openStream()) { |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 203 | info = mapper.readValue(io, SubscriberAndDeviceInformation.class); |
| 204 | local.put(id, info); |
| 205 | return info; |
| 206 | } catch (IOException e) { |
| 207 | // TODO Auto-generated catch block |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame] | 208 | log.warn("Exception while reading remote data " + e.getMessage()); |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 209 | } |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 210 | } |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 211 | log.error("Data not found for id {}", id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 212 | return null; |
| 213 | } |
| 214 | } |