Merge of new rebased code
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / main / java / org / openecomp / appc / dg / common / impl / AbstractResolver.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.dg.common.impl;
23
24 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27
28 import java.util.concurrent.locks.ReentrantLock;
29
30 abstract class AbstractResolver {
31
32     private static final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractResolver.class);
33
34     private long interval;
35
36     private volatile long lastUpdate = 0l;
37     private volatile boolean isUpdateInProgress = false;
38     private volatile RankedAttributesResolver<FlowKey> dgResolver;
39
40     private final ReentrantLock INIT_LOCK = new ReentrantLock();
41
42     AbstractResolver(int interval) {
43         this.interval = interval * 1000;
44     }
45
46     private RankedAttributesResolver<FlowKey> createResolver(String resolverType) {
47         AbstractResolverDataReader reader = ResolverDataReaderFactory.createResolverDataReader(resolverType);
48         return reader.read();
49     }
50
51     private boolean isExpired() {
52         return (System.currentTimeMillis() - lastUpdate) > interval;
53     }
54
55     protected RankedAttributesResolver<FlowKey> resolver(String resolverType) {
56
57         /*
58          * In general case, the method implementation is non-blocking. The first
59          * thread that identifies data expiration will be used to refresh it. In
60          * meanwhile, any other thread will get the old instance without waiting
61          * for the updated one. The only exception is the very first time when
62          * previous instance doesn't exist - in such a cases all the threads
63          * will be waiting on INIT_LOCK while one of them initializes the
64          * resolver instance. NOTE: The initialization is intentionally
65          * implemented in lazy manner to make sure the bundle is initialized
66          * properly on startup regardless whether or not the data is correct.
67          * Afterwards, the resolver may be instantiated as many times as needed.
68          */
69
70         try {
71
72             if (dgResolver == null) {
73                 INIT_LOCK.lock();
74                 if (dgResolver != null) {
75                     INIT_LOCK.unlock();
76                 }
77             }
78
79             if (!isUpdateInProgress && isExpired()) {
80
81                 boolean doUpgrade = false;
82
83                 synchronized (this) {
84                     if (!isUpdateInProgress) {
85                         isUpdateInProgress = true;
86                         doUpgrade = true;
87                     }
88                 }
89
90                 if (doUpgrade) {
91
92                     logger.info("DG resolver configuration data has expired - initiating refresh");
93
94                     try {
95                         RankedAttributesResolver<FlowKey> temp = createResolver(resolverType);
96                         dgResolver = temp;
97                         lastUpdate = System.currentTimeMillis();
98
99                         logger.info("DG resolver configuration data has been refreshed successfully");
100                     } finally {
101                         isUpdateInProgress = false;
102                     }
103                 }
104             }
105         } finally {
106             if (INIT_LOCK.isHeldByCurrentThread()) {
107                 INIT_LOCK.unlock();
108             }
109         }
110
111         return dgResolver;
112     }
113     protected abstract FlowKey resolve(final String...args);
114 }