Merge "New junits and bug fixes"
[policy/engine.git] / ONAP-REST / src / main / java / org / onap / policy / rest / util / MSModelUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-REST
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.rest.util;
23
24 import com.att.research.xacml.util.XACMLProperties;
25 import com.google.gson.Gson;
26 import java.io.File;
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;
39 import java.util.Map;
40 import java.util.Map.Entry;
41 import java.util.Set;
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;
70
71
72 public class MSModelUtils {
73
74     private static final Log logger = LogFactory.getLog(MSModelUtils.class);
75
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";
107
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;
129
130     private static CommonClassDao commonClassDao;
131
132     public MSModelUtils() {
133         // Default Constructor
134     }
135
136     public MSModelUtils(CommonClassDao commonClassDao) {
137         MSModelUtils.commonClassDao = commonClassDao;
138     }
139
140     public MSModelUtils(String onap, String policy) {
141         this.onap = onap;
142         this.policy = policy;
143     }
144
145     private enum ANNOTATION_TYPE {
146         MATCHING, VALIDATION, DICTIONARY
147     };
148
149     public enum MODEL_TYPE {
150         XMI
151     };
152
153     public enum SearchType {
154         TOSCA_DEFINITION_VERSION, TOSCA_SIMPLE_YAML_1_0_0, NODE_TYPE, DATA_TYPE, JSON_MODEL
155     }
156
157     public Map<String, MSAttributeObject> processEpackage(String file, MODEL_TYPE model) {
158         if (model == MODEL_TYPE.XMI) {
159             processXMIEpackage(file);
160         }
161         return classMap;
162
163     }
164
165     private void processXMIEpackage(String xmiFile) {
166         EPackage root = getEpackage(xmiFile);
167         TreeIterator<EObject> treeItr = root.eAllContents();
168         String className;
169         String returnValue;
170
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();
177
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);
184                 }
185             }
186         }
187
188         if (!enumMap.isEmpty()) {
189             addEnumClassMap();
190         }
191         if (!matchingClass.isEmpty()) {
192             checkForMatchingClass();
193         }
194     }
195
196     private void checkForMatchingClass() {
197         HashMap<String, String> tempAttribute = new HashMap<>();
198
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);
207                 }
208                 for (Entry<String, String> eSet : listRef.entrySet()) {
209                     String key3 = eSet.getKey();
210                     tempAttribute.put(key3, MATCHINGTRUE);
211                 }
212
213             }
214             updateMatching(tempAttribute, key);
215         }
216
217     }
218
219
220
221     private void updateMatching(HashMap<String, String> tempAttribute, String key) {
222         Map<String, MSAttributeObject> newClass = classMap;
223
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);
232             }
233
234         }
235     }
236
237     private void addEnumClassMap() {
238         for (Entry<String, MSAttributeObject> value : classMap.entrySet()) {
239             value.getValue().setEnumType(enumMap);
240         }
241     }
242
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);
249         try {
250             resource.load(Collections.emptyMap());
251         } catch (IOException e) {
252             logger.error("Error loading Encore Resource for new Model" + e);
253         }
254
255         return (EPackage) resource.getContents().get(0);
256     }
257
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;
262
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);
268         }
269         returnMap.put(name, valueList.toString());
270         return returnMap;
271     }
272
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(",")));
277         }
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));
291
292         this.classMap.put(className, msAttributeObject);
293     }
294
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<>();
300
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);
307             }
308
309             if (requiredAttribute) {
310                 if (obj instanceof EStructuralFeature) {
311                     checkAnnotation(annotationSet, (EStructuralFeature) obj);
312                 }
313             } else if (requiredMatchAttribute && (obj instanceof EStructuralFeature)) {
314                 findMatchingAnnotation(annotationSet, obj);
315             }
316         }
317         return annotationSet;
318     }
319
320     private void findMatchingAnnotation(HashMap<String, String> annotationSet, EObject obj) {
321         EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
322         if (eStrucClassifier.getEAnnotations().isEmpty()) {
323             return;
324         }
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);
331             } else {
332                 annotationSet.put(eStrucClassifier.getName(), matching);
333             }
334         }
335
336     }
337
338     private void checkAnnotation(HashMap<String, String> annotationSet, EStructuralFeature obj) {
339         EStructuralFeature eStrucClassifier = obj;
340         if (eStrucClassifier.getEAnnotations().isEmpty()) {
341             return;
342         }
343         String matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
344         if (matching != null) {
345             annotationSet.put(eStrucClassifier.getName(), matching);
346         }
347         String range = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
348         if (range != null) {
349             annotationSet.put(eStrucClassifier.getName(), range);
350         }
351         String annotationDict = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
352         if (annotationDict != null) {
353             annotationSet.put(eStrucClassifier.getName(), annotationDict);
354         }
355     }
356
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;
363
364         // Pulling out dependency from file
365         while (treeItr.hasNext() && rollingCount < 2) {
366
367             EObject obj = treeItr.next();
368             if (obj instanceof EClassifier) {
369                 requiredAttribute = isRequiredAttribute(obj, className) || isRequiredAttribute(obj, superClass);
370                 if (requiredAttribute) {
371                     processClass++;
372                 }
373                 rollingCount = rollingCount + processClass;
374             }
375
376             if (requiredAttribute && (obj instanceof EStructuralFeature)) {
377                 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
378                 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
379                     updateSubAttributes(subAttribute, obj, eStrucClassifier);
380                 }
381             }
382         }
383         return subAttribute;
384     }
385
386     private void updateSubAttributes(Map<String, Object> subAttribute, EObject obj,
387             EStructuralFeature eStrucClassifier) {
388         if (!(obj instanceof EReference)) {
389             return;
390         }
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;
397                 }
398                 subAttribute.put(eStrucClassifier.getName(), refType.getName() + required);
399             }
400         }
401     }
402
403     public String checkDefultValue(String defultValue) {
404         if (defultValue != null) {
405             return DEFAULTVALUE + defultValue;
406         }
407         return ":defaultValue-NA";
408
409     }
410
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])) {
415             return REQUIREDTRUE;
416         }
417         return REQUIREDFALSE;
418     }
419
420     public JSONObject buildJavaObject(Map<String, String> map) {
421         return new JSONObject(map);
422     }
423
424     public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass) {
425
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;
431         boolean annotation;
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) {
438                     processClass++;
439                 }
440                 rollingCount = rollingCount + processClass;
441             }
442
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);
451                     }
452                 }
453             }
454         }
455
456         return refAttribute;
457     }
458
459     private void updEnumTypeRefAttrib(HashMap<String, String> refAttribute, EStructuralFeature obj,
460             EStructuralFeature eStrucClassifier) {
461         EClassifier refType = ((EAttributeImpl) obj).getEType();
462         if (!(refType instanceof EEnumImpl)) {
463             return;
464         }
465
466         String array = arrayCheck(obj.getUpperBound());
467         String required = REQUIREDFALSE;
468         if (obj.getLowerBound() == 1) {
469             required = REQUIREDTRUE;
470         }
471         refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
472     }
473
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];
479             String refValue =
480                     StringUtils.replaceEach(one.split("#")[1], new String[] {"//", ")"}, new String[] {"", ""});
481             refAttribute.put(eStrucClassifier.getName(), refValue);
482         } else {
483             String required = REQUIREDFALSE;
484             if (obj.getLowerBound() == 1) {
485                 required = REQUIREDTRUE;
486             }
487             refAttribute.put(eStrucClassifier.getName(),
488                     refType.getName() + arrayCheck(obj.getUpperBound()) + required);
489         }
490     }
491
492     private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
493         String annotationType;
494         EAnnotation eAnnotation;
495         String onapType;
496         String onapValue;
497
498         EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
499
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();
505
506             if (annotationType.contains(type) && onapType.contains(annotation)) {
507                 return true;
508             }
509
510             if (annotationType.contains(type) && onapValue.contains(annotation)) {
511                 return true;
512             }
513         }
514
515         return false;
516     }
517
518
519     private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
520         String annotationType;
521         EAnnotation eAnnotation;
522         String onapType;
523         String onapValue = null;
524
525         EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
526
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) {
534                     return onapValue;
535                 } else {
536                     return onapType + "-" + onapValue;
537                 }
538             }
539         }
540
541         return onapValue;
542     }
543
544     public boolean isRequiredAttribute(EObject obj, String className) {
545         EClassifier eClassifier = (EClassifier) obj;
546         String workingClass = eClassifier.getName().trim();
547         if (workingClass.equalsIgnoreCase(className)) {
548             return true;
549         }
550
551         return false;
552     }
553
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);
561                     break;
562                 }
563             }
564         }
565         return result;
566     }
567
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")) {
573                 return true;
574             }
575         }
576         return false;
577     }
578
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);
584             }
585         }
586         return returnSubTypes;
587     }
588
589     private String findSubTypes(String className, String returnSubTypes, EClass classifier) {
590         EClass eClass = classifier;
591
592         for (EClass eSuperType : eClass.getEAllSuperTypes()) {
593             if (eClass.getName().contentEquals(className)) {
594                 returnSubTypes = eSuperType.getName();
595             }
596         }
597         return returnSubTypes;
598     }
599
600     public Map<String, String> getAttributeList(EPackage root, String className, String superClass) {
601
602         TreeIterator<EObject> treeItr = root.eAllContents();
603         boolean requiredAttribute = false;
604         HashMap<String, String> refAttribute = new HashMap<>();
605
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);
611             }
612
613             if (requiredAttribute && (obj instanceof EStructuralFeature)) {
614                 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
615                 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
616                     checkStrucClassifier(refAttribute, obj, eStrucClassifier);
617                 }
618             }
619         }
620         return refAttribute;
621
622     }
623
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);
631         }
632     }
633
634     private void updEReferenceAttrib(HashMap<String, String> refAttribute, boolean dictionaryTest,
635             EStructuralFeature obj, EStructuralFeature eStrucClassifier) {
636         String eType;
637         String name = eStrucClassifier.getName();
638         if (dictionaryTest) {
639             eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
640         } else {
641             eType = eStrucClassifier.getEType().getInstanceClassName();
642         }
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);
647     }
648
649     public String arrayCheck(int upperBound) {
650
651         if (upperBound == -1) {
652             return MANYTRUE;
653         }
654
655         return MANYFALSE;
656     }
657
658     public List<String> getDependencyList(EClassifier eClassifier) {
659         List<String> returnValue = new ArrayList<>();;
660         EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes();
661         if (somelist.isEmpty()) {
662             return returnValue;
663         }
664         for (EClass depend : somelist) {
665             if (depend.toString().contains(eProxyURI)) {
666                 String one = depend.toString().split(eProxyURI)[1];
667                 String value =
668                         StringUtils.replaceEach(one.split("#")[1], new String[] {"//", ")"}, new String[] {"", ""});
669                 returnValue.add(value);
670             }
671         }
672
673         return returnValue;
674     }
675
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;
680         boolean enumType;
681
682         for (Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()) {
683             String value = map.getValue().split(":")[0];
684             if (value != null) {
685                 classMap.get(className).getEnumType();
686                 enumType = classMap.get(className).getEnumType().containsKey(value);
687                 if (!enumType) {
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());
693                         }
694                     }
695
696                 }
697             }
698         }
699
700         return missingValues;
701     }
702
703     public Map<String, Map<String, String>> recursiveReference(Map<String, MSAttributeObject> classMap,
704             String className) {
705
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]));
713             }
714
715         }
716
717         return returnObject;
718
719     }
720
721     public String createJson(Map<String, MSAttributeObject> classMap, String className) {
722         boolean enumType;
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];
726             if (value != null) {
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);
732                 }
733             }
734         }
735
736         Gson gson = new Gson();
737         return gson.toJson(myObject);
738     }
739
740     public Map<String, String> getRefclass(Map<String, MSAttributeObject> classMap, String className) {
741         HashMap<String, String> missingValues = new HashMap<>();
742
743         if (classMap.get(className).getAttribute() != null || !classMap.get(className).getAttribute().isEmpty()) {
744             missingValues.putAll(classMap.get(className).getAttribute());
745         }
746
747         if (classMap.get(className).getRefAttribute() != null || !classMap.get(className).getRefAttribute().isEmpty()) {
748             missingValues.putAll(classMap.get(className).getRefAttribute());
749         }
750
751         return missingValues;
752     }
753
754     public String createSubAttributes(List<String> dependency, Map<String, MSAttributeObject> classMap,
755             String modelName) {
756
757         HashMap<String, Object> workingMap = new HashMap<>();
758         MSAttributeObject tempObject;
759         if (dependency != null) {
760             if (dependency.isEmpty()) {
761                 return "{}";
762             }
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());
768                 }
769             }
770         }
771
772         return createJson(classMap, modelName);
773     }
774
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);
782                 String rawValue =
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);
787                 }
788             }
789         }
790
791         return returnList;
792     }
793
794     private void updDependencyList(ArrayList<String> returnList, String depend) {
795         if (!returnList.contains(depend) && !depend.isEmpty()) {
796             returnList.add(depend.trim());
797         }
798     }
799
800     /*
801      * For TOSCA Model
802      */
803     public String parseTosca(String fileName) {
804         Map<String, String> map = new LinkedHashMap<>();
805         try {
806             map = load(fileName);
807             if (map != null && map.get(ERROR) != null) {
808                 return map.get(ERROR);
809             }
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);
815
816         } catch (IOException e) {
817             logger.error(e);
818         } catch (ParserException e) {
819             logger.error(e);
820             return e.getMessage();
821         }
822
823         return null;
824     }
825
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) {
835             logger.error(e);
836         } catch (Exception e) {
837             logger.error(e);
838             throw new ParserException("Invalid TOSCA Model format. Please make sure it is a valid YAML file");
839         }
840
841         StringBuilder sb = new StringBuilder();
842         LinkedHashMap<String, String> settings = new LinkedHashMap<>();
843         if (yamlMap == null) {
844             return settings;
845         }
846
847         String message = validations(yamlMap);
848
849         if (message != null) {
850             settings.put(ERROR, message);
851             return settings;
852         }
853
854         findNode(yamlMap);
855
856         if (!isDuplicatedAttributes && orderedElements != null && !orderedElements.isEmpty()) {
857             orderedElements.stream().forEach(string -> {
858                 orderInfo.append(string);
859                 orderInfo.append(",");
860                 logger.info("Content: " + string);
861             });
862
863             orderInfo.append("]");
864
865             dataOrderInfo = orderInfo.toString();
866             dataOrderInfo = dataOrderInfo.replace(",]", "]");
867
868             logger.info("dataOrderInfo :" + dataOrderInfo);
869         }
870
871         List<String> path = new ArrayList<>();
872         serializeMap(settings, sb, path, yamlMap);
873         return settings;
874     }
875
876     @SuppressWarnings("unchecked")
877     private String validations(@SuppressWarnings("rawtypes") Map yamlMap) {
878
879         boolean isNoteTypeFound = false;
880         boolean isDataTypeFound = false;
881         boolean isToscaVersionKeyFound = false;
882         boolean isToscaVersionValueFound = false;
883         @SuppressWarnings("rawtypes")
884         Map m1 = new HashMap();
885         short order = 0;
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;
894                 }
895
896                 switch (me.getKey().toString()) {
897                     case TOSCA_DEFINITION_VERSION:
898                         isToscaVersionKeyFound = true;
899                         order++;
900                         m1.put(TOSCA_DEFINITION_VERSION, order);
901                         break;
902                     case NODE_TYPE:
903                         isNoteTypeFound = true;
904                         order++;
905                         m1.put(NODE_TYPE, order);
906                         break;
907                     case DATA_TYPE:
908                         isDataTypeFound = true;
909                         order++;
910                         m1.put(DATA_TYPE, order);
911                         break;
912                     case JSON_MODEL:
913                         setJsonRuleFormation(me.getValue().toString());
914                         break;
915                     default:
916                         break;
917                 }
918             }
919             if (!isDataTypeFound) {
920                 return "data_types are missing or invalid.";
921             }
922             if (!isToscaVersionKeyFound || !isToscaVersionValueFound) {
923                 return "tosca_definitions_version is missing or invalid.";
924             }
925
926             if (!isNoteTypeFound) {
927                 return "node_types are missing or invalid.";
928             }
929
930             short version = (short) m1.get(TOSCA_DEFINITION_VERSION);
931
932             if (version > 1) {
933                 return "tosca_definitions_version should be defined first.";
934             }
935
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.";
940             }
941
942         }
943
944         return null;
945     }
946
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()) {
951
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);
960             } else {
961                 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
962             }
963         }
964     }
965
966     @SuppressWarnings("unchecked")
967     private void serializeList(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
968             List<String> yamlList) {
969         int counter = 0;
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);
979             } else {
980                 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
981             }
982             counter++;
983         }
984     }
985
986     private void serializeValue(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
987             String name, Object value) {
988         if (value == null) {
989             return;
990         }
991         sb.setLength(0);
992         for (String pathEle : path) {
993             sb.append(pathEle).append('.');
994         }
995         sb.append(name);
996         settings.put(sb.toString(), value.toString());
997     }
998
999
1000     void parseDataAndPolicyNodes(Map<String, String> map) {
1001         for (String key : map.keySet()) {
1002             if (key.contains("policy.nodes.Root")) {
1003                 continue;
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());
1008
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());
1014
1015                 stringBetweenDotsForDataFields(subNodeString);
1016             }
1017         }
1018     }
1019
1020     // Second index of dot should be returned.
1021     public int stringBetweenDots(String str) {
1022         String stringToSearch = str;
1023         String[] ss = stringToSearch.split("\\.");
1024         if (ss != null) {
1025             int len = ss.length;
1026             if (len > 2) {
1027                 uniqueKeys.add(ss[2]);
1028             }
1029         }
1030
1031         return uniqueKeys.size();
1032     }
1033
1034
1035     public void stringBetweenDotsForDataFields(String str) {
1036         String stringToSearch = str;
1037         String[] ss = stringToSearch.split("\\.");
1038         if (ss != null) {
1039             int len = ss.length;
1040
1041             if (len > 2) {
1042                 uniqueDataKeys.add(ss[0] + "%" + ss[2]);
1043             }
1044         }
1045     }
1046
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);
1057             } else {
1058                 hmSub = new LinkedHashMap<>();
1059                 hmSub.put(uniqueDataKeySplit[1], value);
1060             }
1061
1062             dataMapKey.put(uniqueDataKeySplit[0], hmSub);
1063         }
1064
1065         JSONObject mainObject = new JSONObject();
1066         JSONObject json;
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));
1074             }
1075             mainObject.put(s, json);
1076         }
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);
1082         }
1083
1084         logger.info("#############################################################################");
1085         logger.info(mainObject);
1086         logger.info("###############################################################################");
1087     }
1088
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);
1098
1099                 String findRequired = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + REQUIRED;
1100                 String requiredValue = map.get(findRequired);
1101
1102                 String matchable = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + MATCHABLE;
1103
1104                 String matchableValue = map.get(matchable);
1105
1106                 if ("true".equalsIgnoreCase(matchableValue)) {
1107                     String key = uniqueDataKeySplit[uniqueDataKeySplit.length - 1];
1108                     matchableValues.put(key, MATCHINGTRUE);
1109                 }
1110
1111                 if (requiredValue == null || requiredValue.isEmpty()) {
1112                     requiredValue = "false";
1113                 }
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);
1124
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);
1144                         // Its userdefined
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);
1160                             }
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) {
1170                                     break;
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")) {
1177                                         ruleCheck = true;
1178                                         dictionaryNameValRule = dictionaryNameVal.split("#");
1179                                         dictFromDB = commonClassDao.getDataById(DictionaryData.class, DICTIONARYNAME,
1180                                                 dictionaryNameValRule[0]);
1181                                     } else {
1182                                         dictFromDB = commonClassDao.getDataById(DictionaryData.class, DICTIONARYNAME,
1183                                                 dictionaryName[1]);
1184                                     }
1185                                     if (dictFromDB != null && !dictFromDB.isEmpty()) {
1186                                         DictionaryData data = (DictionaryData) dictFromDB.get(0);
1187                                         if (ruleCheck) {
1188                                             constraintsValue = DICTIONARY + data.getDictionaryUrl() + "@"
1189                                                     + data.getDictionaryDataByName() + "&Rule";
1190                                         } else {
1191                                             constraintsValue = DICTIONARY + data.getDictionaryUrl() + "@"
1192                                                     + data.getDictionaryDataByName();
1193                                         }
1194                                     }
1195                                     dataListBuffer.append(constraintsValue + ",");
1196                                 } else {
1197                                     logger.info("constraintsValue => " + constraintsValue);
1198                                     if (constraintsValue.contains("=")) {
1199                                         constraintsValue = constraintsValue.replace("=", "equal-sign");
1200                                     }
1201                                     dataConstraints.add(constraintsValue);
1202                                     dataListBuffer.append(constraintsValue + ",");
1203                                 }
1204                             }
1205                             dataListBuffer.append("]#");
1206                             logger.info(dataListBuffer);
1207                         }
1208                     }
1209                 } else {
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());
1222
1223                 }
1224             } else {
1225                 matchableValues.put(uniqueDataKey, MATCHINGTRUE);
1226             }
1227         }
1228
1229         return dataMapForJson;
1230     }
1231
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;
1237
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 "
1249                                             + valueStr);
1250
1251                         }
1252                         if ("type".equals(keyStr)) {
1253                             if (!key.contains("entry_schema")) {
1254                                 hm.put(keyStr, valueStr);
1255                             }
1256                         } else {
1257                             hm.put(keyStr, valueStr);
1258                         }
1259
1260                     } else {
1261                         hm = new LinkedHashMap<>();
1262                         String keyStr = key.substring(key.lastIndexOf('.') + 1);
1263                         String valueStr = map.get(key);
1264
1265                         if (("type").equals(keyStr)) {
1266                             if (!key.contains("entry_schema")) {
1267                                 hm.put(keyStr, valueStr);
1268                             }
1269                         } else {
1270                             hm.put(keyStr, valueStr);
1271                         }
1272                         mapKey.put(uniqueKey, hm);
1273                     }
1274                 }
1275             }
1276         }
1277         return mapKey;
1278     }
1279
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);
1300                 }
1301             } else if (LIST.equalsIgnoreCase(keyValues.get("type"))) {
1302                 if ("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1303                     matchableValues.put(keySetString, MATCHINGTRUE);
1304                 }
1305                 // List Data type
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(".")) {
1315                             // This is string
1316                             if (StringUtils.isNumeric(key)) { // only integer key for the value of Constrains
1317                                 constraints.add(keyValues.get(key));
1318                             }
1319                         } else {
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;
1327                         }
1328                     }
1329
1330                 }
1331
1332                 if (!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))
1333                         && (constraints == null || constraints.isEmpty())) {
1334                     referenceStringBuilder.append(keySetString + "=MANY-true" + ",");
1335                 }
1336             } else {
1337                 // User defined Datatype.
1338                 if ("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1339                     matchableValues.put(keySetString, MATCHINGTRUE);
1340                 }
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 + ",");
1348                 } else {
1349                     logger.info("keyValues.get(type) is null/empty");
1350                 }
1351
1352             }
1353             if (constraints != null && !constraints.isEmpty()) {
1354                 // List handling.
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();
1364                         }
1365                     }
1366                     listBuffer.append(str + ",");
1367                 }
1368                 listBuffer.append("]#");
1369                 logger.info(listBuffer);
1370
1371
1372                 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1373                 referenceIndividualStringBuilder.append(keySetString + "=" + keySetString.toUpperCase() + MANYFALSE);
1374                 referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1375                 constraints.clear();
1376             }
1377         }
1378
1379         dataListBuffer.append(listBuffer);
1380
1381
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("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1388
1389         this.listConstraints = dataListBuffer.toString();
1390         this.referenceAttributes = referenceStringBuilder.toString();
1391         this.attributeString = attributeStringBuilder.toString();
1392     }
1393
1394     @SuppressWarnings("unchecked")
1395     private void findNode(LinkedHashMap<Object, Object> map) {
1396
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<>();
1403                     return;
1404                 }
1405             }
1406
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);
1410             }
1411         });
1412
1413     }
1414
1415     private void saveNodes(LinkedHashMap<?, ?> map) {
1416
1417         for (Entry<?, ?> entry : map.entrySet()) {
1418
1419             if (orderedElements.indexOf(entry.getKey()) >= 0) { // duplicated attribute names
1420                 isDuplicatedAttributes = true;
1421                 return;
1422             } else {
1423                 orderedElements.add((String) entry.getKey());
1424             }
1425         }
1426     }
1427
1428     public String getAttributeString() {
1429         return attributeString;
1430     }
1431
1432     public void setAttributeString(String attributeString) {
1433         this.attributeString = attributeString;
1434     }
1435
1436     public LinkedHashMap<String, Object> getRetmap() {
1437         return retmap;
1438     }
1439
1440     public void setRetmap(LinkedHashMap<String, Object> retmap) {
1441         this.retmap = retmap;
1442     }
1443
1444     public Map<String, String> getMatchableValues() {
1445         return matchableValues;
1446     }
1447
1448     public void setMatchableValues(Map<String, String> matchableValues) {
1449         this.matchableValues = matchableValues;
1450     }
1451
1452     public String getReferenceAttributes() {
1453         return referenceAttributes;
1454     }
1455
1456     public void setReferenceAttributes(String referenceAttributes) {
1457         this.referenceAttributes = referenceAttributes;
1458     }
1459
1460     public String getListConstraints() {
1461         return listConstraints;
1462     }
1463
1464     public void setListConstraints(String listConstraints) {
1465         this.listConstraints = listConstraints;
1466     }
1467
1468     public String getDataOrderInfo() {
1469         return dataOrderInfo;
1470     }
1471
1472     public void setDataOrderInfo(String dataOrderInfo) {
1473         this.dataOrderInfo = dataOrderInfo;
1474     }
1475
1476     public String getJsonRuleFormation() {
1477         return jsonRuleFormation;
1478     }
1479
1480     public void setJsonRuleFormation(String jsonRuleFormation) {
1481         this.jsonRuleFormation = jsonRuleFormation;
1482     }
1483
1484 }