2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
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=========================================================
22 package org.onap.policy.rest.util;
24 import com.att.research.xacml.util.XACMLProperties;
25 import com.google.gson.Gson;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.Iterator;
37 import java.util.LinkedHashMap;
38 import java.util.List;
40 import java.util.Map.Entry;
42 import org.apache.commons.lang.StringUtils;
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45 import org.eclipse.emf.common.util.EList;
46 import org.eclipse.emf.common.util.EMap;
47 import org.eclipse.emf.common.util.Enumerator;
48 import org.eclipse.emf.common.util.TreeIterator;
49 import org.eclipse.emf.common.util.URI;
50 import org.eclipse.emf.ecore.EAnnotation;
51 import org.eclipse.emf.ecore.EClass;
52 import org.eclipse.emf.ecore.EClassifier;
53 import org.eclipse.emf.ecore.EEnum;
54 import org.eclipse.emf.ecore.EEnumLiteral;
55 import org.eclipse.emf.ecore.EObject;
56 import org.eclipse.emf.ecore.EPackage;
57 import org.eclipse.emf.ecore.EReference;
58 import org.eclipse.emf.ecore.EStructuralFeature;
59 import org.eclipse.emf.ecore.impl.EAttributeImpl;
60 import org.eclipse.emf.ecore.impl.EEnumImpl;
61 import org.eclipse.emf.ecore.resource.Resource;
62 import org.eclipse.emf.ecore.resource.ResourceSet;
63 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
64 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
65 import org.json.JSONObject;
66 import org.onap.policy.rest.XACMLRestProperties;
67 import org.onap.policy.rest.dao.CommonClassDao;
68 import org.onap.policy.rest.jpa.DictionaryData;
69 import org.yaml.snakeyaml.Yaml;
72 public class MSModelUtils {
74 private static final Log logger = LogFactory.getLog(MSModelUtils.class);
76 private HashMap<String, MSAttributeObject> classMap = new HashMap<>();
77 private HashMap<String, String> enumMap = new HashMap<>();
78 private HashMap<String, String> matchingClass = new HashMap<>();
79 private String configuration = "configuration";
80 private String dictionary = "dictionary";
81 private String onap = "";
82 private String policy = "";
83 private String eProxyURI = "eProxyURI:";
84 private List<String> orderedElements = new ArrayList<>();
85 private String dataOrderInfo = "";
86 private Set<String> uniqueDataKeys = new HashSet<>();
87 private Set<String> uniqueKeys = new HashSet<>();
88 private String listConstraints = null;
89 private String referenceAttributes;
90 private LinkedHashMap<String, Object> retmap = new LinkedHashMap<>();
91 private Map<String, String> matchableValues;
92 private static final String PROPERTIES = ".properties.";
93 private static final String DATATYPE = "data_types.policy.data.";
94 private static final String TYPE = ".type";
95 private static final String REQUIRED = ".required";
96 private static final String DICTIONARYNAME = "dictionaryName";
97 private static final String DICTIONARY = "dictionary:";
98 private static final String MATCHABLE = ".matchable";
99 public static final String STRING = "string";
100 public static final String INTEGER = "integer";
101 private static final String BOOLEAN = "boolean";
102 public static final String LIST = "list";
103 public static final String MAP = "map";
104 private static final String DEFAULT = ".default";
105 private static final String MANYFALSE = ":MANY-false";
106 private static final String DESCRIPTION = ".description";
108 private static final String MANYTRUE = ":MANY-true";
109 private static final String DEFAULTVALUE = ":defaultValue-";
110 private static final String REQUIREDVALUE = ":required-";
111 private static final String MATCHABLEKEY = "matchable";
112 private static final String REQUIREDFALSE = ":required-false";
113 private static final String REQUIREDTRUE = ":required-true";
114 private static final String MATCHINGTRUE = "matching-true";
115 private static final String DESCRIPTION_KEY = "description";
116 private static final String DESCRIPTION_TOKEN = ":description-";
117 private static final String PROPERTIES_KEY = "properties";
118 private static final String DATA_TYPE = "data_types";
119 private static final String ERROR = "error";
120 private static final String NODE_TYPE = "node_types";
121 private static final String TOSCA_DEFINITION_VERSION = "tosca_definitions_version";
122 private static final String TOSCA_SIMPLE_YAML_1_0_0 = "tosca_simple_yaml_1_0_0";
123 private static final String JSON_MODEL = "JSON_MODEL";
124 private StringBuilder dataListBuffer = new StringBuilder();
125 private List<String> dataConstraints = new ArrayList<>();
126 private String attributeString = null;
127 private boolean isDuplicatedAttributes = false;
128 private String jsonRuleFormation = null;
130 private static CommonClassDao commonClassDao;
132 public MSModelUtils() {
133 // Default Constructor
136 public MSModelUtils(CommonClassDao commonClassDao) {
137 MSModelUtils.commonClassDao = commonClassDao;
140 public MSModelUtils(String onap, String policy) {
142 this.policy = policy;
145 private enum ANNOTATION_TYPE {
146 MATCHING, VALIDATION, DICTIONARY
149 public enum MODEL_TYPE {
153 public enum SearchType {
154 TOSCA_DEFINITION_VERSION, TOSCA_SIMPLE_YAML_1_0_0, NODE_TYPE, DATA_TYPE, JSON_MODEL
157 public Map<String, MSAttributeObject> processEpackage(String file, MODEL_TYPE model) {
158 if (model == MODEL_TYPE.XMI) {
159 processXMIEpackage(file);
165 private void processXMIEpackage(String xmiFile) {
166 EPackage root = getEpackage(xmiFile);
167 TreeIterator<EObject> treeItr = root.eAllContents();
171 // Pulling out dependency from file
172 while (treeItr.hasNext()) {
173 EObject obj = treeItr.next();
174 if (obj instanceof EClassifier) {
175 EClassifier eClassifier = (EClassifier) obj;
176 className = eClassifier.getName();
178 if (obj instanceof EEnum) {
179 enumMap.putAll(getEEnum(obj));
180 } else if (obj instanceof EClass) {
181 String temp = getDependencyList(eClassifier).toString();
182 returnValue = StringUtils.replaceEach(temp, new String[] {"[", "]"}, new String[] {"", ""});
183 getAttributes(className, returnValue, root);
188 if (!enumMap.isEmpty()) {
191 if (!matchingClass.isEmpty()) {
192 checkForMatchingClass();
196 private void checkForMatchingClass() {
197 HashMap<String, String> tempAttribute = new HashMap<>();
199 for (Entry<String, String> set : matchingClass.entrySet()) {
200 String key = set.getKey();
201 if (classMap.containsKey(key)) {
202 Map<String, String> listAttributes = classMap.get(key).getAttribute();
203 Map<String, String> listRef = classMap.get(key).getRefAttribute();
204 for (Entry<String, String> eSet : listAttributes.entrySet()) {
205 String key2 = eSet.getKey();
206 tempAttribute.put(key2, MATCHINGTRUE);
208 for (Entry<String, String> eSet : listRef.entrySet()) {
209 String key3 = eSet.getKey();
210 tempAttribute.put(key3, MATCHINGTRUE);
214 updateMatching(tempAttribute, key);
221 private void updateMatching(HashMap<String, String> tempAttribute, String key) {
222 Map<String, MSAttributeObject> newClass = classMap;
224 for (Entry<String, MSAttributeObject> updateClass : newClass.entrySet()) {
225 Map<String, String> valueMap = updateClass.getValue().getMatchingSet();
226 String keymap = updateClass.getKey();
227 if (valueMap.containsKey(key)) {
228 Map<String, String> modifyMap = classMap.get(keymap).getMatchingSet();
229 modifyMap.remove(key);
230 modifyMap.putAll(tempAttribute);
231 classMap.get(keymap).setMatchingSet(modifyMap);
237 private void addEnumClassMap() {
238 for (Entry<String, MSAttributeObject> value : classMap.entrySet()) {
239 value.getValue().setEnumType(enumMap);
243 private EPackage getEpackage(String xmiFile) {
244 ResourceSet resSet = new ResourceSetImpl();
245 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
246 Map<String, Object> m = reg.getExtensionToFactoryMap();
247 m.put("xmi", new XMIResourceFactoryImpl());
248 Resource resource = resSet.getResource(URI.createFileURI(xmiFile), true);
250 resource.load(Collections.emptyMap());
251 } catch (IOException e) {
252 logger.error("Error loading Encore Resource for new Model" + e);
255 return (EPackage) resource.getContents().get(0);
258 private HashMap<String, String> getEEnum(EObject obj) {
259 List<String> valueList = new ArrayList<>();
260 HashMap<String, String> returnMap = new HashMap<>();
261 EEnum eenum = (EEnum) obj;
263 String name = eenum.getName();
264 for (EEnumLiteral eEnumLiteral : eenum.getELiterals()) {
265 Enumerator instance = eEnumLiteral.getInstance();
266 String value = instance.getLiteral();
267 valueList.add(value);
269 returnMap.put(name, valueList.toString());
273 public void getAttributes(String className, String dependency, EPackage root) {
274 List<String> dpendList = new ArrayList<>();
275 if (dependency != null) {
276 dpendList = new ArrayList<>(Arrays.asList(dependency.split(",")));
278 MSAttributeObject msAttributeObject = new MSAttributeObject();
279 msAttributeObject.setClassName(className);
280 String extendClass = getSubTypes(root, className);
281 Map<String, String> returnRefList = getRefAttributeList(root, className, extendClass);
282 Map<String, String> returnAttributeList = getAttributeList(root, className, extendClass);
283 Map<String, Object> returnSubList = getSubAttributeList(root, className, extendClass);
284 HashMap<String, String> returnAnnotation = getAnnotation(root, className, extendClass);
285 msAttributeObject.setAttribute(returnAttributeList);
286 msAttributeObject.setRefAttribute(returnRefList);
287 msAttributeObject.setSubClass(returnSubList);
288 msAttributeObject.setDependency(dpendList.toString());
289 msAttributeObject.addMatchingSet(returnAnnotation);
290 msAttributeObject.setPolicyTempalate(isPolicyTemplate(root, className));
292 this.classMap.put(className, msAttributeObject);
295 private HashMap<String, String> getAnnotation(EPackage root, String className, String extendClass) {
296 TreeIterator<EObject> treeItr = root.eAllContents();
297 boolean requiredAttribute = false;
298 boolean requiredMatchAttribute = false;
299 HashMap<String, String> annotationSet = new HashMap<>();
301 // Pulling out dependency from file
302 while (treeItr.hasNext()) {
303 EObject obj = treeItr.next();
304 if (obj instanceof EClassifier) {
305 requiredAttribute = isRequiredAttribute(obj, className);
306 requiredMatchAttribute = isRequiredAttribute(obj, extendClass);
309 if (requiredAttribute) {
310 if (obj instanceof EStructuralFeature) {
311 checkAnnotation(annotationSet, (EStructuralFeature) obj);
313 } else if (requiredMatchAttribute && (obj instanceof EStructuralFeature)) {
314 findMatchingAnnotation(annotationSet, obj);
317 return annotationSet;
320 private void findMatchingAnnotation(HashMap<String, String> annotationSet, EObject obj) {
321 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
322 if (eStrucClassifier.getEAnnotations().isEmpty()) {
325 String matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
326 if (matching != null) {
327 if (obj instanceof EReference) {
328 EClass refType = ((EReference) obj).getEReferenceType();
329 annotationSet.put(refType.getName(), matching);
330 matchingClass.put(refType.getName(), matching);
332 annotationSet.put(eStrucClassifier.getName(), matching);
338 private void checkAnnotation(HashMap<String, String> annotationSet, EStructuralFeature obj) {
339 EStructuralFeature eStrucClassifier = obj;
340 if (eStrucClassifier.getEAnnotations().isEmpty()) {
343 String matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
344 if (matching != null) {
345 annotationSet.put(eStrucClassifier.getName(), matching);
347 String range = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
349 annotationSet.put(eStrucClassifier.getName(), range);
351 String annotationDict = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
352 if (annotationDict != null) {
353 annotationSet.put(eStrucClassifier.getName(), annotationDict);
357 private Map<String, Object> getSubAttributeList(EPackage root, String className, String superClass) {
358 TreeIterator<EObject> treeItr = root.eAllContents();
359 boolean requiredAttribute = false;
360 Map<String, Object> subAttribute = new HashMap<>();
361 int rollingCount = 0;
362 int processClass = 0;
364 // Pulling out dependency from file
365 while (treeItr.hasNext() && rollingCount < 2) {
367 EObject obj = treeItr.next();
368 if (obj instanceof EClassifier) {
369 requiredAttribute = isRequiredAttribute(obj, className) || isRequiredAttribute(obj, superClass);
370 if (requiredAttribute) {
373 rollingCount = rollingCount + processClass;
376 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
377 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
378 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
379 updateSubAttributes(subAttribute, obj, eStrucClassifier);
386 private void updateSubAttributes(Map<String, Object> subAttribute, EObject obj,
387 EStructuralFeature eStrucClassifier) {
388 if (!(obj instanceof EReference)) {
391 if (annotationTest(eStrucClassifier, configuration, onap)) {
392 EClass refType = ((EReference) obj).getEReferenceType();
393 if (!refType.toString().contains(eProxyURI)) {
394 String required = REQUIREDFALSE;
395 if (eStrucClassifier.getLowerBound() == 1) {
396 required = REQUIREDTRUE;
398 subAttribute.put(eStrucClassifier.getName(), refType.getName() + required);
403 public String checkDefultValue(String defultValue) {
404 if (defultValue != null) {
405 return DEFAULTVALUE + defultValue;
407 return ":defaultValue-NA";
411 public String checkRequiredPattern(int upper, int lower) {
412 String pattern = XACMLProperties.getProperty(XACMLRestProperties.PROP_XCORE_REQUIRED_PATTERN);
413 if (pattern != null && upper == Integer.parseInt(pattern.split(",")[1])
414 && lower == Integer.parseInt(pattern.split(",")[0])) {
417 return REQUIREDFALSE;
420 public JSONObject buildJavaObject(Map<String, String> map) {
421 return new JSONObject(map);
424 public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass) {
426 TreeIterator<EObject> treeItr = root.eAllContents();
427 boolean requiredAttribute = false;
428 HashMap<String, String> refAttribute = new HashMap<>();
429 int rollingCount = 0;
430 int processClass = 0;
432 // Pulling out dependency from file
433 while (treeItr.hasNext()) {
434 EObject obj = treeItr.next();
435 if (obj instanceof EClassifier) {
436 requiredAttribute = isRequiredAttribute(obj, className) || isRequiredAttribute(obj, superClass);
437 if (requiredAttribute) {
440 rollingCount = rollingCount + processClass;
443 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
444 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
445 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
446 annotation = annotationTest(eStrucClassifier, configuration, onap);
447 if (annotation && obj instanceof EReference) {
448 updRefAttributes(refAttribute, (EStructuralFeature) obj, eStrucClassifier);
449 } else if (annotation && obj instanceof EAttributeImpl) {
450 updEnumTypeRefAttrib(refAttribute, (EStructuralFeature) obj, eStrucClassifier);
459 private void updEnumTypeRefAttrib(HashMap<String, String> refAttribute, EStructuralFeature obj,
460 EStructuralFeature eStrucClassifier) {
461 EClassifier refType = ((EAttributeImpl) obj).getEType();
462 if (!(refType instanceof EEnumImpl)) {
466 String array = arrayCheck(obj.getUpperBound());
467 String required = REQUIREDFALSE;
468 if (obj.getLowerBound() == 1) {
469 required = REQUIREDTRUE;
471 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
474 private void updRefAttributes(HashMap<String, String> refAttribute, EStructuralFeature obj,
475 EStructuralFeature eStrucClassifier) {
476 EClass refType = ((EReference) obj).getEReferenceType();
477 if (refType.toString().contains(eProxyURI)) {
478 String one = refType.toString().split(eProxyURI)[1];
480 StringUtils.replaceEach(one.split("#")[1], new String[] {"//", ")"}, new String[] {"", ""});
481 refAttribute.put(eStrucClassifier.getName(), refValue);
483 String required = REQUIREDFALSE;
484 if (obj.getLowerBound() == 1) {
485 required = REQUIREDTRUE;
487 refAttribute.put(eStrucClassifier.getName(),
488 refType.getName() + arrayCheck(obj.getUpperBound()) + required);
492 private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
493 String annotationType;
494 EAnnotation eAnnotation;
498 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
500 for (int i = 0; i < value.size(); i++) {
501 annotationType = value.get(i).getSource();
502 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
503 onapType = eAnnotation.getDetails().get(0).getValue();
504 onapValue = eAnnotation.getDetails().get(0).getKey();
506 if (annotationType.contains(type) && onapType.contains(annotation)) {
510 if (annotationType.contains(type) && onapValue.contains(annotation)) {
519 private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
520 String annotationType;
521 EAnnotation eAnnotation;
523 String onapValue = null;
525 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
527 for (int i = 0; i < value.size(); i++) {
528 annotationType = value.get(i).getSource();
529 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
530 onapType = eAnnotation.getDetails().get(0).getKey();
531 if (annotationType.contains(type) && onapType.compareToIgnoreCase(annotation.toString()) == 0) {
532 onapValue = eAnnotation.getDetails().get(0).getValue();
533 if (annotation == ANNOTATION_TYPE.VALIDATION) {
536 return onapType + "-" + onapValue;
544 public boolean isRequiredAttribute(EObject obj, String className) {
545 EClassifier eClassifier = (EClassifier) obj;
546 String workingClass = eClassifier.getName().trim();
547 if (workingClass.equalsIgnoreCase(className)) {
554 private boolean isPolicyTemplate(EPackage root, String className) {
555 boolean result = false;
556 for (EClassifier classifier : root.getEClassifiers()) {
557 if (classifier instanceof EClass) {
558 EClass eClass = (EClass) classifier;
559 if (eClass.getName().contentEquals(className)) {
560 result = checkPolicyTemplate(eClass);
568 private boolean checkPolicyTemplate(EClass eClass) {
569 EList<EAnnotation> value = eClass.getEAnnotations();
570 for (EAnnotation workingValue : value) {
571 EMap<String, String> keyMap = workingValue.getDetails();
572 if (keyMap.containsKey("policyTemplate")) {
579 private String getSubTypes(EPackage root, String className) {
580 String returnSubTypes = null;
581 for (EClassifier classifier : root.getEClassifiers()) {
582 if (classifier instanceof EClass) {
583 returnSubTypes = findSubTypes(className, returnSubTypes, (EClass) classifier);
586 return returnSubTypes;
589 private String findSubTypes(String className, String returnSubTypes, EClass classifier) {
590 EClass eClass = classifier;
592 for (EClass eSuperType : eClass.getEAllSuperTypes()) {
593 if (eClass.getName().contentEquals(className)) {
594 returnSubTypes = eSuperType.getName();
597 return returnSubTypes;
600 public Map<String, String> getAttributeList(EPackage root, String className, String superClass) {
602 TreeIterator<EObject> treeItr = root.eAllContents();
603 boolean requiredAttribute = false;
604 HashMap<String, String> refAttribute = new HashMap<>();
606 // Pulling out dependency from file
607 while (treeItr.hasNext()) {
608 EObject obj = treeItr.next();
609 if (obj instanceof EClassifier) {
610 requiredAttribute = isRequiredAttribute(obj, className) || isRequiredAttribute(obj, superClass);
613 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
614 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
615 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
616 checkStrucClassifier(refAttribute, obj, eStrucClassifier);
624 private void checkStrucClassifier(HashMap<String, String> refAttribute, EObject obj,
625 EStructuralFeature eStrucClassifier) {
626 EClassifier refType = ((EStructuralFeature) obj).getEType();
627 boolean annotation = annotationTest(eStrucClassifier, configuration, onap);
628 boolean dictionaryTest = annotationTest(eStrucClassifier, dictionary, policy);
629 if (annotation && !(obj instanceof EReference) && !(refType instanceof EEnumImpl)) {
630 updEReferenceAttrib(refAttribute, dictionaryTest, (EStructuralFeature) obj, eStrucClassifier);
634 private void updEReferenceAttrib(HashMap<String, String> refAttribute, boolean dictionaryTest,
635 EStructuralFeature obj, EStructuralFeature eStrucClassifier) {
637 String name = eStrucClassifier.getName();
638 if (dictionaryTest) {
639 eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
641 eType = eStrucClassifier.getEType().getInstanceClassName();
643 String defaultValue = checkDefultValue(obj.getDefaultValueLiteral());
644 String array = arrayCheck(obj.getUpperBound());
645 String required = checkRequiredPattern(obj.getUpperBound(), obj.getLowerBound());
646 refAttribute.put(name, eType + defaultValue + required + array);
649 public String arrayCheck(int upperBound) {
651 if (upperBound == -1) {
658 public List<String> getDependencyList(EClassifier eClassifier) {
659 List<String> returnValue = new ArrayList<>();;
660 EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes();
661 if (somelist.isEmpty()) {
664 for (EClass depend : somelist) {
665 if (depend.toString().contains(eProxyURI)) {
666 String one = depend.toString().split(eProxyURI)[1];
668 StringUtils.replaceEach(one.split("#")[1], new String[] {"//", ")"}, new String[] {"", ""});
669 returnValue.add(value);
676 public Map<String, String> buildSubList(Map<String, String> subClassAttributes,
677 Map<String, MSAttributeObject> classMap, String className) {
678 Map<String, String> missingValues = new HashMap<>();
679 Map<String, String> workingMap;
682 for (Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()) {
683 String value = map.getValue().split(":")[0];
685 classMap.get(className).getEnumType();
686 enumType = classMap.get(className).getEnumType().containsKey(value);
688 workingMap = classMap.get(value).getRefAttribute();
689 for (Entry<String, String> subMab : workingMap.entrySet()) {
690 String value2 = subMab.getValue().split(":")[0];
691 if (!subClassAttributes.containsValue(value2)) {
692 missingValues.put(subMab.getKey(), subMab.getValue());
700 return missingValues;
703 public Map<String, Map<String, String>> recursiveReference(Map<String, MSAttributeObject> classMap,
706 Map<String, Map<String, String>> returnObject = new HashMap<>();
707 Map<String, String> returnClass = getRefclass(classMap, className);
708 returnObject.put(className, returnClass);
709 for (Entry<String, String> reAttribute : returnClass.entrySet()) {
710 if (reAttribute.getValue().split(":")[1].contains("MANY")
711 && classMap.get(reAttribute.getValue().split(":")[0]) != null) {
712 returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0]));
721 public String createJson(Map<String, MSAttributeObject> classMap, String className) {
723 Map<String, Map<String, String>> myObject = new HashMap<>();
724 for (Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()) {
725 String value = map.getValue().split(":")[0];
727 enumType = classMap.get(className).getEnumType().containsKey(value);
728 if (!enumType && map.getValue().split(":")[1].contains("MANY")) {
729 Map<String, Map<String, String>> testRecursive =
730 recursiveReference(classMap, map.getValue().split(":")[0]);
731 myObject.putAll(testRecursive);
736 Gson gson = new Gson();
737 return gson.toJson(myObject);
740 public Map<String, String> getRefclass(Map<String, MSAttributeObject> classMap, String className) {
741 HashMap<String, String> missingValues = new HashMap<>();
743 if (classMap.get(className).getAttribute() != null || !classMap.get(className).getAttribute().isEmpty()) {
744 missingValues.putAll(classMap.get(className).getAttribute());
747 if (classMap.get(className).getRefAttribute() != null || !classMap.get(className).getRefAttribute().isEmpty()) {
748 missingValues.putAll(classMap.get(className).getRefAttribute());
751 return missingValues;
754 public String createSubAttributes(List<String> dependency, Map<String, MSAttributeObject> classMap,
757 HashMap<String, Object> workingMap = new HashMap<>();
758 MSAttributeObject tempObject;
759 if (dependency != null) {
760 if (dependency.isEmpty()) {
763 dependency.add(modelName);
764 for (String element : dependency) {
765 tempObject = classMap.get(element);
766 if (tempObject != null) {
767 workingMap.putAll(classMap.get(element).getSubClass());
772 return createJson(classMap, modelName);
775 public List<String> getFullDependencyList(List<String> dependency, Map<String, MSAttributeObject> classMap) {
776 ArrayList<String> returnList = new ArrayList<>();
777 ArrayList<String> workingList;
778 returnList.addAll(dependency);
779 for (String element : dependency) {
780 if (classMap.containsKey(element)) {
781 MSAttributeObject value = classMap.get(element);
783 StringUtils.replaceEach(value.getDependency(), new String[] {"[", "]"}, new String[] {"", ""});
784 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
785 for (String depend : workingList) {
786 updDependencyList(returnList, depend);
794 private void updDependencyList(ArrayList<String> returnList, String depend) {
795 if (!returnList.contains(depend) && !depend.isEmpty()) {
796 returnList.add(depend.trim());
803 public String parseTosca(String fileName) {
804 Map<String, String> map = new LinkedHashMap<>();
806 map = load(fileName);
807 if (map != null && map.get(ERROR) != null) {
808 return map.get(ERROR);
810 parseDataAndPolicyNodes(map);
811 LinkedHashMap<String, String> dataMapForJson = parseDataNodes(map);
812 constructJsonForDataFields(dataMapForJson);
813 LinkedHashMap<String, LinkedHashMap<String, String>> mapKey = parsePolicyNodes(map);
814 createAttributes(mapKey);
816 } catch (IOException e) {
818 } catch (ParserException e) {
820 return e.getMessage();
826 @SuppressWarnings("unchecked")
827 public Map<String, String> load(String fileName) throws IOException, ParserException {
828 File newConfiguration = new File(fileName);
829 StringBuilder orderInfo = new StringBuilder("[");
830 Yaml yaml = new Yaml();
831 LinkedHashMap<Object, Object> yamlMap = null;
832 try (InputStream is = new FileInputStream(newConfiguration)) {
833 yamlMap = (LinkedHashMap<Object, Object>) yaml.load(is);
834 } catch (FileNotFoundException e) {
836 } catch (Exception e) {
838 throw new ParserException("Invalid TOSCA Model format. Please make sure it is a valid YAML file");
841 StringBuilder sb = new StringBuilder();
842 LinkedHashMap<String, String> settings = new LinkedHashMap<>();
843 if (yamlMap == null) {
847 String message = validations(yamlMap);
849 if (message != null) {
850 settings.put(ERROR, message);
856 if (!isDuplicatedAttributes && orderedElements != null && !orderedElements.isEmpty()) {
857 orderedElements.stream().forEach(string -> {
858 orderInfo.append(string);
859 orderInfo.append(",");
860 logger.info("Content: " + string);
863 orderInfo.append("]");
865 dataOrderInfo = orderInfo.toString();
866 dataOrderInfo = dataOrderInfo.replace(",]", "]");
868 logger.info("dataOrderInfo :" + dataOrderInfo);
871 List<String> path = new ArrayList<>();
872 serializeMap(settings, sb, path, yamlMap);
876 @SuppressWarnings("unchecked")
877 private String validations(@SuppressWarnings("rawtypes") Map yamlMap) {
879 boolean isNoteTypeFound = false;
880 boolean isDataTypeFound = false;
881 boolean isToscaVersionKeyFound = false;
882 boolean isToscaVersionValueFound = false;
883 @SuppressWarnings("rawtypes")
884 Map m1 = new HashMap();
886 if (yamlMap != null) {
887 // Get a set of the entries
888 @SuppressWarnings("rawtypes")
889 Set<Entry> entries = yamlMap.entrySet();
890 for (@SuppressWarnings("rawtypes")
891 Map.Entry me : entries) {
892 if (TOSCA_SIMPLE_YAML_1_0_0.equals(me.getValue())) {
893 isToscaVersionValueFound = true;
896 switch (me.getKey().toString()) {
897 case TOSCA_DEFINITION_VERSION:
898 isToscaVersionKeyFound = true;
900 m1.put(TOSCA_DEFINITION_VERSION, order);
903 isNoteTypeFound = true;
905 m1.put(NODE_TYPE, order);
908 isDataTypeFound = true;
910 m1.put(DATA_TYPE, order);
913 setJsonRuleFormation(me.getValue().toString());
919 if (!isDataTypeFound) {
920 return "data_types are missing or invalid.";
922 if (!isToscaVersionKeyFound || !isToscaVersionValueFound) {
923 return "tosca_definitions_version is missing or invalid.";
926 if (!isNoteTypeFound) {
927 return "node_types are missing or invalid.";
930 short version = (short) m1.get(TOSCA_DEFINITION_VERSION);
933 return "tosca_definitions_version should be defined first.";
936 short data = (short) m1.get(DATA_TYPE);
937 short node = (short) m1.get(NODE_TYPE);
938 if (isDataTypeFound && node > data) {
939 return "node_types should be defined before data_types.";
947 @SuppressWarnings({"unchecked", "rawtypes"})
948 private void serializeMap(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
949 Map<Object, Object> yamlMap) {
950 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
952 if (entry.getValue() instanceof Map) {
953 path.add((String) entry.getKey());
954 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
955 path.remove(path.size() - 1);
956 } else if (entry.getValue() instanceof List) {
957 path.add((String) entry.getKey());
958 serializeList(settings, sb, path, (List) entry.getValue());
959 path.remove(path.size() - 1);
961 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
966 @SuppressWarnings("unchecked")
967 private void serializeList(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
968 List<String> yamlList) {
970 for (Object listEle : yamlList) {
971 if (listEle instanceof Map) {
972 path.add(Integer.toString(counter));
973 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
974 path.remove(path.size() - 1);
975 } else if (listEle instanceof List) {
976 path.add(Integer.toString(counter));
977 serializeList(settings, sb, path, (List<String>) listEle);
978 path.remove(path.size() - 1);
980 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
986 private void serializeValue(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
987 String name, Object value) {
992 for (String pathEle : path) {
993 sb.append(pathEle).append('.');
996 settings.put(sb.toString(), value.toString());
1000 void parseDataAndPolicyNodes(Map<String, String> map) {
1001 for (String key : map.keySet()) {
1002 if (key.contains("policy.nodes.Root")) {
1004 } else if (key.contains("policy.nodes")) {
1005 String wordToFind = "policy.nodes.";
1006 int indexForPolicyNode = key.indexOf(wordToFind);
1007 String subNodeString = key.substring(indexForPolicyNode + 13, key.length());
1009 stringBetweenDots(subNodeString);
1010 } else if (key.contains("policy.data")) {
1011 String wordToFind = "policy.data.";
1012 int indexForPolicyNode = key.indexOf(wordToFind);
1013 String subNodeString = key.substring(indexForPolicyNode + 12, key.length());
1015 stringBetweenDotsForDataFields(subNodeString);
1020 // Second index of dot should be returned.
1021 public int stringBetweenDots(String str) {
1022 String stringToSearch = str;
1023 String[] ss = stringToSearch.split("\\.");
1025 int len = ss.length;
1027 uniqueKeys.add(ss[2]);
1031 return uniqueKeys.size();
1035 public void stringBetweenDotsForDataFields(String str) {
1036 String stringToSearch = str;
1037 String[] ss = stringToSearch.split("\\.");
1039 int len = ss.length;
1042 uniqueDataKeys.add(ss[0] + "%" + ss[2]);
1047 void constructJsonForDataFields(LinkedHashMap<String, String> dataMapForJson) {
1048 LinkedHashMap<String, LinkedHashMap<String, String>> dataMapKey = new LinkedHashMap<>();
1049 LinkedHashMap<String, String> hmSub;
1050 for (Map.Entry<String, String> entry : dataMapForJson.entrySet()) {
1051 String uniqueDataKey = entry.getKey();
1052 String[] uniqueDataKeySplit = uniqueDataKey.split("%");
1053 String value = dataMapForJson.get(uniqueDataKey);
1054 if (dataMapKey.containsKey(uniqueDataKeySplit[0])) {
1055 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
1056 hmSub.put(uniqueDataKeySplit[1], value);
1058 hmSub = new LinkedHashMap<>();
1059 hmSub.put(uniqueDataKeySplit[1], value);
1062 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
1065 JSONObject mainObject = new JSONObject();
1067 for (Map.Entry<String, LinkedHashMap<String, String>> entry : dataMapKey.entrySet()) {
1068 String s = entry.getKey();
1069 json = new JSONObject();
1070 HashMap<String, String> jsonHm = dataMapKey.get(s);
1071 for (Map.Entry<String, String> entryMap : jsonHm.entrySet()) {
1072 String key = entryMap.getKey();
1073 json.put(key, jsonHm.get(key));
1075 mainObject.put(s, json);
1077 Iterator<String> keysItr = mainObject.keys();
1078 while (keysItr.hasNext()) {
1079 String key = keysItr.next();
1080 String value = mainObject.get(key).toString();
1081 retmap.put(key, value);
1084 logger.info("#############################################################################");
1085 logger.info(mainObject);
1086 logger.info("###############################################################################");
1089 LinkedHashMap<String, String> parseDataNodes(Map<String, String> map) {
1090 LinkedHashMap<String, String> dataMapForJson = new LinkedHashMap<>();
1091 matchableValues = new HashMap<>();
1092 for (String uniqueDataKey : uniqueDataKeys) {
1093 if (uniqueDataKey.contains("%")) {
1094 String[] uniqueDataKeySplit = uniqueDataKey.split("%");
1095 String findType = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + TYPE;
1096 String typeValue = map.get(findType);
1097 logger.info(typeValue);
1099 String findRequired = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + REQUIRED;
1100 String requiredValue = map.get(findRequired);
1102 String matchable = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + MATCHABLE;
1104 String matchableValue = map.get(matchable);
1106 if ("true".equalsIgnoreCase(matchableValue)) {
1107 String key = uniqueDataKeySplit[uniqueDataKeySplit.length - 1];
1108 matchableValues.put(key, MATCHINGTRUE);
1111 if (requiredValue == null || requiredValue.isEmpty()) {
1112 requiredValue = "false";
1114 if (INTEGER.equalsIgnoreCase(typeValue) || STRING.equalsIgnoreCase(typeValue)
1115 || typeValue.equalsIgnoreCase(BOOLEAN)) {
1116 String findDefault =
1117 DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + DEFAULT;
1118 String findDescription =
1119 DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + DESCRIPTION;
1120 String defaultValue = map.get(findDefault);
1121 String descriptionDefined = map.get(findDescription);
1122 logger.info("defaultValue is:" + defaultValue);
1123 logger.info("requiredValue is:" + requiredValue);
1125 StringBuilder attributeIndividualStringBuilder = new StringBuilder();
1126 attributeIndividualStringBuilder.append(typeValue + DEFAULTVALUE);
1127 attributeIndividualStringBuilder.append(defaultValue + REQUIREDVALUE);
1128 attributeIndividualStringBuilder.append(requiredValue + MANYFALSE);
1129 attributeIndividualStringBuilder.append(DESCRIPTION_TOKEN + descriptionDefined);
1130 dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
1131 } else if (LIST.equalsIgnoreCase(typeValue) || MAP.equalsIgnoreCase(typeValue)) {
1132 logger.info("requiredValue is:" + requiredValue);
1133 String findList = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1134 + ".entry_schema.type";
1135 String findDefaultValue = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1136 + ".entry_schema.default";
1137 String findDescription = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1138 + ".entry_schema.description";
1139 String listValue = map.get(findList);
1140 String defaultValue = map.get(findDefaultValue);
1141 String description = map.get(findDescription);
1142 if (listValue != null) {
1143 logger.info("Type of list is:" + listValue);
1145 if (listValue.contains(".")) {
1146 String trimValue = listValue.substring(listValue.lastIndexOf('.') + 1);
1147 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1148 referenceIndividualStringBuilder.append(trimValue + REQUIREDVALUE);
1149 referenceIndividualStringBuilder.append(requiredValue + MANYTRUE);
1150 referenceIndividualStringBuilder.append(DESCRIPTION_TOKEN + description);
1151 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1152 } else { // Its string
1153 StringBuilder stringListItems = new StringBuilder();
1154 if (LIST.equalsIgnoreCase(typeValue)) {
1155 stringListItems.append(uniqueDataKeySplit[1].toUpperCase() + DEFAULTVALUE + defaultValue
1156 + REQUIREDVALUE + requiredValue + MANYFALSE + DESCRIPTION_TOKEN + description);
1157 } else if (MAP.equalsIgnoreCase(typeValue)) {
1158 stringListItems.append(uniqueDataKeySplit[1].toUpperCase() + DEFAULTVALUE + defaultValue
1159 + REQUIREDVALUE + requiredValue + MANYTRUE + DESCRIPTION_TOKEN + description);
1161 dataMapForJson.put(uniqueDataKey, stringListItems.toString());
1162 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase() + "=[");
1163 for (int i = 0; i < 10; i++) {
1164 String findConstraints = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES
1165 + uniqueDataKeySplit[1] + ".entry_schema.constraints.0.valid_values." + i;
1166 String constraintsValue = map.get(findConstraints);
1167 logger.info(constraintsValue);
1168 boolean ruleCheck = false;
1169 if (constraintsValue == null) {
1171 } else if (constraintsValue.startsWith(DICTIONARY)) {
1172 List<Object> dictFromDB = null;
1173 String[] dictionaryNameValRule;
1174 String[] dictionaryName = constraintsValue.split(":");
1175 String dictionaryNameVal = dictionaryName[1];
1176 if (dictionaryNameVal.contains("#Rules")) {
1178 dictionaryNameValRule = dictionaryNameVal.split("#");
1179 dictFromDB = commonClassDao.getDataById(DictionaryData.class, DICTIONARYNAME,
1180 dictionaryNameValRule[0]);
1182 dictFromDB = commonClassDao.getDataById(DictionaryData.class, DICTIONARYNAME,
1185 if (dictFromDB != null && !dictFromDB.isEmpty()) {
1186 DictionaryData data = (DictionaryData) dictFromDB.get(0);
1188 constraintsValue = DICTIONARY + data.getDictionaryUrl() + "@"
1189 + data.getDictionaryDataByName() + "&Rule";
1191 constraintsValue = DICTIONARY + data.getDictionaryUrl() + "@"
1192 + data.getDictionaryDataByName();
1195 dataListBuffer.append(constraintsValue + ",");
1197 logger.info("constraintsValue => " + constraintsValue);
1198 if (constraintsValue.contains("=")) {
1199 constraintsValue = constraintsValue.replace("=", "equal-sign");
1201 dataConstraints.add(constraintsValue);
1202 dataListBuffer.append(constraintsValue + ",");
1205 dataListBuffer.append("]#");
1206 logger.info(dataListBuffer);
1210 String findUserDefined = DATATYPE + uniqueDataKeySplit[0] + "." + PROPERTIES_KEY + "."
1211 + uniqueDataKeySplit[1] + TYPE;
1212 String findDescription = DATATYPE + uniqueDataKeySplit[0] + "." + PROPERTIES_KEY + "."
1213 + uniqueDataKeySplit[1] + DESCRIPTION;
1214 String userDefinedValue = map.get(findUserDefined);
1215 String description = map.get(findDescription);
1216 String trimValue = userDefinedValue.substring(userDefinedValue.lastIndexOf('.') + 1);
1217 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1218 referenceIndividualStringBuilder.append(trimValue + REQUIREDVALUE);
1219 referenceIndividualStringBuilder.append(requiredValue + MANYFALSE);
1220 referenceIndividualStringBuilder.append(DESCRIPTION_TOKEN + description);
1221 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1225 matchableValues.put(uniqueDataKey, MATCHINGTRUE);
1229 return dataMapForJson;
1232 LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String, String> map)
1233 throws ParserException {
1234 LinkedHashMap<String, LinkedHashMap<String, String>> mapKey = new LinkedHashMap<>();
1235 for (String uniqueKey : uniqueKeys) {
1236 LinkedHashMap<String, String> hm;
1238 for (Entry<String, String> entry : map.entrySet()) {
1239 String key = entry.getKey();
1240 if (key.contains(uniqueKey) && key.contains("policy.nodes")) {
1241 if (mapKey.containsKey(uniqueKey)) {
1242 hm = mapKey.get(uniqueKey);
1243 String keyStr = key.substring(key.lastIndexOf('.') + 1);
1244 String valueStr = map.get(key);
1245 if ("type".equalsIgnoreCase(keyStr) && key.contains("entry_schema.0.type")
1246 || key.contains("entry_schema.type") && valueStr.contains("policy.data.")) {
1247 throw new ParserException(
1248 "For user defined object type, Please make sure no space between 'type:' and object "
1252 if ("type".equals(keyStr)) {
1253 if (!key.contains("entry_schema")) {
1254 hm.put(keyStr, valueStr);
1257 hm.put(keyStr, valueStr);
1261 hm = new LinkedHashMap<>();
1262 String keyStr = key.substring(key.lastIndexOf('.') + 1);
1263 String valueStr = map.get(key);
1265 if (("type").equals(keyStr)) {
1266 if (!key.contains("entry_schema")) {
1267 hm.put(keyStr, valueStr);
1270 hm.put(keyStr, valueStr);
1272 mapKey.put(uniqueKey, hm);
1280 private void createAttributes(LinkedHashMap<String, LinkedHashMap<String, String>> mapKey) {
1281 StringBuilder attributeStringBuilder = new StringBuilder();
1282 StringBuilder referenceStringBuilder = new StringBuilder();
1283 StringBuilder listBuffer = new StringBuilder();
1284 List<String> constraints = new ArrayList<>();
1285 for (Map.Entry<String, LinkedHashMap<String, String>> entry : mapKey.entrySet()) {
1286 String keySetString = entry.getKey();
1287 LinkedHashMap<String, String> keyValues = mapKey.get(keySetString);
1288 if (keyValues.get("type") != null && (STRING.equalsIgnoreCase(keyValues.get("type"))
1289 || INTEGER.equalsIgnoreCase(keyValues.get("type"))
1290 || BOOLEAN.equalsIgnoreCase(keyValues.get("type")))) {
1291 StringBuilder attributeIndividualStringBuilder = new StringBuilder();
1292 attributeIndividualStringBuilder.append(keySetString + "=");
1293 attributeIndividualStringBuilder.append(keyValues.get("type") + DEFAULTVALUE);
1294 attributeIndividualStringBuilder.append(keyValues.get("default") + REQUIREDVALUE);
1295 attributeIndividualStringBuilder.append(keyValues.get("required") + MANYFALSE);
1296 attributeIndividualStringBuilder.append(DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1297 attributeStringBuilder.append(attributeIndividualStringBuilder + ",");
1298 if (keyValues.get(MATCHABLEKEY) != null && "true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1299 matchableValues.put(keySetString, MATCHINGTRUE);
1301 } else if (LIST.equalsIgnoreCase(keyValues.get("type"))) {
1302 if ("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1303 matchableValues.put(keySetString, MATCHINGTRUE);
1306 Set<String> keys = keyValues.keySet();
1307 Iterator<String> itr = keys.iterator();
1308 boolean isDefinedType = false;
1309 while (itr.hasNext()) {
1310 String key = itr.next();
1311 if ((!("type").equals(key) || ("required").equals(key))) {
1312 String value = keyValues.get(key);
1313 // The "." in the value determines if its a string or a user defined type.
1314 if (!value.contains(".")) {
1316 if (StringUtils.isNumeric(key)) { // only integer key for the value of Constrains
1317 constraints.add(keyValues.get(key));
1320 // This is user defined type
1321 String trimValue = value.substring(value.lastIndexOf('.') + 1);
1322 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1323 referenceIndividualStringBuilder.append(keySetString + "=" + trimValue + MANYTRUE
1324 + DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1325 referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1326 isDefinedType = true;
1332 if (!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))
1333 && (constraints == null || constraints.isEmpty())) {
1334 referenceStringBuilder.append(keySetString + "=MANY-true" + ",");
1337 // User defined Datatype.
1338 if ("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1339 matchableValues.put(keySetString, MATCHINGTRUE);
1341 String value = keyValues.get("type");
1342 if (value != null && !value.isEmpty()) {
1343 String trimValue = value.substring(value.lastIndexOf('.') + 1);
1344 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1345 referenceIndividualStringBuilder.append(keySetString + "=" + trimValue + MANYFALSE
1346 + DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1347 referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1349 logger.info("keyValues.get(type) is null/empty");
1353 if (constraints != null && !constraints.isEmpty()) {
1355 listBuffer.append(keySetString.toUpperCase() + "=[");
1356 for (String str : constraints) {
1357 if (str.contains(DICTIONARY)) {
1358 String[] dictionaryName = str.split(":");
1359 List<Object> dictFromDB =
1360 commonClassDao.getDataById(DictionaryData.class, DICTIONARYNAME, dictionaryName[1]);
1361 if (dictFromDB != null && !dictFromDB.isEmpty()) {
1362 DictionaryData data = (DictionaryData) dictFromDB.get(0);
1363 str = DICTIONARY + data.getDictionaryUrl() + "@" + data.getDictionaryDataByName();
1366 listBuffer.append(str + ",");
1368 listBuffer.append("]#");
1369 logger.info(listBuffer);
1372 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1373 referenceIndividualStringBuilder.append(keySetString + "=" + keySetString.toUpperCase() + MANYFALSE);
1374 referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1375 constraints.clear();
1379 dataListBuffer.append(listBuffer);
1382 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1383 logger.info("Whole attribute String is:" + attributeStringBuilder);
1384 logger.info("Whole reference String is:" + referenceStringBuilder);
1385 logger.info("List String is:" + listBuffer);
1386 logger.info("Data list buffer is:" + dataListBuffer);
1387 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1389 this.listConstraints = dataListBuffer.toString();
1390 this.referenceAttributes = referenceStringBuilder.toString();
1391 this.attributeString = attributeStringBuilder.toString();
1394 @SuppressWarnings("unchecked")
1395 private void findNode(LinkedHashMap<Object, Object> map) {
1397 map.forEach((key, value) -> {
1398 // if the value is properties and its type is map object, then save all the keys
1399 if (key.equals(PROPERTIES_KEY) && value instanceof Map) {
1400 saveNodes((LinkedHashMap<?, ?>) value);
1401 if (isDuplicatedAttributes) {
1402 orderedElements = new ArrayList<>();
1407 if (!"policy.nodes.Root".equals(key) && value instanceof Map) {
1408 // value is a Map object, then make a recursive call
1409 findNode((LinkedHashMap<Object, Object>) value);
1415 private void saveNodes(LinkedHashMap<?, ?> map) {
1417 for (Entry<?, ?> entry : map.entrySet()) {
1419 if (orderedElements.indexOf(entry.getKey()) >= 0) { // duplicated attribute names
1420 isDuplicatedAttributes = true;
1423 orderedElements.add((String) entry.getKey());
1428 public String getAttributeString() {
1429 return attributeString;
1432 public void setAttributeString(String attributeString) {
1433 this.attributeString = attributeString;
1436 public LinkedHashMap<String, Object> getRetmap() {
1440 public void setRetmap(LinkedHashMap<String, Object> retmap) {
1441 this.retmap = retmap;
1444 public Map<String, String> getMatchableValues() {
1445 return matchableValues;
1448 public void setMatchableValues(Map<String, String> matchableValues) {
1449 this.matchableValues = matchableValues;
1452 public String getReferenceAttributes() {
1453 return referenceAttributes;
1456 public void setReferenceAttributes(String referenceAttributes) {
1457 this.referenceAttributes = referenceAttributes;
1460 public String getListConstraints() {
1461 return listConstraints;
1464 public void setListConstraints(String listConstraints) {
1465 this.listConstraints = listConstraints;
1468 public String getDataOrderInfo() {
1469 return dataOrderInfo;
1472 public void setDataOrderInfo(String dataOrderInfo) {
1473 this.dataOrderInfo = dataOrderInfo;
1476 public String getJsonRuleFormation() {
1477 return jsonRuleFormation;
1480 public void setJsonRuleFormation(String jsonRuleFormation) {
1481 this.jsonRuleFormation = jsonRuleFormation;