First part of onap rename
[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  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
27 import org.onap.appc.rankingframework.RankedAttributesResolver;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30
31 import java.util.concurrent.locks.ReentrantLock;
32
33 abstract class AbstractResolver {
34
35     private static final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractResolver.class);
36
37     private long interval;
38
39     private volatile long lastUpdate = 0l;
40     private volatile boolean isUpdateInProgress = false;
41     private volatile RankedAttributesResolver<FlowKey> dgResolver;
42
43     private final ReentrantLock INIT_LOCK = new ReentrantLock();
44
45     AbstractResolver(int interval) {
46         this.interval = interval * 1000;
47     }
48
49     private RankedAttributesResolver<FlowKey> createResolver(String resolverType) {
50         AbstractResolverDataReader reader = ResolverDataReaderFactory.createResolverDataReader(resolverType);
51         return reader.read();
52     }
53
54     private boolean isExpired() {
55         return (System.currentTimeMillis() - lastUpdate) > interval;
56     }
57
58     protected RankedAttributesResolver<FlowKey> resolver(String resolverType) {
59
60         /*
61          * In general case, the method implementation is non-blocking. The first
62          * thread that identifies data expiration will be used to refresh it. In
63          * meanwhile, any other thread will get the old instance without waiting
64          * for the updated one. The only exception is the very first time when
65          * previous instance doesn't exist - in such a cases all the threads
66          * will be waiting on INIT_LOCK while one of them initializes the
67          * resolver instance. NOTE: The initialization is intentionally
68          * implemented in lazy manner to make sure the bundle is initialized
69          * properly on startup regardless whether or not the data is correct.
70          * Afterwards, the resolver may be instantiated as many times as needed.
71          */
72
73         try {
74
75             if (dgResolver == null) {
76                 INIT_LOCK.lock();
77                 if (dgResolver != null) {
78                     INIT_LOCK.unlock();
79                 }
80             }
81
82             if (!isUpdateInProgress && isExpired()) {
83
84                 boolean doUpgrade = false;
85
86                 synchronized (this) {
87                     if (!isUpdateInProgress) {
88                         isUpdateInProgress = true;
89                         doUpgrade = true;
90                     }
91                 }
92
93                 if (doUpgrade) {
94
95                     logger.info("DG resolver configuration data has expired - initiating refresh");
96
97                     try {
98                         RankedAttributesResolver<FlowKey> temp = createResolver(resolverType);
99                         dgResolver = temp;
100                         lastUpdate = System.currentTimeMillis();
101
102                         logger.info("DG resolver configuration data has been refreshed successfully");
103                     } finally {
104                         isUpdateInProgress = false;
105                     }
106                 }
107             }
108         } finally {
109             if (INIT_LOCK.isHeldByCurrentThread()) {
110                 INIT_LOCK.unlock();
111             }
112         }
113
114         return dgResolver;
115     }
116     protected abstract FlowKey resolve(final String...args);
117 }