a92d33c1dbdf8a1b592122a949286fe7167a5693
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.workflow.impl;
24
25 import java.util.concurrent.locks.ReentrantLock;
26
27 import org.openecomp.appc.rankingframework.RankedAttributesContext;
28 import org.openecomp.appc.rankingframework.RankedAttributesResolver;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31
32 class WorkflowResolver {
33
34     private static final EELFLogger logger = EELFManager.getInstance().getLogger(WorkFlowManagerImpl.class);
35
36     private long interval;
37
38     private volatile long lastUpdate = 0l;
39     private volatile boolean isUpdateInProgress = false;
40     private volatile RankedAttributesResolver<WorkflowKey> dgResolver;
41
42     private final ReentrantLock INIT_LOCK = new ReentrantLock();
43
44     WorkflowResolver(int interval) {
45         this.interval = interval * 1000;
46     }
47
48     private RankedAttributesResolver<WorkflowKey> createResolver() {
49         WorkflowResolverDataReader reader = new WorkflowResolverDataReader();
50         return reader.read();
51     }
52
53     private boolean isExpired() {
54         return (System.currentTimeMillis() - lastUpdate) > interval;
55     }
56
57     private RankedAttributesResolver<WorkflowKey> resolver() {
58
59         /*
60          * In general case, the method implementation is non-blocking. The first
61          * thread that identifies data expiration will be used to refresh it. In
62          * meanwhile, any other thread will get the old instance without waiting
63          * for the updated one. The only exception is the very first time when
64          * previous instance doesn't exist - in such a cases all the threads
65          * will be waiting on INIT_LOCK while one of them initializes the
66          * resolver instance. NOTE: The initialization is intentionally
67          * implemented in lazy manner to make sure the bundle is initialized
68          * properly on startup regardless whether or not the data is correct.
69          * Afterwards, the resolver may be instantiated as many times as needed.
70          */
71
72         try {
73
74             if (dgResolver == null) {
75                 INIT_LOCK.lock();
76                 if (dgResolver != null) {
77                     INIT_LOCK.unlock();
78                 }
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
92                 if (doUpgrade) {
93
94                     logger.info("DG resolver configuration data has expired - initiating refresh");
95
96                     try {
97                         RankedAttributesResolver<WorkflowKey> temp = createResolver();
98                         dgResolver = temp;
99                         lastUpdate = System.currentTimeMillis();
100
101                         logger.info("DG resolver configuration data has been refreshed successfully");
102                     } finally {
103                         isUpdateInProgress = false;
104                     }
105                 }
106             }
107         } finally {
108             if (INIT_LOCK.isHeldByCurrentThread()) {
109                 INIT_LOCK.unlock();
110             }
111         }
112
113         return dgResolver;
114     }
115
116     WorkflowKey resolve(final String action, final String vnfType, final String vnfVersion,final String apiVersion) {
117
118         RankedAttributesContext context = new RankedAttributesContext() {
119             @Override
120             public Object getAttributeValue(String name) {
121                 switch (name) {
122                     case "action":
123                         return action;
124                     case "api_version":
125                         return apiVersion;
126                     case "vnf_type":
127                         return vnfType;
128                     case "vnf_version":
129                         return vnfVersion;
130                     default:
131                         throw new IllegalStateException(name);
132                 }
133             }
134         };
135
136         WorkflowKey wfKey = resolver().resolve(context);
137
138         return wfKey;
139     }
140 }