2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
44 import java.util.stream.Collectors;
46 import javax.annotation.PostConstruct;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.context.annotation.EnableAspectJAutoProxy;
50 import org.springframework.stereotype.Service;
51 import org.onap.portalapp.portal.domain.EPApp;
52 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalapp.portal.service.AppsCacheService;
55 import org.onap.portalapp.portal.service.AppsCacheServiceImple;
56 import org.onap.portalapp.portal.transport.OnboardingApp;
58 @Service("appsCacheService")
59 @org.springframework.context.annotation.Configuration
60 @EnableAspectJAutoProxy
62 public class AppsCacheServiceImple implements AppsCacheService {
64 EPAppService appsService;
66 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheServiceImple.class);
68 final class CacheConfiguration {
70 private long updateTime = 0;
71 private int updateInterval = 10;
73 public CacheConfiguration (long _updateTime, int _updateInterval) {
74 updateTime = _updateTime;
75 updateInterval = _updateInterval;
79 CacheConfiguration quickRefreshCacheConf = null;
80 CacheConfiguration slowRefreshCacheConf = null;
83 private static volatile Map<Long, EPApp> appsMap;
84 private static volatile Map<String, EPApp> uebAppsMap;
88 quickRefreshCacheConf = new CacheConfiguration(0, 120);
89 slowRefreshCacheConf = new CacheConfiguration(0, 3600);
91 this.refreshAppsMap(quickRefreshCacheConf);
94 private void refreshAppsMap(CacheConfiguration conf) {
95 long now = System.currentTimeMillis();
97 if(noNeedToUpdate(now, conf))
100 synchronized (this) {
101 if(noNeedToUpdate(now, conf))
103 List<EPApp> allApps = appsService.getAppsFullList();
104 Map<Long, EPApp> newAppsMap = new HashMap<Long, EPApp>();
105 for (EPApp app : allApps) {
106 newAppsMap.put(app.getId(), app);
109 Map<String, EPApp> newUebAppsMap = new HashMap<String, EPApp>();
110 for (EPApp app : allApps) {
111 newUebAppsMap.put(app.getUebKey(), app);
113 // Switch cache with the new one.
114 appsMap = newAppsMap;
115 uebAppsMap = newUebAppsMap;
116 conf.updateTime = now;
121 private boolean noNeedToUpdate(long now, CacheConfiguration conf) {
122 long secondsPassed = (now - conf.updateTime)/1000;
123 if(secondsPassed < conf.updateInterval){
124 logger.debug(EELFLoggerDelegate.debugLogger, "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = " + conf.updateInterval);
125 return true; // no need to update cache
127 return false; // its time to update
131 public String getAppEndpoint(Long appId) {
132 refreshAppsMap(quickRefreshCacheConf);
133 EPApp app = appsMap.get(appId);
135 return app.getAppRestEndpoint();
139 @SuppressWarnings("unchecked")
141 public List<OnboardingApp> getAppsFullList() {
142 refreshAppsMap(quickRefreshCacheConf);
143 List<EPApp> appList = new ArrayList<EPApp> (appsMap.values());
144 appList.removeIf(app -> app.getId() == 1);
145 List<EPApp> appsFinalList = appList.stream()
146 .filter(app -> app.getEnabled() == true && app.getOpen() == false).collect(Collectors.toList());
148 List<OnboardingApp> onboardingAppsList = new ArrayList<OnboardingApp>();
149 for (EPApp app : appsFinalList) {
150 OnboardingApp onboardingApp = new OnboardingApp();
151 appsService.createOnboardingFromApp(app, onboardingApp);
152 onboardingAppsList.add(onboardingApp);
154 return onboardingAppsList;
158 public EPApp getApp(Long appId) {
159 refreshAppsMap(quickRefreshCacheConf);
160 EPApp app = appsMap.get(appId);
167 public EPApp getAppFromUeb(String appKey) {
168 return getAppFromUeb(appKey,0);
172 public EPApp getAppFromUeb(String appKey, Integer quickCacheRefresh) {
173 refreshAppsMap(quickCacheRefresh == 1 ? quickRefreshCacheConf:slowRefreshCacheConf);
174 EPApp app = uebAppsMap.get(appKey);