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