Merge "Fix final 10 vulnerabilities in policy/engine"
[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.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import org.apache.commons.lang.StringUtils;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.eclipse.emf.common.util.EList;
36 import org.eclipse.emf.common.util.EMap;
37 import org.eclipse.emf.common.util.Enumerator;
38 import org.eclipse.emf.common.util.TreeIterator;
39 import org.eclipse.emf.common.util.URI;
40 import org.eclipse.emf.ecore.EAnnotation;
41 import org.eclipse.emf.ecore.EClass;
42 import org.eclipse.emf.ecore.EClassifier;
43 import org.eclipse.emf.ecore.EEnum;
44 import org.eclipse.emf.ecore.EEnumLiteral;
45 import org.eclipse.emf.ecore.EObject;
46 import org.eclipse.emf.ecore.EPackage;
47 import org.eclipse.emf.ecore.EReference;
48 import org.eclipse.emf.ecore.EStructuralFeature;
49 import org.eclipse.emf.ecore.impl.EAttributeImpl;
50 import org.eclipse.emf.ecore.impl.EEnumImpl;
51 import org.eclipse.emf.ecore.resource.Resource;
52 import org.eclipse.emf.ecore.resource.ResourceSet;
53 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
54 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
55 import org.json.JSONObject;
56 import org.onap.policy.rest.XACMLRestProperties;
57
58 import com.att.research.xacml.util.XACMLProperties;
59 import com.google.gson.Gson;
60
61
62 public class MSModelUtils {
63
64         private static final Log logger = LogFactory.getLog(MSModelUtils.class);
65
66         private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
67         private HashMap<String, String> enumMap = new HashMap<>();
68         private HashMap<String, String> matchingClass = new HashMap<>();
69         private String configuration = "configuration";
70         private String dictionary = "dictionary";
71         private String onap = "";
72         private String policy = "";
73         private String eProxyURI = "eProxyURI:";
74         
75         public MSModelUtils(String onap, String policy){
76                 this.onap = onap;
77                 this.policy = policy;
78         }
79
80         private enum ANNOTATION_TYPE{
81                 MATCHING, VALIDATION, DICTIONARY
82         };
83
84         public enum MODEL_TYPE {
85                 XMI
86         };
87
88
89         public Map<String, MSAttributeObject> processEpackage(String file, MODEL_TYPE model){
90                 if (model == MODEL_TYPE.XMI ){
91                         processXMIEpackage(file);
92                 }
93                 return classMap;
94
95         } 
96
97         private void processXMIEpackage(String xmiFile){
98                 EPackage root = getEpackage(xmiFile);
99                 TreeIterator<EObject> treeItr = root.eAllContents();
100                 String className;
101                 String returnValue;
102
103                 //    Pulling out dependency from file
104                 while (treeItr.hasNext()) {         
105                         EObject obj = treeItr.next();
106                         if (obj instanceof EClassifier) {
107                                 EClassifier eClassifier = (EClassifier) obj;
108                                 className = eClassifier.getName();
109
110                                 if (obj instanceof EEnum) {
111                                         enumMap.putAll(getEEnum(obj));
112                                 }else if (obj instanceof EClass) {
113                                         String temp = getDependencyList(eClassifier, className).toString();
114                                         returnValue = StringUtils.replaceEach(temp, new String[]{"[", "]"}, new String[]{"", ""});
115                                         getAttributes(className, returnValue, root);
116                                 }                                       
117                         }
118                 }
119
120                 if (!enumMap.isEmpty()){
121                         addEnumClassMap();
122                 }
123                 if (!matchingClass.isEmpty()){
124                         CheckForMatchingClass();
125                 }
126         }
127
128         private void CheckForMatchingClass() {
129                 HashMap<String, String> tempAttribute = new HashMap<>();
130
131                 for (Entry<String, String> set : matchingClass.entrySet()){
132                         String key = set.getKey();
133                         if (classMap.containsKey(key)){
134                                 Map<String, String> listAttributes = classMap.get(key).getAttribute();
135                                 Map<String, String> listRef = classMap.get(key).getRefAttribute();
136                                 for (  Entry<String, String> eSet : listAttributes.entrySet()){
137                                         String key2 = eSet.getKey();
138                                         tempAttribute.put(key2, "matching-true");
139                                 }
140                                 for (  Entry<String, String> eSet : listRef.entrySet()){
141                                         String key3 = eSet.getKey();
142                                         tempAttribute.put(key3, "matching-true");
143                                 }
144
145                         }
146                         UpdateMatching(tempAttribute, key);
147                 }
148
149         }
150
151
152
153         private void UpdateMatching(HashMap<String, String> tempAttribute, String key) {
154                 Map<String, MSAttributeObject> newClass = classMap;
155
156                 for (Entry<String, MSAttributeObject> updateClass :  newClass.entrySet()){
157                         Map<String, String> valueMap = updateClass.getValue().getMatchingSet();
158                         String keymap = updateClass.getKey();
159                         if (valueMap.containsKey(key)){
160                                 Map<String, String> modifyMap = classMap.get(keymap).getMatchingSet();
161                                 modifyMap.remove(key);
162                                 modifyMap.putAll(tempAttribute);
163                                 classMap.get(keymap).setMatchingSet(modifyMap);
164                         }
165
166                 }
167         }
168
169         private void addEnumClassMap() {
170                 for (Entry<String, MSAttributeObject> value :classMap.entrySet()){
171                         value.getValue().setEnumType(enumMap);
172                 }
173         }
174
175         private EPackage getEpackage(String xmiFile) {
176                 ResourceSet resSet = new ResourceSetImpl();
177                 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
178                 Map<String, Object> m = reg.getExtensionToFactoryMap();
179                 m.put("xmi", new XMIResourceFactoryImpl());
180                 Resource resource = resSet.getResource(URI.createFileURI(xmiFile), true);
181                 try {
182                         resource.load(Collections.emptyMap());
183                 } catch (IOException e) {
184                         logger.error("Error loading Encore Resource for new Model" + e);
185                 }
186
187                 return (EPackage) resource.getContents().get(0);
188         }
189
190         private HashMap<String, String> getEEnum(EObject obj) {
191                 List<String> valueList = new ArrayList<>();
192                 HashMap<String, String> returnMap = new HashMap<>();
193                 EEnum eenum = (EEnum)obj;
194
195                 String name = eenum.getName();
196                 for (EEnumLiteral eEnumLiteral : eenum.getELiterals())
197                 {
198                         Enumerator instance = eEnumLiteral.getInstance();
199                         String value = instance.getLiteral();
200                         valueList.add(value);
201                 }
202                 returnMap.put(name, valueList.toString());
203                 return returnMap;
204         }
205
206         public void getAttributes(String className, String dependency, EPackage root) {
207                 List<String> dpendList = new ArrayList<>();
208                 if (dependency!=null){
209                         dpendList = new ArrayList<>(Arrays.asList(dependency.split(",")));
210                 }
211                 MSAttributeObject msAttributeObject = new MSAttributeObject();
212                 msAttributeObject.setClassName(className);
213                 String extendClass = getSubTypes(root, className);
214                 Map<String, String> returnRefList = getRefAttributeList(root, className, extendClass);
215                 Map<String, String> returnAttributeList = getAttributeList(root, className, extendClass);
216                 Map<String, Object> returnSubList = getSubAttributeList(root, className, extendClass);
217                 HashMap<String, String> returnAnnotation = getAnnotation(root, className, extendClass);
218                 msAttributeObject.setAttribute(returnAttributeList);
219                 msAttributeObject.setRefAttribute(returnRefList);
220                 msAttributeObject.setSubClass(returnSubList);
221                 msAttributeObject.setDependency(dpendList.toString());
222                 msAttributeObject.addMatchingSet(returnAnnotation);
223                 msAttributeObject.setPolicyTempalate(isPolicyTemplate(root, className));
224
225                 this.classMap.put(className, msAttributeObject);        
226         }
227
228         private HashMap<String, String> getAnnotation(EPackage root, String className, String extendClass) {
229                 TreeIterator<EObject> treeItr = root.eAllContents();
230                 boolean requiredAttribute = false; 
231                 boolean requiredMatchAttribute = false;
232                 HashMap<String, String> annotationSet = new HashMap<>();
233                 String  matching;
234                 String range;
235                 String dictionary;
236
237                 //    Pulling out dependency from file
238                 while (treeItr.hasNext()) {         
239                         EObject obj = treeItr.next();
240                         if (obj instanceof EClassifier) {
241                                 requiredAttribute = isRequiredAttribute(obj,  className );
242                                 requiredMatchAttribute = isRequiredAttribute(obj,  extendClass );
243                         }
244
245                         if (requiredAttribute){
246                                 if (obj instanceof EStructuralFeature) {
247                                         EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
248                                         if (!eStrucClassifier.getEAnnotations().isEmpty()) {
249                                                 matching  = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
250                                                 if (matching!=null){
251                                                         annotationSet.put(eStrucClassifier.getName(), matching);
252                                                 }
253                                                 range  = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
254                                                 if (range!=null){
255                                                         annotationSet.put(eStrucClassifier.getName(), range);
256                                                 }
257                                                 dictionary = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
258                                                 if (dictionary!=null){
259                                                         annotationSet.put(eStrucClassifier.getName(), dictionary);
260                                                 }
261                                         }
262                                 }
263                         } else if (requiredMatchAttribute){
264                                 if (obj instanceof EStructuralFeature) {
265                                         EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
266                                         if (!eStrucClassifier.getEAnnotations().isEmpty()) {
267                                                 matching  = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
268                                                 if (matching!=null){
269                                                         if (obj instanceof EReference){
270                                                                 EClass refType = ((EReference) obj).getEReferenceType();
271                                                                 annotationSet.put(refType.getName(), matching);
272                                                                 matchingClass.put(refType.getName(), matching);
273                                                         }else{
274                                                                 annotationSet.put(eStrucClassifier.getName(), matching);
275                                                         }
276                                                 }
277                                         }
278                                 }
279                         }
280                 }
281                 return annotationSet;
282         }
283
284         private Map<String, Object> getSubAttributeList(EPackage root, String className , String superClass) {
285                 TreeIterator<EObject> treeItr = root.eAllContents();
286                 boolean requiredAttribute = false; 
287                 Map<String, Object> subAttribute = new HashMap<>();
288                 int rollingCount = 0;
289                 int processClass = 0;
290                 boolean annotation;
291
292                 //    Pulling out dependency from file
293                 while (treeItr.hasNext() && rollingCount < 2) {  
294
295                         EObject obj = treeItr.next();
296                         if (obj instanceof EClassifier) {
297                                 if (isRequiredAttribute(obj,  className ) || isRequiredAttribute(obj,  superClass )){
298                                         requiredAttribute = true;
299                                 }else {
300                                         requiredAttribute = false;
301                                 }
302                                 if (requiredAttribute){
303                                         processClass++;
304                                 }
305                                 rollingCount = rollingCount+processClass;
306                         }
307
308                         if (requiredAttribute)   {
309                                 if (obj instanceof EStructuralFeature) {
310                                         EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
311                                         if (!eStrucClassifier.getEAnnotations().isEmpty()) {
312                                                 annotation = annotationTest(eStrucClassifier, configuration, onap);
313                                                 if (annotation &&  obj instanceof EReference) {
314                                                         EClass refType = ((EReference) obj).getEReferenceType();
315                                                         if(!refType.toString().contains(eProxyURI)){
316                                                                 String required = ":required-false";
317                                                                 if(eStrucClassifier.getLowerBound() == 1){
318                                                                         required = ":required-true";
319                                                                 }
320                                                                 subAttribute.put(eStrucClassifier.getName(), refType.getName() + required);                                             
321                                                         }
322                                                 }       
323                                         }
324                                 }
325                         }
326                 }
327                 return subAttribute;
328         }
329
330         public String checkDefultValue(String defultValue) {
331                 if (defultValue!=null){
332                         return ":defaultValue-"+ defultValue;
333                 }
334                 return ":defaultValue-NA";
335
336         }
337
338         public String checkRequiredPattern(int upper, int lower) {      
339
340                 String pattern = XACMLProperties.getProperty(XACMLRestProperties.PROP_XCORE_REQUIRED_PATTERN);
341
342                 if (pattern!=null){
343                         if (upper == Integer.parseInt(pattern.split(",")[1]) && lower==Integer.parseInt(pattern.split(",")[0])){
344                                 return ":required-true";
345                         }
346                 }
347
348                 return ":required-false";
349         }
350
351         public JSONObject buildJavaObject(Map<String, String> map){
352
353                 return  new JSONObject(map);
354         }
355
356         public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass){
357
358                 TreeIterator<EObject> treeItr = root.eAllContents();
359                 boolean requiredAttribute = false; 
360                 HashMap<String, String> refAttribute = new HashMap<>();
361                 int rollingCount = 0;
362                 int processClass = 0;
363                 boolean annotation;
364                 //    Pulling out dependency from file
365                 while (treeItr.hasNext()) {         
366                         EObject obj = treeItr.next();
367                         if (obj instanceof EClassifier) {
368                                 if (isRequiredAttribute(obj,  className ) || isRequiredAttribute(obj,  superClass )){
369                                         requiredAttribute = true;
370                                 }else {
371                                         requiredAttribute = false;
372                                 }       
373                                 if (requiredAttribute){
374                                         processClass++;
375                                 }
376                                 rollingCount = rollingCount+processClass;
377                         }
378
379                         if (requiredAttribute)   {
380                                 if (obj instanceof EStructuralFeature) {
381                                         EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
382                                         if (!eStrucClassifier.getEAnnotations().isEmpty()) {
383                                                 annotation = annotationTest(eStrucClassifier, configuration, onap);
384                                                 if ( annotation &&  obj instanceof EReference) {
385                                                         EClass refType = ((EReference) obj).getEReferenceType();
386                                                         if(refType.toString().contains(eProxyURI)){
387                                                                 String one = refType.toString().split(eProxyURI)[1];
388                                                                 String refValue = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});                                                    
389                                                                 refAttribute.put(eStrucClassifier.getName(), refValue);                                                 
390                                                         } else {
391                                                                 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
392                                                                 String required = ":required-false";
393                                                                 if(((EStructuralFeature) obj).getLowerBound() == 1){
394                                                                         required = ":required-true";
395                                                                 }
396                                                                 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
397                                                         }
398                                                 } else if (annotation &&  obj instanceof EAttributeImpl){
399                                                         EClassifier refType = ((EAttributeImpl) obj).getEType();
400                                                         if (refType instanceof EEnumImpl){
401                                                                 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
402                                                                 String required = ":required-false";
403                                                                 if(((EStructuralFeature) obj).getLowerBound() == 1){
404                                                                         required = ":required-true";
405                                                                 }
406                                                                 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);                                                     
407                                                         }
408                                                 }       
409                                         }
410                                 }
411                         }
412                 }
413                 
414                 return refAttribute;
415         }
416
417         private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
418                 String annotationType;
419                 EAnnotation eAnnotation;
420                 String onapType;
421                 String onapValue;
422
423                 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
424
425                 for (int i = 0; i < value.size(); i++){
426                         annotationType = value.get(i).getSource();
427                         eAnnotation = eStrucClassifier.getEAnnotations().get(i);
428                         onapType = eAnnotation.getDetails().get(0).getValue();
429                         onapValue = eAnnotation.getDetails().get(0).getKey();
430                         if (annotationType.contains(type) && onapType.contains(annotation)){
431                                 return true;
432                         } else if (annotationType.contains(type) && onapValue.contains(annotation)){
433                                 return true;
434                         }
435                 }
436
437                 return false;
438         }
439
440
441         private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
442                 String annotationType;
443                 EAnnotation eAnnotation;
444                 String onapType;
445                 String onapValue = null;
446
447                 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
448
449                 for (int i = 0; i < value.size(); i++){
450                         annotationType = value.get(i).getSource();
451                         eAnnotation = eStrucClassifier.getEAnnotations().get(i);
452                         onapType = eAnnotation.getDetails().get(0).getKey();
453                         if (annotationType.contains(type) && onapType.compareToIgnoreCase(annotation.toString())==0){
454                                 onapValue = eAnnotation.getDetails().get(0).getValue();
455                                 if (annotation == ANNOTATION_TYPE.VALIDATION){
456                                         return onapValue;
457                                 } else {
458                                         return onapType + "-" + onapValue;
459                                 }
460                         }
461                 }
462
463                 return onapValue;
464         }
465         public boolean isRequiredAttribute(EObject obj, String className){
466                 EClassifier eClassifier = (EClassifier) obj;
467                 String workingClass = eClassifier.getName();
468                 workingClass.trim();
469                 if (workingClass.equalsIgnoreCase(className)){
470                         return  true;
471                 }
472
473                 return false;
474         } 
475
476         private boolean isPolicyTemplate(EPackage root, String className){
477
478                 for (EClassifier classifier : root.getEClassifiers()){ 
479                         if (classifier instanceof EClass) { 
480                                 EClass eClass = (EClass)classifier; 
481                                 if (eClass.getName().contentEquals(className)){
482                                         EList<EAnnotation> value = eClass.getEAnnotations();
483                                         for (EAnnotation workingValue : value){
484                                                 EMap<String, String> keyMap = workingValue.getDetails();        
485                                                 if (keyMap.containsKey("policyTemplate")){
486                                                         return true;
487                                                 }
488                                         }       
489                                 }
490                         }
491                 }
492                 return false;
493         }
494         private String getSubTypes(EPackage root, String className) {
495                 String returnSubTypes = null;
496                 for (EClassifier classifier : root.getEClassifiers()){ 
497                         if (classifier instanceof EClass) { 
498                                 EClass eClass = (EClass)classifier; 
499
500                                 for (EClass eSuperType : eClass.getEAllSuperTypes()) 
501                                 { 
502                                         if (eClass.getName().contentEquals(className)){
503                                                 returnSubTypes = eSuperType.getName();
504                                         }
505                                 } 
506                         } 
507                 } 
508                 return returnSubTypes;
509         } 
510
511         public Map<String, String> getAttributeList(EPackage root, String className, String superClass){
512
513                 TreeIterator<EObject> treeItr = root.eAllContents();
514                 boolean requiredAttribute = false; 
515                 HashMap<String, String> refAttribute = new HashMap<>();
516                 boolean annotation;
517                 boolean dictionaryTest;
518                 String defaultValue;
519                 String eType;
520
521                 //    Pulling out dependency from file
522                 while (treeItr.hasNext()) {         
523                         EObject obj = treeItr.next();
524                         if (obj instanceof EClassifier) {
525                                 if (isRequiredAttribute(obj,  className ) || isRequiredAttribute(obj,  superClass )){
526                                         requiredAttribute = true;
527                                 }else {
528                                         requiredAttribute = false;
529                                 }
530
531                         }
532
533                         if (requiredAttribute){
534                                 if (obj instanceof EStructuralFeature) {
535                                         EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
536                                         if (!eStrucClassifier.getEAnnotations().isEmpty()) {
537                                                 annotation = annotationTest(eStrucClassifier, configuration, onap);
538                                                 dictionaryTest = annotationTest(eStrucClassifier, dictionary, policy);
539                                                 EClassifier refType = ((EStructuralFeature) obj).getEType();
540                                                 if (annotation && !(obj instanceof EReference) && !(refType instanceof EEnumImpl)) {
541                                                         String name = eStrucClassifier.getName();
542                                                         if (dictionaryTest){
543                                                                 eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
544                                                         }else {
545                                                                 eType = eStrucClassifier.getEType().getInstanceClassName();
546                                                         }
547                                                         defaultValue = checkDefultValue(((EStructuralFeature) obj).getDefaultValueLiteral());
548
549                                                         String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
550                                                         String required = checkRequiredPattern(((EStructuralFeature) obj).getUpperBound(), ((EStructuralFeature) obj).getLowerBound());
551                                                         String attributeValue =  eType + defaultValue + required + array;
552                                                         refAttribute.put(name, attributeValue); 
553                                                 }
554                                         }
555                                 }
556                         }
557                 }
558                 return refAttribute;
559
560         }
561
562         public String arrayCheck(int upperBound) {
563
564                 if (upperBound == -1){
565                         return ":MANY-true";
566                 }
567
568                 return ":MANY-false";
569         }
570
571         public List<String> getDependencyList(EClassifier eClassifier, String className){
572                 List<String> returnValue = new ArrayList<>();;
573                 EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes();
574                 if (somelist.isEmpty()){
575                         return returnValue;
576                 }
577                 for(EClass depend: somelist){
578                         if (depend.toString().contains(eProxyURI)){
579                                 String one = depend.toString().split(eProxyURI)[1];
580                                 String value = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
581                                 returnValue.add(value);
582                         }
583                 }
584
585                 return returnValue;
586         }
587
588         public Map<String, String> buildSubList(Map<String, String> subClassAttributes, Map<String, MSAttributeObject> classMap, String className){
589                 Map<String, String> missingValues = new HashMap<>();
590                 Map<String, String> workingMap;
591                 boolean enumType;
592
593                 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
594                         String value = map.getValue().split(":")[0];
595                         if (value!=null){
596                                 classMap.get(className).getEnumType();
597                                 enumType = classMap.get(className).getEnumType().containsKey(value);
598                                 if (!enumType){
599                                         workingMap =  classMap.get(value).getRefAttribute();
600                                         for ( Entry<String, String> subMab : workingMap.entrySet()){
601                                                 String value2 = subMab.getValue().split(":")[0];
602                                                 if (!subClassAttributes.containsValue(value2)){
603                                                         missingValues.put(subMab.getKey(), subMab.getValue());
604                                                 }
605                                         }
606
607                                 }
608                         }
609                 }
610
611                 return missingValues;
612         }
613
614         public Map<String, Map<String, String>> recursiveReference(Map<String, MSAttributeObject> classMap, String className){
615
616                 Map<String, Map<String, String>> returnObject = new HashMap<>();
617                 Map<String, String> returnClass = getRefclass(classMap, className);
618                 returnObject.put(className, returnClass);
619                 for (Entry<String, String> reAttribute :returnClass.entrySet()){
620                         if (reAttribute.getValue().split(":")[1].contains("MANY")){
621                                 if (classMap.get(reAttribute.getValue().split(":")[0]) != null){
622                                         returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0]));
623                                 }
624                         }
625
626                 }
627
628                 return returnObject;
629
630         }
631
632         public String createJson(Map<String, Object> subClassAttributes, Map<String, MSAttributeObject> classMap, String className) {
633                 boolean enumType;
634                 Map<String, Map<String, String>> myObject = new HashMap<>();
635                 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
636                         String value = map.getValue().split(":")[0];
637                         if (value!=null){
638                                 enumType = classMap.get(className).getEnumType().containsKey(value);
639                                 if (!enumType){
640                                         if (map.getValue().split(":")[1].contains("MANY")){
641                                                 Map<String, Map<String, String>> testRecursive = recursiveReference(classMap, map.getValue().split(":")[0] );
642                                                 myObject.putAll(testRecursive);
643                                         }
644                                 }
645                         }
646                 }
647
648                 Gson gson = new Gson(); 
649                 return gson.toJson(myObject);
650         }
651
652         public Map<String, String> getRefclass(Map<String, MSAttributeObject> classMap, String className){
653                 HashMap<String, String> missingValues = new HashMap<>();
654
655                 if (classMap.get(className).getAttribute()!=null || !classMap.get(className).getAttribute().isEmpty()){
656                         missingValues.putAll(classMap.get(className).getAttribute());
657                 }
658
659                 if (classMap.get(className).getRefAttribute()!=null || !classMap.get(className).getRefAttribute().isEmpty()){
660                         missingValues.putAll(classMap.get(className).getRefAttribute());
661                 }
662
663                 return missingValues;   
664         }
665
666         public String createSubAttributes(List<String> dependency, Map<String, MSAttributeObject> classMap, String modelName) {
667
668                 HashMap <String,  Object>  workingMap = new HashMap<>();
669                 MSAttributeObject tempObject;
670                 if (dependency!=null){
671                         if (dependency.isEmpty()){
672                                 return "{}";
673                         }       
674                         dependency.add(modelName);
675                         for (String element: dependency){
676                                 tempObject = classMap.get(element);
677                                 if (tempObject!=null){
678                                         workingMap.putAll(classMap.get(element).getSubClass());
679                                 }
680                         }
681                 }
682
683                 return createJson(workingMap, classMap, modelName);
684         }
685
686         public List<String> getFullDependencyList(List<String> dependency, Map<String,MSAttributeObject > classMap) {
687                 ArrayList<String> returnList = new ArrayList<>();
688                 ArrayList<String> workingList;
689                 returnList.addAll(dependency);
690                 for (String element : dependency ){
691                         if (classMap.containsKey(element)){
692                                 MSAttributeObject value = classMap.get(element);
693                                 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
694                                 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
695                                 for(String depend : workingList){
696                                         if (!returnList.contains(depend) && !depend.isEmpty()){
697                                                 returnList.add(depend.trim());
698                                         }
699                                 }
700                         }
701                 }
702
703                 return returnList;
704         }
705 }