2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
10 * Unless otherwise specified, all software contained herein is licensed
11 * under the Apache License, Version 2.0 (the "License");
12 * you may not use this software except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * Unless otherwise specified, all documentation contained herein is licensed
24 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25 * you may not use this documentation except in compliance with the License.
26 * You may obtain a copy of the License at
28 * https://creativecommons.org/licenses/by/4.0/
30 * Unless required by applicable law or agreed to in writing, documentation
31 * distributed under the License is distributed on an "AS IS" BASIS,
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
36 * ============LICENSE_END============================================
41 package org.onap.portal.service.fn.old;
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
47 import java.util.stream.Collectors;
48 import javax.annotation.PostConstruct;
49 import org.onap.portal.domain.db.fn.FnApp;
50 import org.onap.portal.domain.dto.transport.OnboardingApp;
51 import org.onap.portal.service.fn.FnAppService;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.context.annotation.Configuration;
55 import org.springframework.context.annotation.EnableAspectJAutoProxy;
56 import org.springframework.stereotype.Service;
58 @Service("appsCacheService")
60 @EnableAspectJAutoProxy
61 public class AppsCacheService {
64 FnAppService appsService;
66 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheService.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 private CacheConfiguration quickRefreshCacheConf = null;
80 private CacheConfiguration slowRefreshCacheConf = null;
83 private static volatile Map<Long, FnApp> appsMap;
84 private static volatile Map<String, FnApp> 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<FnApp> allApps = appsService.getAppsFullList();
104 Map<Long, FnApp> newAppsMap = new HashMap<>();
105 for (FnApp app : allApps) {
106 newAppsMap.put(app.getId(), app);
109 Map<String, FnApp> newUebAppsMap = new HashMap<>();
110 for (FnApp 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
130 public String getAppEndpoint(Long appId) {
131 refreshAppsMap(quickRefreshCacheConf);
132 FnApp app = appsMap.get(appId);
134 return app.getAppRestEndpoint();
138 public List<OnboardingApp> getAppsFullList() {
139 refreshAppsMap(quickRefreshCacheConf);
140 List<FnApp> appList = new ArrayList<FnApp> (appsMap.values());
141 appList.removeIf(app -> app.getId() == 1);
142 List<FnApp> appsFinalList = appList.stream()
143 .filter(app -> app.getEnabled() && !app.getOpen()).collect(Collectors.toList());
145 List<OnboardingApp> onboardingAppsList = new ArrayList<OnboardingApp>();
146 for (FnApp app : appsFinalList) {
147 OnboardingApp onboardingApp = new OnboardingApp();
148 appsService.createOnboardingFromApp(app, onboardingApp);
149 onboardingAppsList.add(onboardingApp);
151 return onboardingAppsList;
154 public FnApp getApp(Long appId) {
155 refreshAppsMap(quickRefreshCacheConf);
156 FnApp app = appsMap.get(appId);
162 public FnApp getAppFromUeb(String appKey) {
163 return getAppFromUeb(appKey,0);
166 public FnApp getAppFromUeb(String appKey, Integer quickCacheRefresh) {
167 refreshAppsMap(quickCacheRefresh == 1 ? quickRefreshCacheConf:slowRefreshCacheConf);
168 FnApp app = uebAppsMap.get(appKey);