blob: c13170455a646725f1fa780ebd4dfb4228e9ff06 [file] [log] [blame]
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
16package org.opencord.sadis;
17
18import java.io.IOException;
19import java.io.InputStream;
20import java.net.URL;
21import java.util.concurrent.TimeUnit;
22
23import com.fasterxml.jackson.databind.ObjectMapper;
24import com.google.common.cache.Cache;
25import com.google.common.cache.CacheBuilder;
26
27public abstract class SubscriberAndDeviceInformationAdapter implements SubscriberAndDeviceInformationService {
28 private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 0;
29 private static final long DEFAULT_TTL = 0;
30
31 private String url;
32 private Cache<String, SubscriberAndDeviceInformation> cache;
33 private int maxiumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
34 private long cacheEntryTtl = DEFAULT_TTL;
35
36 public SubscriberAndDeviceInformationAdapter() {
37 cache = CacheBuilder.newBuilder().maximumSize(maxiumCacheSize)
38 .expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
39 }
40
41 public void configure(String url, int maximumCacheSeize, long cacheEntryTtl) {
42 // Rebuild cache if needed
43 if (url != this.url || maximumCacheSeize != this.maxiumCacheSize || cacheEntryTtl != this.cacheEntryTtl) {
44 this.url = url;
45 this.maxiumCacheSize = maximumCacheSeize;
46 this.cacheEntryTtl = cacheEntryTtl;
47 Cache<String, SubscriberAndDeviceInformation> newCache = CacheBuilder.newBuilder()
48 .maximumSize(maxiumCacheSize).expireAfterAccess(cacheEntryTtl, TimeUnit.SECONDS).build();
49 Cache<String, SubscriberAndDeviceInformation> oldCache = cache;
50
51 synchronized (this) {
52 cache = newCache;
53 }
54
55 oldCache.invalidateAll();
56 oldCache.cleanUp();
57 }
58 }
59
60 /*
61 * (non-Javadoc)
62 *
63 * @see
64 * org.opencord.sadis.SubscriberAndDeviceInformationService#clearCache()
65 */
66 @Override
67 public void invalidateAll() {
68 cache.invalidateAll();
69 }
70
71 /*
72 * (non-Javadoc)
73 *
74 * @see
75 * org.opencord.sadis.SubscriberAndDeviceInformationService#get(java.lang.
76 * String)
77 */
78 @Override
79 public SubscriberAndDeviceInformation get(String id) {
80 Cache<String, SubscriberAndDeviceInformation> local;
81 synchronized (this) {
82 local = cache;
83 }
84
85 SubscriberAndDeviceInformation info = local.getIfPresent(id);
86 if (info != null) {
87 return info;
88 }
89
90 /*
91 * Not in cache, if we have a URL configured we can attempt to get it
92 * from there.
93 */
94 if (this.url == null) {
95 return null;
96 }
97
98 // Augment URL with query parameters
99 StringBuilder buf = new StringBuilder(this.url);
100 if (buf.charAt(buf.length() - 1) != '/') {
101 buf.append('/');
102 }
103
104 buf.append(id);
105
106 try (InputStream io = new URL(buf.toString()).openStream()) {
107 ObjectMapper mapper = new ObjectMapper();
108 info = mapper.readValue(io, SubscriberAndDeviceInformation.class);
109 local.put(id, info);
110 return info;
111 } catch (IOException e) {
112 // TODO Auto-generated catch block
113 e.printStackTrace();
114 }
115 return null;
116 }
117}