09a78e85a924c934e2a90342e30efcd007b700c4
[portal.git] / ecomp-portal-BE-os / src / main / java / org / openecomp / portalapp / portal / service / AppsCacheServiceImple.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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.openecomp.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.openecomp.portalapp.portal.domain.EPApp;
47 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
48 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.context.annotation.EnableAspectJAutoProxy;
51 import org.springframework.stereotype.Service;
52
53 @Service("appsCacheService")
54 @org.springframework.context.annotation.Configuration
55 @EnableAspectJAutoProxy
56 @EPMetricsLog
57 public class AppsCacheServiceImple implements AppsCacheService {
58         @Autowired
59         EPAppService appsService;
60         
61         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AppsCacheServiceImple.class);
62         
63         private static long updateTime = 0;
64         private static final int cacheUpdateIntervalInSeconds = 10;
65         
66         private static volatile Map<Long, EPApp> appsMap;
67         
68         @PostConstruct
69         public void init() {
70                 this.refreshAppsMap();
71         }
72
73         private Map<Long, EPApp> refreshAppsMap() {
74                 long now = System.currentTimeMillis();
75                 
76                 if(noNeedToUpdate(now))
77                         return null;
78                 
79                 synchronized (this) {
80                         if(noNeedToUpdate(now))
81                                 return null;
82                         List<EPApp> allApps = appsService.getAppsFullList();
83                         Map<Long, EPApp> newAppsMap = new HashMap<Long, EPApp>();
84                         for (EPApp app : allApps) {
85                                 newAppsMap.put(app.getId(), app);
86                         }
87                         // Switch cache with the new one.
88                         appsMap = newAppsMap;
89                         updateTime = now;
90                 }
91                 
92                 return appsMap;
93         }
94
95         private boolean noNeedToUpdate(long now) {
96                 long secondsPassed = (now - updateTime)/1000;
97                 if(secondsPassed < cacheUpdateIntervalInSeconds){
98                         logger.debug(EELFLoggerDelegate.debugLogger, "no need to refresh yet, seconds since last refresh: " + secondsPassed + ", refresh interval (sec) = " + cacheUpdateIntervalInSeconds);
99                         return true; // no need to update cache
100                 }
101                 return false; // its time to update
102         }
103
104         @Override
105         public String getAppEndpoint(Long appId) {
106                 refreshAppsMap();
107                 EPApp app = appsMap.get(appId);
108                 if(app != null)
109                         return app.getAppRestEndpoint();
110                 return null;
111         }
112         
113         @Override
114         public EPApp getApp(Long appId) {
115                 refreshAppsMap();
116                 EPApp app = appsMap.get(appId);
117                 if(app != null)
118                         return app;
119                 return null;            
120         }
121
122 }