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 static final Object lock = new Object();
66 private static Map<Class<? extends AbstractModelElement>, String> modelElementClasses = new ConcurrentHashMap<>();
69 modelElementClasses.put(Policy.class, Policy.getType());
70 modelElementClasses.put(Tca.class, Tca.getType());
71 modelElementClasses.put(Holmes.class, Holmes.getType());
76 * Retain data required to parse the ModelElement objects. (Rather than
77 * parse them all - parse them on demand if requested.)
80 * The model name coming form the UI
82 * The closed loop name coming from the UI
84 * Type of operation PUT,UPDATE,DELETE
86 * The test flag coming from the UI (for validation only, no
87 * query are physically executed)
88 * @param modelBpmnText
89 * The BPMN flow in JSON from the UI
90 * @param modelPropText
91 * The BPMN parameters for all boxes defined in modelBpmnTest
93 public ModelProperties(String modelName, String controlName, String actionCd, boolean isATest, String modelBpmnText,
94 String modelPropText) {
96 this.modelName = modelName;
97 this.controlName = controlName;
98 this.actionCd = actionCd;
99 this.testOnly = isATest;
100 modelBpmn = ModelBpmn.create(modelBpmnText);
101 modelJson = JacksonUtils.getObjectMapperInstance().readTree(modelPropText);
102 instantiateMissingModelElements();
103 } catch (IOException e) {
104 throw new ModelBpmnException("Exception occurred when trying to decode the BPMN Properties JSON", e);
109 * This method is meant to ensure that one ModelElement instance exists for
110 * each ModelElement class. As new ModelElement classes could have been
111 * registered after instantiation of this ModelProperties, we need to build
112 * the missing ModelElement instances.
114 private final void instantiateMissingModelElements() {
115 if (modelElementClasses.size() != modelElements.size()) {
116 Set<String> missingTypes = new HashSet<>(modelElementClasses.values());
117 missingTypes.removeAll(modelElements.keySet());
118 // Parse the list of base Model Elements and build up the
120 modelElementClasses.entrySet().stream().parallel()
121 .filter(entry -> (AbstractModelElement.class.isAssignableFrom(entry.getKey())
122 && missingTypes.contains(entry.getValue())))
125 modelElements.put(entry.getValue(),
127 .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
165 * @return A model Properties created from the parameters found in
166 * camelExchange object
168 public static ModelProperties create(Exchange camelExchange) {
169 String modelProp = (String) camelExchange.getProperty("modelProp");
170 String modelBpmnProp = (String) camelExchange.getProperty("modelBpmnProp");
171 String modelName = (String) camelExchange.getProperty("modelName");
172 String controlName = (String) camelExchange.getProperty("controlName");
173 String actionCd = (String) camelExchange.getProperty("actionCd");
174 boolean isTest = (boolean) camelExchange.getProperty("isTest");
175 return new ModelProperties(modelName, controlName, actionCd, isTest, modelBpmnProp, modelProp);
179 * return appropriate model element given the type
184 public AbstractModelElement getModelElementByType(String type) {
185 AbstractModelElement modelElement = modelElements.get(type);
186 if (modelElement == null) {
187 throw new IllegalArgumentException("Invalid or not found ModelElement type: " + type);
193 * @return the modelName
195 public String getModelName() {
200 * @return the controlName
202 public String getControlName() {
207 * @return the controlNameAndPolicyUniqueId
209 public String getControlNameAndPolicyUniqueId() {
210 return controlName + "_" + policyUniqueId;
214 * @return the currentPolicyName
216 private String getCurrentPolicyName() {
217 return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
220 private String createScopeSeparator(String policyScope) {
221 return policyScope.contains(".") ? "" : ".";
225 * @return the currentPolicyScopeAndPolicyName
227 public String getCurrentPolicyScopeAndPolicyName() {
228 return normalizePolicyScopeName(modelName + createScopeSeparator(modelName) + getCurrentPolicyName());
232 * @return The policyName that wil be used in input parameters of DCAE
235 public String getPolicyNameForDcaeDeploy(ClampProperties refProp) {
236 return normalizePolicyScopeName(modelName + createScopeSeparator(modelName)
237 + refProp.getStringValue(PolicyClient.POLICY_MS_NAME_PREFIX_PROPERTY_NAME) + getCurrentPolicyName());
241 * @return the policyScopeAndNameWithUniqueId
243 public String getPolicyScopeAndNameWithUniqueId() {
244 return normalizePolicyScopeName(
245 modelName + createScopeSeparator(modelName) + getCurrentPolicyName() + "_" + policyUniqueId);
249 * @return the currentPolicyScopeAndFullPolicyName
251 public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
252 return normalizePolicyScopeName(
253 modelName + createScopeSeparator(modelName) + policyNamePrefix + getCurrentPolicyName());
257 * Replace all '-' with '_' within policy scope and name.
262 private String normalizePolicyScopeName(String inName) {
263 return inName.replaceAll("-", "_");
267 * @return the currentModelElementId
269 public String getCurrentModelElementId() {
270 return currentModelElementId;
274 * When generating a policy request for a model element, must set the id of
275 * that model element using this method. Used to generate the policy name.
277 * @param currentModelElementId
278 * the currentModelElementId to set
280 public void setCurrentModelElementId(String currentModelElementId) {
281 this.currentModelElementId = currentModelElementId;
285 * @return the policyUniqueId
287 public String getPolicyUniqueId() {
288 return policyUniqueId;
292 * When generating a policy request for a model element, must set the unique
293 * id of that policy using this method. Used to generate the policy name.
295 * @param policyUniqueId
296 * the policyUniqueId to set
298 public void setPolicyUniqueId(String policyUniqueId) {
299 this.policyUniqueId = policyUniqueId;
303 * @return the actionCd
305 public String getActionCd() {
310 * @return the testOnly
312 public boolean isTestOnly() {
317 * @return the isCreateRequest
319 public boolean isCreateRequest() {
321 case CldsEvent.ACTION_SUBMIT:
322 case CldsEvent.ACTION_RESTART:
328 public boolean isStopRequest() {
330 case CldsEvent.ACTION_STOP:
339 public Global getGlobal() {
340 if (global == null) {
341 global = new Global(modelJson);
346 public static final synchronized void registerModelElement(Class<? extends AbstractModelElement> modelElementClass,
348 if (!modelElementClasses.containsKey(modelElementClass.getClass())) {
349 modelElementClasses.put(modelElementClass, type);
353 public <T extends AbstractModelElement> T getType(Class<T> clazz) {
354 instantiateMissingModelElements();
355 String type = modelElementClasses.get(clazz);
356 return (type != null ? (T) modelElements.get(type) : null);