2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 HashMap<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();
100 String className = null;
101 String returnValue = null;
103 // Pulling out dependency from file
104 while (treeItr.hasNext()) {
105 EObject obj = (EObject) 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 = null;
158 for (Entry<String, MSAttributeObject> updateClass : newClass.entrySet()){
159 HashMap<String, String> valueMap = updateClass.getValue().getMatchingSet();
160 String keymap = updateClass.getKey();
161 if (valueMap.containsKey(key)){
162 HashMap<String, String> modifyMap = classMap.get(keymap).getMatchingSet();
163 modifyMap.remove(key);
164 modifyMap.putAll(tempAttribute);
165 classMap.get(keymap).setMatchingSet(modifyMap);
171 private void addEnumClassMap() {
172 for (Entry<String, MSAttributeObject> value :classMap.entrySet()){
173 value.getValue().setEnumType(enumMap);
177 private EPackage getEpackage(String xmiFile) {
178 ResourceSet resSet = new ResourceSetImpl();
179 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
180 Map<String, Object> m = reg.getExtensionToFactoryMap();
181 m.put("xmi", new XMIResourceFactoryImpl());
182 Resource resource = resSet.getResource(URI.createFileURI(xmiFile), true);
184 resource.load(Collections.EMPTY_MAP);
185 } catch (IOException e) {
186 logger.error("Error loading Encore Resource for new Model" + e);
189 EPackage root = (EPackage) resource.getContents().get(0);
194 private HashMap<String, String> getEEnum(EObject obj) {
195 List<String> valueList = new ArrayList<>();
196 HashMap<String, String> returnMap = new HashMap<>();
197 EEnum eenum = (EEnum)obj;
199 String name = eenum.getName();
200 for (EEnumLiteral eEnumLiteral : eenum.getELiterals())
202 Enumerator instance = eEnumLiteral.getInstance();
203 String value = instance.getLiteral();
204 valueList.add(value);
206 returnMap.put(name, valueList.toString());
210 public void getAttributes(String className, String dependency, EPackage root) {
211 List<String> dpendList = new ArrayList<>();
212 if (dependency!=null){
213 dpendList = new ArrayList<String>(Arrays.asList(dependency.split(",")));
215 MSAttributeObject msAttributeObject = new MSAttributeObject();
216 msAttributeObject.setClassName(className);
217 String extendClass = getSubTypes(root, className);
218 HashMap<String, String> returnRefList = getRefAttributeList(root, className, extendClass);
219 HashMap<String, String> returnAttributeList = getAttributeList(root, className, extendClass);
220 HashMap<String, Object> returnSubList = getSubAttributeList(root, className, extendClass);
221 HashMap<String, String> returnAnnotation = getAnnotation(root, className, extendClass);
222 msAttributeObject.setAttribute(returnAttributeList);
223 msAttributeObject.setRefAttribute(returnRefList);
224 msAttributeObject.setSubClass(returnSubList);
225 msAttributeObject.setDependency(dpendList.toString());
226 msAttributeObject.addMatchingSet(returnAnnotation);
227 msAttributeObject.setPolicyTempalate(isPolicyTemplate(root, className));
229 this.classMap.put(className, msAttributeObject);
232 private HashMap<String, String> getAnnotation(EPackage root, String className, String extendClass) {
233 TreeIterator<EObject> treeItr = root.eAllContents();
234 boolean requiredAttribute = false;
235 boolean requiredMatchAttribute = false;
236 HashMap<String, String> annotationSet = new HashMap<>();
237 String matching = null;
239 String dictionary = null;
241 // Pulling out dependency from file
242 while (treeItr.hasNext()) {
243 EObject obj = treeItr.next();
244 if (obj instanceof EClassifier) {
245 requiredAttribute = isRequiredAttribute(obj, className );
246 requiredMatchAttribute = isRequiredAttribute(obj, extendClass );
249 if (requiredAttribute){
250 if (obj instanceof EStructuralFeature) {
251 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
252 if (eStrucClassifier.getEAnnotations().size() != 0) {
253 matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
255 annotationSet.put(eStrucClassifier.getName(), matching);
257 range = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
259 annotationSet.put(eStrucClassifier.getName(), range);
261 dictionary = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
262 if (dictionary!=null){
263 annotationSet.put(eStrucClassifier.getName(), dictionary);
267 } else if (requiredMatchAttribute){
268 if (obj instanceof EStructuralFeature) {
269 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
270 if (eStrucClassifier.getEAnnotations().size() != 0) {
271 matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
273 if (obj instanceof EReference){
274 EClass refType = ((EReference) obj).getEReferenceType();
275 annotationSet.put(refType.getName(), matching);
276 matchingClass.put(refType.getName(), matching);
278 annotationSet.put(eStrucClassifier.getName(), matching);
285 return annotationSet;
288 private HashMap<String, Object> getSubAttributeList(EPackage root, String className , String superClass) {
289 TreeIterator<EObject> treeItr = root.eAllContents();
290 boolean requiredAttribute = false;
291 HashMap<String, Object> subAttribute = new HashMap<String, Object>();
292 int rollingCount = 0;
293 int processClass = 0;
294 boolean annotation = false;
296 // Pulling out dependency from file
297 while (treeItr.hasNext() && rollingCount < 2) {
299 EObject obj = treeItr.next();
300 if (obj instanceof EClassifier) {
301 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
302 requiredAttribute = true;
304 requiredAttribute = false;
306 if (requiredAttribute){
309 rollingCount = rollingCount+processClass;
312 if (requiredAttribute) {
313 if (obj instanceof EStructuralFeature) {
314 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
315 if (eStrucClassifier.getEAnnotations().size() != 0) {
316 annotation = annotationTest(eStrucClassifier, configuration, onap);
317 if (annotation && obj instanceof EReference) {
318 EClass refType = ((EReference) obj).getEReferenceType();
319 if(!refType.toString().contains(eProxyURI)){
320 subAttribute.put(eStrucClassifier.getName(), refType.getName());
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(HashMap<String, String> map, String attributeType){
353 JSONObject returnValue = new JSONObject(map);
359 public HashMap<String, String> getRefAttributeList(EPackage root, String className, String superClass){
361 TreeIterator<EObject> treeItr = root.eAllContents();
362 boolean requiredAttribute = false;
363 HashMap<String, String> refAttribute = new HashMap<>();
364 int rollingCount = 0;
365 int processClass = 0;
366 boolean annotation = false;
367 // Pulling out dependency from file
368 while (treeItr.hasNext()) {
369 EObject obj = treeItr.next();
370 if (obj instanceof EClassifier) {
371 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
372 requiredAttribute = true;
374 requiredAttribute = false;
376 if (requiredAttribute){
379 rollingCount = rollingCount+processClass;
382 if (requiredAttribute) {
383 if (obj instanceof EStructuralFeature) {
384 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
385 if (eStrucClassifier.getEAnnotations().size() != 0) {
386 annotation = annotationTest(eStrucClassifier, configuration, onap);
387 if ( annotation && obj instanceof EReference) {
388 EClass refType = ((EReference) obj).getEReferenceType();
389 if(refType.toString().contains(eProxyURI)){
390 String one = refType.toString().split(eProxyURI)[1];
391 String refValue = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
392 refAttribute.put(eStrucClassifier.getName(), refValue);
394 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
395 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array);
397 } else if (annotation && obj instanceof EAttributeImpl){
398 EClassifier refType = ((EAttributeImpl) obj).getEType();
399 if (refType instanceof EEnumImpl){
400 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
401 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array); }
410 private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
411 String annotationType = null;
412 EAnnotation eAnnotation = null;
413 String onapType = null;
414 String onapValue = null;
416 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
418 for (int i = 0; i < value.size(); i++){
419 annotationType = value.get(i).getSource();
420 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
421 onapType = eAnnotation.getDetails().get(0).getValue();
422 onapValue = eAnnotation.getDetails().get(0).getKey();
423 if (annotationType.contains(type) && onapType.contains(annotation)){
425 } else if (annotationType.contains(type) && onapValue.contains(annotation)){
434 private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
435 String annotationType = null;
436 EAnnotation eAnnotation = null;
437 String onapType = null;
438 String onapValue = null;
440 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
442 for (int i = 0; i < value.size(); i++){
443 annotationType = value.get(i).getSource();
444 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
445 onapType = eAnnotation.getDetails().get(0).getKey();
446 if (annotationType.contains(type) && onapType.compareToIgnoreCase(annotation.toString())==0){
447 onapValue = eAnnotation.getDetails().get(0).getValue();
448 if (annotation == ANNOTATION_TYPE.VALIDATION){
451 return onapType + "-" + onapValue;
458 public boolean isRequiredAttribute(EObject obj, String className){
459 EClassifier eClassifier = (EClassifier) obj;
460 String workingClass = eClassifier.getName();
462 if (workingClass.equalsIgnoreCase(className)){
469 private boolean isPolicyTemplate(EPackage root, String className){
471 for (EClassifier classifier : root.getEClassifiers()){
472 if (classifier instanceof EClass) {
473 EClass eClass = (EClass)classifier;
474 if (eClass.getName().contentEquals(className)){
475 EList<EAnnotation> value = eClass.getEAnnotations();
476 for (EAnnotation workingValue : value){
477 EMap<String, String> keyMap = workingValue.getDetails();
478 if (keyMap.containsKey("policyTemplate")){
487 private String getSubTypes(EPackage root, String className) {
488 String returnSubTypes = null;
489 for (EClassifier classifier : root.getEClassifiers()){
490 if (classifier instanceof EClass) {
491 EClass eClass = (EClass)classifier;
493 for (EClass eSuperType : eClass.getEAllSuperTypes())
495 if (eClass.getName().contentEquals(className)){
496 returnSubTypes = eSuperType.getName();
501 return returnSubTypes;
504 public HashMap<String, String> getAttributeList(EPackage root, String className, String superClass){
506 TreeIterator<EObject> treeItr = root.eAllContents();
507 boolean requiredAttribute = false;
508 HashMap<String, String> refAttribute = new HashMap<>();
509 boolean annotation = false;
510 boolean dictionaryTest = false;
511 String defaultValue = null;
514 // Pulling out dependency from file
515 while (treeItr.hasNext()) {
516 EObject obj = treeItr.next();
517 if (obj instanceof EClassifier) {
518 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
519 requiredAttribute = true;
521 requiredAttribute = false;
526 if (requiredAttribute){
527 if (obj instanceof EStructuralFeature) {
528 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
529 if (eStrucClassifier.getEAnnotations().size() != 0) {
530 annotation = annotationTest(eStrucClassifier, configuration, onap);
531 dictionaryTest = annotationTest(eStrucClassifier, dictionary, policy);
532 EClassifier refType = ((EStructuralFeature) obj).getEType();
533 if (annotation && !(obj instanceof EReference) && !(refType instanceof EEnumImpl)) {
534 String name = eStrucClassifier.getName();
536 eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
538 eType = eStrucClassifier.getEType().getInstanceClassName();
540 defaultValue = checkDefultValue(((EStructuralFeature) obj).getDefaultValueLiteral());
542 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
543 String required = checkRequiredPattern(((EStructuralFeature) obj).getUpperBound(), ((EStructuralFeature) obj).getLowerBound());
544 String attributeValue = eType + defaultValue + required + array;
545 refAttribute.put(name, attributeValue);
555 public String arrayCheck(int upperBound) {
557 if (upperBound == -1){
561 return ":MANY-false";
564 public List<String> getDependencyList(EClassifier eClassifier, String className){
565 List<String> returnValue = new ArrayList<>();;
566 EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes();
567 if (somelist.isEmpty()){
570 for(EClass depend: somelist){
571 if (depend.toString().contains(eProxyURI)){
572 String one = depend.toString().split(eProxyURI)[1];
573 String value = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
574 returnValue.add(value);
581 public Map<String, String> buildSubList(HashMap<String, String> subClassAttributes, HashMap<String, MSAttributeObject> classMap, String className){
582 Map<String, String> missingValues = new HashMap<>();
583 Map<String, String> workingMap = new HashMap<>();
586 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
587 String value = map.getValue().split(":")[0];
589 classMap.get(className).getEnumType();
590 enumType = classMap.get(className).getEnumType().containsKey(value);
592 workingMap = classMap.get(value).getRefAttribute();
593 for ( Entry<String, String> subMab : workingMap.entrySet()){
594 String value2 = subMab.getValue().split(":")[0];
595 if (!subClassAttributes.containsValue(value2)){
596 missingValues.put(subMab.getKey(), subMab.getValue());
604 return missingValues;
607 public Map<String, HashMap<String, String>> recursiveReference(HashMap<String, MSAttributeObject> classMap, String className){
609 Map<String, HashMap<String, String>> returnObject = new HashMap<>();
610 HashMap<String, String> returnClass = getRefclass(classMap, className);
611 returnObject.put(className, returnClass);
612 for (Entry<String, String> reAttribute :returnClass.entrySet()){
613 if (reAttribute.getValue().split(":")[1].contains("MANY")){
614 if (classMap.get(reAttribute.getValue().split(":")[0]) != null){
615 returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0]));
625 public String createJson(HashMap<String, Object> subClassAttributes, HashMap<String, MSAttributeObject> classMap, String className) {
627 Map<String, HashMap<String, String>> myObject = new HashMap<>();
628 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
629 String value = map.getValue().split(":")[0];
631 enumType = classMap.get(className).getEnumType().containsKey(value);
633 if (map.getValue().split(":")[1].contains("MANY")){
634 Map<String, HashMap<String, String>> testRecursive = recursiveReference(classMap, map.getValue().split(":")[0] );
635 myObject.putAll(testRecursive);
641 Gson gson = new Gson();
642 String json = gson.toJson(myObject);
647 public HashMap<String, String> getRefclass(HashMap<String, MSAttributeObject> classMap, String className){
648 HashMap<String, String> missingValues = new HashMap<>();
650 if (classMap.get(className).getAttribute()!=null || !classMap.get(className).getAttribute().isEmpty()){
651 missingValues.putAll(classMap.get(className).getAttribute());
654 if (classMap.get(className).getRefAttribute()!=null || !classMap.get(className).getRefAttribute().isEmpty()){
655 missingValues.putAll(classMap.get(className).getRefAttribute());
658 return missingValues;
661 public String createSubAttributes(ArrayList<String> dependency, HashMap<String, MSAttributeObject> classMap, String modelName) {
663 HashMap <String, Object> workingMap = new HashMap<>();
664 MSAttributeObject tempObject = new MSAttributeObject();
665 if (dependency!=null){
666 if (dependency.size()==0){
669 dependency.add(modelName);
670 for (String element: dependency){
671 tempObject = classMap.get(element);
672 if (tempObject!=null){
673 workingMap.putAll(classMap.get(element).getSubClass());
678 String returnValue = createJson(workingMap, classMap, modelName);
682 public ArrayList<String> getFullDependencyList(ArrayList<String> dependency, HashMap<String,MSAttributeObject > classMap) {
683 ArrayList<String> returnList = new ArrayList<>();
684 ArrayList<String> workingList = new ArrayList<>();
685 returnList.addAll(dependency);
686 for (String element : dependency ){
687 if (classMap.containsKey(element)){
688 MSAttributeObject value = classMap.get(element);
689 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
690 workingList = new ArrayList<String>(Arrays.asList(rawValue.split(",")));
691 for(String depend : workingList){
692 if (!returnList.contains(depend) && !depend.isEmpty()){
693 returnList.add(depend.trim());