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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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);
221 * @return the currentPolicyScopeAndPolicyName
223 public String getCurrentPolicyScopeAndPolicyName() {
224 return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName());
228 * @return The policyName that wil be used in input parameters of DCAE
231 public String getPolicyNameForDcaeDeploy(ClampProperties refProp) {
232 return normalizePolicyScopeName(modelName + "."
233 + refProp.getStringValue(PolicyClient.POLICY_MS_NAME_PREFIX_PROPERTY_NAME) + 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() {
312 * @return the testOnly
314 public boolean isTestOnly() {
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);