2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.dg.common.impl;
24 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
28 import java.util.concurrent.locks.ReentrantLock;
30 abstract class AbstractResolver {
32 private static final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractResolver.class);
34 private long interval;
36 private volatile long lastUpdate = 0l;
37 private volatile boolean isUpdateInProgress = false;
38 private volatile RankedAttributesResolver<FlowKey> dgResolver;
40 private final ReentrantLock INIT_LOCK = new ReentrantLock();
42 AbstractResolver(int interval) {
43 this.interval = interval * 1000;
46 private RankedAttributesResolver<FlowKey> createResolver(String resolverType) {
47 AbstractResolverDataReader reader = ResolverDataReaderFactory.createResolverDataReader(resolverType);
51 private boolean isExpired() {
52 return (System.currentTimeMillis() - lastUpdate) > interval;
55 protected RankedAttributesResolver<FlowKey> resolver(String resolverType) {
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.
72 if (dgResolver == null) {
74 if (dgResolver != null) {
79 if (!isUpdateInProgress && isExpired()) {
81 boolean doUpgrade = false;
84 if (!isUpdateInProgress) {
85 isUpdateInProgress = true;
92 logger.info("DG resolver configuration data has expired - initiating refresh");
95 RankedAttributesResolver<FlowKey> temp = createResolver(resolverType);
97 lastUpdate = System.currentTimeMillis();
99 logger.info("DG resolver configuration data has been refreshed successfully");
101 isUpdateInProgress = false;
106 if (INIT_LOCK.isHeldByCurrentThread()) {
113 protected abstract FlowKey resolve(final String...args);