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.fasterxml.jackson.core.JsonProcessingException;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import org.onap.clamp.clds.model.CldsEvent;
30 import org.onap.clamp.clds.model.CldsModel;
31 import org.camunda.bpm.engine.delegate.DelegateExecution;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.logging.Logger;
38 * Parse model properties.
40 public class ModelProperties {
41 private static final Logger logger = Logger.getLogger(ModelProperties.class.getName());
43 private ModelBpmn modelBpmn;
44 private JsonNode modelJson;
46 private final String modelName;
47 private final String controlName;
48 private final String actionCd;
50 private Global global;
51 private Collector collector;
52 private StringMatch stringMatch;
53 private Policy policy;
56 private String currentModelElementId;
57 private String policyUniqueId;
60 * Retain data required to parse the ModelElement objects. (Rather than
61 * parse them all - parse them on demand if requested.)
66 * @param modelBpmnPropText
67 * @param modelPropText
68 * @throws JsonProcessingException
71 public ModelProperties(String modelName, String controlName, String actionCd, String modelBpmnPropText, String modelPropText) throws IOException {
72 this.modelName = modelName;
73 this.controlName = controlName;
74 this.actionCd = actionCd;
75 modelBpmn = ModelBpmn.create(modelBpmnPropText);
76 ObjectMapper mapper = new ObjectMapper();
77 modelJson = mapper.readTree(modelPropText);
81 * Get the VF for a model. If return null if there is no VF.
86 public static String getVf(CldsModel model) {
87 List<String> vfs = null;
89 ObjectMapper mapper = new ObjectMapper();
90 JsonNode modelJson = mapper.readTree(model.getPropText());
91 Global global = new Global(modelJson);
92 vfs = global.getResourceVf();
93 } catch (IOException e) {
97 if (vfs != null && !vfs.isEmpty()) {
104 * Create ModelProperties for Camunda Delegate.
108 * @throws JsonProcessingException
109 * @throws IOException
111 public static ModelProperties create(DelegateExecution execution) throws IOException {
112 String modelProp = (String) execution.getVariable("modelProp");
113 String modelBpmnProp = (String) execution.getVariable("modelBpmnProp");
114 String modelName = (String) execution.getVariable("modelName");
115 String controlName = (String) execution.getVariable("controlName");
116 String actionCd = (String) execution.getVariable("actionCd");
118 return new ModelProperties(modelName, controlName, actionCd, modelBpmnProp, modelProp);
122 * return appropriate model element given the type
127 public ModelElement getModelElementByType(String type) {
130 case ModelElement.TYPE_COLLECTOR:
133 case ModelElement.TYPE_STRING_MATCH:
134 me = getStringMatch();
136 case ModelElement.TYPE_POLICY:
139 case ModelElement.TYPE_TCA:
143 throw new IllegalArgumentException("Invalid ModelElement type: " + type);
149 * @return the modelName
151 public String getModelName() {
156 * @return the controlName
158 public String getControlName() {
163 * @return the controlNameAndPolicyUniqueId
165 public String getControlNameAndPolicyUniqueId() {
166 return controlName + "_" + policyUniqueId;
170 * @return the currentPolicyName
172 private String getCurrentPolicyName() {
173 return normalizePolicyScopeName(controlName + "_" + currentModelElementId);
177 * @return the currentPolicyScopeAndPolicyName
179 public String getCurrentPolicyScopeAndPolicyName() {
180 return normalizePolicyScopeName(modelName + "." + getCurrentPolicyName());
184 * @return the currentPolicyScopeAndFullPolicyName
186 public String getCurrentPolicyScopeAndFullPolicyName(String policyNamePrefix) {
187 return normalizePolicyScopeName(modelName + "." + policyNamePrefix + getCurrentPolicyName());
191 * @return the currentPolicyScopeAndFullPolicyNameWithVersion
193 public String getCurrentPolicyScopeAndFullPolicyNameWithVersion(String policyNamePrefix, int version) {
194 return normalizePolicyScopeName(
195 modelName + "." + policyNamePrefix + getCurrentPolicyName() + "." + version + ".xml");
199 * Replace all '-' with '_' within policy scope and name.
204 private String normalizePolicyScopeName(String inName) {
205 return inName.replaceAll("-", "_");
209 * @return the currentModelElementId
211 public String getCurrentModelElementId() {
212 return currentModelElementId;
216 * When generating a policy request for a model element, must set the id of
217 * that model element using this method. Used to generate the policy name.
219 * @param currentModelElementId
220 * the currentModelElementId to set
222 public void setCurrentModelElementId(String currentModelElementId) {
223 this.currentModelElementId = currentModelElementId;
227 * @return the policyUniqueId
229 public String getPolicyUniqueId() {
230 return policyUniqueId;
234 * When generating a policy request for a model element, must set the unique
235 * id of that policy using this method. Used to generate the policy name.
237 * @param policyUniqueId
238 * the policyUniqueId to set
240 public void setPolicyUniqueId(String policyUniqueId) {
241 this.policyUniqueId = policyUniqueId;
245 * @return the collector
247 public Collector getCollector() {
248 if (collector == null) {
249 collector = new Collector(this, modelBpmn, modelJson);
255 * @return the actionCd
257 public String getActionCd() {
262 * @return the isCreateRequest
264 public boolean isCreateRequest() {
266 case CldsEvent.ACTION_SUBMIT:
267 case CldsEvent.ACTION_RESTART:
273 public boolean isStopRequest() {
275 case CldsEvent.ACTION_STOP:
284 public Global getGlobal() {
285 if (global == null) {
286 global = new Global(modelJson);
292 * @return the stringMatch
294 public StringMatch getStringMatch() {
295 if (stringMatch == null) {
296 stringMatch = new StringMatch(this, modelBpmn, modelJson);
304 public Policy getPolicy() {
305 if (policy == null) {
306 policy = new Policy(this, modelBpmn, modelJson);
314 public Tca getTca() {
316 tca = new Tca(this, modelBpmn, modelJson);