[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / AppsCacheServiceImple.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.service;\r
21 \r
22 import java.util.HashMap;\r
23 import java.util.List;\r
24 import java.util.Map;\r
25 \r
26 import javax.annotation.PostConstruct;\r
27 \r
28 import org.springframework.beans.factory.annotation.Autowired;\r
29 import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
30 import org.springframework.stereotype.Service;\r
31 \r
32 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
33 import org.openecomp.portalapp.portal.domain.EPApp;\r
34 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;\r
35 \r
36 @Service("appsCacheService")\r
37 @org.springframework.context.annotation.Configuration\r
38 @EnableAspectJAutoProxy\r
39 @EPMetricsLog\r
40 public class AppsCacheServiceImple implements AppsCacheService {\r
41         @Autowired\r
42         EPAppService appsService;\r
43         \r
44         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheServiceImple.class);\r
45         \r
46         final class CacheConfiguration {\r
47                 \r
48                 private long updateTime = 0;\r
49                 private int updateInterval = 10;\r
50                 \r
51                 public CacheConfiguration (long _updateTime, int _updateInterval) {\r
52                         updateTime = _updateTime;\r
53                         updateInterval = _updateInterval;\r
54                 }\r
55         }\r
56         \r
57         CacheConfiguration appConf = null;\r
58         CacheConfiguration analyticsAppConf = null;\r
59         \r
60         \r
61         private static volatile Map<Long, EPApp> appsMap;\r
62         private static volatile Map<String, EPApp> anlyticsAppsMap;\r
63         \r
64         @PostConstruct\r
65         public void init() {\r
66                 appConf = new CacheConfiguration(0, 10);\r
67                 analyticsAppConf = new CacheConfiguration(0, 3600);\r
68                 \r
69                 this.refreshAppsMap(appConf);\r
70         }\r
71 \r
72         private Map<Long, EPApp> refreshAppsMap(CacheConfiguration conf) {\r
73                 long now = System.currentTimeMillis();\r
74                 \r
75                 if(noNeedToUpdate(now, conf))\r
76                         return null;\r
77                 \r
78                 synchronized (this) {\r
79                         if(noNeedToUpdate(now, conf))\r
80                                 return null;\r
81                         List<EPApp> allApps = appsService.getAppsFullList();\r
82                         Map<Long, EPApp> newAppsMap = new HashMap<Long, EPApp>();\r
83                         for (EPApp app : allApps) {\r
84                                 newAppsMap.put(app.getId(), app);\r
85                         }\r
86                         \r
87                         Map<String, EPApp> newAnalyticsAppsMap = new HashMap<String, EPApp>();\r
88                         for (EPApp app : allApps) {\r
89                                 newAnalyticsAppsMap.put(app.getUebKey(), app);\r
90                         }\r
91                         // Switch cache with the new one.\r
92                         appsMap = newAppsMap;\r
93                         anlyticsAppsMap = newAnalyticsAppsMap;\r
94                         conf.updateTime = now;\r
95                 }\r
96                 \r
97                 return appsMap;\r
98         }\r
99 \r
100         private boolean noNeedToUpdate(long now, CacheConfiguration conf) {\r
101                 long secondsPassed = (now - conf.updateTime)/1000;\r
102                 if(secondsPassed < conf.updateInterval){\r
103                         logger.debug(EELFLoggerDelegate.debugLogger, "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = " + conf.updateInterval);\r
104                         return true; // no need to update cache\r
105                 }\r
106                 return false; // its time to update\r
107         }\r
108 \r
109         @Override\r
110         public String getAppEndpoint(Long appId) {\r
111                 refreshAppsMap(appConf);\r
112                 EPApp app = appsMap.get(appId);\r
113                 if(app != null)\r
114                         return app.getAppRestEndpoint();\r
115                 return null;\r
116         }\r
117         \r
118         @Override\r
119         public EPApp getApp(Long appId) {\r
120                 refreshAppsMap(appConf);\r
121                 EPApp app = appsMap.get(appId);\r
122                 if(app != null)\r
123                         return app;\r
124                 return null;            \r
125         }\r
126         \r
127         @Override\r
128         public EPApp getAppForAnalytics(String appKey) {\r
129                 refreshAppsMap(analyticsAppConf);\r
130                 EPApp app = anlyticsAppsMap.get(appKey);\r
131                 if(app != null)\r
132                         return app;\r
133                 return null;            \r
134         }\r
135 \r
136 }\r