7703420b8a81df4024a4746238b07fa5c6d0d5f8
[portal.git] / portal-BE / src / main / java / org / onap / portal / service / fn / old / AppsCacheService.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.service.fn.old;
42
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.stream.Collectors;
48 import javax.annotation.PostConstruct;
49 import org.onap.portal.domain.db.fn.FnApp;
50 import org.onap.portal.domain.dto.transport.OnboardingApp;
51 import org.onap.portal.service.fn.FnAppService;
52 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.context.annotation.Configuration;
55 import org.springframework.context.annotation.EnableAspectJAutoProxy;
56 import org.springframework.stereotype.Service;
57
58 @Service("appsCacheService")
59 @Configuration
60 @EnableAspectJAutoProxy
61 public class AppsCacheService {
62         @Autowired
63         private
64         FnAppService appsService;
65         
66         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheService.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         private CacheConfiguration quickRefreshCacheConf = null;
80         private CacheConfiguration slowRefreshCacheConf = null;
81         
82         
83         private static volatile Map<Long, FnApp> appsMap;
84         private static volatile Map<String, FnApp> 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<FnApp> allApps = appsService.getAppsFullList();
104                         Map<Long, FnApp> newAppsMap = new HashMap<>();
105                         for (FnApp app : allApps) {
106                                 newAppsMap.put(app.getId(), app);
107                         }
108                         
109                         Map<String, FnApp> newUebAppsMap = new HashMap<>();
110                         for (FnApp 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         public String getAppEndpoint(Long appId) {
131                 refreshAppsMap(quickRefreshCacheConf);
132                 FnApp app = appsMap.get(appId);
133                 if(app != null)
134                         return app.getAppRestEndpoint();
135                 return null;
136         }
137
138         public List<OnboardingApp> getAppsFullList() {
139                 refreshAppsMap(quickRefreshCacheConf);
140                 List<FnApp> appList = new ArrayList<>(appsMap.values());
141                 appList.removeIf(app -> app.getId() == 1);
142                 List<FnApp> appsFinalList = appList.stream()
143                 .filter(app -> app.getEnabled() && !app.getOpen()).collect(Collectors.toList());
144                 
145                 List<OnboardingApp> onboardingAppsList = new ArrayList<>();
146                 for (FnApp app : appsFinalList) {
147                         OnboardingApp onboardingApp = new OnboardingApp();
148                         appsService.createOnboardingFromApp(app, onboardingApp);
149                         onboardingAppsList.add(onboardingApp);
150                 }
151                 return onboardingAppsList;      
152         }
153
154         public FnApp getApp(Long appId) {
155                 refreshAppsMap(quickRefreshCacheConf);
156                 FnApp app = appsMap.get(appId);
157                 return app;
158         }
159
160         public FnApp getAppFromUeb(String appKey) {
161                 return  getAppFromUeb(appKey,0);        
162         }
163
164         public FnApp getAppFromUeb(String appKey, Integer quickCacheRefresh) {
165                 refreshAppsMap(quickCacheRefresh == 1 ? quickRefreshCacheConf:slowRefreshCacheConf);
166                 FnApp app = uebAppsMap.get(appKey);
167                 return app;
168         }
169
170 }