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