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;
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.app.FnAppService;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.stereotype.Service;
57 public class AppsCacheService {
60 private final FnAppService appsService;
62 private final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheService.class);
65 public AppsCacheService(FnAppService appsService) {
66 this.appsService = appsService;
69 final class CacheConfiguration {
71 private long updateTime;
72 private final int updateInterval;
74 CacheConfiguration(long updateTime, int updateInterval) {
75 this.updateTime = updateTime;
76 this.updateInterval = updateInterval;
80 private CacheConfiguration quickRefreshCacheConf = null;
81 private CacheConfiguration slowRefreshCacheConf = null;
84 private static volatile Map<Long, FnApp> appsMap;
85 private static volatile Map<String, FnApp> uebAppsMap;
89 quickRefreshCacheConf = new CacheConfiguration(0, 120);
90 slowRefreshCacheConf = new CacheConfiguration(0, 3600);
92 this.refreshAppsMap(quickRefreshCacheConf);
95 private void refreshAppsMap(CacheConfiguration conf) {
96 long now = System.currentTimeMillis();
98 if (noNeedToUpdate(now, conf)) {
102 synchronized (this) {
103 if (noNeedToUpdate(now, conf)) {
106 List<FnApp> allApps = appsService.getAppsFullList();
107 Map<Long, FnApp> newAppsMap = new HashMap<>();
108 for (FnApp app : allApps) {
109 newAppsMap.put(app.getId(), app);
112 Map<String, FnApp> newUebAppsMap = new HashMap<>();
113 for (FnApp app : allApps) {
114 newUebAppsMap.put(app.getUebKey(), app);
116 // Switch cache with the new one.
117 appsMap = newAppsMap;
118 uebAppsMap = newUebAppsMap;
119 conf.updateTime = now;
124 private boolean noNeedToUpdate(long now, CacheConfiguration conf) {
125 long secondsPassed = (now - conf.updateTime) / 1000;
126 if (secondsPassed < conf.updateInterval) {
127 logger.debug(EELFLoggerDelegate.debugLogger,
128 "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = "
129 + conf.updateInterval);
130 return true; // no need to update cache
132 return false; // its time to update
135 public String getAppEndpoint(Long appId) {
136 refreshAppsMap(quickRefreshCacheConf);
137 FnApp app = appsMap.get(appId);
139 return app.getAppRestEndpoint();
144 public List<OnboardingApp> getAppsFullList() {
145 refreshAppsMap(quickRefreshCacheConf);
146 List<FnApp> appList = new ArrayList<>(appsMap.values());
147 appList.removeIf(app -> app.getId() == 1);
148 List<FnApp> appsFinalList = appList.stream()
149 .filter(app -> app.getEnabled() && !app.getOpen()).collect(Collectors.toList());
151 List<OnboardingApp> onboardingAppsList = new ArrayList<>();
152 for (FnApp app : appsFinalList) {
153 OnboardingApp onboardingApp = new OnboardingApp();
154 appsService.createOnboardingFromApp(app, onboardingApp);
155 onboardingAppsList.add(onboardingApp);
157 return onboardingAppsList;
160 public FnApp getApp(Long appId) {
161 refreshAppsMap(quickRefreshCacheConf);
162 return appsMap.get(appId);
165 public FnApp getAppFromUeb(String appKey) {
166 return getAppFromUeb(appKey, 0);
169 public FnApp getAppFromUeb(String appKey, Integer quickCacheRefresh) {
170 refreshAppsMap(quickCacheRefresh == 1 ? quickRefreshCacheConf : slowRefreshCacheConf);
171 return uebAppsMap.get(appKey);