2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.rest.util;
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;
30 import java.util.Map.Entry;
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;
58 import com.att.research.xacml.util.XACMLProperties;
59 import com.google.gson.Gson;
62 public class MSModelUtils {
64 private static final Log logger = LogFactory.getLog(MSModelUtils.class);
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:";
75 public MSModelUtils(String onap, String policy){
80 private enum ANNOTATION_TYPE{
81 MATCHING, VALIDATION, DICTIONARY
84 public enum MODEL_TYPE {
89 public Map<String, MSAttributeObject> processEpackage(String file, MODEL_TYPE model){
90 if (model == MODEL_TYPE.XMI ){
91 processXMIEpackage(file);
97 private void processXMIEpackage(String xmiFile){
98 EPackage root = getEpackage(xmiFile);
99 TreeIterator<EObject> treeItr = root.eAllContents();
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();
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);
120 if (!enumMap.isEmpty()){
123 if (!matchingClass.isEmpty()){
124 CheckForMatchingClass();
128 private void CheckForMatchingClass() {
129 HashMap<String, String> tempAttribute = new HashMap<>();
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");
140 for ( Entry<String, String> eSet : listRef.entrySet()){
141 String key3 = eSet.getKey();
142 tempAttribute.put(key3, "matching-true");
146 UpdateMatching(tempAttribute, key);
153 private void UpdateMatching(HashMap<String, String> tempAttribute, String key) {
154 Map<String, MSAttributeObject> newClass = classMap;
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);
169 private void addEnumClassMap() {
170 for (Entry<String, MSAttributeObject> value :classMap.entrySet()){
171 value.getValue().setEnumType(enumMap);
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);
182 resource.load(Collections.emptyMap());
183 } catch (IOException e) {
184 logger.error("Error loading Encore Resource for new Model" + e);
187 return (EPackage) resource.getContents().get(0);
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;
195 String name = eenum.getName();
196 for (EEnumLiteral eEnumLiteral : eenum.getELiterals())
198 Enumerator instance = eEnumLiteral.getInstance();
199 String value = instance.getLiteral();
200 valueList.add(value);
202 returnMap.put(name, valueList.toString());
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(",")));
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));
225 this.classMap.put(className, msAttributeObject);
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<>();
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 );
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);
251 annotationSet.put(eStrucClassifier.getName(), matching);
253 range = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
255 annotationSet.put(eStrucClassifier.getName(), range);
257 dictionary = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
258 if (dictionary!=null){
259 annotationSet.put(eStrucClassifier.getName(), dictionary);
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);
269 if (obj instanceof EReference){
270 EClass refType = ((EReference) obj).getEReferenceType();
271 annotationSet.put(refType.getName(), matching);
272 matchingClass.put(refType.getName(), matching);
274 annotationSet.put(eStrucClassifier.getName(), matching);
281 return annotationSet;
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;
292 // Pulling out dependency from file
293 while (treeItr.hasNext() && rollingCount < 2) {
295 EObject obj = treeItr.next();
296 if (obj instanceof EClassifier) {
297 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
298 requiredAttribute = true;
300 requiredAttribute = false;
302 if (requiredAttribute){
305 rollingCount = rollingCount+processClass;
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";
320 subAttribute.put(eStrucClassifier.getName(), refType.getName() + required);
330 public String checkDefultValue(String defultValue) {
331 if (defultValue!=null){
332 return ":defaultValue-"+ defultValue;
334 return ":defaultValue-NA";
338 public String checkRequiredPattern(int upper, int lower) {
340 String pattern = XACMLProperties.getProperty(XACMLRestProperties.PROP_XCORE_REQUIRED_PATTERN);
343 if (upper == Integer.parseInt(pattern.split(",")[1]) && lower==Integer.parseInt(pattern.split(",")[0])){
344 return ":required-true";
348 return ":required-false";
351 public JSONObject buildJavaObject(Map<String, String> map){
353 return new JSONObject(map);
356 public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass){
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;
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;
371 requiredAttribute = false;
373 if (requiredAttribute){
376 rollingCount = rollingCount+processClass;
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);
391 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
392 String required = ":required-false";
393 if(((EStructuralFeature) obj).getLowerBound() == 1){
394 required = ":required-true";
396 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
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";
406 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
417 private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
418 String annotationType;
419 EAnnotation eAnnotation;
423 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
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)){
432 } else if (annotationType.contains(type) && onapValue.contains(annotation)){
441 private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
442 String annotationType;
443 EAnnotation eAnnotation;
445 String onapValue = null;
447 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
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){
458 return onapType + "-" + onapValue;
465 public boolean isRequiredAttribute(EObject obj, String className){
466 EClassifier eClassifier = (EClassifier) obj;
467 String workingClass = eClassifier.getName();
469 if (workingClass.equalsIgnoreCase(className)){
476 private boolean isPolicyTemplate(EPackage root, String className){
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")){
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;
500 for (EClass eSuperType : eClass.getEAllSuperTypes())
502 if (eClass.getName().contentEquals(className)){
503 returnSubTypes = eSuperType.getName();
508 return returnSubTypes;
511 public Map<String, String> getAttributeList(EPackage root, String className, String superClass){
513 TreeIterator<EObject> treeItr = root.eAllContents();
514 boolean requiredAttribute = false;
515 HashMap<String, String> refAttribute = new HashMap<>();
517 boolean dictionaryTest;
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;
528 requiredAttribute = false;
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();
543 eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
545 eType = eStrucClassifier.getEType().getInstanceClassName();
547 defaultValue = checkDefultValue(((EStructuralFeature) obj).getDefaultValueLiteral());
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);
562 public String arrayCheck(int upperBound) {
564 if (upperBound == -1){
568 return ":MANY-false";
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()){
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);
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;
593 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
594 String value = map.getValue().split(":")[0];
596 classMap.get(className).getEnumType();
597 enumType = classMap.get(className).getEnumType().containsKey(value);
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());
611 return missingValues;
614 public Map<String, Map<String, String>> recursiveReference(Map<String, MSAttributeObject> classMap, String className){
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]));
632 public String createJson(Map<String, Object> subClassAttributes, Map<String, MSAttributeObject> classMap, String className) {
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];
638 enumType = classMap.get(className).getEnumType().containsKey(value);
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);
648 Gson gson = new Gson();
649 return gson.toJson(myObject);
652 public Map<String, String> getRefclass(Map<String, MSAttributeObject> classMap, String className){
653 HashMap<String, String> missingValues = new HashMap<>();
655 if (classMap.get(className).getAttribute()!=null || !classMap.get(className).getAttribute().isEmpty()){
656 missingValues.putAll(classMap.get(className).getAttribute());
659 if (classMap.get(className).getRefAttribute()!=null || !classMap.get(className).getRefAttribute().isEmpty()){
660 missingValues.putAll(classMap.get(className).getRefAttribute());
663 return missingValues;
666 public String createSubAttributes(List<String> dependency, Map<String, MSAttributeObject> classMap, String modelName) {
668 HashMap <String, Object> workingMap = new HashMap<>();
669 MSAttributeObject tempObject;
670 if (dependency!=null){
671 if (dependency.isEmpty()){
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());
683 return createJson(workingMap, classMap, modelName);
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());