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