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