[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common / 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.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;
30 import org.springframework.stereotype.Service;
31
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
33 import org.openecomp.portalapp.portal.domain.EPApp;
34 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
35
36 @Service("appsCacheService")
37 @org.springframework.context.annotation.Configuration
38 @EnableAspectJAutoProxy
39 @EPMetricsLog
40 public class AppsCacheServiceImple implements AppsCacheService {
41         @Autowired
42         EPAppService appsService;
43         
44         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheServiceImple.class);
45         
46         final class CacheConfiguration {
47                 
48                 private long updateTime = 0;
49                 private int updateInterval = 10;
50                 
51                 public CacheConfiguration (long _updateTime, int _updateInterval) {
52                         updateTime = _updateTime;
53                         updateInterval = _updateInterval;
54                 }
55         }
56         
57         CacheConfiguration appConf = null;
58         CacheConfiguration analyticsAppConf = null;
59         
60         
61         private static volatile Map<Long, EPApp> appsMap;
62         private static volatile Map<String, EPApp> anlyticsAppsMap;
63         
64         @PostConstruct
65         public void init() {
66                 appConf = new CacheConfiguration(0, 10);
67                 analyticsAppConf = new CacheConfiguration(0, 3600);
68                 
69                 this.refreshAppsMap(appConf);
70         }
71
72         private Map<Long, EPApp> refreshAppsMap(CacheConfiguration conf) {
73                 long now = System.currentTimeMillis();
74                 
75                 if(noNeedToUpdate(now, conf))
76                         return null;
77                 
78                 synchronized (this) {
79                         if(noNeedToUpdate(now, conf))
80                                 return null;
81                         List<EPApp> allApps = appsService.getAppsFullList();
82                         Map<Long, EPApp> newAppsMap = new HashMap<Long, EPApp>();
83                         for (EPApp app : allApps) {
84                                 newAppsMap.put(app.getId(), app);
85                         }
86                         
87                         Map<String, EPApp> newAnalyticsAppsMap = new HashMap<String, EPApp>();
88                         for (EPApp app : allApps) {
89                                 newAnalyticsAppsMap.put(app.getUebKey(), app);
90                         }
91                         // Switch cache with the new one.
92                         appsMap = newAppsMap;
93                         anlyticsAppsMap = newAnalyticsAppsMap;
94                         conf.updateTime = now;
95                 }
96                 
97                 return appsMap;
98         }
99
100         private boolean noNeedToUpdate(long now, CacheConfiguration conf) {
101                 long secondsPassed = (now - conf.updateTime)/1000;
102                 if(secondsPassed < conf.updateInterval){
103                         logger.debug(EELFLoggerDelegate.debugLogger, "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = " + conf.updateInterval);
104                         return true; // no need to update cache
105                 }
106                 return false; // its time to update
107         }
108
109         @Override
110         public String getAppEndpoint(Long appId) {
111                 refreshAppsMap(appConf);
112                 EPApp app = appsMap.get(appId);
113                 if(app != null)
114                         return app.getAppRestEndpoint();
115                 return null;
116         }
117         
118         @Override
119         public EPApp getApp(Long appId) {
120                 refreshAppsMap(appConf);
121                 EPApp app = appsMap.get(appId);
122                 if(app != null)
123                         return app;
124                 return null;            
125         }
126         
127         @Override
128         public EPApp getAppForAnalytics(String appKey) {
129                 refreshAppsMap(analyticsAppConf);
130                 EPApp app = anlyticsAppsMap.get(appKey);
131                 if(app != null)
132                         return app;
133                 return null;            
134         }
135
136 }