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