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.openecomp.portalapp.portal.domain.EPApp;
29 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
30 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.context.annotation.EnableAspectJAutoProxy;
33 import org.springframework.stereotype.Service;
35 @Service("appsCacheService")
36 @org.springframework.context.annotation.Configuration
37 @EnableAspectJAutoProxy
39 public class AppsCacheServiceImple implements AppsCacheService {
41 EPAppService appsService;
43 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheServiceImple.class);
45 private static long updateTime = 0;
46 private static final int cacheUpdateIntervalInSeconds = 10;
48 private static volatile Map<Long, EPApp> appsMap;
52 this.refreshAppsMap();
55 private Map<Long, EPApp> refreshAppsMap() {
56 long now = System.currentTimeMillis();
58 if(noNeedToUpdate(now))
62 if(noNeedToUpdate(now))
64 List<EPApp> allApps = appsService.getAppsFullList();
65 Map<Long, EPApp> newAppsMap = new HashMap<Long, EPApp>();
66 for (EPApp app : allApps) {
67 newAppsMap.put(app.getId(), app);
69 // Switch cache with the new one.
77 private boolean noNeedToUpdate(long now) {
78 long secondsPassed = (now - updateTime)/1000;
79 if(secondsPassed < cacheUpdateIntervalInSeconds){
80 logger.debug(EELFLoggerDelegate.debugLogger, "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = " + cacheUpdateIntervalInSeconds);
81 return true; // no need to update cache
83 return false; // its time to update
87 public String getAppEndpoint(Long appId) {
89 EPApp app = appsMap.get(appId);
91 return app.getAppRestEndpoint();
96 public EPApp getApp(Long appId) {
98 EPApp app = appsMap.get(appId);