Merge "Remove exclusion of dme"
[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-2018 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
43 import org.apache.commons.lang.StringUtils;
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46 import org.eclipse.emf.common.util.EList;
47 import org.eclipse.emf.common.util.EMap;
48 import org.eclipse.emf.common.util.Enumerator;
49 import org.eclipse.emf.common.util.TreeIterator;
50 import org.eclipse.emf.common.util.URI;
51 import org.eclipse.emf.ecore.EAnnotation;
52 import org.eclipse.emf.ecore.EClass;
53 import org.eclipse.emf.ecore.EClassifier;
54 import org.eclipse.emf.ecore.EEnum;
55 import org.eclipse.emf.ecore.EEnumLiteral;
56 import org.eclipse.emf.ecore.EObject;
57 import org.eclipse.emf.ecore.EPackage;
58 import org.eclipse.emf.ecore.EReference;
59 import org.eclipse.emf.ecore.EStructuralFeature;
60 import org.eclipse.emf.ecore.impl.EAttributeImpl;
61 import org.eclipse.emf.ecore.impl.EEnumImpl;
62 import org.eclipse.emf.ecore.resource.Resource;
63 import org.eclipse.emf.ecore.resource.ResourceSet;
64 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
65 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
66 import org.json.JSONObject;
67 import org.onap.policy.rest.XACMLRestProperties;
68 import org.yaml.snakeyaml.Yaml;
69
70
71 public class MSModelUtils {
72
73     private static final Log logger = LogFactory.getLog(MSModelUtils.class);
74
75     private HashMap<String, MSAttributeObject> classMap = new HashMap<>();
76     private HashMap<String, String> enumMap = new HashMap<>();
77     private HashMap<String, String> matchingClass = new HashMap<>();
78     private String configuration = "configuration";
79     private String dictionary = "dictionary";
80     private String onap = "";
81     private String policy = "";
82     private String eProxyURI = "eProxyURI:";
83     private List<String> orderedElements = new ArrayList<>();
84     private String dataOrderInfo = "";
85     private Set<String> uniqueDataKeys = new HashSet<>();
86     private Set<String> uniqueKeys = new HashSet<>();
87     private String listConstraints = null;
88     private String referenceAttributes;
89     private LinkedHashMap<String, Object> retmap = new LinkedHashMap<>();
90     private Map<String, String> matchableValues;
91     private static final String PROPERTIES = ".properties.";
92     private static final String DATATYPE = "data_types.policy.data.";
93     private static final String TYPE = ".type";
94     private static final String REQUIRED = ".required";
95     private static final String MATCHABLE = ".matchable";
96     public static final String  STRING = "string";
97     public static final String  INTEGER = "integer";
98     private static final String BOOLEAN = "boolean";
99     public static final String  LIST = "list";
100     public static final String  MAP = "map";
101     private static final String DEFAULT = ".default";
102     private static final String MANYFALSE = ":MANY-false";
103     private static final String DESCRIPTION = ".description";
104
105     private static final String MANYTRUE = ":MANY-true";
106     private static final String DEFAULTVALUE = ":defaultValue-";
107     private static final String REQUIREDVALUE = ":required-";
108     private static final String MATCHABLEKEY = "matchable";
109     private static final String REQUIREDFALSE = ":required-false";
110     private static final String REQUIREDTRUE = ":required-true";
111     private static final String MATCHINGTRUE = "matching-true";
112     private static final String DESCRIPTION_KEY = "description";  
113     private static final String DESCRIPTION_TOKEN =":description-";
114     private static final String PROPERTIES_KEY = "properties";
115     private static final String DATA_TYPE = "data_types";
116     private static final String ERROR = "error";
117     private static final String NODE_TYPE = "node_types";
118     private static final String TOSCA_DEFINITION_VERSION = "tosca_definitions_version";
119     private StringBuilder dataListBuffer = new StringBuilder();
120     private List<String> dataConstraints = new ArrayList<>();
121     private String attributeString = null;
122     private boolean isDuplicatedAttributes = false;
123
124     public MSModelUtils() {}
125
126     public MSModelUtils(String onap, String policy) {
127         this.onap = onap;
128         this.policy = policy;
129     }
130
131     private enum ANNOTATION_TYPE {
132         MATCHING, VALIDATION, DICTIONARY
133     };
134
135     public enum MODEL_TYPE {
136         XMI
137     };
138
139
140     public Map<String, MSAttributeObject> processEpackage(String file, MODEL_TYPE model) {
141         if (model == MODEL_TYPE.XMI) {
142             processXMIEpackage(file);
143         }
144         return classMap;
145
146     }
147
148     private void processXMIEpackage(String xmiFile){
149         EPackage root = getEpackage(xmiFile);
150         TreeIterator<EObject> treeItr = root.eAllContents();
151         String className;
152         String returnValue;
153
154         //    Pulling out dependency from file
155         while (treeItr.hasNext()) {
156             EObject obj = treeItr.next();
157             if (obj instanceof EClassifier) {
158                 EClassifier eClassifier = (EClassifier) obj;
159                 className = eClassifier.getName();
160
161                 if (obj instanceof EEnum) {
162                     enumMap.putAll(getEEnum(obj));
163                 }else if (obj instanceof EClass) {
164                     String temp = getDependencyList(eClassifier).toString();
165                     returnValue = StringUtils.replaceEach(temp, new String[]{"[", "]"}, new String[]{"", ""});
166                     getAttributes(className, returnValue, root);
167                 }
168             }
169         }
170
171         if (!enumMap.isEmpty()){
172             addEnumClassMap();
173         }
174         if (!matchingClass.isEmpty()){
175             checkForMatchingClass();
176         }
177     }
178
179     private void checkForMatchingClass() {
180         HashMap<String, String> tempAttribute = new HashMap<>();
181
182         for (Entry<String, String> set : matchingClass.entrySet()){
183             String key = set.getKey();
184             if (classMap.containsKey(key)){
185                 Map<String, String> listAttributes = classMap.get(key).getAttribute();
186                 Map<String, String> listRef = classMap.get(key).getRefAttribute();
187                 for (  Entry<String, String> eSet : listAttributes.entrySet()){
188                     String key2 = eSet.getKey();
189                     tempAttribute.put(key2, MATCHINGTRUE);
190                 }
191                 for (  Entry<String, String> eSet : listRef.entrySet()){
192                     String key3 = eSet.getKey();
193                     tempAttribute.put(key3, MATCHINGTRUE);
194                 }
195
196             }
197             updateMatching(tempAttribute, key);
198         }
199
200     }
201
202
203
204     private void updateMatching(HashMap<String, String> tempAttribute, String key) {
205         Map<String, MSAttributeObject> newClass = classMap;
206
207         for (Entry<String, MSAttributeObject> updateClass :  newClass.entrySet()){
208             Map<String, String> valueMap = updateClass.getValue().getMatchingSet();
209             String keymap = updateClass.getKey();
210             if (valueMap.containsKey(key)){
211                 Map<String, String> modifyMap = classMap.get(keymap).getMatchingSet();
212                 modifyMap.remove(key);
213                 modifyMap.putAll(tempAttribute);
214                 classMap.get(keymap).setMatchingSet(modifyMap);
215             }
216
217         }
218     }
219
220     private void addEnumClassMap() {
221         for (Entry<String, MSAttributeObject> value :classMap.entrySet()){
222             value.getValue().setEnumType(enumMap);
223         }
224     }
225
226     private EPackage getEpackage(String xmiFile) {
227         ResourceSet resSet = new ResourceSetImpl();
228         Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
229         Map<String, Object> m = reg.getExtensionToFactoryMap();
230         m.put("xmi", new XMIResourceFactoryImpl());
231         Resource resource = resSet.getResource(URI.createFileURI(xmiFile), true);
232         try {
233             resource.load(Collections.emptyMap());
234         } catch (IOException e) {
235             logger.error("Error loading Encore Resource for new Model" + e);
236         }
237
238         return (EPackage) resource.getContents().get(0);
239     }
240
241     private HashMap<String, String> getEEnum(EObject obj) {
242         List<String> valueList = new ArrayList<>();
243         HashMap<String, String> returnMap = new HashMap<>();
244         EEnum eenum = (EEnum)obj;
245
246         String name = eenum.getName();
247         for (EEnumLiteral eEnumLiteral : eenum.getELiterals())
248         {
249             Enumerator instance = eEnumLiteral.getInstance();
250             String value = instance.getLiteral();
251             valueList.add(value);
252         }
253         returnMap.put(name, valueList.toString());
254         return returnMap;
255     }
256
257     public void getAttributes(String className, String dependency, EPackage root) {
258         List<String> dpendList = new ArrayList<>();
259         if (dependency!=null){
260             dpendList = new ArrayList<>(Arrays.asList(dependency.split(",")));
261         }
262         MSAttributeObject msAttributeObject = new MSAttributeObject();
263         msAttributeObject.setClassName(className);
264         String extendClass = getSubTypes(root, className);
265         Map<String, String> returnRefList = getRefAttributeList(root, className, extendClass);
266         Map<String, String> returnAttributeList = getAttributeList(root, className, extendClass);
267         Map<String, Object> returnSubList = getSubAttributeList(root, className, extendClass);
268         HashMap<String, String> returnAnnotation = getAnnotation(root, className, extendClass);
269         msAttributeObject.setAttribute(returnAttributeList);
270         msAttributeObject.setRefAttribute(returnRefList);
271         msAttributeObject.setSubClass(returnSubList);
272         msAttributeObject.setDependency(dpendList.toString());
273         msAttributeObject.addMatchingSet(returnAnnotation);
274         msAttributeObject.setPolicyTempalate(isPolicyTemplate(root, className));
275
276         this.classMap.put(className, msAttributeObject);
277     }
278
279     private HashMap<String, String> getAnnotation(EPackage root, String className, String extendClass) {
280         TreeIterator<EObject> treeItr = root.eAllContents();
281         boolean requiredAttribute = false;
282         boolean requiredMatchAttribute = false;
283         HashMap<String, String> annotationSet = new HashMap<>();
284
285         //    Pulling out dependency from file
286         while (treeItr.hasNext()) {
287             EObject obj = treeItr.next();
288             if (obj instanceof EClassifier) {
289                 requiredAttribute = isRequiredAttribute(obj,  className );
290                 requiredMatchAttribute = isRequiredAttribute(obj,  extendClass );
291             }
292
293             if (requiredAttribute){
294                 if (obj instanceof EStructuralFeature) {
295                     checkAnnotation(annotationSet, (EStructuralFeature) obj);
296                 }
297             } else if (requiredMatchAttribute && (obj instanceof EStructuralFeature)) {
298                 findMatchingAnnotation(annotationSet, obj);
299             }
300         }
301         return annotationSet;
302     }
303
304     private void findMatchingAnnotation(HashMap<String, String> annotationSet, EObject obj) {
305         EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
306         if (eStrucClassifier.getEAnnotations().isEmpty()) {
307             return;
308         }
309         String matching  = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
310         if (matching!=null){
311             if (obj instanceof EReference){
312                 EClass refType = ((EReference) obj).getEReferenceType();
313                 annotationSet.put(refType.getName(), matching);
314                 matchingClass.put(refType.getName(), matching);
315             }else{
316                 annotationSet.put(eStrucClassifier.getName(), matching);
317             }
318         }
319
320     }
321
322     private void checkAnnotation(HashMap<String, String> annotationSet, EStructuralFeature obj) {
323         EStructuralFeature eStrucClassifier = obj;
324         if (eStrucClassifier.getEAnnotations().isEmpty()) {
325             return;
326         }
327         String matching  = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
328         if (matching!=null){
329             annotationSet.put(eStrucClassifier.getName(), matching);
330         }
331         String range  = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
332         if (range!=null){
333             annotationSet.put(eStrucClassifier.getName(), range);
334         }
335         String annotationDict = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
336         if (annotationDict!=null){
337             annotationSet.put(eStrucClassifier.getName(), annotationDict);
338         }
339     }
340
341     private Map<String, Object> getSubAttributeList(EPackage root, String className , String superClass) {
342         TreeIterator<EObject> treeItr = root.eAllContents();
343         boolean requiredAttribute = false;
344         Map<String, Object> subAttribute = new HashMap<>();
345         int rollingCount = 0;
346         int processClass = 0;
347
348         //    Pulling out dependency from file
349         while (treeItr.hasNext() && rollingCount < 2) {
350
351             EObject obj = treeItr.next();
352             if (obj instanceof EClassifier) {
353                 requiredAttribute = isRequiredAttribute(obj,  className ) || isRequiredAttribute(obj,  superClass );
354                 if (requiredAttribute){
355                     processClass++;
356                 }
357                 rollingCount = rollingCount+processClass;
358             }
359
360             if (requiredAttribute && (obj instanceof EStructuralFeature)) {
361                 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
362                 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
363                     updateSubAttributes(subAttribute, obj, eStrucClassifier);
364                 }
365             }
366         }
367         return subAttribute;
368     }
369
370     private void updateSubAttributes(Map<String, Object> subAttribute, EObject obj, EStructuralFeature eStrucClassifier) {
371         if (!(obj instanceof EReference)) {
372             return;
373         }
374         if (annotationTest(eStrucClassifier, configuration, onap)) {
375             EClass refType = ((EReference) obj).getEReferenceType();
376             if(!refType.toString().contains(eProxyURI)){
377                 String required = REQUIREDFALSE;
378                 if(eStrucClassifier.getLowerBound() == 1){
379                     required = REQUIREDTRUE;
380                 }
381                 subAttribute.put(eStrucClassifier.getName(), refType.getName() + required);
382             }
383         }
384     }
385
386     public String checkDefultValue(String defultValue) {
387         if (defultValue!=null){
388             return DEFAULTVALUE+ defultValue;
389         }
390         return ":defaultValue-NA";
391
392     }
393
394     public String checkRequiredPattern(int upper, int lower) {
395         String pattern = XACMLProperties.getProperty(XACMLRestProperties.PROP_XCORE_REQUIRED_PATTERN);
396         if (pattern!=null && upper == Integer.parseInt(pattern.split(",")[1]) && lower==Integer.parseInt(pattern.split(",")[0])){
397             return REQUIREDTRUE;
398         }
399         return REQUIREDFALSE;
400     }
401
402     public JSONObject buildJavaObject(Map<String, String> map){
403         return  new JSONObject(map);
404     }
405
406     public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass){
407
408         TreeIterator<EObject> treeItr = root.eAllContents();
409         boolean requiredAttribute = false;
410         HashMap<String, String> refAttribute = new HashMap<>();
411         int rollingCount = 0;
412         int processClass = 0;
413         boolean annotation;
414         //    Pulling out dependency from file
415         while (treeItr.hasNext()) {
416             EObject obj = treeItr.next();
417             if (obj instanceof EClassifier) {
418                 requiredAttribute = isRequiredAttribute(obj,  className ) || isRequiredAttribute(obj,  superClass );
419                 if (requiredAttribute){
420                     processClass++;
421                 }
422                 rollingCount = rollingCount+processClass;
423             }
424
425             if (requiredAttribute && (obj instanceof EStructuralFeature)) {
426                     EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
427                     if (!eStrucClassifier.getEAnnotations().isEmpty()) {
428                         annotation = annotationTest(eStrucClassifier, configuration, onap);
429                         if ( annotation &&  obj instanceof EReference) {
430                             updRefAttributes(refAttribute, (EStructuralFeature) obj, eStrucClassifier);
431                         } else if (annotation &&  obj instanceof EAttributeImpl) {
432                             updEnumTypeRefAttrib(refAttribute, (EStructuralFeature) obj, eStrucClassifier);
433                         }
434                     }
435             }
436         }
437
438         return refAttribute;
439     }
440
441     private void updEnumTypeRefAttrib(HashMap<String, String> refAttribute, EStructuralFeature obj, EStructuralFeature eStrucClassifier) {
442         EClassifier refType = ((EAttributeImpl) obj).getEType();
443         if (!(refType instanceof EEnumImpl)){
444             return;
445         }
446
447         String array = arrayCheck(obj.getUpperBound());
448         String required = REQUIREDFALSE;
449         if(obj.getLowerBound() == 1){
450             required = REQUIREDTRUE;
451         }
452         refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
453     }
454
455     private void updRefAttributes(HashMap<String, String> refAttribute, EStructuralFeature obj, EStructuralFeature eStrucClassifier) {
456         EClass refType = ((EReference) obj).getEReferenceType();
457         if(refType.toString().contains(eProxyURI)){
458             String one = refType.toString().split(eProxyURI)[1];
459             String refValue = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
460             refAttribute.put(eStrucClassifier.getName(), refValue);
461         } else {
462             String required = REQUIREDFALSE;
463             if(obj.getLowerBound() == 1){
464                 required = REQUIREDTRUE;
465             }
466             refAttribute.put(eStrucClassifier.getName(), refType.getName() + arrayCheck(obj.getUpperBound()) + required);
467         }
468     }
469
470     private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
471         String annotationType;
472         EAnnotation eAnnotation;
473         String onapType;
474         String onapValue;
475
476         EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
477
478         for (int i = 0; i < value.size(); i++){
479             annotationType = value.get(i).getSource();
480             eAnnotation = eStrucClassifier.getEAnnotations().get(i);
481             onapType = eAnnotation.getDetails().get(0).getValue();
482             onapValue = eAnnotation.getDetails().get(0).getKey();
483
484             if (annotationType.contains(type) && onapType.contains(annotation)){
485                 return true;
486             }
487
488             if (annotationType.contains(type) && onapValue.contains(annotation)){
489                 return true;
490             }
491         }
492
493         return false;
494     }
495
496
497     private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
498         String annotationType;
499         EAnnotation eAnnotation;
500         String onapType;
501         String onapValue = null;
502
503         EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
504
505         for (int i = 0; i < value.size(); i++){
506             annotationType = value.get(i).getSource();
507             eAnnotation = eStrucClassifier.getEAnnotations().get(i);
508             onapType = eAnnotation.getDetails().get(0).getKey();
509             if (annotationType.contains(type) && onapType.compareToIgnoreCase(annotation.toString())==0){
510                 onapValue = eAnnotation.getDetails().get(0).getValue();
511                 if (annotation == ANNOTATION_TYPE.VALIDATION){
512                     return onapValue;
513                 } else {
514                     return onapType + "-" + onapValue;
515                 }
516             }
517         }
518
519         return onapValue;
520     }
521     public boolean isRequiredAttribute(EObject obj, String className){
522         EClassifier eClassifier = (EClassifier) obj;
523         String workingClass = eClassifier.getName().trim();
524         if (workingClass.equalsIgnoreCase(className)){
525             return  true;
526         }
527
528         return false;
529     }
530
531     private boolean isPolicyTemplate(EPackage root, String className){
532         boolean result = false;
533         for (EClassifier classifier : root.getEClassifiers()){
534             if (classifier instanceof EClass) {
535                 EClass eClass = (EClass)classifier;
536                 if (eClass.getName().contentEquals(className)) {
537                     result = checkPolicyTemplate(eClass);
538                     break;
539                 }
540             }
541         }
542         return result;
543     }
544
545     private boolean checkPolicyTemplate(EClass eClass) {
546         EList<EAnnotation> value = eClass.getEAnnotations();
547         for (EAnnotation workingValue : value){
548             EMap<String, String> keyMap = workingValue.getDetails();
549             if (keyMap.containsKey("policyTemplate")){
550                 return true;
551             }
552         }
553         return false;
554     }
555
556     private String getSubTypes(EPackage root, String className) {
557         String returnSubTypes = null;
558         for (EClassifier classifier : root.getEClassifiers()){
559             if (classifier instanceof EClass) {
560                 returnSubTypes = findSubTypes(className, returnSubTypes, (EClass) classifier);
561             }
562         }
563         return returnSubTypes;
564     }
565
566     private String findSubTypes(String className, String returnSubTypes, EClass classifier) {
567         EClass eClass = classifier;
568
569         for (EClass eSuperType : eClass.getEAllSuperTypes())
570         {
571             if (eClass.getName().contentEquals(className)){
572                 returnSubTypes = eSuperType.getName();
573             }
574         }
575         return returnSubTypes;
576     }
577
578     public Map<String, String> getAttributeList(EPackage root, String className, String superClass){
579
580         TreeIterator<EObject> treeItr = root.eAllContents();
581         boolean requiredAttribute = false;
582         HashMap<String, String> refAttribute = new HashMap<>();
583
584         //    Pulling out dependency from file
585         while (treeItr.hasNext()) {
586             EObject obj = treeItr.next();
587             if (obj instanceof EClassifier) {
588                 requiredAttribute = isRequiredAttribute(obj,  className ) || isRequiredAttribute(obj,  superClass );
589             }
590
591             if (requiredAttribute && (obj instanceof EStructuralFeature)) {
592                 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
593                 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
594                     checkStrucClassifier(refAttribute, obj, eStrucClassifier);
595                 }
596             }
597         }
598         return refAttribute;
599
600     }
601
602     private void checkStrucClassifier(HashMap<String, String> refAttribute, EObject obj, EStructuralFeature eStrucClassifier) {
603         EClassifier refType = ((EStructuralFeature) obj).getEType();
604         boolean annotation = annotationTest(eStrucClassifier, configuration, onap);
605         boolean dictionaryTest = annotationTest(eStrucClassifier, dictionary, policy);
606         if (annotation && !(obj instanceof EReference) && !(refType instanceof EEnumImpl)) {
607             updEReferenceAttrib(refAttribute, dictionaryTest, (EStructuralFeature) obj, eStrucClassifier);
608         }
609     }
610
611     private void updEReferenceAttrib(HashMap<String, String> refAttribute, boolean dictionaryTest, EStructuralFeature obj, EStructuralFeature eStrucClassifier) {
612         String eType;
613         String name = eStrucClassifier.getName();
614         if (dictionaryTest){
615             eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
616         }else {
617             eType = eStrucClassifier.getEType().getInstanceClassName();
618         }
619         String defaultValue = checkDefultValue(obj.getDefaultValueLiteral());
620         String array = arrayCheck(obj.getUpperBound());
621         String required = checkRequiredPattern(obj.getUpperBound(), obj.getLowerBound());
622         refAttribute.put(name, eType + defaultValue + required + array);
623     }
624
625     public String arrayCheck(int upperBound) {
626
627         if (upperBound == -1){
628             return MANYTRUE;
629         }
630
631         return MANYFALSE;
632     }
633
634     public List<String> getDependencyList(EClassifier eClassifier){
635         List<String> returnValue = new ArrayList<>();;
636         EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes();
637         if (somelist.isEmpty()){
638             return returnValue;
639         }
640         for(EClass depend: somelist){
641             if (depend.toString().contains(eProxyURI)){
642                 String one = depend.toString().split(eProxyURI)[1];
643                 String value = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
644                 returnValue.add(value);
645             }
646         }
647
648         return returnValue;
649     }
650
651     public Map<String, String> buildSubList(Map<String, String> subClassAttributes, Map<String, MSAttributeObject> classMap, String className){
652         Map<String, String> missingValues = new HashMap<>();
653         Map<String, String> workingMap;
654         boolean enumType;
655
656         for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
657             String value = map.getValue().split(":")[0];
658             if (value!=null){
659                 classMap.get(className).getEnumType();
660                 enumType = classMap.get(className).getEnumType().containsKey(value);
661                 if (!enumType){
662                     workingMap =  classMap.get(value).getRefAttribute();
663                     for ( Entry<String, String> subMab : workingMap.entrySet()){
664                         String value2 = subMab.getValue().split(":")[0];
665                         if (!subClassAttributes.containsValue(value2)){
666                             missingValues.put(subMab.getKey(), subMab.getValue());
667                         }
668                     }
669
670                 }
671             }
672         }
673
674         return missingValues;
675     }
676
677     public Map<String, Map<String, String>> recursiveReference(Map<String, MSAttributeObject> classMap, String className){
678
679         Map<String, Map<String, String>> returnObject = new HashMap<>();
680         Map<String, String> returnClass = getRefclass(classMap, className);
681         returnObject.put(className, returnClass);
682         for (Entry<String, String> reAttribute :returnClass.entrySet()){
683             if (reAttribute.getValue().split(":")[1].contains("MANY") &&
684                     classMap.get(reAttribute.getValue().split(":")[0]) != null){
685                     returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0]));
686             }
687
688         }
689
690         return returnObject;
691
692     }
693
694     public String createJson(Map<String, MSAttributeObject> classMap, String className) {
695         boolean enumType;
696         Map<String, Map<String, String>> myObject = new HashMap<>();
697         for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
698             String value = map.getValue().split(":")[0];
699             if (value!=null){
700                 enumType = classMap.get(className).getEnumType().containsKey(value);
701                 if (!enumType && map.getValue().split(":")[1].contains("MANY")){
702                         Map<String, Map<String, String>> testRecursive = recursiveReference(classMap, map.getValue().split(":")[0] );
703                         myObject.putAll(testRecursive);
704                 }
705             }
706         }
707
708         Gson gson = new Gson();
709         return gson.toJson(myObject);
710     }
711
712     public Map<String, String> getRefclass(Map<String, MSAttributeObject> classMap, String className){
713         HashMap<String, String> missingValues = new HashMap<>();
714
715         if (classMap.get(className).getAttribute()!=null || !classMap.get(className).getAttribute().isEmpty()){
716             missingValues.putAll(classMap.get(className).getAttribute());
717         }
718
719         if (classMap.get(className).getRefAttribute()!=null || !classMap.get(className).getRefAttribute().isEmpty()){
720             missingValues.putAll(classMap.get(className).getRefAttribute());
721         }
722
723         return missingValues;
724     }
725
726     public String createSubAttributes(List<String> dependency, Map<String, MSAttributeObject> classMap, String modelName) {
727
728         HashMap <String,  Object>  workingMap = new HashMap<>();
729         MSAttributeObject tempObject;
730         if (dependency!=null){
731             if (dependency.isEmpty()){
732                 return "{}";
733             }
734             dependency.add(modelName);
735             for (String element: dependency){
736                 tempObject = classMap.get(element);
737                 if (tempObject!=null){
738                     workingMap.putAll(classMap.get(element).getSubClass());
739                 }
740             }
741         }
742
743         return createJson(classMap, modelName);
744     }
745
746     public List<String> getFullDependencyList(List<String> dependency, Map<String,MSAttributeObject > classMap) {
747         ArrayList<String> returnList = new ArrayList<>();
748         ArrayList<String> workingList;
749         returnList.addAll(dependency);
750         for (String element : dependency ){
751             if (classMap.containsKey(element)){
752                 MSAttributeObject value = classMap.get(element);
753                 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
754                 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
755                 for(String depend : workingList) {
756                     updDependencyList(returnList, depend);
757                 }
758             }
759         }
760
761         return returnList;
762     }
763
764     private void updDependencyList(ArrayList<String> returnList, String depend) {
765         if (!returnList.contains(depend) && !depend.isEmpty()){
766             returnList.add(depend.trim());
767         }
768     }
769
770     /*
771      * For TOSCA Model
772      */
773     public String parseTosca(String fileName) {
774         Map<String, String> map = new LinkedHashMap<>();
775         try {
776             map = load(fileName);
777             if (map != null && map.get(ERROR) != null) {
778                 return map.get(ERROR);
779             }
780             parseDataAndPolicyNodes(map);
781             LinkedHashMap<String, String> dataMapForJson = parseDataNodes(map);
782             constructJsonForDataFields(dataMapForJson);
783             LinkedHashMap<String, LinkedHashMap<String, String>> mapKey = parsePolicyNodes(map);
784             createAttributes(mapKey);
785
786         } catch (IOException e) {
787             logger.error(e);
788         } catch (ParserException e) {
789             logger.error(e);
790             return e.getMessage();
791         }
792
793         return null;
794     }
795
796     @SuppressWarnings("unchecked")
797     public Map<String, String> load(String fileName) throws IOException, ParserException {
798         File newConfiguration = new File(fileName);
799         StringBuilder orderInfo = new StringBuilder("[");
800         Yaml yaml = new Yaml();
801         LinkedHashMap<Object, Object> yamlMap = null;
802         try (InputStream is = new FileInputStream(newConfiguration)) {
803             yamlMap = (LinkedHashMap<Object, Object>) yaml.load(is);
804         } catch (FileNotFoundException e) {
805             logger.error(e);
806         } catch (Exception e) {
807             logger.error(e);
808             throw new ParserException("Invalid TOSCA Model format. Please make sure it is a valid YAML file");
809         }
810
811         StringBuilder sb = new StringBuilder();
812         LinkedHashMap<String, String> settings = new LinkedHashMap<>();
813         if (yamlMap == null) {
814             return settings;
815         }
816
817         String message = validations(yamlMap);
818
819         if (message != null) {
820             settings.put(ERROR, message);
821             return settings;
822         }
823
824         findNode(yamlMap);
825
826         if (!isDuplicatedAttributes && orderedElements != null && !orderedElements.isEmpty()) {
827             orderedElements.stream().forEach(string -> {
828                 orderInfo.append(string);
829                 orderInfo.append(",");
830                 logger.info("Content: " + string);
831             });
832
833             orderInfo.append("]");
834
835             dataOrderInfo = orderInfo.toString();
836             dataOrderInfo = dataOrderInfo.replace(",]", "]");
837
838             logger.info("dataOrderInfo :" + dataOrderInfo);
839         }
840
841         List<String> path = new ArrayList<>();
842         serializeMap(settings, sb, path, yamlMap);
843         return settings;
844     }
845
846     @SuppressWarnings("unchecked")
847     private String validations(@SuppressWarnings("rawtypes") LinkedHashMap yamlMap) {
848
849         boolean isNoteTypeFound = false;
850         boolean isDataTypeFound = false;
851         boolean isToscaVersionKeyFound = false;
852         boolean isToscaVersionValueFound = false;
853         @SuppressWarnings("rawtypes")
854         Map m1 = new HashMap();
855         short order = 0;
856         if (yamlMap != null) {
857             // Get a set of the entries
858             @SuppressWarnings("rawtypes")
859             Set set = yamlMap.entrySet();
860             // Get an iterator
861             @SuppressWarnings("rawtypes")
862             Iterator i = set.iterator();
863             // Display elements
864             while (i.hasNext()) {
865                 @SuppressWarnings("rawtypes")
866                 Map.Entry me = (Map.Entry) i.next();
867
868                 if (TOSCA_DEFINITION_VERSION.equals(me.getKey())) {
869                     isToscaVersionKeyFound = true;
870                     order++;
871                     m1.put(TOSCA_DEFINITION_VERSION, order);
872                 }
873
874                 if ("tosca_simple_yaml_1_0_0".equals(me.getValue())) {
875                     isToscaVersionValueFound = true;
876                 }
877
878                 if (NODE_TYPE.equals(me.getKey())) {
879                     isNoteTypeFound = true;
880                     order++;
881                     m1.put(NODE_TYPE, order);
882                 }
883
884                 if (DATA_TYPE.equals(me.getKey())) {
885                     isDataTypeFound = true;
886                     order++;
887                     m1.put(DATA_TYPE, order);
888                 }
889
890             }
891
892             if (!isToscaVersionKeyFound || !isToscaVersionValueFound) {
893                 return "tosca_definitions_version is missing or invalid.";
894             }
895
896             if (!isNoteTypeFound) {
897                 return "node_types are missing or invalid.";
898             }
899
900             short version = (short) m1.get(TOSCA_DEFINITION_VERSION);
901
902             if (version > 1) {
903                 return "tosca_definitions_version should be defined first.";
904             }
905
906             short data = (short) m1.get(DATA_TYPE);
907             short node = (short) m1.get(NODE_TYPE);
908             if (isDataTypeFound && node > data) {
909                 return "node_types should be defined before data_types.";
910             }
911
912         }
913
914         return null;
915     }
916
917     @SuppressWarnings({"unchecked", "rawtypes"})
918     private void serializeMap(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
919             Map<Object, Object> yamlMap) {
920         for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
921
922             if (entry.getValue() instanceof Map) {
923                 path.add((String) entry.getKey());
924                 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
925                 path.remove(path.size() - 1);
926             } else if (entry.getValue() instanceof List) {
927                 path.add((String) entry.getKey());
928                 serializeList(settings, sb, path, (List) entry.getValue());
929                 path.remove(path.size() - 1);
930             } else {
931                 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
932             }
933         }
934     }
935
936     @SuppressWarnings("unchecked")
937     private void serializeList(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
938             List<String> yamlList) {
939         int counter = 0;
940         for (Object listEle : yamlList) {
941             if (listEle instanceof Map) {
942                 path.add(Integer.toString(counter));
943                 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
944                 path.remove(path.size() - 1);
945             } else if (listEle instanceof List) {
946                 path.add(Integer.toString(counter));
947                 serializeList(settings, sb, path, (List<String>) listEle);
948                 path.remove(path.size() - 1);
949             } else {
950                 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
951             }
952             counter++;
953         }
954     }
955
956     private void serializeValue(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path,
957             String name, Object value) {
958         if (value == null) {
959             return;
960         }
961         sb.setLength(0);
962         for (String pathEle : path) {
963             sb.append(pathEle).append('.');
964         }
965         sb.append(name);
966         settings.put(sb.toString(), value.toString());
967     }
968
969
970     void parseDataAndPolicyNodes(Map<String, String> map) {
971         for (String key : map.keySet()) {
972             if (key.contains("policy.nodes.Root")) {
973                 continue;
974             } else if (key.contains("policy.nodes")) {
975                 String wordToFind = "policy.nodes.";
976                 int indexForPolicyNode = key.indexOf(wordToFind);
977                 String subNodeString = key.substring(indexForPolicyNode + 13, key.length());
978
979                 stringBetweenDots(subNodeString);
980             } else if (key.contains("policy.data")) {
981                 String wordToFind = "policy.data.";
982                 int indexForPolicyNode = key.indexOf(wordToFind);
983                 String subNodeString = key.substring(indexForPolicyNode + 12, key.length());
984
985                 stringBetweenDotsForDataFields(subNodeString);
986             }
987         }
988     }
989
990     // Second index of dot should be returned.
991     public int stringBetweenDots(String str) {
992         String stringToSearch = str;
993         String[] ss = stringToSearch.split("\\.");
994         if (ss != null) {
995             int len = ss.length;
996             if (len > 2) {
997                 uniqueKeys.add(ss[2]);
998             }
999         }
1000
1001         return uniqueKeys.size();
1002     }
1003
1004
1005     public void stringBetweenDotsForDataFields(String str) {
1006         String stringToSearch = str;
1007         String[] ss = stringToSearch.split("\\.");
1008         if (ss != null) {
1009             int len = ss.length;
1010
1011             if (len > 2) {
1012                 uniqueDataKeys.add(ss[0] + "%" + ss[2]);
1013             }
1014         }
1015     }
1016
1017     void constructJsonForDataFields(LinkedHashMap<String, String> dataMapForJson) {
1018         LinkedHashMap<String, LinkedHashMap<String, String>> dataMapKey = new LinkedHashMap<>();
1019         LinkedHashMap<String, String> hmSub;
1020         for (Map.Entry<String, String> entry : dataMapForJson.entrySet()) {
1021             String uniqueDataKey = entry.getKey();
1022             String[] uniqueDataKeySplit = uniqueDataKey.split("%");
1023             String value = dataMapForJson.get(uniqueDataKey);
1024             if (dataMapKey.containsKey(uniqueDataKeySplit[0])) {
1025                 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
1026                 hmSub.put(uniqueDataKeySplit[1], value);
1027             } else {
1028                 hmSub = new LinkedHashMap<>();
1029                 hmSub.put(uniqueDataKeySplit[1], value);
1030             }
1031
1032             dataMapKey.put(uniqueDataKeySplit[0], hmSub);
1033         }
1034
1035         JSONObject mainObject = new JSONObject();
1036         JSONObject json;
1037         for (Map.Entry<String, LinkedHashMap<String, String>> entry : dataMapKey.entrySet()) {
1038             String s = entry.getKey();
1039             json = new JSONObject();
1040             HashMap<String, String> jsonHm = dataMapKey.get(s);
1041             for (Map.Entry<String, String> entryMap : jsonHm.entrySet()) {
1042                 String key = entryMap.getKey();
1043                 json.put(key, jsonHm.get(key));
1044             }
1045             mainObject.put(s, json);
1046         }
1047         Iterator<String> keysItr = mainObject.keys();
1048         while (keysItr.hasNext()) {
1049             String key = keysItr.next();
1050             String value = mainObject.get(key).toString();
1051             retmap.put(key, value);
1052         }
1053
1054         logger.info("#############################################################################");
1055         logger.info(mainObject);
1056         logger.info("###############################################################################");
1057     }
1058
1059     LinkedHashMap<String, String> parseDataNodes(Map<String, String> map) {
1060         LinkedHashMap<String, String> dataMapForJson = new LinkedHashMap<>();
1061         matchableValues = new HashMap<>();
1062         for (String uniqueDataKey : uniqueDataKeys) {
1063             if (uniqueDataKey.contains("%")) {
1064                 String[] uniqueDataKeySplit = uniqueDataKey.split("%");
1065                 String findType = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + TYPE;
1066                 String typeValue = map.get(findType);
1067                 logger.info(typeValue);
1068
1069                 String findRequired = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + REQUIRED;
1070                 String requiredValue = map.get(findRequired);
1071
1072                 String matchable = DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + MATCHABLE;
1073
1074                 String matchableValue = map.get(matchable);
1075
1076                 if ("true".equalsIgnoreCase(matchableValue)) {
1077                     String key = uniqueDataKeySplit[uniqueDataKeySplit.length - 1];
1078                     matchableValues.put(key, MATCHINGTRUE);
1079                 }
1080
1081                 if (requiredValue == null || requiredValue.isEmpty()) {
1082                     requiredValue = "false";
1083                 }
1084                 if (INTEGER.equalsIgnoreCase(typeValue) || STRING.equalsIgnoreCase(typeValue)
1085                         || typeValue.equalsIgnoreCase(BOOLEAN)) {
1086                     String findDefault =
1087                             DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + DEFAULT;
1088                     String findDescription =
1089                             DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1] + DESCRIPTION;
1090                     String defaultValue = map.get(findDefault);
1091                     String descriptionDefined = map.get(findDescription);
1092                     logger.info("defaultValue is:" + defaultValue);
1093                     logger.info("requiredValue is:" + requiredValue);
1094
1095                     StringBuilder attributeIndividualStringBuilder = new StringBuilder();
1096                     attributeIndividualStringBuilder.append(typeValue + DEFAULTVALUE);
1097                     attributeIndividualStringBuilder.append(defaultValue + REQUIREDVALUE);
1098                     attributeIndividualStringBuilder.append(requiredValue + MANYFALSE);
1099                     attributeIndividualStringBuilder.append(DESCRIPTION_TOKEN + descriptionDefined);
1100                     dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
1101                 } else if (LIST.equalsIgnoreCase(typeValue) || MAP.equalsIgnoreCase(typeValue)) {
1102                     logger.info("requiredValue is:" + requiredValue);
1103                     String findList =
1104                             DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1105                                     + ".entry_schema.type";
1106                     String findDefaultValue =
1107                             DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1108                                     + ".entry_schema.default";
1109                     String findDescription =
1110                             DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1111                                     + ".entry_schema.description";
1112                     String listValue = map.get(findList);
1113                     String defaultValue = map.get(findDefaultValue);
1114                     String description = map.get(findDescription);
1115                     if (listValue != null) {
1116                         logger.info("Type of list is:" + listValue);
1117                         // Its userdefined
1118                         if (listValue.contains(".")) {
1119                             String trimValue = listValue.substring(listValue.lastIndexOf('.') + 1);
1120                             StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1121                             referenceIndividualStringBuilder.append(trimValue + REQUIREDVALUE);
1122                             referenceIndividualStringBuilder.append(requiredValue + MANYTRUE);
1123                             referenceIndividualStringBuilder.append(DESCRIPTION_TOKEN + description);
1124                             dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1125                         } else {  // Its string
1126                             StringBuilder stringListItems = new StringBuilder();
1127                             if (LIST.equalsIgnoreCase(typeValue)) {
1128                                 stringListItems.append(uniqueDataKeySplit[1].toUpperCase() + DEFAULTVALUE
1129                                         + defaultValue + REQUIREDVALUE + requiredValue + MANYFALSE + DESCRIPTION_TOKEN
1130                                         + description);
1131                             } else if (MAP.equalsIgnoreCase(typeValue)) {
1132                                 stringListItems.append(uniqueDataKeySplit[1].toUpperCase() + DEFAULTVALUE
1133                                         + defaultValue + REQUIREDVALUE + requiredValue + MANYTRUE + DESCRIPTION_TOKEN
1134                                         + description);
1135                             }
1136                             dataMapForJson.put(uniqueDataKey, stringListItems.toString());
1137                             dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase() + "=[");
1138                             for (int i = 0; i < 10; i++) {
1139                                 String findConstraints =
1140                                         DATATYPE + uniqueDataKeySplit[0] + PROPERTIES + uniqueDataKeySplit[1]
1141                                                 + ".entry_schema.constraints.0.valid_values." + i;
1142                                 String constraintsValue = map.get(findConstraints);
1143                                 logger.info(constraintsValue);
1144                                 if (constraintsValue == null) {
1145                                     break;
1146                                 } else {
1147                                     logger.info("constraintsValue => " + constraintsValue);
1148                                     if (constraintsValue.contains("=")) {
1149                                         constraintsValue = constraintsValue.replace("=", "equal-sign");
1150                                     }
1151                                     dataConstraints.add(constraintsValue);
1152                                     dataListBuffer.append(constraintsValue + ",");
1153                                 }
1154                             }
1155                             dataListBuffer.append("]#");
1156                             logger.info(dataListBuffer);
1157                         }
1158                     }
1159                 } else {
1160                     String findUserDefined =
1161                             DATATYPE + uniqueDataKeySplit[0] + "." + PROPERTIES_KEY + "." + uniqueDataKeySplit[1]
1162                                     + ".type";
1163                     String findDescription =
1164                             DATATYPE + uniqueDataKeySplit[0] + "." + PROPERTIES_KEY + "." + uniqueDataKeySplit[1]
1165                                     + ".description";
1166                     String userDefinedValue = map.get(findUserDefined);
1167                     String description = map.get(findDescription);
1168                     String trimValue = userDefinedValue.substring(userDefinedValue.lastIndexOf('.') + 1);
1169                     StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1170                     referenceIndividualStringBuilder.append(trimValue + REQUIREDVALUE);
1171                     referenceIndividualStringBuilder.append(requiredValue + MANYFALSE);
1172                     referenceIndividualStringBuilder.append(DESCRIPTION_TOKEN + description);
1173                     dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1174
1175                 }
1176             } else {
1177                 matchableValues.put(uniqueDataKey, MATCHINGTRUE);
1178             }
1179         }
1180
1181         return dataMapForJson;
1182     }
1183
1184     LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String, String> map)
1185             throws ParserException {
1186         LinkedHashMap<String, LinkedHashMap<String, String>> mapKey = new LinkedHashMap<>();
1187         for (String uniqueKey : uniqueKeys) {
1188             LinkedHashMap<String, String> hm;
1189
1190             for (Entry<String, String> entry : map.entrySet()) {
1191                 String key = entry.getKey();
1192                 if (key.contains(uniqueKey) && key.contains("policy.nodes")) {
1193                     if (mapKey.containsKey(uniqueKey)) {
1194                         hm = mapKey.get(uniqueKey);
1195                         String keyStr = key.substring(key.lastIndexOf('.') + 1);
1196                         String valueStr = map.get(key);
1197                         if ("type".equalsIgnoreCase(keyStr)
1198                                 && key.contains("entry_schema.0.type") || key.contains("entry_schema.type")
1199                                         && valueStr.contains("policy.data.")) {
1200                             throw new ParserException(
1201                                 "For user defined object type, Please make sure no space between 'type:' and object "
1202                                             + valueStr);
1203
1204                         }
1205                         if ("type".equals(keyStr)) {
1206                             if (!key.contains("entry_schema")) {
1207                                 hm.put(keyStr, valueStr);
1208                             }
1209                         } else {
1210                             hm.put(keyStr, valueStr);
1211                         }
1212
1213                     } else {
1214                         hm = new LinkedHashMap<>();
1215                         String keyStr = key.substring(key.lastIndexOf('.') + 1);
1216                         String valueStr = map.get(key);
1217
1218                         if (("type").equals(keyStr)) {
1219                             if (!key.contains("entry_schema")) {
1220                                 hm.put(keyStr, valueStr);
1221                             }
1222                         } else {
1223                             hm.put(keyStr, valueStr);
1224                         }
1225                         mapKey.put(uniqueKey, hm);
1226                     }
1227                 }
1228             }
1229         }
1230         return mapKey;
1231     }
1232
1233     void createAttributes(LinkedHashMap<String, LinkedHashMap<String, String>> mapKey) {
1234         StringBuilder attributeStringBuilder = new StringBuilder();
1235         StringBuilder referenceStringBuilder = new StringBuilder();
1236         StringBuilder listBuffer = new StringBuilder();
1237         List<String> constraints = new ArrayList<>();
1238         for (Map.Entry<String, LinkedHashMap<String, String>> entry : mapKey.entrySet()) {
1239             String keySetString = entry.getKey();
1240             LinkedHashMap<String, String> keyValues = mapKey.get(keySetString);
1241             if (keyValues.get("type") != null && (STRING.equalsIgnoreCase(keyValues.get("type"))
1242                     || INTEGER.equalsIgnoreCase(keyValues.get("type"))
1243                     || BOOLEAN.equalsIgnoreCase(keyValues.get("type")))) {
1244                 StringBuilder attributeIndividualStringBuilder = new StringBuilder();
1245                 attributeIndividualStringBuilder.append(keySetString + "=");
1246                 attributeIndividualStringBuilder.append(keyValues.get("type") + DEFAULTVALUE);
1247                 attributeIndividualStringBuilder.append(keyValues.get("default") + REQUIREDVALUE);
1248                 attributeIndividualStringBuilder.append(keyValues.get("required") + MANYFALSE);
1249                 attributeIndividualStringBuilder.append(DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1250                 attributeStringBuilder.append(attributeIndividualStringBuilder + ",");
1251                 if (keyValues.get(MATCHABLEKEY) != null && "true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1252                     matchableValues.put(keySetString, MATCHINGTRUE);
1253                 }
1254             } else if (LIST.equalsIgnoreCase(keyValues.get("type"))) {
1255                 if ("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1256                     matchableValues.put(keySetString, MATCHINGTRUE);
1257                 }
1258                 // List Data type
1259                 Set<String> keys = keyValues.keySet();
1260                 Iterator<String> itr = keys.iterator();
1261                 boolean isDefinedType = false;
1262                 while (itr.hasNext()) {
1263                     String key = itr.next();
1264                     if ((!("type").equals(key) || ("required").equals(key))) {
1265                         String value = keyValues.get(key);
1266                         // The "." in the value determines if its a string or a user defined type.
1267                         if (!value.contains(".")) {
1268                             // This is string
1269                             if (StringUtils.isNumeric(key)) { // only integer key for the value of Constrains
1270                                 constraints.add(keyValues.get(key));
1271                             }
1272                         } else {
1273                             // This is user defined type
1274                             String trimValue = value.substring(value.lastIndexOf('.') + 1);
1275                             StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1276                             referenceIndividualStringBuilder.append(keySetString + "=" + trimValue + MANYTRUE
1277                                     + DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1278                             referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1279                             isDefinedType = true;
1280                         }
1281                     }
1282
1283                 }
1284
1285                 if (!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))) {
1286                     if (constraints == null || constraints.isEmpty()) {
1287                         referenceStringBuilder.append(keySetString + "=MANY-true" + ",");
1288                     }
1289                 }
1290             } else {
1291                 // User defined Datatype.
1292                 if ("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))) {
1293                     matchableValues.put(keySetString, MATCHINGTRUE);
1294                 }
1295                 String value = keyValues.get("type");
1296                 if (value != null && !value.isEmpty()) {
1297                     String trimValue = value.substring(value.lastIndexOf('.') + 1);
1298                     StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1299                     referenceIndividualStringBuilder.append(keySetString + "=" + trimValue + MANYFALSE
1300                             + DESCRIPTION_TOKEN + keyValues.get(DESCRIPTION_KEY));
1301                     referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1302                 } else {
1303                     logger.info("keyValues.get(type) is null/empty");
1304                 }
1305
1306             }
1307             if (constraints != null && !constraints.isEmpty()) {
1308                 // List handling.
1309                 listBuffer.append(keySetString.toUpperCase() + "=[");
1310                 for (String str : constraints) {
1311                     listBuffer.append(str + ",");
1312                 }
1313                 listBuffer.append("]#");
1314                 logger.info(listBuffer);
1315
1316
1317                 StringBuilder referenceIndividualStringBuilder = new StringBuilder();
1318                 referenceIndividualStringBuilder.append(keySetString + "=" + keySetString.toUpperCase() + MANYFALSE);
1319                 referenceStringBuilder.append(referenceIndividualStringBuilder + ",");
1320                 constraints.clear();
1321             }
1322         }
1323
1324         dataListBuffer.append(listBuffer);
1325
1326
1327         logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1328         logger.info("Whole attribute String is:" + attributeStringBuilder);
1329         logger.info("Whole reference String is:" + referenceStringBuilder);
1330         logger.info("List String is:" + listBuffer);
1331         logger.info("Data list buffer is:" + dataListBuffer);
1332         logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1333
1334         this.listConstraints = dataListBuffer.toString();
1335         this.referenceAttributes = referenceStringBuilder.toString();
1336         this.attributeString = attributeStringBuilder.toString();
1337     }
1338
1339     @SuppressWarnings("unchecked")
1340     private void findNode(LinkedHashMap<Object, Object> map) {
1341
1342         map.forEach((key, value) -> {
1343             // if the value is properties and its type is map object, then save all the keys
1344             if (key.equals(PROPERTIES_KEY) && value instanceof Map) {
1345                 saveNodes((LinkedHashMap<?, ?>) value);
1346                 if (isDuplicatedAttributes) {
1347                     orderedElements = new ArrayList<>();
1348                     return;
1349                 }
1350             }
1351
1352             if (!"policy.nodes.Root".equals(key) && value instanceof Map) {
1353                 // value is a Map object, then make a recursive call
1354                 findNode((LinkedHashMap<Object, Object>) value);
1355             }
1356         });
1357
1358     }
1359
1360     private void saveNodes(LinkedHashMap<?, ?> map) {
1361
1362         for (Entry<?, ?> entry : map.entrySet()) {
1363
1364             if (orderedElements.indexOf((String) entry.getKey()) >= 0) { // duplicated attribute names
1365                 isDuplicatedAttributes = true;
1366                 return;
1367             } else {
1368                 orderedElements.add((String) entry.getKey());
1369             }
1370         }
1371     }
1372
1373     public String getAttributeString() {
1374         return attributeString;
1375     }
1376
1377     public void setAttributeString(String attributeString) {
1378         this.attributeString = attributeString;
1379     }
1380
1381     public LinkedHashMap<String, Object> getRetmap() {
1382         return retmap;
1383     }
1384
1385     public void setRetmap(LinkedHashMap<String, Object> retmap) {
1386         this.retmap = retmap;
1387     }
1388
1389     public Map<String, String> getMatchableValues() {
1390         return matchableValues;
1391     }
1392
1393     public void setMatchableValues(Map<String, String> matchableValues) {
1394         this.matchableValues = matchableValues;
1395     }
1396
1397     public String getReferenceAttributes() {
1398         return referenceAttributes;
1399     }
1400
1401     public void setReferenceAttributes(String referenceAttributes) {
1402         this.referenceAttributes = referenceAttributes;
1403     }
1404
1405     public String getListConstraints() {
1406         return listConstraints;
1407     }
1408
1409     public void setListConstraints(String listConstraints) {
1410         this.listConstraints = listConstraints;
1411     }
1412
1413     public String getDataOrderInfo() {
1414         return dataOrderInfo;
1415     }
1416
1417     public void setDataOrderInfo(String dataOrderInfo) {
1418         this.dataOrderInfo = dataOrderInfo;
1419     }
1420
1421 }