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.CldsEvent;
43 import org.onap.clamp.clds.model.CldsModel;
44 import org.onap.clamp.clds.service.CldsService;
45 import org.onap.clamp.clds.util.JacksonUtils;
48 * Parse model properties.
50 public class ModelProperties {
52 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsService.class);
53 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
54 private ModelBpmn modelBpmn;
55 private JsonNode modelJson;
56 private final String modelName;
57 private final String controlName;
58 private final String actionCd;
59 // Flag indicate whether it is triggered by Validation Test button from UI
60 private final boolean testOnly;
61 private Global global;
62 private final Map<String, AbstractModelElement> modelElements = new ConcurrentHashMap<>();
63 private String currentModelElementId;
64 private String policyUniqueId;
65 private String guardUniqueId;
66 public static final String POLICY_GUARD_SUFFIX = "_Guard_";
67 private static final Object lock = new Object();
68 private static Map<Class<? extends AbstractModelElement>, String> modelElementClasses = new ConcurrentHashMap<>();
71 modelElementClasses.put(Policy.class, Policy.getType());
72 modelElementClasses.put(Tca.class, Tca.getType());
73 modelElementClasses.put(Holmes.class, Holmes.getType());
78 * Retain data required to parse the ModelElement objects. (Rather than
79 * parse them all - parse them on demand if requested.)
82 * The model name coming form the UI
84 * The closed loop name coming from the UI
86 * Type of operation PUT,UPDATE,DELETE
88 * The test flag coming from the UI (for validation only, no
89 * query are physically executed)
90 * @param modelBpmnText
91 * The BPMN flow in JSON from the UI
92 * @param modelPropText
93 * The BPMN parameters for all boxes defined in modelBpmnTest
95 public ModelProperties(String modelName, String controlName, String actionCd, boolean isATest, String modelBpmnText,
96 String modelPropText) {
98 this.modelName = modelName;
99 this.controlName = controlName;
100 this.actionCd = actionCd;
101 this.testOnly = isATest;
102 modelBpmn = ModelBpmn.create(modelBpmnText);
103 modelJson = JacksonUtils.getObjectMapperInstance().readTree(modelPropText);
104 instantiateMissingModelElements();
105 } catch (IOException e) {
106 throw new ModelBpmnException("Exception occurred when trying to decode the BPMN Properties JSON", e);
111 * This method is meant to ensure that one ModelElement instance exists for
112 * each ModelElement class. As new ModelElement classes could have been
113 * registered after instantiation of this ModelProperties, we need to build
114 * the missing 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 -> (AbstractModelElement.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 JsonNode modelJson = JacksonUtils.getObjectMapperInstance().readTree(model.getPropText());
149 Global global = new Global(modelJson);
150 vfs = global.getResourceVf();
151 } catch (IOException e) {
152 logger.warn("no VF found", e);
155 if (vfs != null && !vfs.isEmpty()) {
162 * Create ModelProperties extracted from a CamelExchange.
164 * @param camelExchange
165 * The camel Exchange object that contains all info provided to
167 * @return A model Properties created from the parameters found in
168 * camelExchange object
170 public static ModelProperties create(Exchange camelExchange) {
171 String modelProp = (String) camelExchange.getProperty("modelProp");
172 String modelBpmnProp = (String) camelExchange.getProperty("modelBpmnProp");
173 String modelName = (String) camelExchange.getProperty("modelName");
174 String controlName = (String) camelExchange.getProperty("controlName");
175 String actionCd = (String) camelExchange.getProperty("actionCd");
176 boolean isTest = (boolean) camelExchange.getProperty("isTest");
177 return new ModelProperties(modelName, controlName, actionCd, isTest, modelBpmnProp, modelProp);
181 * return appropriate model element given the type
186 public AbstractModelElement getModelElementByType(String type) {
187 AbstractModelElement modelElement = modelElements.get(type);
188 if (modelElement == null) {
189 throw new IllegalArgumentException("Invalid or not found ModelElement type: " + type);
195 * @return the modelName
197 public String getModelName() {
202 * @return the controlName
204 public String getControlName() {
209 * @return the controlNameAndPolicyUniqueId
211 public String getControlNameAndPolicyUniqueId() {
212 return controlName + "_" + policyUniqueId;
216 * @return the currentPolicyName
218 private String getCurrentPolicyName() {
219 return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
222 private String createScopeSeparator(String policyScope) {
223 return policyScope.contains(".") ? "" : ".";
227 * @return the currentPolicyScopeAndPolicyName
229 public String getCurrentPolicyScopeAndPolicyName() {
230 return normalizePolicyScopeName(modelName + createScopeSeparator(modelName) + getCurrentPolicyName());
234 * @return The policyName that wil be used in input parameters of DCAE
237 public String getPolicyNameForDcaeDeploy(ClampProperties refProp) {
238 return normalizePolicyScopeName(modelName + createScopeSeparator(modelName)
239 + refProp.getStringValue(PolicyClient.POLICY_MS_NAME_PREFIX_PROPERTY_NAME) + getCurrentPolicyName());
243 * @return the policyScopeAndNameWithUniqueId
245 public String getPolicyScopeAndNameWithUniqueId() {
246 return normalizePolicyScopeName(
247 modelName + createScopeSeparator(modelName) + getCurrentPolicyName() + "_" + policyUniqueId);
251 * @return the policyScopeAndNameWithUniqueId
253 public String getPolicyScopeAndNameWithUniqueGuardId() {
254 return normalizePolicyScopeName(
255 modelName + createScopeSeparator(modelName) + getCurrentPolicyName() + "_" + policyUniqueId+POLICY_GUARD_SUFFIX+guardUniqueId);
259 * @return the currentPolicyScopeAndFullPolicyName
261 public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
262 return normalizePolicyScopeName(
263 modelName + createScopeSeparator(modelName) + policyNamePrefix + getCurrentPolicyName());
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
285 * that 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
311 * id 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() {
335 * @return the isCreateRequest
337 public boolean isCreateRequest() {
339 case CldsEvent.ACTION_SUBMIT:
340 case CldsEvent.ACTION_RESTART:
346 public boolean isStopRequest() {
348 case CldsEvent.ACTION_STOP:
357 public Global getGlobal() {
358 if (global == null) {
359 global = new Global(modelJson);
364 public static final synchronized void registerModelElement(Class<? extends AbstractModelElement> modelElementClass,
366 if (!modelElementClasses.containsKey(modelElementClass.getClass())) {
367 modelElementClasses.put(modelElementClass, type);
371 public <T extends AbstractModelElement> T getType(Class<T> clazz) {
372 instantiateMissingModelElements();
373 String type = modelElementClasses.get(clazz);
374 return (type != null ? (T) modelElements.get(type) : null);