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 * Modifications Copyright (C) 2019 Nordix Foundation.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.rest.util;
25 import com.att.research.xacml.util.XACMLProperties;
26 import com.google.gson.Gson;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.Iterator;
39 import java.util.LinkedHashMap;
40 import java.util.List;
42 import java.util.Map.Entry;
48 import org.apache.commons.lang.StringUtils;
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.eclipse.emf.common.util.EList;
52 import org.eclipse.emf.common.util.EMap;
53 import org.eclipse.emf.common.util.Enumerator;
54 import org.eclipse.emf.common.util.TreeIterator;
55 import org.eclipse.emf.common.util.URI;
56 import org.eclipse.emf.ecore.EAnnotation;
57 import org.eclipse.emf.ecore.EClass;
58 import org.eclipse.emf.ecore.EClassifier;
59 import org.eclipse.emf.ecore.EEnum;
60 import org.eclipse.emf.ecore.EEnumLiteral;
61 import org.eclipse.emf.ecore.EObject;
62 import org.eclipse.emf.ecore.EPackage;
63 import org.eclipse.emf.ecore.EReference;
64 import org.eclipse.emf.ecore.EStructuralFeature;
65 import org.eclipse.emf.ecore.impl.EAttributeImpl;
66 import org.eclipse.emf.ecore.impl.EEnumImpl;
67 import org.eclipse.emf.ecore.resource.Resource;
68 import org.eclipse.emf.ecore.resource.ResourceSet;
69 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
70 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
71 import org.json.JSONObject;
72 import org.onap.policy.rest.XacmlRestProperties;
73 import org.onap.policy.rest.dao.CommonClassDao;
74 import org.onap.policy.rest.jpa.DictionaryData;
75 import org.yaml.snakeyaml.Yaml;
79 public class MsModelUtils {
81 private static final Log logger = LogFactory.getLog(MsModelUtils.class);
84 private static final String BOOLEAN = "boolean";
85 private static final String CONFIGURATION = "configuration";
86 private static final String DATATYPE = "data_types.policy.data.";
87 private static final String DATA_TYPE = "data_types";
88 private static final String DEFAULT = ".default";
89 private static final String DEFAULTVALUE = ":defaultValue-";
90 private static final String DESCRIPTION = ".description";
91 private static final String DESCRIPTION_KEY = "description";
92 private static final String DESCRIPTION_TOKEN = ":description-";
93 private static final String DICTIONARY = "dictionary:";
94 private static final String DICTIONARYNAME = "dictionaryName";
95 private static final String ERROR = "error";
96 private static final String E_PROXY_URI = "eProxyURI:";
97 private static final String INTEGER = "integer";
98 private static final String JSON_MODEL = "JSON_MODEL";
99 private static final String LIST = "list";
100 private static final String MANYFALSE = ":MANY-false";
101 private static final String MANYTRUE = ":MANY-true";
102 private static final String MAP = "map";
103 private static final String MATCHABLE = ".matchable";
104 private static final String MATCHABLEKEY = "matchable";
105 private static final String MATCHINGTRUE = "matching-true";
106 private static final String NODE_TYPE = "node_types";
107 private static final String PROPERTIES = ".properties.";
108 private static final String PROPERTIES_KEY = "properties";
109 private static final String REQUIRED = ".required";
110 private static final String REQUIREDFALSE = ":required-false";
111 private static final String REQUIREDTRUE = ":required-true";
112 private static final String REQUIREDVALUE = ":required-";
113 private static final String STRING = "string";
114 private static final String TOSCA_DEFINITION_VERSION = "tosca_definitions_version";
115 private static final String TOSCA_SIMPLE_YAML_1_0_0 = "tosca_simple_yaml_1_0_0";
116 private static final String TYPE = ".type";
118 private static CommonClassDao commonClassDao;
120 private HashMap<String, MsAttributeObject> classMap = new HashMap<>();
121 private HashMap<String, String> enumMap = new HashMap<>();
122 private HashMap<String, String> matchingClass = new HashMap<>();
123 private String onap = "";
124 private String policy = "";
125 private List<String> orderedElements = new ArrayList<>();
126 private String dataOrderInfo = "";
127 private Set<String> uniqueDataKeys = new HashSet<>();
128 private Set<String> uniqueKeys = new HashSet<>();
129 private String listConstraints = null;
130 private String referenceAttributes;
131 private LinkedHashMap<String, Object> retmap = new LinkedHashMap<>();
132 private Map<String, String> matchableValues;
133 private StringBuilder dataListBuffer = new StringBuilder();
134 private List<String> dataConstraints = new ArrayList<>();
135 private String attributeString = null;
136 private boolean isDuplicatedAttributes = false;
137 private String jsonRuleFormation = null;
140 * The Enum AnnotationType.
142 private enum AnnotationType {
149 * The Enum ModelType.
151 public enum ModelType {
156 * The Enum SearchType.
158 public enum SearchType {
159 TOSCA_DEFINITION_VERSION,
160 TOSCA_SIMPLE_YAML_1_0_0,
167 * Instantiates a new ms model utils.
169 public MsModelUtils() {
170 // Default Constructor
174 * Instantiates a new ms model utils.
176 * @param commonClassDao the common class dao
178 public MsModelUtils(CommonClassDao commonClassDao) {
179 MsModelUtils.commonClassDao = commonClassDao;
183 * Instantiates a new ms model utils.
185 * @param onap the onap
186 * @param policy the policy
188 public MsModelUtils(String onap, String policy) {
190 this.policy = policy;
196 * @param file the file
197 * @param model the model
200 public Map<String, MsAttributeObject> processEpackage(String file, ModelType model) {
201 if (model == ModelType.XMI) {
202 processXmiEpackage(file);
208 * Process XMI epackage.
210 * @param xmiFile the xmi file
212 private void processXmiEpackage(String xmiFile) {
213 EPackage root = getEpackage(xmiFile);
214 TreeIterator<EObject> treeItr = root.eAllContents();
218 // Pulling out dependency from file
219 while (treeItr.hasNext()) {
220 EObject obj = treeItr.next();
221 if (obj instanceof EClassifier) {
222 EClassifier eclassifier = (EClassifier) obj;
223 className = eclassifier.getName();
225 if (obj instanceof EEnum) {
226 enumMap.putAll(getEEnum(obj));
227 } else if (obj instanceof EClass) {
228 String temp = getDependencyList(eclassifier).toString();
229 returnValue = StringUtils.replaceEach(temp, new String[]
230 { "[", "]" }, new String[]
232 getAttributes(className, returnValue, root);
237 if (!enumMap.isEmpty()) {
240 if (!matchingClass.isEmpty()) {
241 checkForMatchingClass();
246 * Check for matching class.
248 private void checkForMatchingClass() {
249 HashMap<String, String> tempAttribute = new HashMap<>();
251 for (Entry<String, String> set : matchingClass.entrySet()) {
252 String key = set.getKey();
253 if (classMap.containsKey(key)) {
254 Map<String, String> listAttributes = classMap.get(key).getAttribute();
255 Map<String, String> listRef = classMap.get(key).getRefAttribute();
256 for (Entry<String, String> eset : listAttributes.entrySet()) {
257 String key2 = eset.getKey();
258 tempAttribute.put(key2, MATCHINGTRUE);
260 for (Entry<String, String> eset : listRef.entrySet()) {
261 String key3 = eset.getKey();
262 tempAttribute.put(key3, MATCHINGTRUE);
266 updateMatching(tempAttribute, key);
274 * @param tempAttribute the temp attribute
277 private void updateMatching(HashMap<String, String> tempAttribute, String key) {
278 Map<String, MsAttributeObject> newClass = classMap;
280 for (Entry<String, MsAttributeObject> updateClass : newClass.entrySet()) {
281 Map<String, String> valueMap = updateClass.getValue().getMatchingSet();
282 String keymap = updateClass.getKey();
283 if (valueMap.containsKey(key)) {
284 Map<String, String> modifyMap = classMap.get(keymap).getMatchingSet();
285 modifyMap.remove(key);
286 modifyMap.putAll(tempAttribute);
287 classMap.get(keymap).setMatchingSet(modifyMap);
294 * Adds the enum class map.
296 private void addEnumClassMap() {
297 for (Entry<String, MsAttributeObject> value : classMap.entrySet()) {
298 value.getValue().setEnumType(enumMap);
305 * @param xmiFile the xmi file
306 * @return the epackage
308 private EPackage getEpackage(String xmiFile) {
309 ResourceSet resSet = new ResourceSetImpl();
310 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
311 Map<String, Object> objectMap = reg.getExtensionToFactoryMap();
312 objectMap.put("xmi", new XMIResourceFactoryImpl());
313 Resource resource = resSet.getResource(URI.createFileURI(xmiFile), true);
315 resource.load(Collections.emptyMap());
316 } catch (IOException e) {
317 logger.error("Error loading Encore Resource for new Model" + e);
320 return (EPackage) resource.getContents().get(0);
329 private HashMap<String, String> getEEnum(EObject obj) {
330 List<String> valueList = new ArrayList<>();
331 HashMap<String, String> returnMap = new HashMap<>();
332 EEnum eenum = (EEnum) obj;
334 String name = eenum.getName();
335 for (EEnumLiteral enumLiteral : eenum.getELiterals()) {
336 Enumerator instance = enumLiteral.getInstance();
337 String value = instance.getLiteral();
338 valueList.add(value);
340 returnMap.put(name, valueList.toString());
345 * Gets the attributes.
347 * @param className the class name
348 * @param dependency the dependency
349 * @param root the root
351 public void getAttributes(String className, String dependency, EPackage root) {
352 List<String> dpendList = new ArrayList<>();
353 if (dependency != null) {
354 dpendList = new ArrayList<>(Arrays.asList(dependency.split(",")));
356 MsAttributeObject msAttributeObject = new MsAttributeObject();
357 msAttributeObject.setClassName(className);
358 String extendClass = getSubTypes(root, className);
359 Map<String, String> returnRefList = getRefAttributeList(root, className, extendClass);
360 Map<String, String> returnAttributeList = getAttributeList(root, className, extendClass);
361 Map<String, Object> returnSubList = getSubAttributeList(root, className, extendClass);
362 HashMap<String, String> returnAnnotation = getAnnotation(root, className, extendClass);
363 msAttributeObject.setAttribute(returnAttributeList);
364 msAttributeObject.setRefAttribute(returnRefList);
365 msAttributeObject.setSubClass(returnSubList);
366 msAttributeObject.setDependency(dpendList.toString());
367 msAttributeObject.addMatchingSet(returnAnnotation);
368 msAttributeObject.setPolicyTempalate(isPolicyTemplate(root, className));
370 this.classMap.put(className, msAttributeObject);
374 * Gets the annotation.
376 * @param root the root
377 * @param className the class name
378 * @param extendClass the extend class
379 * @return the annotation
381 private HashMap<String, String> getAnnotation(EPackage root, String className, String extendClass) {
382 TreeIterator<EObject> treeItr = root.eAllContents();
383 boolean requiredAttribute = false;
384 boolean requiredMatchAttribute = false;
385 HashMap<String, String> annotationSet = new HashMap<>();
387 // Pulling out dependency from file
388 while (treeItr.hasNext()) {
389 EObject obj = treeItr.next();
390 if (obj instanceof EClassifier) {
391 requiredAttribute = isRequiredAttribute(obj, className);
392 requiredMatchAttribute = isRequiredAttribute(obj, extendClass);
395 if (requiredAttribute) {
396 if (obj instanceof EStructuralFeature) {
397 checkAnnotation(annotationSet, (EStructuralFeature) obj);
399 } else if (requiredMatchAttribute && (obj instanceof EStructuralFeature)) {
400 findMatchingAnnotation(annotationSet, obj);
403 return annotationSet;
407 * Find matching annotation.
409 * @param annotationSet the annotation set
412 private void findMatchingAnnotation(HashMap<String, String> annotationSet, EObject obj) {
413 EStructuralFeature estrucClassifier = (EStructuralFeature) obj;
414 if (estrucClassifier.getEAnnotations().isEmpty()) {
417 String matching = annotationValue(estrucClassifier, AnnotationType.MATCHING, policy);
418 if (matching != null) {
419 if (obj instanceof EReference) {
420 EClass refType = ((EReference) obj).getEReferenceType();
421 annotationSet.put(refType.getName(), matching);
422 matchingClass.put(refType.getName(), matching);
424 annotationSet.put(estrucClassifier.getName(), matching);
433 * @param annotationSet the annotation set
436 private void checkAnnotation(HashMap<String, String> annotationSet, EStructuralFeature obj) {
437 EStructuralFeature estrucClassifier = obj;
438 if (estrucClassifier.getEAnnotations().isEmpty()) {
441 String matching = annotationValue(estrucClassifier, AnnotationType.MATCHING, policy);
442 if (matching != null) {
443 annotationSet.put(estrucClassifier.getName(), matching);
445 String range = annotationValue(estrucClassifier, AnnotationType.VALIDATION, policy);
447 annotationSet.put(estrucClassifier.getName(), range);
449 String annotationDict = annotationValue(estrucClassifier, AnnotationType.DICTIONARY, policy);
450 if (annotationDict != null) {
451 annotationSet.put(estrucClassifier.getName(), annotationDict);
456 * Gets the sub attribute list.
458 * @param root the root
459 * @param className the class name
460 * @param superClass the super class
461 * @return the sub attribute list
463 private Map<String, Object> getSubAttributeList(EPackage root, String className, String superClass) {
464 TreeIterator<EObject> treeItr = root.eAllContents();
465 boolean requiredAttribute = false;
466 Map<String, Object> subAttribute = new HashMap<>();
467 int rollingCount = 0;
468 int processClass = 0;
470 // Pulling out dependency from file
471 while (treeItr.hasNext() && rollingCount < 2) {
473 EObject obj = treeItr.next();
474 if (obj instanceof EClassifier) {
475 requiredAttribute = isRequiredAttribute(obj, className) || isRequiredAttribute(obj, superClass);
476 if (requiredAttribute) {
479 rollingCount = rollingCount + processClass;
482 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
483 EStructuralFeature estrucClassifier = (EStructuralFeature) obj;
484 if (!estrucClassifier.getEAnnotations().isEmpty()) {
485 updateSubAttributes(subAttribute, obj, estrucClassifier);
493 * Update sub attributes.
495 * @param subAttribute the sub attribute
497 * @param estrucClassifier the e struc classifier
499 private void updateSubAttributes(Map<String, Object> subAttribute, EObject obj,
500 EStructuralFeature estrucClassifier) {
501 if (!(obj instanceof EReference)) {
504 if (annotationTest(estrucClassifier, CONFIGURATION, onap)) {
505 EClass refType = ((EReference) obj).getEReferenceType();
506 if (!refType.toString().contains(E_PROXY_URI)) {
507 String required = REQUIREDFALSE;
508 if (estrucClassifier.getLowerBound() == 1) {
509 required = REQUIREDTRUE;
511 subAttribute.put(estrucClassifier.getName(), refType.getName() + required);
517 * Check defult value.
519 * @param defultValue the defult value
522 public String checkDefultValue(String defultValue) {
523 if (defultValue != null) {
524 return DEFAULTVALUE + defultValue;
526 return ":defaultValue-NA";
531 * Check required pattern.
533 * @param upper the upper
534 * @param lower the lower
537 public String checkRequiredPattern(int upper, int lower) {
538 String pattern = XACMLProperties.getProperty(XacmlRestProperties.PROP_XCORE_REQUIRED_PATTERN);
539 if (pattern != null && upper == Integer.parseInt(pattern.split(",")[1])
540 && lower == Integer.parseInt(pattern.split(",")[0])) {
543 return REQUIREDFALSE;
547 * Builds the java object.
550 * @return the JSON object
552 public JSONObject buildJavaObject(Map<String, String> map) {
553 return new JSONObject(map);
557 * Gets the ref attribute list.
559 * @param root the root
560 * @param className the class name
561 * @param superClass the super class
562 * @return the ref attribute list
564 public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass) {
566 TreeIterator<EObject> treeItr = root.eAllContents();
567 boolean requiredAttribute = false;
568 HashMap<String, String> refAttribute = new HashMap<>();
569 int rollingCount = 0;
570 int processClass = 0;
572 // Pulling out dependency from file
573 while (treeItr.hasNext()) {
574 EObject obj = treeItr.next();
575 if (obj instanceof EClassifier) {
576 requiredAttribute = isRequiredAttribute(obj, className) || isRequiredAttribute(obj, superClass);
577 if (requiredAttribute) {
580 rollingCount = rollingCount + processClass;
583 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
584 EStructuralFeature estrucClassifier = (EStructuralFeature) obj;
585 if (!estrucClassifier.getEAnnotations().isEmpty()) {
586 annotation = annotationTest(estrucClassifier, CONFIGURATION, onap);
587 if (annotation && obj instanceof EReference) {
588 updRefAttributes(refAttribute, (EStructuralFeature) obj, estrucClassifier);
589 } else if (annotation && obj instanceof EAttributeImpl) {
590 updEnumTypeRefAttrib(refAttribute, (EStructuralFeature) obj, estrucClassifier);
600 * Upd enum type ref attrib.
602 * @param refAttribute the ref attribute
604 * @param estrucClassifier the e struc classifier
606 private void updEnumTypeRefAttrib(HashMap<String, String> refAttribute, EStructuralFeature obj,
607 EStructuralFeature estrucClassifier) {
608 EClassifier refType = ((EAttributeImpl) obj).getEType();
609 if (!(refType instanceof EEnumImpl)) {
613 String array = arrayCheck(obj.getUpperBound());
614 String required = REQUIREDFALSE;
615 if (obj.getLowerBound() == 1) {
616 required = REQUIREDTRUE;
618 refAttribute.put(estrucClassifier.getName(), refType.getName() + array + required);
622 * Upd ref attributes.
624 * @param refAttribute the ref attribute
626 * @param estrucClassifier the e struc classifier
628 private void updRefAttributes(HashMap<String, String> refAttribute, EStructuralFeature obj,
629 EStructuralFeature estrucClassifier) {
630 EClass refType = ((EReference) obj).getEReferenceType();
631 if (refType.toString().contains(E_PROXY_URI)) {
632 String one = refType.toString().split(E_PROXY_URI)[1];
633 String refValue = StringUtils.replaceEach(one.split("#")[1], new String[]
634 { "//", ")" }, new String[]
636 refAttribute.put(estrucClassifier.getName(), refValue);
638 String required = REQUIREDFALSE;
639 if (obj.getLowerBound() == 1) {
640 required = REQUIREDTRUE;
642 refAttribute.put(estrucClassifier.getName(),
643 refType.getName() + arrayCheck(obj.getUpperBound()) + required);
650 * @param estrucClassifier the e struc classifier
651 * @param annotation the annotation
652 * @param type the type
653 * @return true, if successful
655 private boolean annotationTest(EStructuralFeature estrucClassifier, String annotation, String type) {
656 String annotationType;
657 EAnnotation eannotation;
661 EList<EAnnotation> value = estrucClassifier.getEAnnotations();
663 for (int i = 0; i < value.size(); i++) {
664 annotationType = value.get(i).getSource();
665 eannotation = estrucClassifier.getEAnnotations().get(i);
666 onapType = eannotation.getDetails().get(0).getValue();
667 onapValue = eannotation.getDetails().get(0).getKey();
669 if (annotationType.contains(type) && onapType.contains(annotation)) {
673 if (annotationType.contains(type) && onapValue.contains(annotation)) {
684 * @param estrucClassifier the e struc classifier
685 * @param annotation the annotation
686 * @param type the type
689 private String annotationValue(EStructuralFeature estrucClassifier, AnnotationType annotation, String type) {
690 String annotationType;
691 EAnnotation eannotation;
693 String onapValue = null;
695 EList<EAnnotation> value = estrucClassifier.getEAnnotations();
697 for (int i = 0; i < value.size(); i++) {
698 annotationType = value.get(i).getSource();
699 eannotation = estrucClassifier.getEAnnotations().get(i);
700 onapType = eannotation.getDetails().get(0).getKey();
701 if (annotationType.contains(type) && onapType.compareToIgnoreCase(annotation.toString()) == 0) {
702 onapValue = eannotation.getDetails().get(0).getValue();
703 if (annotation == AnnotationType.VALIDATION) {
706 return onapType + "-" + onapValue;
715 * Checks if is required attribute.
718 * @param className the class name
719 * @return true, if is required attribute
721 public boolean isRequiredAttribute(EObject obj, String className) {
722 EClassifier eclassifier = (EClassifier) obj;
723 String workingClass = eclassifier.getName().trim();
724 return workingClass.equalsIgnoreCase(className);
728 * Checks if is policy template.
730 * @param root the root
731 * @param className the class name
732 * @return true, if is policy template
734 private boolean isPolicyTemplate(EPackage root, String className) {
735 boolean result = false;
736 for (EClassifier classifier : root.getEClassifiers()) {
737 if (classifier instanceof EClass) {
738 EClass eclass = (EClass) classifier;
739 if (eclass.getName().contentEquals(className)) {
740 result = checkPolicyTemplate(eclass);
749 * Check policy template.
751 * @param eclass the e class
752 * @return true, if successful
754 private boolean checkPolicyTemplate(EClass eclass) {
755 EList<EAnnotation> value = eclass.getEAnnotations();
756 for (EAnnotation workingValue : value) {
757 EMap<String, String> keyMap = workingValue.getDetails();
758 if (keyMap.containsKey("policyTemplate")) {
766 * Gets the sub types.
768 * @param root the root
769 * @param className the class name
770 * @return the sub types
772 private String getSubTypes(EPackage root, String className) {
773 String returnSubTypes = null;
774 for (EClassifier classifier : root.getEClassifiers()) {
775 if (classifier instanceof EClass) {
776 returnSubTypes = findSubTypes(className, returnSubTypes, (EClass) classifier);
779 return returnSubTypes;
785 * @param className the class name
786 * @param returnSubTypes the return sub types
787 * @param classifier the classifier
790 private String findSubTypes(String className, String returnSubTypes, EClass classifier) {
791 EClass eclass = classifier;
793 for (EClass esuperType : eclass.getEAllSuperTypes()) {
794 if (eclass.getName().contentEquals(className)) {
795 returnSubTypes = esuperType.getName();
798 return returnSubTypes;
802 * Gets the attribute list.
804 * @param root the root
805 * @param className the class name
806 * @param superClass the super class
807 * @return the attribute list
809 public Map<String, String> getAttributeList(EPackage root, String className, String superClass) {
811 TreeIterator<EObject> treeItr = root.eAllContents();
812 boolean requiredAttribute = false;
813 HashMap<String, String> refAttribute = new HashMap<>();
815 // Pulling out dependency from file
816 while (treeItr.hasNext()) {
817 EObject obj = treeItr.next();
818 if (obj instanceof EClassifier) {
819 requiredAttribute = isRequiredAttribute(obj, className) || isRequiredAttribute(obj, superClass);
822 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
823 EStructuralFeature estrucClassifier = (EStructuralFeature) obj;
824 if (!estrucClassifier.getEAnnotations().isEmpty()) {
825 checkStrucClassifier(refAttribute, obj, estrucClassifier);
834 * Check struc classifier.
836 * @param refAttribute the ref attribute
838 * @param estrucClassifier the e struc classifier
840 private void checkStrucClassifier(HashMap<String, String> refAttribute, EObject obj,
841 EStructuralFeature estrucClassifier) {
842 EClassifier refType = ((EStructuralFeature) obj).getEType();
843 boolean annotation = annotationTest(estrucClassifier, CONFIGURATION, onap);
844 boolean dictionaryTest = annotationTest(estrucClassifier, DICTIONARY, policy);
845 if (annotation && !(obj instanceof EReference) && !(refType instanceof EEnumImpl)) {
846 updEReferenceAttrib(refAttribute, dictionaryTest, (EStructuralFeature) obj, estrucClassifier);
851 * Upd E reference attrib.
853 * @param refAttribute the ref attribute
854 * @param dictionaryTest the dictionary test
856 * @param estrucClassifier the e struc classifier
858 private void updEReferenceAttrib(HashMap<String, String> refAttribute, boolean dictionaryTest,
859 EStructuralFeature obj, EStructuralFeature estrucClassifier) {
861 String name = estrucClassifier.getName();
862 if (dictionaryTest) {
863 etype = annotationValue(estrucClassifier, AnnotationType.DICTIONARY, policy);
865 etype = estrucClassifier.getEType().getInstanceClassName();
867 String defaultValue = checkDefultValue(obj.getDefaultValueLiteral());
868 String array = arrayCheck(obj.getUpperBound());
869 String required = checkRequiredPattern(obj.getUpperBound(), obj.getLowerBound());
870 refAttribute.put(name, etype + defaultValue + required + array);
876 * @param upperBound the upper bound
879 public String arrayCheck(int upperBound) {
881 if (upperBound == -1) {
889 * Gets the dependency list.
891 * @param eclassifier the e classifier
892 * @return the dependency list
894 public List<String> getDependencyList(EClassifier eclassifier) {
895 List<String> returnValue = new ArrayList<>();
897 EList<EClass> somelist = ((EClass) eclassifier).getEAllSuperTypes();
898 if (somelist.isEmpty()) {
901 for (EClass depend : somelist) {
902 if (depend.toString().contains(E_PROXY_URI)) {
903 String one = depend.toString().split(E_PROXY_URI)[1];
904 String value = StringUtils.replaceEach(one.split("#")[1], new String[]
905 { "//", ")" }, new String[]
907 returnValue.add(value);
915 * Builds the sub list.
917 * @param subClassAttributes the sub class attributes
918 * @param classMap the class map
919 * @param className the class name
922 public Map<String, String> buildSubList(Map<String, String> subClassAttributes,
923 Map<String, MsAttributeObject> classMap, String className) {
924 Map<String, String> missingValues = new HashMap<>();
925 Map<String, String> workingMap;
928 for (Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()) {
929 String value = map.getValue().split(":")[0];
931 classMap.get(className).getEnumType();
932 enumType = classMap.get(className).getEnumType().containsKey(value);
934 workingMap = classMap.get(value).getRefAttribute();
935 for (Entry<String, String> subMab : workingMap.entrySet()) {
936 String value2 = subMab.getValue().split(":")[0];
937 if (!subClassAttributes.containsValue(value2)) {
938 missingValues.put(subMab.getKey(), subMab.getValue());
946 return missingValues;
950 * Recursive reference.
952 * @param classMap the class map
953 * @param className the class name
956 public Map<String, Map<String, String>> recursiveReference(Map<String, MsAttributeObject> classMap,
959 Map<String, Map<String, String>> returnObject = new HashMap<>();
960 Map<String, String> returnClass = getRefclass(classMap, className);
961 returnObject.put(className, returnClass);
962 for (Entry<String, String> reAttribute : returnClass.entrySet()) {
963 if (reAttribute.getValue().split(":")[1].contains("MANY")
964 && classMap.get(reAttribute.getValue().split(":")[0]) != null) {
965 returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0]));
977 * @param classMap the class map
978 * @param className the class name
981 public String createJson(Map<String, MsAttributeObject> classMap, String className) {
983 Map<String, Map<String, String>> myObject = new HashMap<>();
984 for (Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()) {
985 String value = map.getValue().split(":")[0];
987 enumType = classMap.get(className).getEnumType().containsKey(value);
988 if (!enumType && map.getValue().split(":")[1].contains("MANY")) {
989 Map<String, Map<String, String>> testRecursive = recursiveReference(classMap,
990 map.getValue().split(":")[0]);
991 myObject.putAll(testRecursive);
996 Gson gson = new Gson();
997 return gson.toJson(myObject);
1001 * Gets the refclass.
1003 * @param classMap the class map
1004 * @param className the class name
1005 * @return the refclass
1007 public Map<String, String> getRefclass(Map<String, MsAttributeObject> classMap, String className) {
1008 HashMap<String, String> missingValues = new HashMap<>();
1010 if (classMap.get(className).getAttribute() != null || !classMap.get(className).getAttribute().isEmpty()) {
1011 missingValues.putAll(classMap.get(className).getAttribute());
1014 if (classMap.get(className).getRefAttribute() != null || !classMap.get(className).getRefAttribute().isEmpty()) {
1015 missingValues.putAll(classMap.get(className).getRefAttribute());
1018 return missingValues;
1022 * Creates the sub attributes.
1024 * @param dependency the dependency
1025 * @param classMap the class map
1026 * @param modelName the model name
1027 * @return the string
1029 public String createSubAttributes(List<String> dependency, Map<String, MsAttributeObject> classMap,
1032 HashMap<String, Object> workingMap = new HashMap<>();
1033 MsAttributeObject tempObject;
1034 if (dependency != null) {
1035 if (dependency.isEmpty()) {
1038 dependency.add(modelName);
1039 for (String element : dependency) {
1040 tempObject = classMap.get(element);
1041 if (tempObject != null) {
1042 workingMap.putAll(classMap.get(element).getSubClass());
1047 return createJson(classMap, modelName);
1051 * Gets the full dependency list.
1053 * @param dependency the dependency
1054 * @param classMap the class map
1055 * @return the full dependency list
1057 public List<String> getFullDependencyList(List<String> dependency, Map<String, MsAttributeObject> classMap) {
1058 ArrayList<String> returnList = new ArrayList<>();
1059 ArrayList<String> workingList;
1060 returnList.addAll(dependency);
1061 for (String element : dependency) {
1062 if (classMap.containsKey(element)) {
1063 MsAttributeObject value = classMap.get(element);
1064 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]
1065 { "[", "]" }, new String[]
1067 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
1068 for (String depend : workingList) {
1069 updDependencyList(returnList, depend);
1078 * Upd dependency list.
1080 * @param returnList the return list
1081 * @param depend the depend
1083 private void updDependencyList(ArrayList<String> returnList, String depend) {
1084 if (!returnList.contains(depend) && !depend.isEmpty()) {
1085 returnList.add(depend.trim());
1090 * Parses the TOSCA model.
1092 * @param fileName the file name
1093 * @return the string
1095 public String parseTosca(String fileName) {
1096 Map<String, String> map = new LinkedHashMap<>();
1098 map = load(fileName);
1099 if (map != null && map.get(ERROR) != null) {
1100 return map.get(ERROR);
1102 parseDataAndPolicyNodes(map);
1103 LinkedHashMap<String, String> dataMapForJson = parseDataNodes(map);
1104 constructJsonForDataFields(dataMapForJson);
1105 LinkedHashMap<String, LinkedHashMap<String, String>> mapKey = parsePolicyNodes(map);
1106 createAttributes(mapKey);
1108 } catch (IOException e) {
1110 } catch (ParserException e) {
1112 return e.getMessage();
1121 * @param fileName the file name
1123 * @throws IOException Signals that an I/O exception has occurred.
1124 * @throws ParserException the parser exception
1126 public Map<String, String> load(String fileName) throws IOException, ParserException {
1127 File newConfiguration = new File(fileName);
1128 StringBuilder orderInfo = new StringBuilder("[");
1129 Yaml yaml = new Yaml();
1130 LinkedHashMap<Object, Object> yamlMap = null;
1131 try (InputStream is = new FileInputStream(newConfiguration)) {
1132 yamlMap = yaml.load(is);
1133 } catch (FileNotFoundException e) {
1135 } catch (Exception e) {
1137 throw new ParserException("Invalid TOSCA Model format. Please make sure it is a valid YAML file");
1140 LinkedHashMap<String, String> settings = new LinkedHashMap<>();
1141 if (yamlMap == null) {
1145 String message = validations(yamlMap);
1147 if (message != null) {
1148 settings.put(ERROR, message);
1154 if (!isDuplicatedAttributes && orderedElements != null && !orderedElements.isEmpty()) {
1155 orderedElements.stream().forEach(string -> {
1156 orderInfo.append(string);
1157 orderInfo.append(",");
1158 logger.info("Content: " + string);
1161 orderInfo.append("]");
1163 dataOrderInfo = orderInfo.toString();
1164 dataOrderInfo = dataOrderInfo.replace(",]", "]");
1166 logger.info("dataOrderInfo :" + dataOrderInfo);
1169 List<String> path = new ArrayList<>();
1170 serializeMap(settings, new StringBuilder(), path, yamlMap);
1177 * @param yamlMap the yaml map
1178 * @return the string
1180 @SuppressWarnings("unchecked")
1181 private String validations(@SuppressWarnings("rawtypes") Map yamlMap) {
1183 boolean isNoteTypeFound = false;
1184 boolean isDataTypeFound = false;
1185 boolean isToscaVersionKeyFound = false;
1186 boolean isToscaVersionValueFound = false;
1187 @SuppressWarnings("rawtypes")
1188 Map m1 = new HashMap();
1190 if (yamlMap != null) {
1191 // Get a set of the entries
1192 @SuppressWarnings("rawtypes")
1193 Set<Entry> entries = yamlMap.entrySet();
1194 for (Map.Entry<Object, Object> me : entries) {
1195 if (TOSCA_SIMPLE_YAML_1_0_0.equals(me.getValue())) {
1196 isToscaVersionValueFound = true;
1199 switch (me.getKey().toString()) {
1200 case TOSCA_DEFINITION_VERSION:
1201 isToscaVersionKeyFound = true;
1203 m1.put(TOSCA_DEFINITION_VERSION, order);
1206 isNoteTypeFound = true;
1208 m1.put(NODE_TYPE, order);
1211 isDataTypeFound = true;
1213 m1.put(DATA_TYPE, order);
1216 setJsonRuleFormation(me.getValue().toString());
1222 if (!isDataTypeFound) {
1223 return "data_types are missing or invalid.";
1225 if (!isToscaVersionKeyFound || !isToscaVersionValueFound) {
1226 return "tosca_definitions_version is missing or invalid.";
1229 if (!isNoteTypeFound) {
1230 return "node_types are missing or invalid.";
1233 short version = (short) m1.get(TOSCA_DEFINITION_VERSION);
1236 return "tosca_definitions_version should be defined first.";
1239 short data = (short) m1.get(DATA_TYPE);
1240 short node = (short) m1.get(NODE_TYPE);
1241 if (isDataTypeFound && node > data) {
1242 return "node_types should be defined before data_types.";
1253 * @param settings the settings
1255 * @param path the path
1256 * @param yamlMap the yaml map
1259 { "unchecked", "rawtypes" })
1260 private void serializeMap(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
1261 Map<Object, Object> yamlMap) {
1262 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
1264 if (entry.getValue() instanceof Map) {
1265 path.add((String) entry.getKey());
1266 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
1267 path.remove(path.size() - 1);
1268 } else if (entry.getValue() instanceof List) {
1269 path.add((String) entry.getKey());
1270 serializeList(settings, sb, path, (List) entry.getValue());
1271 path.remove(path.size() - 1);
1273 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
1281 * @param settings the settings
1283 * @param path the path
1284 * @param yamlList the yaml list
1286 @SuppressWarnings("unchecked")
1287 private void serializeList(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
1288 List<String> yamlList) {
1290 for (Object listEle : yamlList) {
1291 if (listEle instanceof Map) {
1292 path.add(Integer.toString(counter));
1293 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
1294 path.remove(path.size() - 1);
1295 } else if (listEle instanceof List) {
1296 path.add(Integer.toString(counter));
1297 serializeList(settings, sb, path, (List<String>) listEle);
1298 path.remove(path.size() - 1);
1300 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
1309 * @param settings the settings
1311 * @param path the path
1312 * @param name the name
1313 * @param value the value
1315 private void serializeValue(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
1316 String name, Object value) {
1317 if (value == null) {
1321 for (String pathEle : path) {
1322 sb.append(pathEle).append('.');
1325 settings.put(sb.toString(), value.toString());
1329 * Parses the data and policy nodes.
1331 * @param map the map
1333 void parseDataAndPolicyNodes(Map<String, String> map) {
1334 for (String key : map.keySet()) {
1335 if (key.contains("policy.nodes.Root")) {
1337 } else if (key.contains("policy.nodes")) {
1338 String wordToFind = "policy.nodes.";
1339 int indexForPolicyNode = key.indexOf(wordToFind);
1340 String subNodeString = key.substring(indexForPolicyNode + 13, key.length());
1342 stringBetweenDots(subNodeString);
1343 } else if (key.contains("policy.data")) {
1344 String wordToFind = "policy.data.";
1345 int indexForPolicyNode = key.indexOf(wordToFind);
1346 String subNodeString = key.substring(indexForPolicyNode + 12, key.length());
1348 stringBetweenDotsForDataFields(subNodeString);
1354 * String between dots.
1356 * @param str the str
1359 // Second index of dot should be returned.
1360 public int stringBetweenDots(String str) {
1361 String stringToSearch = str;
1362 String[] ss = stringToSearch.split("\\.");
1364 int len = ss.length;
1366 uniqueKeys.add(ss[2]);
1370 return uniqueKeys.size();
1374 * String between dots for data fields.
1376 * @param str the str
1378 public void stringBetweenDotsForDataFields(String str) {
1379 String stringToSearch = str;
1380 String[] ss = stringToSearch.split("\\.");
1382 int len = ss.length;
1385 uniqueDataKeys.add(ss[0] + "%" + ss[2]);
1391 * Construct json for data fields.
1393 * @param dataMapForJson the data map for json
1395 void constructJsonForDataFields(LinkedHashMap<String, String> dataMapForJson) {
1396 LinkedHashMap<String, LinkedHashMap<String, String>> dataMapKey = new LinkedHashMap<>();
1397 LinkedHashMap<String, String> hmSub;
1398 for (Map.Entry<String, String> entry : dataMapForJson.entrySet()) {
1399 String uniqueDataKey = entry.getKey();
1400 String[] uniqueDataKeySplit = uniqueDataKey.split("%");
1401 String value = dataMapForJson.get(uniqueDataKey);
1402 if (dataMapKey.containsKey(uniqueDataKeySplit[0])) {
1403 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
1404 hmSub.put(uniqueDataKeySplit[1], value);
1406 hmSub = new LinkedHashMap<>();
1407 hmSub.put(uniqueDataKeySplit[1], value);
1410 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
1413 JSONObject mainObject = new JSONObject();
1415 for (Map.Entry<String, LinkedHashMap<String, String>> entry : dataMapKey.entrySet()) {
1416 String keyString = entry.getKey();
1417 json = new JSONObject();
1418 HashMap<String, String> jsonHm = dataMapKey.get(keyString);
1419 for (Map.Entry<String, String> entryMap : jsonHm.entrySet()) {
1420 String key = entryMap.getKey();
1421 json.put(key, jsonHm.get(key));
1423 mainObject.put(keyString, json);
1425 Iterator<String> keysItr = mainObject.keys();
1426 while (keysItr.hasNext()) {
1427 String key = keysItr.next();
1428 String value = mainObject.get(key).toString();
1429 retmap.put(key, value);
1432 logger.info("#############################################################################");
1433 logger.info(mainObject);
1434 logger.info("###############################################################################");
1438 * Parses the data nodes.
1440 * @param map the map
1441 * @return the linked hash map
1443 LinkedHashMap<String, String> parseDataNodes(Map<String, String> map) {
1444 LinkedHashMap<String, String> dataMapForJson = new LinkedHashMap<>();
1445 matchableValues = new HashMap<>();
1446 for (String uniqueDataKey : uniqueDataKeys) {
1447 if (uniqueDataKey.contains("%")) {
1448 String[] uniqueDataKeySplit = uniqueDataKey.split("%");
1449 String findType = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + TYPE;
1450 String typeValue = map.get(findType);
1451 logger.info(typeValue);
1453 String findRequired = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + REQUIRED;
1454 String requiredValue = map.get(findRequired);
1456 String matchable = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + MATCHABLE;
1458 String matchableValue = map.get(matchable);
1460 if ("true".equalsIgnoreCase(matchableValue)) {
1461 String key = uniqueDataKeySplit[uniqueDataKeySplit.length - 1];
1462 matchableValues.put(key, MATCHINGTRUE);
1465 if (requiredValue == null || requiredValue.isEmpty()) {
1466 requiredValue = "false";
1468 if (INTEGER.equalsIgnoreCase(typeValue) || STRING.equalsIgnoreCase(typeValue)
1469 || typeValue.equalsIgnoreCase(BOOLEAN)) {
1470 String findDefault = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1472 String defaultValue = map.get(findDefault);
1473 logger.info("defaultValue is:" + defaultValue);
1474 logger.info("requiredValue is:" + requiredValue);
1476 StringBuilder attributeIndividualStringBuilder = new StringBuilder();
1477 attributeIndividualStringBuilder.append(typeValue + DEFAULTVALUE);
1478 attributeIndividualStringBuilder.append(defaultValue + REQUIREDVALUE);
1479 attributeIndividualStringBuilder.append(requiredValue + MANYFALSE);
1481 String findDescription = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1483 attributeIndividualStringBuilder.append(DESCRIPTION_TOKEN + map.get(findDescription));
1484 dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
1485 } else if (LIST.equalsIgnoreCase(typeValue) || MAP.equalsIgnoreCase(typeValue)) {
1486 logger.info("requiredValue is:" + requiredValue);
1487 String findList = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1488 + ".entry_schema.type";
1489 String findDefaultValue = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1490 + ".entry_schema.default";
1491 String findDescription = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1492 + ".entry_schema.description";
1493 String listValue = map.get(findList);
1494 String defaultValue = map.get(findDefaultValue);
1495 String description = map.get(findDescription);
1496 if (listValue != null) {
1497 logger.info("Type of list is:" + listValue);
1499 if (listValue.contains(".")) {
1500 String trimValue = listValue.substring(listValue.lastIndexOf('.') + 1);
1501 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1502 referenceIndividualStringBuilder.append(trimValue + REQUIREDVALUE);
1503 referenceIndividualStringBuilder.append(requiredValue + MANYTRUE);
1504 referenceIndividualStringBuilder.append(DESCRIPTION_TOKEN + description);
1505 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1506 } else { // Its string
1507 StringBuilder stringListItems = new StringBuilder();
1508 if (LIST.equalsIgnoreCase(typeValue)) {
1509 stringListItems.append(uniqueDataKeySplit[1].toUpperCase() + DEFAULTVALUE + defaultValue
1510 + REQUIREDVALUE + requiredValue + MANYFALSE + DESCRIPTION_TOKEN
1512 } else if (MAP.equalsIgnoreCase(typeValue)) {
1513 stringListItems.append(uniqueDataKeySplit[1].toUpperCase() + DEFAULTVALUE + defaultValue
1514 + REQUIREDVALUE + requiredValue + MANYTRUE + DESCRIPTION_TOKEN
1517 dataMapForJson.put(uniqueDataKey, stringListItems.toString());
1518 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase() + "=[");
1519 for (int i = 0; i < 10; i++) {
1520 String findConstraints = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES
1521 + uniqueDataKeySplit[1] + ".entry_schema.constraints.0.valid_values."
1523 String constraintsValue = map.get(findConstraints);
1524 logger.info(constraintsValue);
1525 boolean ruleCheck = false;
1526 if (constraintsValue == null) {
1528 } else if (constraintsValue.startsWith(DICTIONARY)) {
1529 List<Object> dictFromDB = null;
1530 String[] dictionaryNameValRule;
1531 String[] dictionaryName = constraintsValue.split(":");
1532 String dictionaryNameVal = dictionaryName[1];
1533 if (dictionaryNameVal.contains("#Rules")) {
1535 dictionaryNameValRule = dictionaryNameVal.split("#");
1536 dictFromDB = commonClassDao.getDataById(DictionaryData.class, DICTIONARYNAME,
1537 dictionaryNameValRule[0]);
1539 dictFromDB = commonClassDao.getDataById(DictionaryData.class, DICTIONARYNAME,
1542 if (dictFromDB != null && !dictFromDB.isEmpty()) {
1543 DictionaryData data = (DictionaryData) dictFromDB.get(0);
1545 constraintsValue = DICTIONARY + data.getDictionaryUrl() + "@"
1546 + data.getDictionaryDataByName() + "&Rule";
1548 constraintsValue = DICTIONARY + data.getDictionaryUrl() + "@"
1549 + data.getDictionaryDataByName();
1552 dataListBuffer.append(constraintsValue + ",");
1554 logger.info("constraintsValue => " + constraintsValue);
1555 if (constraintsValue.contains("=")) {
1556 constraintsValue = constraintsValue.replace("=", "equal-sign");
1558 dataConstraints.add(constraintsValue);
1559 dataListBuffer.append(constraintsValue + ",");
1562 dataListBuffer.append("]#");
1563 logger.info(dataListBuffer);
1567 String findUserDefined = DATATYPE + uniqueDataKeySplit[0] + "." + PROPERTIES_KEY + "."
1568 + uniqueDataKeySplit[1] + TYPE;
1569 String findDescription = DATATYPE + uniqueDataKeySplit[0] + "." + PROPERTIES_KEY + "."
1570 + uniqueDataKeySplit[1] + DESCRIPTION;
1571 String userDefinedValue = map.get(findUserDefined);
1572 String description = map.get(findDescription);
1573 String trimValue = userDefinedValue.substring(userDefinedValue.lastIndexOf('.') + 1);
1574 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1575 referenceIndividualStringBuilder.append(trimValue + REQUIREDVALUE);
1576 referenceIndividualStringBuilder.append(requiredValue + MANYFALSE);
1577 referenceIndividualStringBuilder.append(DESCRIPTION_TOKEN + description);
1578 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1582 matchableValues.put(uniqueDataKey, MATCHINGTRUE);
1586 return dataMapForJson;
1590 * Parses the policy nodes.
1592 * @param map the map
1593 * @return the linked hash map
1594 * @throws ParserException the parser exception
1596 LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String, String> map)
1597 throws ParserException {
1598 LinkedHashMap<String, LinkedHashMap<String, String>> mapKey = new LinkedHashMap<>();
1599 for (String uniqueKey : uniqueKeys) {
1600 LinkedHashMap<String, String> hm;
1602 for (Entry<String, String> entry : map.entrySet()) {
1603 String key = entry.getKey();
1604 if (key.contains(uniqueKey) && key.contains("policy.nodes")) {
1605 if (mapKey.containsKey(uniqueKey)) {
1606 hm = mapKey.get(uniqueKey);
1607 String keyStr = key.substring(key.lastIndexOf('.') + 1);
1608 String valueStr = map.get(key);
1609 if ("type".equalsIgnoreCase(keyStr) && key.contains("entry_schema.0.type")
1610 || key.contains("entry_schema.type") && valueStr.contains("policy.data.")) {
1611 throw new ParserException("For user defined object type,"
1612 + " Please make sure no space between 'type:' and object " + valueStr);
1615 if ("type".equals(keyStr)) {
1616 if (!key.contains("entry_schema")) {
1617 hm.put(keyStr, valueStr);
1620 hm.put(keyStr, valueStr);
1624 hm = new LinkedHashMap<>();
1625 String keyStr = key.substring(key.lastIndexOf('.') + 1);
1626 String valueStr = map.get(key);
1628 if (("type").equals(keyStr)) {
1629 if (!key.contains("entry_schema")) {
1630 hm.put(keyStr, valueStr);
1633 hm.put(keyStr, valueStr);
1635 mapKey.put(uniqueKey, hm);
1644 * Creates the attributes.
1646 * @param mapKey the map key
1648 private void createAttributes(LinkedHashMap<String, LinkedHashMap<String, String>> mapKey) {
1649 StringBuilder attributeStringBuilder = new StringBuilder();
1650 StringBuilder referenceStringBuilder = new StringBuilder();
1651 StringBuilder listBuffer = new StringBuilder();
1652 List<String> constraints = new ArrayList<>();
1653 for (Map.Entry<String, LinkedHashMap<String, String>> entry : mapKey.entrySet()) {
1654 String keySetString = entry.getKey();
1655 LinkedHashMap<String, String> keyValues = mapKey.get(keySetString);
1656 if (keyValues.get("type") != null && (STRING.equalsIgnoreCase(keyValues.get("type"))
1657 || INTEGER.equalsIgnoreCase(keyValues.get("type"))
1658 || BOOLEAN.equalsIgnoreCase(keyValues.get("type")))) {
1659 StringBuilder attributeIndividualStringBuilder = new StringBuilder();
1660 attributeIndividualStringBuilder.append(keySetString + "=");
1661 attributeIndividualStringBuilder.append(keyValues.get("type") + DEFAULTVALUE);
1662 attributeIndividualStringBuilder.append(keyValues.get("default") + REQUIREDVALUE);
1663 attributeIndividualStringBuilder.append(keyValues.get("required") + MANYFALSE);
1664 attributeIndividualStringBuilder.append(DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1665 attributeStringBuilder.append(attributeIndividualStringBuilder + ",");
1666 if (keyValues.get(MATCHABLEKEY) != null && "true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1667 matchableValues.put(keySetString, MATCHINGTRUE);
1669 } else if (LIST.equalsIgnoreCase(keyValues.get("type"))) {
1670 if ("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1671 matchableValues.put(keySetString, MATCHINGTRUE);
1674 Set<String> keys = keyValues.keySet();
1675 Iterator<String> itr = keys.iterator();
1676 boolean isDefinedType = false;
1677 while (itr.hasNext()) {
1678 String key = itr.next();
1679 if ((!("type").equals(key) || ("required").equals(key))) {
1680 String value = keyValues.get(key);
1681 // The "." in the value determines if its a string or a user defined type.
1682 if (!value.contains(".")) {
1684 if (StringUtils.isNumeric(key)) { // only integer key for the value of Constrains
1685 constraints.add(keyValues.get(key));
1688 // This is user defined type
1689 String trimValue = value.substring(value.lastIndexOf('.') + 1);
1690 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1691 referenceIndividualStringBuilder.append(keySetString + "=" + trimValue + MANYTRUE
1692 + DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1693 referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1694 isDefinedType = true;
1700 if (!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))
1701 && (constraints == null || constraints.isEmpty())) {
1702 referenceStringBuilder.append(keySetString + "=MANY-true" + ",");
1705 // User defined Datatype.
1706 if ("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1707 matchableValues.put(keySetString, MATCHINGTRUE);
1709 String value = keyValues.get("type");
1710 if (value != null && !value.isEmpty()) {
1711 String trimValue = value.substring(value.lastIndexOf('.') + 1);
1712 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1713 referenceIndividualStringBuilder.append(keySetString + "=" + trimValue + MANYFALSE
1714 + DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1715 referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1717 logger.info("keyValues.get(type) is null/empty");
1721 if (constraints != null && !constraints.isEmpty()) {
1723 listBuffer.append(keySetString.toUpperCase() + "=[");
1724 for (String str : constraints) {
1725 if (str.contains(DICTIONARY)) {
1726 String[] dictionaryName = str.split(":");
1727 List<Object> dictFromDB = commonClassDao.getDataById(DictionaryData.class, DICTIONARYNAME,
1729 if (dictFromDB != null && !dictFromDB.isEmpty()) {
1730 DictionaryData data = (DictionaryData) dictFromDB.get(0);
1731 str = DICTIONARY + data.getDictionaryUrl() + "@" + data.getDictionaryDataByName();
1734 listBuffer.append(str + ",");
1736 listBuffer.append("]#");
1737 logger.info(listBuffer);
1739 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1740 referenceIndividualStringBuilder.append(keySetString + "=" + keySetString.toUpperCase() + MANYFALSE);
1741 referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1742 constraints.clear();
1746 dataListBuffer.append(listBuffer);
1748 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1749 logger.info("Whole attribute String is:" + attributeStringBuilder);
1750 logger.info("Whole reference String is:" + referenceStringBuilder);
1751 logger.info("List String is:" + listBuffer);
1752 logger.info("Data list buffer is:" + dataListBuffer);
1753 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1755 this.listConstraints = dataListBuffer.toString();
1756 this.referenceAttributes = referenceStringBuilder.toString();
1757 this.attributeString = attributeStringBuilder.toString();
1763 * @param map the map
1765 @SuppressWarnings("unchecked")
1766 private void findNode(LinkedHashMap<Object, Object> map) {
1768 map.forEach((key, value) -> {
1769 // if the value is properties and its type is map object, then save all the keys
1770 if (key.equals(PROPERTIES_KEY) && value instanceof Map) {
1771 saveNodes((LinkedHashMap<?, ?>) value);
1772 if (isDuplicatedAttributes) {
1773 orderedElements = new ArrayList<>();
1778 if (!"policy.nodes.Root".equals(key) && value instanceof Map) {
1779 // value is a Map object, then make a recursive call
1780 findNode((LinkedHashMap<Object, Object>) value);
1789 * @param map the map
1791 private void saveNodes(LinkedHashMap<?, ?> map) {
1793 for (Entry<?, ?> entry : map.entrySet()) {
1795 if (orderedElements.indexOf(entry.getKey()) >= 0) { // duplicated attribute names
1796 isDuplicatedAttributes = true;
1799 orderedElements.add((String) entry.getKey());