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