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(Collector.class, Collector.getType());
76 modelElementClasses.put(Policy.class, Policy.getType());
77 modelElementClasses.put(StringMatch.class, StringMatch.getType());
78 modelElementClasses.put(Tca.class, Tca.getType());
83 * Retain data required to parse the ModelElement objects. (Rather than
84 * parse them all - parse them on demand if requested.)
87 * The model name coming form the UI
89 * The closed loop name coming from the UI
91 * Type of operation PUT,UPDATE,DELETE
93 * The test flag coming from the UI (for validation only, no
94 * query are physically executed)
95 * @param modelBpmnText
96 * The BPMN flow in JSON from the UI
97 * @param modelPropText
98 * The BPMN parameters for all boxes defined in modelBpmnTest
100 * In case there is an issue with the JSON decoding
102 public ModelProperties(String modelName, String controlName, String actionCd, boolean isTest, String modelBpmnText,
103 String modelPropText) throws IOException {
104 this.modelName = modelName;
105 this.controlName = controlName;
106 this.actionCd = actionCd;
107 this.isTest = isTest;
108 modelBpmn = ModelBpmn.create(modelBpmnText);
109 modelJson = new ObjectMapper().readTree(modelPropText);
111 instantiateMissingModelElements();
115 * This method is meant to ensure that one ModelElement instance exists for
116 * each ModelElement class.
118 * As new ModelElement classes could have been registered after
119 * instantiation of this ModelProperties, we need to build the missing
120 * ModelElement instances.
122 private final void instantiateMissingModelElements() {
123 if (modelElementClasses.size() != modelElements.size()) {
124 Set<String> missingTypes = new HashSet<>(modelElementClasses.values());
125 missingTypes.removeAll(modelElements.keySet());
126 // Parse the list of base Model Elements and build up the
128 modelElementClasses.entrySet().stream().parallel()
129 .filter(entry -> (AbstractModelElement.class.isAssignableFrom(entry.getKey())
130 && missingTypes.contains(entry.getValue())))
133 modelElements.put(entry.getValue(),
135 .getConstructor(ModelProperties.class, ModelBpmn.class, JsonNode.class)
136 .newInstance(this, modelBpmn, modelJson)));
137 } catch (InstantiationException | NoSuchMethodException | IllegalAccessException
138 | InvocationTargetException e) {
139 logger.warn("Unable to instantiate a ModelElement, exception follows: " + e);
146 * Get the VF for a model. If return null if there is no VF.
151 public static String getVf(CldsModel model) {
152 List<String> vfs = null;
154 ObjectMapper mapper = new ObjectMapper();
155 JsonNode modelJson = mapper.readTree(model.getPropText());
156 Global global = new Global(modelJson);
157 vfs = global.getResourceVf();
158 } catch (IOException e) {
159 logger.warn("no VF found", e);
162 if (vfs != null && !vfs.isEmpty()) {
169 * Create ModelProperties for Camunda Delegate.
173 * @throws JsonProcessingException
174 * @throws IOException
176 public static ModelProperties create(DelegateExecution execution) throws IOException {
177 String modelProp = new String((byte[]) execution.getVariable("modelProp"));
178 String modelBpmnProp = (String) execution.getVariable("modelBpmnProp");
179 String modelName = (String) execution.getVariable("modelName");
180 String controlName = (String) execution.getVariable("controlName");
181 String actionCd = (String) execution.getVariable("actionCd");
182 boolean isTest = (boolean) execution.getVariable("isTest");
184 return new ModelProperties(modelName, controlName, actionCd, isTest, modelBpmnProp, modelProp);
188 * return appropriate model element given the type
193 public AbstractModelElement getModelElementByType(String type) {
194 AbstractModelElement modelElement = modelElements.get(type);
195 if (modelElement == null) {
196 throw new IllegalArgumentException("Invalid or not found ModelElement type: " + type);
202 * @return the modelName
204 public String getModelName() {
209 * @return the controlName
211 public String getControlName() {
216 * @return the controlNameAndPolicyUniqueId
218 public String getControlNameAndPolicyUniqueId() {
219 return controlName + "_" + policyUniqueId;
223 * @return the currentPolicyName
225 private String getCurrentPolicyName() {
226 return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
230 * @return the currentPolicyScopeAndPolicyName
232 public String getCurrentPolicyScopeAndPolicyName() {
233 return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName());
237 * @return the policyScopeAndNameWithUniqueId
239 public String getPolicyScopeAndNameWithUniqueId() {
240 return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName() + "_" + policyUniqueId);
244 * @return the currentPolicyScopeAndFullPolicyName
246 public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
247 return normalizePolicyScopeName(modelName + "." + policyNamePrefix + getCurrentPolicyName());
251 * @return the currentPolicyScopeAndFullPolicyNameWithVersion
253 public String getCurrentPolicyScopeAndFullPolicyNameWithVersion(String policyNamePrefix, int version) {
254 return normalizePolicyScopeName(
255 modelName + "." + policyNamePrefix + getCurrentPolicyName() + "." + version + ".xml");
259 * Replace all '-' with '_' within policy scope and name.
264 private String normalizePolicyScopeName(String inName) {
265 return inName.replaceAll("-", "_");
269 * @return the currentModelElementId
271 public String getCurrentModelElementId() {
272 return currentModelElementId;
276 * When generating a policy request for a model element, must set the id of
277 * that model element using this method. Used to generate the policy name.
279 * @param currentModelElementId
280 * the currentModelElementId to set
282 public void setCurrentModelElementId(String currentModelElementId) {
283 this.currentModelElementId = currentModelElementId;
287 * @return the policyUniqueId
289 public String getPolicyUniqueId() {
290 return policyUniqueId;
294 * When generating a policy request for a model element, must set the unique
295 * id of that policy using this method. Used to generate the policy name.
297 * @param policyUniqueId
298 * the policyUniqueId to set
300 public void setPolicyUniqueId(String policyUniqueId) {
301 this.policyUniqueId = policyUniqueId;
305 * @return the actionCd
307 public String getActionCd() {
314 public boolean isTest() {
319 * @return the isCreateRequest
321 public boolean isCreateRequest() {
323 case CldsEvent.ACTION_SUBMIT:
324 case CldsEvent.ACTION_RESTART:
330 public boolean isStopRequest() {
332 case CldsEvent.ACTION_STOP:
341 public Global getGlobal() {
342 if (global == null) {
343 global = new Global(modelJson);
348 public static final synchronized void registerModelElement(Class<? extends AbstractModelElement> modelElementClass,
350 if (!modelElementClasses.containsKey(modelElementClass.getClass())) {
351 modelElementClasses.put(modelElementClass, type);
355 public <T extends AbstractModelElement> T getType(Class<T> clazz) {
356 instantiateMissingModelElements();
357 String type = modelElementClasses.get(clazz);
358 return (type != null ? (T) modelElements.get(type) : null);