UserRolesController methods up
[portal.git] / portal-BE / src / main / java / org / onap / portal / service / 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;
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.stereotype.Service;
55
56 @Service
57 public class AppsCacheService {
58
59
60   private final FnAppService appsService;
61
62   private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheService.class);
63
64   @Autowired
65   public AppsCacheService(FnAppService appsService) {
66     this.appsService = appsService;
67   }
68
69   final class CacheConfiguration {
70
71     private long updateTime;
72     private final int updateInterval;
73
74     CacheConfiguration(long updateTime, int updateInterval) {
75       this.updateTime = updateTime;
76       this.updateInterval = updateInterval;
77     }
78   }
79
80   private CacheConfiguration quickRefreshCacheConf = null;
81   private CacheConfiguration slowRefreshCacheConf = null;
82
83
84   private static volatile Map<Long, FnApp> appsMap;
85   private static volatile Map<String, FnApp> 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
102     synchronized (this) {
103       if (noNeedToUpdate(now, conf)) {
104         return;
105       }
106       List<FnApp> allApps = appsService.getAppsFullList();
107       Map<Long, FnApp> newAppsMap = new HashMap<>();
108       for (FnApp app : allApps) {
109         newAppsMap.put(app.getId(), app);
110       }
111
112       Map<String, FnApp> newUebAppsMap = new HashMap<>();
113       for (FnApp app : allApps) {
114         newUebAppsMap.put(app.getUebKey(), app);
115       }
116       // Switch cache with the new one.
117       appsMap = newAppsMap;
118       uebAppsMap = newUebAppsMap;
119       conf.updateTime = now;
120     }
121
122   }
123
124   private boolean noNeedToUpdate(long now, CacheConfiguration conf) {
125     long secondsPassed = (now - conf.updateTime) / 1000;
126     if (secondsPassed < conf.updateInterval) {
127       logger.debug(EELFLoggerDelegate.debugLogger,
128           "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = "
129               + conf.updateInterval);
130       return true; // no need to update cache
131     }
132     return false; // its time to update
133   }
134
135   public String getAppEndpoint(Long appId) {
136     refreshAppsMap(quickRefreshCacheConf);
137     FnApp app = appsMap.get(appId);
138     if (app != null) {
139       return app.getAppRestEndpoint();
140     }
141     return null;
142   }
143
144   public List<OnboardingApp> getAppsFullList() {
145     refreshAppsMap(quickRefreshCacheConf);
146     List<FnApp> appList = new ArrayList<>(appsMap.values());
147     appList.removeIf(app -> app.getId() == 1);
148     List<FnApp> appsFinalList = appList.stream()
149         .filter(app -> app.getEnabled() && !app.getOpen()).collect(Collectors.toList());
150
151     List<OnboardingApp> onboardingAppsList = new ArrayList<OnboardingApp>();
152     for (FnApp app : appsFinalList) {
153       OnboardingApp onboardingApp = new OnboardingApp();
154       appsService.createOnboardingFromApp(app, onboardingApp);
155       onboardingAppsList.add(onboardingApp);
156     }
157     return onboardingAppsList;
158   }
159
160   public FnApp getApp(Long appId) {
161     refreshAppsMap(quickRefreshCacheConf);
162     return appsMap.get(appId);
163   }
164
165   public FnApp getAppFromUeb(String appKey) {
166     return getAppFromUeb(appKey, 0);
167   }
168
169   public FnApp getAppFromUeb(String appKey, Integer quickCacheRefresh) {
170     refreshAppsMap(quickCacheRefresh == 1 ? quickRefreshCacheConf : slowRefreshCacheConf);
171     return uebAppsMap.get(appKey);
172   }
173
174 }