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