Merge "Fix NPE & other issues"
[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-2018 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  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.stream.Collectors;
45
46 import javax.annotation.PostConstruct;
47
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.context.annotation.EnableAspectJAutoProxy;
50 import org.springframework.stereotype.Service;
51 import org.onap.portalapp.portal.domain.EPApp;
52 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalapp.portal.service.AppsCacheService;
55 import org.onap.portalapp.portal.service.AppsCacheServiceImple;
56 import org.onap.portalapp.portal.transport.OnboardingApp;
57
58 @Service("appsCacheService")
59 @org.springframework.context.annotation.Configuration
60 @EnableAspectJAutoProxy
61 @EPMetricsLog
62 public class AppsCacheServiceImple implements AppsCacheService {
63         @Autowired
64         EPAppService appsService;
65         
66         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheServiceImple.class);
67         
68         final class CacheConfiguration {
69                 
70                 private long updateTime = 0;
71                 private int updateInterval = 10;
72                 
73                 public CacheConfiguration (long _updateTime, int _updateInterval) {
74                         updateTime = _updateTime;
75                         updateInterval = _updateInterval;
76                 }
77         }
78         
79         CacheConfiguration quickRefreshCacheConf = null;
80         CacheConfiguration slowRefreshCacheConf = null;
81         
82         
83         private static volatile Map<Long, EPApp> appsMap;
84         private static volatile Map<String, EPApp> uebAppsMap;
85         
86         @PostConstruct
87         public void init() {
88                 quickRefreshCacheConf = new CacheConfiguration(0, 120);
89                 slowRefreshCacheConf = new CacheConfiguration(0, 3600);
90                 
91                 this.refreshAppsMap(quickRefreshCacheConf);
92         }
93
94         private void refreshAppsMap(CacheConfiguration conf) {
95                 long now = System.currentTimeMillis();
96                 
97                 if(noNeedToUpdate(now, conf))
98                         return;
99                 
100                 synchronized (this) {
101                         if(noNeedToUpdate(now, conf))
102                                 return;
103                         List<EPApp> allApps = appsService.getAppsFullList();
104                         Map<Long, EPApp> newAppsMap = new HashMap<Long, EPApp>();
105                         for (EPApp app : allApps) {
106                                 newAppsMap.put(app.getId(), app);
107                         }
108                         
109                         Map<String, EPApp> newUebAppsMap = new HashMap<String, EPApp>();
110                         for (EPApp app : allApps) {
111                                 newUebAppsMap.put(app.getUebKey(), app);
112                         }
113                         // Switch cache with the new one.
114                         appsMap = newAppsMap;
115                         uebAppsMap = newUebAppsMap;
116                         conf.updateTime = now;
117                 }
118                 
119         }
120
121         private boolean noNeedToUpdate(long now, CacheConfiguration conf) {
122                 long secondsPassed = (now - conf.updateTime)/1000;
123                 if(secondsPassed < conf.updateInterval){
124                         logger.debug(EELFLoggerDelegate.debugLogger, "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = " + conf.updateInterval);
125                         return true; // no need to update cache
126                 }
127                 return false; // its time to update
128         }
129
130         @Override
131         public String getAppEndpoint(Long appId) {
132                 refreshAppsMap(quickRefreshCacheConf);
133                 EPApp app = appsMap.get(appId);
134                 if(app != null)
135                         return app.getAppRestEndpoint();
136                 return null;
137         }
138         
139         @SuppressWarnings("unchecked")
140         @Override
141         public List<OnboardingApp> getAppsFullList() {
142                 refreshAppsMap(quickRefreshCacheConf);
143                 List<EPApp> appList = new ArrayList<EPApp> (appsMap.values());
144                 appList.removeIf(app -> app.getId() == 1);
145                 List<EPApp> appsFinalList = appList.stream()
146                 .filter(app -> app.getEnabled() == true && app.getOpen() == false).collect(Collectors.toList());
147                 
148                 List<OnboardingApp> onboardingAppsList = new ArrayList<OnboardingApp>();
149                 for (EPApp app : appsFinalList) {
150                         OnboardingApp onboardingApp = new OnboardingApp();
151                         appsService.createOnboardingFromApp(app, onboardingApp);
152                         onboardingAppsList.add(onboardingApp);
153                 }
154                 return onboardingAppsList;      
155         }
156         
157         @Override
158         public EPApp getApp(Long appId) {
159                 refreshAppsMap(quickRefreshCacheConf);
160                 EPApp app = appsMap.get(appId);
161                 if(app != null)
162                         return app;
163                 return null;            
164         }
165         
166         @Override
167         public EPApp getAppFromUeb(String appKey) {
168                 return  getAppFromUeb(appKey,0);        
169         }
170         
171         @Override
172         public EPApp getAppFromUeb(String appKey, Integer quickCacheRefresh) {
173                 refreshAppsMap(quickCacheRefresh == 1 ? quickRefreshCacheConf:slowRefreshCacheConf);
174                 EPApp app = uebAppsMap.get(appKey);
175                 if(app != null)
176                         return app;
177                 return null;            
178         }
179
180 }