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 |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 95 | if ((url != null && url != this.url) || maximumCacheSeize != this.maxiumCacheSize || |
| 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 | |
| 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 vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame^] | 129 | * 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 160 | * 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 Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 177 | * from there, else check for it in the locally configured data |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 178 | */ |
| 179 | if (this.url == null) { |
Deepa vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame^] | 180 | info = (localCfgData == null) ? null : localCfgData.get(id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 181 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 182 | 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. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 192 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 193 | buf.append(id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 194 | |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 195 | try (InputStream io = new URL(buf.toString()).openStream()) { |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 196 | 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 vaddireddy | 386f38b | 2017-08-02 06:24:01 +0000 | [diff] [blame^] | 201 | log.warn("Exception while reading remote data " + e.getMessage()); |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 202 | } |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 203 | } |
Amit Ghosh | 38b232a | 2017-07-23 15:11:56 +0100 | [diff] [blame] | 204 | log.error("Data not found for id {}", id); |
David K. Bainbridge | eda2b05 | 2017-07-12 09:41:04 -0700 | [diff] [blame] | 205 | return null; |
| 206 | } |
| 207 | } |