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