[CLAMP-1] Initial ONAP CLAMP seed code commit
[clamp.git] / src / main / java / org / onap / clamp / clds / model / prop / ModelProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.model.prop;
25
26 import com.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.onap.clamp.clds.model.CldsEvent;
30 import org.onap.clamp.clds.model.CldsModel;
31 import org.camunda.bpm.engine.delegate.DelegateExecution;
32
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.logging.Logger;
36
37 /**
38  * Parse model properties.
39  */
40 public class ModelProperties {
41     private static final Logger logger = Logger.getLogger(ModelProperties.class.getName());
42
43     private ModelBpmn     modelBpmn;
44     private JsonNode      modelJson;
45
46     private final String  modelName;
47     private final String  controlName;
48     private final String  actionCd;
49
50     private Global        global;
51     private Collector     collector;
52     private StringMatch   stringMatch;
53     private Policy        policy;
54     private Tca           tca;
55
56     private String        currentModelElementId;
57     private String        policyUniqueId;
58
59     /**
60      * Retain data required to parse the ModelElement objects. (Rather than
61      * parse them all - parse them on demand if requested.)
62      *
63      * @param modelName
64      * @param controlName
65      * @param actionCd
66      * @param modelBpmnPropText
67      * @param modelPropText
68      * @throws JsonProcessingException
69      * @throws IOException
70      */
71     public ModelProperties(String modelName, String controlName, String actionCd, String modelBpmnPropText, String modelPropText) throws IOException {
72         this.modelName = modelName;
73         this.controlName = controlName;
74         this.actionCd = actionCd;
75         modelBpmn = ModelBpmn.create(modelBpmnPropText);
76         ObjectMapper mapper = new ObjectMapper();
77         modelJson = mapper.readTree(modelPropText);
78     }
79
80     /**
81      * Get the VF for a model. If return null if there is no VF.
82      *
83      * @param model
84      * @return
85      */
86     public static String getVf(CldsModel model) {
87         List<String> vfs = null;
88         try {
89             ObjectMapper mapper = new ObjectMapper();
90             JsonNode modelJson = mapper.readTree(model.getPropText());
91             Global global = new Global(modelJson);
92             vfs = global.getResourceVf();
93         } catch (IOException e) {
94             // VF is null
95         }
96         String vf = null;
97         if (vfs != null && !vfs.isEmpty()) {
98             vf = vfs.get(0);
99         }
100         return vf;
101     }
102
103     /**
104      * Create ModelProperties for Camunda Delegate.
105      *
106      * @param execution
107      * @return
108      * @throws JsonProcessingException
109      * @throws IOException
110      */
111     public static ModelProperties create(DelegateExecution execution) throws IOException {
112         String modelProp = (String) execution.getVariable("modelProp");
113         String modelBpmnProp = (String) execution.getVariable("modelBpmnProp");
114         String modelName = (String) execution.getVariable("modelName");
115         String controlName = (String) execution.getVariable("controlName");
116         String actionCd = (String) execution.getVariable("actionCd");
117
118         return new ModelProperties(modelName, controlName, actionCd, modelBpmnProp, modelProp);
119     }
120
121     /**
122      * return appropriate model element given the type
123      *
124      * @param type
125      * @return
126      */
127     public ModelElement getModelElementByType(String type) {
128         ModelElement me;
129         switch (type) {
130             case ModelElement.TYPE_COLLECTOR:
131                 me = getCollector();
132                 break;
133             case ModelElement.TYPE_STRING_MATCH:
134                 me = getStringMatch();
135                 break;
136             case ModelElement.TYPE_POLICY:
137                 me = getPolicy();
138                 break;
139             case ModelElement.TYPE_TCA:
140                 me = getTca();
141                 break;
142             default:
143                 throw new IllegalArgumentException("Invalid ModelElement type: " + type);
144         }
145         return me;
146     }
147
148     /**
149      * @return the modelName
150      */
151     public String getModelName() {
152         return modelName;
153     }
154
155     /**
156      * @return the controlName
157      */
158     public String getControlName() {
159         return controlName;
160     }
161
162     /**
163      * @return the controlNameAndPolicyUniqueId
164      */
165     public String getControlNameAndPolicyUniqueId() {
166         return controlName + "_" + policyUniqueId;
167     }
168
169     /**
170      * @return the currentPolicyName
171      */
172     private String getCurrentPolicyName() {
173         return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
174     }
175
176     /**
177      * @return the currentPolicyScopeAndPolicyName
178      */
179     public String getCurrentPolicyScopeAndPolicyName() {
180         return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName());
181     }
182
183     /**
184      * @return the currentPolicyScopeAndFullPolicyName
185      */
186     public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
187         return normalizePolicyScopeName(modelName + "." + policyNamePrefix + getCurrentPolicyName());
188     }
189
190     /**
191      * @return the currentPolicyScopeAndFullPolicyNameWithVersion
192      */
193     public String getCurrentPolicyScopeAndFullPolicyNameWithVersion(String policyNamePrefix, int version) {
194         return normalizePolicyScopeName(
195                 modelName + "." + policyNamePrefix + getCurrentPolicyName() + "." + version + ".xml");
196     }
197
198     /**
199      * Replace all '-' with '_' within policy scope and name.
200      *
201      * @param inName
202      * @return
203      */
204     private String normalizePolicyScopeName(String inName) {
205         return inName.replaceAll("-", "_");
206     }
207
208     /**
209      * @return the currentModelElementId
210      */
211     public String getCurrentModelElementId() {
212         return currentModelElementId;
213     }
214
215     /**
216      * When generating a policy request for a model element, must set the id of
217      * that model element using this method. Used to generate the policy name.
218      *
219      * @param currentModelElementId
220      *            the currentModelElementId to set
221      */
222     public void setCurrentModelElementId(String currentModelElementId) {
223         this.currentModelElementId = currentModelElementId;
224     }
225
226     /**
227      * @return the policyUniqueId
228      */
229     public String getPolicyUniqueId() {
230         return policyUniqueId;
231     }
232
233     /**
234      * When generating a policy request for a model element, must set the unique
235      * id of that policy using this method. Used to generate the policy name.
236      * 
237      * @param policyUniqueId
238      *            the policyUniqueId to set
239      */
240     public void setPolicyUniqueId(String policyUniqueId) {
241         this.policyUniqueId = policyUniqueId;
242     }
243
244     /**
245      * @return the collector
246      */
247     public Collector getCollector() {
248         if (collector == null) {
249             collector = new Collector(this, modelBpmn, modelJson);
250         }
251         return collector;
252     }
253
254     /**
255      * @return the actionCd
256      */
257     public String getActionCd() {
258         return actionCd;
259     }
260
261     /**
262      * @return the isCreateRequest
263      */
264     public boolean isCreateRequest() {
265         switch (actionCd) {
266             case CldsEvent.ACTION_SUBMIT:
267             case CldsEvent.ACTION_RESTART:
268                 return true;
269         }
270         return false;
271     }
272
273     public boolean isStopRequest() {
274         switch (actionCd) {
275             case CldsEvent.ACTION_STOP:
276                 return true;
277         }
278         return false;
279     }
280
281     /**
282      * @return the global
283      */
284     public Global getGlobal() {
285         if (global == null) {
286             global = new Global(modelJson);
287         }
288         return global;
289     }
290
291     /**
292      * @return the stringMatch
293      */
294     public StringMatch getStringMatch() {
295         if (stringMatch == null) {
296             stringMatch = new StringMatch(this, modelBpmn, modelJson);
297         }
298         return stringMatch;
299     }
300
301     /**
302      * @return the policy
303      */
304     public Policy getPolicy() {
305         if (policy == null) {
306             policy = new Policy(this, modelBpmn, modelJson);
307         }
308         return policy;
309     }
310
311     /**
312      * @return the tca
313      */
314     public Tca getTca() {
315         if (tca == null) {
316             tca = new Tca(this, modelBpmn, modelJson);
317         }
318         return tca;
319     }
320 }