nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / service / AppsCacheServiceImple.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.annotation.PostConstruct;
27
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;
34
35 @Service("appsCacheService")
36 @org.springframework.context.annotation.Configuration
37 @EnableAspectJAutoProxy
38 @EPMetricsLog
39 public class AppsCacheServiceImple implements AppsCacheService {
40         @Autowired
41         EPAppService appsService;
42         
43         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheServiceImple.class);
44         
45         private static long updateTime = 0;
46         private static final int cacheUpdateIntervalInSeconds = 10;
47         
48         private static volatile Map<Long, EPApp> appsMap;
49         
50         @PostConstruct
51         public void init() {
52                 this.refreshAppsMap();
53         }
54
55         private Map<Long, EPApp> refreshAppsMap() {
56                 long now = System.currentTimeMillis();
57                 
58                 if(noNeedToUpdate(now))
59                         return null;
60                 
61                 synchronized (this) {
62                         if(noNeedToUpdate(now))
63                                 return null;
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);
68                         }
69                         // Switch cache with the new one.
70                         appsMap = newAppsMap;
71                         updateTime = now;
72                 }
73                 
74                 return appsMap;
75         }
76
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
82                 }
83                 return false; // its time to update
84         }
85
86         @Override
87         public String getAppEndpoint(Long appId) {
88                 refreshAppsMap();
89                 EPApp app = appsMap.get(appId);
90                 if(app != null)
91                         return app.getAppRestEndpoint();
92                 return null;
93         }
94         
95         @Override
96         public EPApp getApp(Long appId) {
97                 refreshAppsMap();
98                 EPApp app = appsMap.get(appId);
99                 if(app != null)
100                         return app;
101                 return null;            
102         }
103
104 }