2 * ================================================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ================================================================================
\r
20 package org.openecomp.portalapp.portal.service;
\r
22 import java.util.HashMap;
\r
23 import java.util.List;
\r
24 import java.util.Map;
\r
26 import javax.annotation.PostConstruct;
\r
28 import org.springframework.beans.factory.annotation.Autowired;
\r
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
\r
30 import org.springframework.stereotype.Service;
\r
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
33 import org.openecomp.portalapp.portal.domain.EPApp;
\r
34 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
\r
36 @Service("appsCacheService")
\r
37 @org.springframework.context.annotation.Configuration
\r
38 @EnableAspectJAutoProxy
\r
40 public class AppsCacheServiceImple implements AppsCacheService {
\r
42 EPAppService appsService;
\r
44 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheServiceImple.class);
\r
46 final class CacheConfiguration {
\r
48 private long updateTime = 0;
\r
49 private int updateInterval = 10;
\r
51 public CacheConfiguration (long _updateTime, int _updateInterval) {
\r
52 updateTime = _updateTime;
\r
53 updateInterval = _updateInterval;
\r
57 CacheConfiguration appConf = null;
\r
58 CacheConfiguration analyticsAppConf = null;
\r
61 private static volatile Map<Long, EPApp> appsMap;
\r
62 private static volatile Map<String, EPApp> anlyticsAppsMap;
\r
65 public void init() {
\r
66 appConf = new CacheConfiguration(0, 10);
\r
67 analyticsAppConf = new CacheConfiguration(0, 3600);
\r
69 this.refreshAppsMap(appConf);
\r
72 private Map<Long, EPApp> refreshAppsMap(CacheConfiguration conf) {
\r
73 long now = System.currentTimeMillis();
\r
75 if(noNeedToUpdate(now, conf))
\r
78 synchronized (this) {
\r
79 if(noNeedToUpdate(now, conf))
\r
81 List<EPApp> allApps = appsService.getAppsFullList();
\r
82 Map<Long, EPApp> newAppsMap = new HashMap<Long, EPApp>();
\r
83 for (EPApp app : allApps) {
\r
84 newAppsMap.put(app.getId(), app);
\r
87 Map<String, EPApp> newAnalyticsAppsMap = new HashMap<String, EPApp>();
\r
88 for (EPApp app : allApps) {
\r
89 newAnalyticsAppsMap.put(app.getUebKey(), app);
\r
91 // Switch cache with the new one.
\r
92 appsMap = newAppsMap;
\r
93 anlyticsAppsMap = newAnalyticsAppsMap;
\r
94 conf.updateTime = now;
\r
100 private boolean noNeedToUpdate(long now, CacheConfiguration conf) {
\r
101 long secondsPassed = (now - conf.updateTime)/1000;
\r
102 if(secondsPassed < conf.updateInterval){
\r
103 logger.debug(EELFLoggerDelegate.debugLogger, "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = " + conf.updateInterval);
\r
104 return true; // no need to update cache
\r
106 return false; // its time to update
\r
110 public String getAppEndpoint(Long appId) {
\r
111 refreshAppsMap(appConf);
\r
112 EPApp app = appsMap.get(appId);
\r
114 return app.getAppRestEndpoint();
\r
119 public EPApp getApp(Long appId) {
\r
120 refreshAppsMap(appConf);
\r
121 EPApp app = appsMap.get(appId);
\r
128 public EPApp getAppForAnalytics(String appKey) {
\r
129 refreshAppsMap(analyticsAppConf);
\r
130 EPApp app = anlyticsAppsMap.get(appKey);
\r