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 com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.ObjectMapper;
32 import java.io.IOException;
33 import java.lang.reflect.InvocationTargetException;
34 import java.util.HashSet;
35 import java.util.List;
38 import java.util.concurrent.ConcurrentHashMap;
40 import org.camunda.bpm.engine.delegate.DelegateExecution;
41 import org.onap.clamp.clds.model.CldsEvent;
42 import org.onap.clamp.clds.model.CldsModel;
43 import org.onap.clamp.clds.service.CldsService;
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;
65 private final Map<String, AbstractModelElement> modelElements = new ConcurrentHashMap<>();
67 private String currentModelElementId;
68 private String policyUniqueId;
70 private static final Object lock = new Object();
71 private static Map<Class<? extends AbstractModelElement>, String> modelElementClasses = new ConcurrentHashMap<>();
75 modelElementClasses.put(Policy.class, Policy.getType());
76 modelElementClasses.put(Tca.class, Tca.getType());
77 modelElementClasses.put(Holmes.class, Holmes.getType());
82 * Retain data required to parse the ModelElement objects. (Rather than
83 * parse them all - parse them on demand if requested.)
86 * The model name coming form the UI
88 * The closed loop name coming from the UI
90 * Type of operation PUT,UPDATE,DELETE
92 * The test flag coming from the UI (for validation only, no
93 * query are physically executed)
94 * @param modelBpmnText
95 * The BPMN flow in JSON from the UI
96 * @param modelPropText
97 * The BPMN parameters for all boxes defined in modelBpmnTest
99 * In case there is an issue with the JSON decoding
101 public ModelProperties(String modelName, String controlName, String actionCd, boolean isTest, String modelBpmnText,
102 String modelPropText) throws IOException {
103 this.modelName = modelName;
104 this.controlName = controlName;
105 this.actionCd = actionCd;
106 this.isTest = isTest;
107 modelBpmn = ModelBpmn.create(modelBpmnText);
108 modelJson = new ObjectMapper().readTree(modelPropText);
110 instantiateMissingModelElements();
114 * This method is meant to ensure that one ModelElement instance exists for
115 * each ModelElement class.
117 * As new ModelElement classes could have been registered after
118 * instantiation of this ModelProperties, we need to build the missing
119 * ModelElement instances.
121 private final void instantiateMissingModelElements() {
122 if (modelElementClasses.size() != modelElements.size()) {
123 Set<String> missingTypes = new HashSet<>(modelElementClasses.values());
124 missingTypes.removeAll(modelElements.keySet());
125 // Parse the list of base Model Elements and build up the
127 modelElementClasses.entrySet().stream().parallel()
128 .filter(entry -> (AbstractModelElement.class.isAssignableFrom(entry.getKey())
129 && missingTypes.contains(entry.getValue())))
132 modelElements.put(entry.getValue(),
134 .getConstructor(ModelProperties.class, ModelBpmn.class, JsonNode.class)
135 .newInstance(this, modelBpmn, modelJson)));
136 } catch (InstantiationException | NoSuchMethodException | IllegalAccessException
137 | InvocationTargetException e) {
138 logger.warn("Unable to instantiate a ModelElement, exception follows: ", e);
145 * Get the VF for a model. If return null if there is no VF.
150 public static String getVf(CldsModel model) {
151 List<String> vfs = null;
153 ObjectMapper mapper = new ObjectMapper();
154 JsonNode modelJson = mapper.readTree(model.getPropText());
155 Global global = new Global(modelJson);
156 vfs = global.getResourceVf();
157 } catch (IOException e) {
158 logger.warn("no VF found", e);
161 if (vfs != null && !vfs.isEmpty()) {
168 * Create ModelProperties for Camunda Delegate.
172 * @throws JsonProcessingException
173 * @throws IOException
175 public static ModelProperties create(DelegateExecution execution) throws IOException {
176 String modelProp = new String((byte[]) execution.getVariable("modelProp"));
177 String modelBpmnProp = (String) execution.getVariable("modelBpmnProp");
178 String modelName = (String) execution.getVariable("modelName");
179 String controlName = (String) execution.getVariable("controlName");
180 String actionCd = (String) execution.getVariable("actionCd");
181 boolean isTest = (boolean) execution.getVariable("isTest");
183 return new ModelProperties(modelName, controlName, actionCd, isTest, modelBpmnProp, modelProp);
187 * return appropriate model element given the type
192 public AbstractModelElement getModelElementByType(String type) {
193 AbstractModelElement modelElement = modelElements.get(type);
194 if (modelElement == null) {
195 throw new IllegalArgumentException("Invalid or not found ModelElement type: " + type);
201 * @return the modelName
203 public String getModelName() {
208 * @return the controlName
210 public String getControlName() {
215 * @return the controlNameAndPolicyUniqueId
217 public String getControlNameAndPolicyUniqueId() {
218 return controlName + "_" + policyUniqueId;
222 * @return the currentPolicyName
224 private String getCurrentPolicyName() {
225 return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
229 * @return the currentPolicyScopeAndPolicyName
231 public String getCurrentPolicyScopeAndPolicyName() {
232 return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName());
236 * @return the policyScopeAndNameWithUniqueId
238 public String getPolicyScopeAndNameWithUniqueId() {
239 return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName() + "_" + policyUniqueId);
243 * @return the currentPolicyScopeAndFullPolicyName
245 public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
246 return normalizePolicyScopeName(modelName + "." + policyNamePrefix + getCurrentPolicyName());
250 * @return the currentPolicyScopeAndFullPolicyNameWithVersion
252 public String getCurrentPolicyScopeAndFullPolicyNameWithVersion(String policyNamePrefix, int version) {
253 return normalizePolicyScopeName(
254 modelName + "." + policyNamePrefix + getCurrentPolicyName() + "." + version + ".xml");
258 * Replace all '-' with '_' within policy scope and name.
263 private String normalizePolicyScopeName(String inName) {
264 return inName.replaceAll("-", "_");
268 * @return the currentModelElementId
270 public String getCurrentModelElementId() {
271 return currentModelElementId;
275 * When generating a policy request for a model element, must set the id of
276 * that model element using this method. Used to generate the policy name.
278 * @param currentModelElementId
279 * the currentModelElementId to set
281 public void setCurrentModelElementId(String currentModelElementId) {
282 this.currentModelElementId = currentModelElementId;
286 * @return the policyUniqueId
288 public String getPolicyUniqueId() {
289 return policyUniqueId;
293 * When generating a policy request for a model element, must set the unique
294 * id of that policy using this method. Used to generate the policy name.
296 * @param policyUniqueId
297 * the policyUniqueId to set
299 public void setPolicyUniqueId(String policyUniqueId) {
300 this.policyUniqueId = policyUniqueId;
304 * @return the actionCd
306 public String getActionCd() {
313 public boolean isTest() {
318 * @return the isCreateRequest
320 public boolean isCreateRequest() {
322 case CldsEvent.ACTION_SUBMIT:
323 case CldsEvent.ACTION_RESTART:
329 public boolean isStopRequest() {
331 case CldsEvent.ACTION_STOP:
340 public Global getGlobal() {
341 if (global == null) {
342 global = new Global(modelJson);
347 public static final synchronized void registerModelElement(Class<? extends AbstractModelElement> modelElementClass,
349 if (!modelElementClasses.containsKey(modelElementClass.getClass())) {
350 modelElementClasses.put(modelElementClass, type);
354 public <T extends AbstractModelElement> T getType(Class<T> clazz) {
355 instantiateMissingModelElements();
356 String type = modelElementClasses.get(clazz);
357 return (type != null ? (T) modelElements.get(type) : null);