2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ===================================================================
24 package org.onap.clamp.clds.model.properties;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
30 import java.io.IOException;
31 import java.lang.reflect.InvocationTargetException;
32 import java.util.HashSet;
33 import java.util.List;
36 import java.util.concurrent.ConcurrentHashMap;
38 import org.apache.camel.Exchange;
39 import org.onap.clamp.clds.client.req.policy.PolicyClient;
40 import org.onap.clamp.clds.config.ClampProperties;
41 import org.onap.clamp.clds.exception.ModelBpmnException;
42 import org.onap.clamp.clds.model.CldsModel;
43 import org.onap.clamp.clds.service.CldsService;
44 import org.onap.clamp.clds.util.JacksonUtils;
47 * Parse model properties.
49 public class ModelProperties {
51 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsService.class);
52 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
53 private ModelBpmn modelBpmn;
54 private JsonNode modelJson;
55 private final String modelName;
56 private final String controlName;
57 private final String actionCd;
58 // Flag indicate whether it is triggered by Validation Test button from UI
59 private final boolean testOnly;
60 private Global global;
61 private final Map<String, AbstractModelElement> modelElements = new ConcurrentHashMap<>();
62 private String currentModelElementId;
63 private String policyUniqueId;
64 private String guardUniqueId;
65 public static final String POLICY_GUARD_SUFFIX = "_Guard_";
66 private static final Object lock = new Object();
67 private static Map<Class<? extends AbstractModelElement>, String> modelElementClasses = new ConcurrentHashMap<>();
70 modelElementClasses.put(Policy.class, Policy.getType());
71 modelElementClasses.put(Tca.class, Tca.getType());
72 modelElementClasses.put(Holmes.class, Holmes.getType());
77 * Retain data required to parse the ModelElement objects. (Rather than parse
78 * them all - parse them on demand if requested.)
81 * The model name coming form the UI
83 * The closed loop name coming from the UI
85 * Type of operation PUT,UPDATE,DELETE
87 * The test flag coming from the UI (for validation only, no query are
88 * physically executed)
89 * @param modelBpmnText
90 * The BPMN flow in JSON from the UI
91 * @param modelPropText
92 * The BPMN parameters for all boxes defined in modelBpmnTest
94 public ModelProperties(String modelName, String controlName, String actionCd, boolean isATest, String modelBpmnText,
95 String modelPropText) {
97 this.modelName = modelName;
98 this.controlName = controlName;
99 this.actionCd = actionCd;
100 this.testOnly = isATest;
101 modelBpmn = ModelBpmn.create(modelBpmnText);
102 modelJson = JacksonUtils.getObjectMapperInstance().readTree(modelPropText);
103 instantiateMissingModelElements();
104 } catch (IOException e) {
105 throw new ModelBpmnException("Exception occurred when trying to decode the BPMN Properties JSON", e);
110 * This method is meant to ensure that one ModelElement instance exists for each
111 * ModelElement class. As new ModelElement classes could have been registered
112 * after instantiation of this ModelProperties, we need to build the missing
113 * ModelElement instances.
115 private final void instantiateMissingModelElements() {
116 if (modelElementClasses.size() != modelElements.size()) {
117 Set<String> missingTypes = new HashSet<>(modelElementClasses.values());
118 missingTypes.removeAll(modelElements.keySet());
119 // Parse the list of base Model Elements and build up the
121 modelElementClasses.entrySet().stream().parallel()
122 .filter(entry -> (AbstractModelElement.class.isAssignableFrom(entry.getKey())
123 && missingTypes.contains(entry.getValue())))
126 modelElements.put(entry.getValue(),
127 (entry.getKey().getConstructor(ModelProperties.class, ModelBpmn.class, JsonNode.class)
128 .newInstance(this, modelBpmn, modelJson)));
129 } catch (InstantiationException | NoSuchMethodException | IllegalAccessException
130 | InvocationTargetException e) {
131 logger.warn("Unable to instantiate a ModelElement, exception follows: ", e);
138 * Get the VF for a model. If return null if there is no VF.
143 public static String getVf(CldsModel model) {
144 List<String> vfs = null;
146 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(model.getPropText());
147 Global global = new Global(modelJson);
148 vfs = global.getResourceVf();
149 } catch (IOException e) {
150 logger.warn("no VF found", e);
153 if (vfs != null && !vfs.isEmpty()) {
160 * Create ModelProperties extracted from a CamelExchange.
162 * @param camelExchange
163 * The camel Exchange object that contains all info provided to the flow
164 * @return A model Properties created from the parameters found in camelExchange
167 public static ModelProperties create(Exchange camelExchange) {
168 String modelProp = (String) camelExchange.getProperty("modelProp");
169 String modelBpmnProp = (String) camelExchange.getProperty("modelBpmnProp");
170 String modelName = (String) camelExchange.getProperty("modelName");
171 String controlName = (String) camelExchange.getProperty("controlName");
172 String actionCd = (String) camelExchange.getProperty("actionCd");
173 boolean isTest = (boolean) camelExchange.getProperty("isTest");
174 return new ModelProperties(modelName, controlName, actionCd, isTest, modelBpmnProp, modelProp);
178 * @return the modelName
180 public String getModelName() {
185 * @return the controlName
187 public String getControlName() {
192 * @return the controlNameAndPolicyUniqueId
194 public String getControlNameAndPolicyUniqueId() {
195 return controlName + "_" + policyUniqueId;
199 * @return the currentPolicyName
201 private String getCurrentPolicyName() {
202 return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
205 private String createScopeSeparator(String policyScope) {
206 return policyScope.contains(".") ? "" : ".";
210 * @return the currentPolicyScopeAndPolicyName
212 public String getCurrentPolicyScopeAndPolicyName() {
213 return normalizePolicyScopeName(modelName + createScopeSeparator(modelName) + getCurrentPolicyName());
217 * @return The policyName that wil be used in input parameters of DCAE deploy
219 public String getPolicyNameForDcaeDeploy(ClampProperties refProp) {
220 return normalizePolicyScopeName(modelName + createScopeSeparator(modelName)
221 + refProp.getStringValue(PolicyClient.POLICY_MS_NAME_PREFIX_PROPERTY_NAME) + getCurrentPolicyName());
225 * @return the policyScopeAndNameWithUniqueId
227 public String getPolicyScopeAndNameWithUniqueId() {
228 return normalizePolicyScopeName(
229 modelName + createScopeSeparator(modelName) + getCurrentPolicyName() + "_" + policyUniqueId);
233 * @return the policyScopeAndNameWithUniqueId
235 public String getPolicyScopeAndNameWithUniqueGuardId() {
236 return normalizePolicyScopeName(modelName + createScopeSeparator(modelName) + getCurrentPolicyName() + "_"
237 + policyUniqueId + POLICY_GUARD_SUFFIX + guardUniqueId);
241 * @return the currentPolicyScopeAndFullPolicyName
243 public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
244 return normalizePolicyScopeName(
245 modelName + createScopeSeparator(modelName) + policyNamePrefix + getCurrentPolicyName());
249 * @return the PolicyNameWithScopeContext
251 public String getPolicyNameWithScopeContext(String policyScope, String policyType, String vnfScope, String context,
252 String userDefinedName) {
253 return normalizePolicyScopeName(policyScope + createScopeSeparator(policyScope) + policyType + "_" + vnfScope
254 + "_" + context + "_" + modelName + "_" + userDefinedName);
258 * @return the PolicyNameWithPrefixScopeContext
260 public String getPolicyNameWithPrefixScopeContext(String policyScope, String policyType, String vnfScope,
261 String context, String userDefinedName, String policyPrefix) {
262 return normalizePolicyScopeName(policyScope + createScopeSeparator(policyScope) + policyPrefix + policyType
263 + "_" + vnfScope + "_" + context + "_" + modelName + "_" + userDefinedName);
267 * Replace all '-' with '_' within policy scope and name.
272 private String normalizePolicyScopeName(String inName) {
273 return inName.replaceAll("-", "_");
277 * @return the currentModelElementId
279 public String getCurrentModelElementId() {
280 return currentModelElementId;
284 * When generating a policy request for a model element, must set the id of that
285 * model element using this method. Used to generate the policy name.
287 * @param currentModelElementId
288 * the currentModelElementId to set
290 public void setCurrentModelElementId(String currentModelElementId) {
291 this.currentModelElementId = currentModelElementId;
295 * @return the policyUniqueId
297 public String getPolicyUniqueId() {
298 return policyUniqueId;
301 public String getGuardUniqueId() {
302 return guardUniqueId;
305 public void setGuardUniqueId(String guardUniqueId) {
306 this.guardUniqueId = guardUniqueId;
310 * When generating a policy request for a model element, must set the unique id
311 * of that policy using this method. Used to generate the policy name.
313 * @param policyUniqueId
314 * the policyUniqueId to set
316 public void setPolicyUniqueId(String policyUniqueId) {
317 this.policyUniqueId = policyUniqueId;
321 * @return the actionCd
323 public String getActionCd() {
328 * @return the testOnly
330 public boolean isTestOnly() {
337 public Global getGlobal() {
338 if (global == null) {
339 global = new Global(modelJson);
344 public static final synchronized void registerModelElement(Class<? extends AbstractModelElement> modelElementClass,
346 if (!modelElementClasses.containsKey(modelElementClass.getClass())) {
347 modelElementClasses.put(modelElementClass, type);
351 public <T extends AbstractModelElement> T getType(Class<T> clazz) {
352 instantiateMissingModelElements();
353 String type = modelElementClasses.get(clazz);
354 return (type != null ? (T) modelElements.get(type) : null);