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