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============================================
20 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.model.prop;
26 import java.io.IOException;
27 import java.lang.reflect.InvocationTargetException;
28 import java.util.HashSet;
29 import java.util.List;
32 import java.util.concurrent.ConcurrentHashMap;
34 import org.camunda.bpm.engine.delegate.DelegateExecution;
35 import org.onap.clamp.clds.model.CldsEvent;
36 import org.onap.clamp.clds.model.CldsModel;
37 import org.onap.clamp.clds.service.CldsService;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.fasterxml.jackson.core.JsonProcessingException;
42 import com.fasterxml.jackson.databind.JsonNode;
43 import com.fasterxml.jackson.databind.ObjectMapper;
46 * Parse model properties.
48 public class ModelProperties {
49 protected static final EELFLogger logger = EELFManager.getInstance()
50 .getLogger(CldsService.class);
51 protected static final EELFLogger auditLogger = EELFManager.getInstance()
54 private ModelBpmn modelBpmn;
55 private JsonNode modelJson;
57 private final String modelName;
58 private final String controlName;
59 private final String actionCd;
60 // Flag indicate whether it is triggered by Validation Test button from UI
61 private final boolean isTest;
63 private Global global;
66 private final Map<String, ModelElement> modelElements = new ConcurrentHashMap<>();
68 private String currentModelElementId;
69 private String policyUniqueId;
71 private static final Object lock = new Object();
72 private static Map<Class<? extends ModelElement>, String> modelElementClasses = new ConcurrentHashMap<>();
76 modelElementClasses.put(Collector.class, Collector.getType());
77 modelElementClasses.put(Policy.class, Policy.getType());
78 modelElementClasses.put(StringMatch.class, StringMatch.getType());
79 modelElementClasses.put(Tca.class, Tca.getType());
84 * Retain data required to parse the ModelElement objects. (Rather than
85 * parse them all - parse them on demand if requested.)
91 * @param modelBpmnPropText
92 * @param modelPropText
93 * @throws JsonProcessingException
96 public ModelProperties(String modelName, String controlName, String actionCd, boolean isTest, String modelBpmnPropText,
97 String modelPropText) throws IOException {
98 this.modelName = modelName;
99 this.controlName = controlName;
100 this.actionCd = actionCd;
101 this.isTest = isTest;
102 modelBpmn = ModelBpmn.create(modelBpmnPropText);
103 modelJson = new ObjectMapper().readTree(modelPropText);
105 instantiateMissingModelElements();
109 * This method is meant to ensure that one ModelElement instance exists for
110 * each ModelElement class.
112 * As new ModelElement classes could have been registered after
113 * instantiation of this ModelProperties, we need to build the missing
114 * ModelElement instances.
116 private final void instantiateMissingModelElements() {
117 if (modelElementClasses.size() != modelElements.size()) {
118 Set<String> missingTypes = new HashSet<>(modelElementClasses.values());
119 missingTypes.removeAll(modelElements.keySet());
120 // Parse the list of base Model Elements and build up the
122 modelElementClasses.entrySet().stream().parallel()
123 .filter(entry -> (ModelElement.class.isAssignableFrom(entry.getKey())
124 && missingTypes.contains(entry.getValue())))
127 modelElements.put(entry.getValue(),
129 .getConstructor(ModelProperties.class, ModelBpmn.class, JsonNode.class)
130 .newInstance(this, modelBpmn, modelJson)));
131 } catch (InstantiationException | NoSuchMethodException | IllegalAccessException
132 | InvocationTargetException e) {
133 logger.warn("Unable to instantiate a ModelElement, exception follows: " + e);
140 * Get the VF for a model. If return null if there is no VF.
145 public static String getVf(CldsModel model) {
146 List<String> vfs = null;
148 ObjectMapper mapper = new ObjectMapper();
149 JsonNode modelJson = mapper.readTree(model.getPropText());
150 Global global = new Global(modelJson);
151 vfs = global.getResourceVf();
152 } catch (IOException e) {
156 if (vfs != null && !vfs.isEmpty()) {
163 * Create ModelProperties for Camunda Delegate.
167 * @throws JsonProcessingException
168 * @throws IOException
170 public static ModelProperties create(DelegateExecution execution) throws IOException {
171 // String modelProp = (String) execution.getVariable("modelProp");
172 String modelProp = new String((byte[]) execution.getVariable("modelProp"));
173 String modelBpmnProp = (String) execution.getVariable("modelBpmnProp");
174 String modelName = (String) execution.getVariable("modelName");
175 String controlName = (String) execution.getVariable("controlName");
176 String actionCd = (String) execution.getVariable("actionCd");
177 boolean isTest = (boolean)execution.getVariable("isTest");
179 return new ModelProperties(modelName, controlName, actionCd, isTest, modelBpmnProp, modelProp);
183 * return appropriate model element given the type
188 public ModelElement getModelElementByType(String type) {
189 ModelElement modelElement = modelElements.get(type);
190 if (modelElement == null) {
191 throw new IllegalArgumentException("Invalid or not found ModelElement type: " + type);
197 * @return the modelName
199 public String getModelName() {
204 * @return the controlName
206 public String getControlName() {
211 * @return the controlNameAndPolicyUniqueId
213 public String getControlNameAndPolicyUniqueId() {
214 return controlName + "_" + policyUniqueId;
218 * @return the currentPolicyName
220 private String getCurrentPolicyName() {
221 return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
225 * @return the currentPolicyScopeAndPolicyName
227 public String getCurrentPolicyScopeAndPolicyName() {
228 return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName());
232 * @return the policyScopeAndNameWithUniqueId
234 public String getPolicyScopeAndNameWithUniqueId() {
235 return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName() + "_" + policyUniqueId);
239 * @return the currentPolicyScopeAndFullPolicyName
241 public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
242 return normalizePolicyScopeName(modelName + "." + policyNamePrefix + getCurrentPolicyName());
246 * @return the currentPolicyScopeAndFullPolicyNameWithVersion
248 public String getCurrentPolicyScopeAndFullPolicyNameWithVersion(String policyNamePrefix, int version) {
249 return normalizePolicyScopeName(
250 modelName + "." + policyNamePrefix + getCurrentPolicyName() + "." + version + ".xml");
254 * Replace all '-' with '_' within policy scope and name.
259 private String normalizePolicyScopeName(String inName) {
260 return inName.replaceAll("-", "_");
264 * @return the currentModelElementId
266 public String getCurrentModelElementId() {
267 return currentModelElementId;
271 * When generating a policy request for a model element, must set the id of
272 * that model element using this method. Used to generate the policy name.
274 * @param currentModelElementId
275 * the currentModelElementId to set
277 public void setCurrentModelElementId(String currentModelElementId) {
278 this.currentModelElementId = currentModelElementId;
282 * @return the policyUniqueId
284 public String getPolicyUniqueId() {
285 return policyUniqueId;
289 * When generating a policy request for a model element, must set the unique
290 * id of that policy using this method. Used to generate the policy name.
292 * @param policyUniqueId
293 * the policyUniqueId to set
295 public void setPolicyUniqueId(String policyUniqueId) {
296 this.policyUniqueId = policyUniqueId;
300 * @return the actionCd
302 public String getActionCd() {
309 public boolean isTest() {
314 * @return the isCreateRequest
316 public boolean isCreateRequest() {
318 case CldsEvent.ACTION_SUBMIT:
319 case CldsEvent.ACTION_RESTART:
325 public boolean isStopRequest() {
327 case CldsEvent.ACTION_STOP:
336 public Global getGlobal() {
337 if (global == null) {
338 global = new Global(modelJson);
343 public static final synchronized void registerModelElement(Class<? extends ModelElement> modelElementClass,
345 if (!modelElementClasses.containsKey(modelElementClass.getClass())) {
346 modelElementClasses.put(modelElementClass, type);
350 public <T extends ModelElement> T getType(Class<T> clazz) {
351 instantiateMissingModelElements();
352 String type = modelElementClasses.get(clazz);
353 return (type != null ? (T) modelElements.get(type) : null);
359 public Tca getTca() {
361 tca = new Tca(this, modelBpmn, modelJson);