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