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;
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;
38 import java.util.Map.Entry;
40 import org.apache.commons.lang.StringUtils;
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43 import org.eclipse.emf.common.util.EList;
44 import org.eclipse.emf.common.util.EMap;
45 import org.eclipse.emf.common.util.Enumerator;
46 import org.eclipse.emf.common.util.TreeIterator;
47 import org.eclipse.emf.common.util.URI;
48 import org.eclipse.emf.ecore.EAnnotation;
49 import org.eclipse.emf.ecore.EClass;
50 import org.eclipse.emf.ecore.EClassifier;
51 import org.eclipse.emf.ecore.EEnum;
52 import org.eclipse.emf.ecore.EEnumLiteral;
53 import org.eclipse.emf.ecore.EObject;
54 import org.eclipse.emf.ecore.EPackage;
55 import org.eclipse.emf.ecore.EReference;
56 import org.eclipse.emf.ecore.EStructuralFeature;
57 import org.eclipse.emf.ecore.impl.EAttributeImpl;
58 import org.eclipse.emf.ecore.impl.EEnumImpl;
59 import org.eclipse.emf.ecore.resource.Resource;
60 import org.eclipse.emf.ecore.resource.ResourceSet;
61 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
62 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
63 import org.json.JSONObject;
64 import org.onap.policy.rest.XACMLRestProperties;
65 import org.yaml.snakeyaml.Yaml;
67 import com.att.research.xacml.util.XACMLProperties;
68 import com.google.gson.Gson;
71 public class MSModelUtils {
73 private static final Log logger = LogFactory.getLog(MSModelUtils.class);
75 private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
76 private HashMap<String, String> enumMap = new HashMap<>();
77 private HashMap<String, String> matchingClass = new HashMap<>();
78 private String configuration = "configuration";
79 private String dictionary = "dictionary";
80 private String onap = "";
81 private String policy = "";
82 private String eProxyURI = "eProxyURI:";
83 private List<String> orderedElements = new ArrayList<>();
84 private String dataOrderInfo = null;
85 private Set<String> uniqueDataKeys= new HashSet<>();
86 private Set<String> uniqueKeys= new HashSet<>();
87 private String listConstraints = null;
88 private String referenceAttributes;
89 private LinkedHashMap<String, Object> retmap = new LinkedHashMap<>();
90 private Map<String, String> matchableValues;
91 public static final String PROPERTIES=".properties.";
92 public static final String DATATYPE = "data_types.policy.data.";
93 public static final String TYPE=".type";
94 public static final String REQUIRED=".required";
95 public static final String MATCHABLE=".matchable";
96 public static final String STRING="string";
97 public static final String INTEGER="integer";
98 public static final String LIST="list";
99 public static final String MAP="map";
100 public static final String DEFAULT=".default";
101 public static final String MANYFALSE=":MANY-false";
102 public static final String MANYTRUE=":MANY-true";
103 public static final String DEFAULTVALUE=":defaultValue-";
104 public static final String REQUIREDVALUE=":required-";
105 public static final String MATCHABLEKEY="matchable";
106 public static final String REQUIREDFALSE=":required-false";
107 public static final String REQUIREDTRUE=":required-true";
108 public static final String MATCHINGTRUE="matching-true";
110 private StringBuilder dataListBuffer=new StringBuilder();
111 private List<String> dataConstraints= new ArrayList <>();
112 private String attributeString = null;
114 public MSModelUtils(){
117 public MSModelUtils(String onap, String policy){
119 this.policy = policy;
122 private enum ANNOTATION_TYPE{
123 MATCHING, VALIDATION, DICTIONARY
126 public enum MODEL_TYPE {
131 public Map<String, MSAttributeObject> processEpackage(String file, MODEL_TYPE model){
132 if (model == MODEL_TYPE.XMI ){
133 processXMIEpackage(file);
139 private void processXMIEpackage(String xmiFile){
140 EPackage root = getEpackage(xmiFile);
141 TreeIterator<EObject> treeItr = root.eAllContents();
145 // Pulling out dependency from file
146 while (treeItr.hasNext()) {
147 EObject obj = treeItr.next();
148 if (obj instanceof EClassifier) {
149 EClassifier eClassifier = (EClassifier) obj;
150 className = eClassifier.getName();
152 if (obj instanceof EEnum) {
153 enumMap.putAll(getEEnum(obj));
154 }else if (obj instanceof EClass) {
155 String temp = getDependencyList(eClassifier).toString();
156 returnValue = StringUtils.replaceEach(temp, new String[]{"[", "]"}, new String[]{"", ""});
157 getAttributes(className, returnValue, root);
162 if (!enumMap.isEmpty()){
165 if (!matchingClass.isEmpty()){
166 CheckForMatchingClass();
170 private void CheckForMatchingClass() {
171 HashMap<String, String> tempAttribute = new HashMap<>();
173 for (Entry<String, String> set : matchingClass.entrySet()){
174 String key = set.getKey();
175 if (classMap.containsKey(key)){
176 Map<String, String> listAttributes = classMap.get(key).getAttribute();
177 Map<String, String> listRef = classMap.get(key).getRefAttribute();
178 for ( Entry<String, String> eSet : listAttributes.entrySet()){
179 String key2 = eSet.getKey();
180 tempAttribute.put(key2, MATCHINGTRUE);
182 for ( Entry<String, String> eSet : listRef.entrySet()){
183 String key3 = eSet.getKey();
184 tempAttribute.put(key3, MATCHINGTRUE);
188 UpdateMatching(tempAttribute, key);
195 private void UpdateMatching(HashMap<String, String> tempAttribute, String key) {
196 Map<String, MSAttributeObject> newClass = classMap;
198 for (Entry<String, MSAttributeObject> updateClass : newClass.entrySet()){
199 Map<String, String> valueMap = updateClass.getValue().getMatchingSet();
200 String keymap = updateClass.getKey();
201 if (valueMap.containsKey(key)){
202 Map<String, String> modifyMap = classMap.get(keymap).getMatchingSet();
203 modifyMap.remove(key);
204 modifyMap.putAll(tempAttribute);
205 classMap.get(keymap).setMatchingSet(modifyMap);
211 private void addEnumClassMap() {
212 for (Entry<String, MSAttributeObject> value :classMap.entrySet()){
213 value.getValue().setEnumType(enumMap);
217 private EPackage getEpackage(String xmiFile) {
218 ResourceSet resSet = new ResourceSetImpl();
219 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
220 Map<String, Object> m = reg.getExtensionToFactoryMap();
221 m.put("xmi", new XMIResourceFactoryImpl());
222 Resource resource = resSet.getResource(URI.createFileURI(xmiFile), true);
224 resource.load(Collections.emptyMap());
225 } catch (IOException e) {
226 logger.error("Error loading Encore Resource for new Model" + e);
229 return (EPackage) resource.getContents().get(0);
232 private HashMap<String, String> getEEnum(EObject obj) {
233 List<String> valueList = new ArrayList<>();
234 HashMap<String, String> returnMap = new HashMap<>();
235 EEnum eenum = (EEnum)obj;
237 String name = eenum.getName();
238 for (EEnumLiteral eEnumLiteral : eenum.getELiterals())
240 Enumerator instance = eEnumLiteral.getInstance();
241 String value = instance.getLiteral();
242 valueList.add(value);
244 returnMap.put(name, valueList.toString());
248 public void getAttributes(String className, String dependency, EPackage root) {
249 List<String> dpendList = new ArrayList<>();
250 if (dependency!=null){
251 dpendList = new ArrayList<>(Arrays.asList(dependency.split(",")));
253 MSAttributeObject msAttributeObject = new MSAttributeObject();
254 msAttributeObject.setClassName(className);
255 String extendClass = getSubTypes(root, className);
256 Map<String, String> returnRefList = getRefAttributeList(root, className, extendClass);
257 Map<String, String> returnAttributeList = getAttributeList(root, className, extendClass);
258 Map<String, Object> returnSubList = getSubAttributeList(root, className, extendClass);
259 HashMap<String, String> returnAnnotation = getAnnotation(root, className, extendClass);
260 msAttributeObject.setAttribute(returnAttributeList);
261 msAttributeObject.setRefAttribute(returnRefList);
262 msAttributeObject.setSubClass(returnSubList);
263 msAttributeObject.setDependency(dpendList.toString());
264 msAttributeObject.addMatchingSet(returnAnnotation);
265 msAttributeObject.setPolicyTempalate(isPolicyTemplate(root, className));
267 this.classMap.put(className, msAttributeObject);
270 private HashMap<String, String> getAnnotation(EPackage root, String className, String extendClass) {
271 TreeIterator<EObject> treeItr = root.eAllContents();
272 boolean requiredAttribute = false;
273 boolean requiredMatchAttribute = false;
274 HashMap<String, String> annotationSet = new HashMap<>();
277 String annotationDict;
279 // Pulling out dependency from file
280 while (treeItr.hasNext()) {
281 EObject obj = treeItr.next();
282 if (obj instanceof EClassifier) {
283 requiredAttribute = isRequiredAttribute(obj, className );
284 requiredMatchAttribute = isRequiredAttribute(obj, extendClass );
287 if (requiredAttribute){
288 if (obj instanceof EStructuralFeature) {
289 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
290 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
291 matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
293 annotationSet.put(eStrucClassifier.getName(), matching);
295 range = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
297 annotationSet.put(eStrucClassifier.getName(), range);
299 annotationDict = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
300 if (annotationDict!=null){
301 annotationSet.put(eStrucClassifier.getName(), annotationDict);
305 } else if (requiredMatchAttribute && (obj instanceof EStructuralFeature)) {
306 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
307 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
308 matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
310 if (obj instanceof EReference){
311 EClass refType = ((EReference) obj).getEReferenceType();
312 annotationSet.put(refType.getName(), matching);
313 matchingClass.put(refType.getName(), matching);
315 annotationSet.put(eStrucClassifier.getName(), matching);
321 return annotationSet;
324 private Map<String, Object> getSubAttributeList(EPackage root, String className , String superClass) {
325 TreeIterator<EObject> treeItr = root.eAllContents();
326 boolean requiredAttribute = false;
327 Map<String, Object> subAttribute = new HashMap<>();
328 int rollingCount = 0;
329 int processClass = 0;
332 // Pulling out dependency from file
333 while (treeItr.hasNext() && rollingCount < 2) {
335 EObject obj = treeItr.next();
336 if (obj instanceof EClassifier) {
337 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
338 requiredAttribute = true;
340 requiredAttribute = false;
342 if (requiredAttribute){
345 rollingCount = rollingCount+processClass;
348 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
349 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
350 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
351 annotation = annotationTest(eStrucClassifier, configuration, onap);
352 if (annotation && obj instanceof EReference) {
353 EClass refType = ((EReference) obj).getEReferenceType();
354 if(!refType.toString().contains(eProxyURI)){
355 String required = REQUIREDFALSE;
356 if(eStrucClassifier.getLowerBound() == 1){
357 required = REQUIREDTRUE;
359 subAttribute.put(eStrucClassifier.getName(), refType.getName() + required);
368 public String checkDefultValue(String defultValue) {
369 if (defultValue!=null){
370 return DEFAULTVALUE+ defultValue;
372 return ":defaultValue-NA";
376 public String checkRequiredPattern(int upper, int lower) {
378 String pattern = XACMLProperties.getProperty(XACMLRestProperties.PROP_XCORE_REQUIRED_PATTERN);
381 if (upper == Integer.parseInt(pattern.split(",")[1]) && lower==Integer.parseInt(pattern.split(",")[0])){
386 return REQUIREDFALSE;
389 public JSONObject buildJavaObject(Map<String, String> map){
391 return new JSONObject(map);
394 public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass){
396 TreeIterator<EObject> treeItr = root.eAllContents();
397 boolean requiredAttribute = false;
398 HashMap<String, String> refAttribute = new HashMap<>();
399 int rollingCount = 0;
400 int processClass = 0;
402 // Pulling out dependency from file
403 while (treeItr.hasNext()) {
404 EObject obj = treeItr.next();
405 if (obj instanceof EClassifier) {
406 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
407 requiredAttribute = true;
409 requiredAttribute = false;
411 if (requiredAttribute){
414 rollingCount = rollingCount+processClass;
417 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
418 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
419 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
420 annotation = annotationTest(eStrucClassifier, configuration, onap);
421 if ( annotation && obj instanceof EReference) {
422 EClass refType = ((EReference) obj).getEReferenceType();
423 if(refType.toString().contains(eProxyURI)){
424 String one = refType.toString().split(eProxyURI)[1];
425 String refValue = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
426 refAttribute.put(eStrucClassifier.getName(), refValue);
428 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
429 String required = REQUIREDFALSE;
430 if(((EStructuralFeature) obj).getLowerBound() == 1){
431 required = REQUIREDTRUE;
433 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
435 } else if (annotation && obj instanceof EAttributeImpl){
436 EClassifier refType = ((EAttributeImpl) obj).getEType();
437 if (refType instanceof EEnumImpl){
438 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
439 String required = REQUIREDFALSE;
440 if(((EStructuralFeature) obj).getLowerBound() == 1){
441 required = REQUIREDTRUE;
443 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
453 private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
454 String annotationType;
455 EAnnotation eAnnotation;
459 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
461 for (int i = 0; i < value.size(); i++){
462 annotationType = value.get(i).getSource();
463 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
464 onapType = eAnnotation.getDetails().get(0).getValue();
465 onapValue = eAnnotation.getDetails().get(0).getKey();
467 if (annotationType.contains(type) && onapType.contains(annotation)){
471 if (annotationType.contains(type) && onapValue.contains(annotation)){
480 private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
481 String annotationType;
482 EAnnotation eAnnotation;
484 String onapValue = null;
486 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
488 for (int i = 0; i < value.size(); i++){
489 annotationType = value.get(i).getSource();
490 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
491 onapType = eAnnotation.getDetails().get(0).getKey();
492 if (annotationType.contains(type) && onapType.compareToIgnoreCase(annotation.toString())==0){
493 onapValue = eAnnotation.getDetails().get(0).getValue();
494 if (annotation == ANNOTATION_TYPE.VALIDATION){
497 return onapType + "-" + onapValue;
504 public boolean isRequiredAttribute(EObject obj, String className){
505 EClassifier eClassifier = (EClassifier) obj;
506 String workingClass = eClassifier.getName().trim();
507 if (workingClass.equalsIgnoreCase(className)){
514 private boolean isPolicyTemplate(EPackage root, String className){
516 for (EClassifier classifier : root.getEClassifiers()){
517 if (classifier instanceof EClass) {
518 EClass eClass = (EClass)classifier;
519 if (eClass.getName().contentEquals(className)){
520 EList<EAnnotation> value = eClass.getEAnnotations();
521 for (EAnnotation workingValue : value){
522 EMap<String, String> keyMap = workingValue.getDetails();
523 if (keyMap.containsKey("policyTemplate")){
532 private String getSubTypes(EPackage root, String className) {
533 String returnSubTypes = null;
534 for (EClassifier classifier : root.getEClassifiers()){
535 if (classifier instanceof EClass) {
536 EClass eClass = (EClass)classifier;
538 for (EClass eSuperType : eClass.getEAllSuperTypes())
540 if (eClass.getName().contentEquals(className)){
541 returnSubTypes = eSuperType.getName();
546 return returnSubTypes;
549 public Map<String, String> getAttributeList(EPackage root, String className, String superClass){
551 TreeIterator<EObject> treeItr = root.eAllContents();
552 boolean requiredAttribute = false;
553 HashMap<String, String> refAttribute = new HashMap<>();
555 boolean dictionaryTest;
559 // Pulling out dependency from file
560 while (treeItr.hasNext()) {
561 EObject obj = treeItr.next();
562 if (obj instanceof EClassifier) {
563 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
564 requiredAttribute = true;
566 requiredAttribute = false;
571 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
572 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
573 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
574 annotation = annotationTest(eStrucClassifier, configuration, onap);
575 dictionaryTest = annotationTest(eStrucClassifier, dictionary, policy);
576 EClassifier refType = ((EStructuralFeature) obj).getEType();
577 if (annotation && !(obj instanceof EReference) && !(refType instanceof EEnumImpl)) {
578 String name = eStrucClassifier.getName();
580 eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
582 eType = eStrucClassifier.getEType().getInstanceClassName();
584 defaultValue = checkDefultValue(((EStructuralFeature) obj).getDefaultValueLiteral());
586 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
587 String required = checkRequiredPattern(((EStructuralFeature) obj).getUpperBound(), ((EStructuralFeature) obj).getLowerBound());
588 String attributeValue = eType + defaultValue + required + array;
589 refAttribute.put(name, attributeValue);
598 public String arrayCheck(int upperBound) {
600 if (upperBound == -1){
607 public List<String> getDependencyList(EClassifier eClassifier){
608 List<String> returnValue = new ArrayList<>();;
609 EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes();
610 if (somelist.isEmpty()){
613 for(EClass depend: somelist){
614 if (depend.toString().contains(eProxyURI)){
615 String one = depend.toString().split(eProxyURI)[1];
616 String value = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
617 returnValue.add(value);
624 public Map<String, String> buildSubList(Map<String, String> subClassAttributes, Map<String, MSAttributeObject> classMap, String className){
625 Map<String, String> missingValues = new HashMap<>();
626 Map<String, String> workingMap;
629 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
630 String value = map.getValue().split(":")[0];
632 classMap.get(className).getEnumType();
633 enumType = classMap.get(className).getEnumType().containsKey(value);
635 workingMap = classMap.get(value).getRefAttribute();
636 for ( Entry<String, String> subMab : workingMap.entrySet()){
637 String value2 = subMab.getValue().split(":")[0];
638 if (!subClassAttributes.containsValue(value2)){
639 missingValues.put(subMab.getKey(), subMab.getValue());
647 return missingValues;
650 public Map<String, Map<String, String>> recursiveReference(Map<String, MSAttributeObject> classMap, String className){
652 Map<String, Map<String, String>> returnObject = new HashMap<>();
653 Map<String, String> returnClass = getRefclass(classMap, className);
654 returnObject.put(className, returnClass);
655 for (Entry<String, String> reAttribute :returnClass.entrySet()){
656 if (reAttribute.getValue().split(":")[1].contains("MANY") &&
657 classMap.get(reAttribute.getValue().split(":")[0]) != null){
658 returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0]));
667 public String createJson(Map<String, MSAttributeObject> classMap, String className) {
669 Map<String, Map<String, String>> myObject = new HashMap<>();
670 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
671 String value = map.getValue().split(":")[0];
673 enumType = classMap.get(className).getEnumType().containsKey(value);
674 if (!enumType && map.getValue().split(":")[1].contains("MANY")){
675 Map<String, Map<String, String>> testRecursive = recursiveReference(classMap, map.getValue().split(":")[0] );
676 myObject.putAll(testRecursive);
681 Gson gson = new Gson();
682 return gson.toJson(myObject);
685 public Map<String, String> getRefclass(Map<String, MSAttributeObject> classMap, String className){
686 HashMap<String, String> missingValues = new HashMap<>();
688 if (classMap.get(className).getAttribute()!=null || !classMap.get(className).getAttribute().isEmpty()){
689 missingValues.putAll(classMap.get(className).getAttribute());
692 if (classMap.get(className).getRefAttribute()!=null || !classMap.get(className).getRefAttribute().isEmpty()){
693 missingValues.putAll(classMap.get(className).getRefAttribute());
696 return missingValues;
699 public String createSubAttributes(List<String> dependency, Map<String, MSAttributeObject> classMap, String modelName) {
701 HashMap <String, Object> workingMap = new HashMap<>();
702 MSAttributeObject tempObject;
703 if (dependency!=null){
704 if (dependency.isEmpty()){
707 dependency.add(modelName);
708 for (String element: dependency){
709 tempObject = classMap.get(element);
710 if (tempObject!=null){
711 workingMap.putAll(classMap.get(element).getSubClass());
716 return createJson(classMap, modelName);
719 public List<String> getFullDependencyList(List<String> dependency, Map<String,MSAttributeObject > classMap) {
720 ArrayList<String> returnList = new ArrayList<>();
721 ArrayList<String> workingList;
722 returnList.addAll(dependency);
723 for (String element : dependency ){
724 if (classMap.containsKey(element)){
725 MSAttributeObject value = classMap.get(element);
726 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
727 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
728 for(String depend : workingList){
729 if (!returnList.contains(depend) && !depend.isEmpty()){
730 returnList.add(depend.trim());
742 public String parseTosca (String fileName){
743 LinkedHashMap<String,String> map= new LinkedHashMap<>();
749 if(map.get("error") != null){
750 return map.get("error");
754 parseDataAndPolicyNodes(map);
756 LinkedHashMap<String,String> dataMapForJson=parseDataNodes(map);
758 constructJsonForDataFields(dataMapForJson);
760 LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= parsePolicyNodes(map);
762 createAttributes(mapKey);
764 } catch (IOException e) {
766 }catch(ParserException e){
768 return e.getMessage();
774 @SuppressWarnings("unchecked")
775 public LinkedHashMap<String, String> load(String fileName) throws IOException,ParserException {
776 File newConfiguration = new File(fileName);
777 StringBuilder orderInfo = new StringBuilder("[");
778 Yaml yaml = new Yaml();
779 LinkedHashMap<Object, Object> yamlMap = null;
780 try(InputStream is = new FileInputStream(newConfiguration)){
781 yamlMap = (LinkedHashMap<Object, Object>) yaml.load(is);
782 } catch (FileNotFoundException e) {
785 throw new ParserException("Invalid TOSCA Model format. Please make sure it is a valid YAML file");
788 StringBuilder sb = new StringBuilder();
789 LinkedHashMap<String, String> settings = new LinkedHashMap<>();
790 if (yamlMap == null) {
794 String message = validations(yamlMap);
797 settings.put("error", message);
803 orderedElements.stream().forEach((string) -> {
804 orderInfo.append(string);
805 orderInfo.append(",");
806 logger.info("Content: " + string);
809 orderInfo.append("]");
811 dataOrderInfo = orderInfo.toString();
812 dataOrderInfo = dataOrderInfo.replace(",]", "]");
814 logger.info("dataOrderInfo :" + dataOrderInfo);
816 List<String> path = new ArrayList <>();
817 serializeMap(settings, sb, path, yamlMap);
821 @SuppressWarnings("unchecked")
822 private String validations(@SuppressWarnings("rawtypes") LinkedHashMap yamlMap){
824 boolean isNoteTypeFound = false;
825 boolean isDataTypeFound = false;
826 boolean isToscaVersionKeyFound = false;
827 boolean isToscaVersionValueFound = false;
828 @SuppressWarnings("rawtypes")
829 Map m1 = new HashMap();
832 // Get a set of the entries
833 @SuppressWarnings("rawtypes")
834 Set set = yamlMap.entrySet();
836 @SuppressWarnings("rawtypes")
837 Iterator i = set.iterator();
840 @SuppressWarnings("rawtypes")
841 Map.Entry me = (Map.Entry)i.next();
843 if("tosca_definitions_version".equals(me.getKey())){
844 isToscaVersionKeyFound = true;
846 m1.put("tosca_definitions_version", order);
849 if("tosca_simple_yaml_1_0_0".equals(me.getValue())){
850 isToscaVersionValueFound = true;
853 if("node_types".equals(me.getKey())){
854 isNoteTypeFound = true;
856 m1.put("node_types", order);
859 if("data_types".equals(me.getKey())){
860 isDataTypeFound = true;
862 m1.put("data_types", order);
868 if(!isDataTypeFound){
869 return "data_types are missing or invalid.";
872 if(!isToscaVersionKeyFound || !isToscaVersionValueFound){
873 return "tosca_definitions_version is missing or invalid.";
876 if(!isNoteTypeFound){
877 return "node_types are missing or invalid.";
880 short version = (short) m1.get("tosca_definitions_version");
883 return "tosca_definitions_version should be defined first.";
886 short data = (short) m1.get("data_types");
887 short node = (short) m1.get("node_types");
889 return "node_types should be defined before data_types.";
897 @SuppressWarnings({ "unchecked", "rawtypes" })
898 private void serializeMap(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
899 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
901 if (entry.getValue() instanceof Map) {
902 path.add((String) entry.getKey());
903 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
904 path.remove(path.size() - 1);
905 } else if (entry.getValue() instanceof List) {
906 path.add((String) entry.getKey());
907 serializeList(settings, sb, path, (List) entry.getValue());
908 path.remove(path.size() - 1);
910 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
915 @SuppressWarnings("unchecked")
916 private void serializeList(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) {
918 for (Object listEle : yamlList) {
919 if (listEle instanceof Map) {
920 path.add(Integer.toString(counter));
921 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
922 path.remove(path.size() - 1);
923 } else if (listEle instanceof List) {
924 path.add(Integer.toString(counter));
925 serializeList(settings, sb, path, (List<String>) listEle);
926 path.remove(path.size() - 1);
928 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
934 private void serializeValue(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) {
939 for (String pathEle : path) {
940 sb.append(pathEle).append('.');
943 settings.put(sb.toString(), value.toString());
947 void parseDataAndPolicyNodes(LinkedHashMap<String,String> map){
948 for(String key:map.keySet()){
949 if(key.contains("policy.nodes.Root"))
953 else if(key.contains("policy.nodes")){
954 String wordToFind = "policy.nodes.";
955 int indexForPolicyNode=key.indexOf(wordToFind);
956 String subNodeString= key.substring(indexForPolicyNode+13, key.length());
958 stringBetweenDots(subNodeString);
960 else if(key.contains("policy.data")){
961 String wordToFind="policy.data.";
962 int indexForPolicyNode=key.indexOf(wordToFind);
963 String subNodeString= key.substring(indexForPolicyNode+12, key.length());
965 stringBetweenDotsForDataFields(subNodeString);
970 // Second index of dot should be returned.
971 public int stringBetweenDots(String str){
972 String stringToSearch=str;
973 String[]ss=stringToSearch.split("\\.");
977 uniqueKeys.add(ss[2]);
981 return uniqueKeys.size();
985 public void stringBetweenDotsForDataFields(String str){
986 String stringToSearch=str;
987 String[]ss=stringToSearch.split("\\.");
992 uniqueDataKeys.add(ss[0]+"%"+ss[2]);
997 void constructJsonForDataFields(LinkedHashMap<String,String> dataMapForJson){
998 LinkedHashMap<String,LinkedHashMap<String,String>> dataMapKey= new LinkedHashMap <>();
999 LinkedHashMap<String, String> hmSub;
1000 for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
1001 String uniqueDataKey= entry.getKey();
1002 String[] uniqueDataKeySplit=uniqueDataKey.split("%");
1003 String value= dataMapForJson.get(uniqueDataKey);
1004 if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
1005 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
1006 hmSub.put(uniqueDataKeySplit[1], value);
1009 hmSub=new LinkedHashMap <>();
1010 hmSub.put(uniqueDataKeySplit[1], value);
1013 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
1016 JSONObject mainObject= new JSONObject();
1018 for(Map.Entry<String,LinkedHashMap<String,String>> entry: dataMapKey.entrySet()){
1019 String s=entry.getKey();
1020 json= new JSONObject();
1021 HashMap<String,String> jsonHm=dataMapKey.get(s);
1022 for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
1023 String key=entryMap.getKey();
1024 json.put(key, jsonHm.get(key));
1026 mainObject.put(s,json);
1028 Iterator<String> keysItr = mainObject.keys();
1029 while(keysItr.hasNext()) {
1030 String key = keysItr.next();
1031 String value = mainObject.get(key).toString();
1032 retmap.put(key, value);
1035 logger.info("#############################################################################");
1036 logger.info(mainObject);
1037 logger.info("###############################################################################");
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);
1050 String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED;
1051 String requiredValue= map.get(findRequired);
1053 String matchable =DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+MATCHABLE;
1055 String matchableValue= map.get(matchable);
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);
1063 matchableValues.put(uniqueDataKey, MATCHINGTRUE);
1067 if(requiredValue == null || requiredValue.isEmpty()){
1068 requiredValue = "false";
1070 if(typeValue != null && (typeValue.equalsIgnoreCase(STRING)||
1071 typeValue.equalsIgnoreCase(INTEGER))){
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);
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());
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);
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());
1099 StringBuilder stringListItems= new StringBuilder();
1100 if(LIST.equalsIgnoreCase(typeValue)){
1101 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":required-"+requiredValue +":MANY-false");
1102 }else if( MAP.equalsIgnoreCase(typeValue)){
1103 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":required-"+requiredValue +":MANY-true");
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 if((constraintsValue==null || constraintsValue.isEmpty()) && i==0){ //if no constraints at all ( index i as 0 can tell this )
1113 isConstraintsFound = false;
1114 //if type is list but no constraints
1115 String newValue = dataMapForJson.get(uniqueDataKey).replace("MANY-false", "MANY-true");
1116 newValue = newValue.replace(uniqueDataKeySplit[1].toUpperCase()+":", "");
1117 dataMapForJson.put(uniqueDataKey, newValue);
1120 isConstraintsFound = true;
1121 if(i == 0){ // only need to add one time for the same attribute
1122 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
1125 if(constraintsValue.contains("=")){
1126 constraintsValue = constraintsValue.replace("=", "equal-sign");
1128 dataConstraints.add(constraintsValue);
1129 dataListBuffer.append(constraintsValue+",");
1132 if(isConstraintsFound){
1133 dataListBuffer.append("]#");
1137 logger.info("entry_schema.type is not defined correctly");
1141 String findUserDefined=DATATYPE+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+TYPE;
1142 String userDefinedValue=map.get(findUserDefined);
1143 String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
1144 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1145 referenceIndividualStringBuilder.append(trimValue+REQUIREDVALUE);
1146 referenceIndividualStringBuilder.append(requiredValue+MANYFALSE);
1147 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1153 return dataMapForJson;
1157 LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String,String> map) throws ParserException{
1158 LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= new LinkedHashMap <>();
1159 for(String uniqueKey: uniqueKeys){
1160 LinkedHashMap<String,String> hm;
1162 for(Map.Entry<String,String> entry:map.entrySet()){
1163 String key=entry.getKey();
1164 if(key.contains(uniqueKey) && key.contains("policy.nodes")){
1165 if(mapKey.containsKey(uniqueKey)){
1166 hm = mapKey.get(uniqueKey);
1167 String keyStr= key.substring(key.lastIndexOf('.')+1);
1168 String valueStr= map.get(key);
1169 if("type".equalsIgnoreCase(keyStr) && ((key.contains("entry_schema.0.type") || key.contains("entry_schema.type") && valueStr.contains("policy.data.")))){
1170 throw new ParserException("For using user defined object type, Please make sure no space between 'type:' and object " + valueStr );
1173 if("type".equals(keyStr)){
1174 if(!key.contains("entry_schema"))
1176 hm.put(keyStr,valueStr);
1179 hm.put(keyStr,valueStr);
1183 hm = new LinkedHashMap <>();
1184 String keyStr= key.substring(key.lastIndexOf('.')+1);
1185 String valueStr= map.get(key);
1186 if(key.contains(".objective.")){
1187 throw new ParserException("Attribute objective is a key word. Please use a different name");
1189 if(("type").equals(keyStr)){
1190 if(!key.contains("entry_schema"))
1192 hm.put(keyStr,valueStr);
1195 hm.put(keyStr,valueStr);
1197 mapKey.put(uniqueKey, hm);
1205 void createAttributes(LinkedHashMap<String,LinkedHashMap<String,String>> mapKey){
1206 StringBuilder attributeStringBuilder= new StringBuilder();
1207 StringBuilder referenceStringBuilder= new StringBuilder();
1208 StringBuilder listBuffer= new StringBuilder();
1209 List<String> constraints= new ArrayList<>();
1210 for(Map.Entry<String,LinkedHashMap<String,String>> entry: mapKey.entrySet()){
1211 String keySetString= entry.getKey();
1212 LinkedHashMap<String,String> keyValues=mapKey.get(keySetString);
1213 if(STRING.equalsIgnoreCase(keyValues.get("type"))||
1214 INTEGER.equalsIgnoreCase(keyValues.get("type"))){
1215 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
1216 attributeIndividualStringBuilder.append(keySetString+"=");
1217 attributeIndividualStringBuilder.append(keyValues.get("type")+DEFAULTVALUE);
1218 attributeIndividualStringBuilder.append(keyValues.get("default")+REQUIREDVALUE);
1219 attributeIndividualStringBuilder.append(keyValues.get("required")+MANYFALSE);
1220 attributeStringBuilder.append(attributeIndividualStringBuilder+",");
1221 if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1222 matchableValues.put(keySetString, MATCHINGTRUE);
1225 else if(LIST.equalsIgnoreCase(keyValues.get("type"))){
1227 if(("true").equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1228 matchableValues.put(keySetString, MATCHINGTRUE);
1231 Set<String> keys= keyValues.keySet();
1232 Iterator<String> itr=keys.iterator();
1233 boolean isDefinedType = false;
1234 while(itr.hasNext()){
1235 String key= itr.next();
1236 if((!("type").equals(key) ||("required").equals(key)))
1238 String value= keyValues.get(key);
1239 //The "." in the value determines if its a string or a user defined type.
1240 if (!value.contains(".")){
1242 if(StringUtils.isNumeric(key) ){ //only integer key for the value of Constrains
1243 constraints.add(keyValues.get(key));
1246 //This is user defined type
1247 String trimValue=value.substring(value.lastIndexOf('.')+1);
1248 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1249 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYTRUE);
1250 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1251 isDefinedType = true;
1257 if(!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))){ //type is not user defined and is a list but no constraints defined.
1258 if(constraints == null || constraints.isEmpty()){
1259 referenceStringBuilder.append(keySetString+"=MANY-true"+",");
1263 //User defined Datatype.
1264 if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1265 matchableValues.put(keySetString, MATCHINGTRUE);
1267 String value=keyValues.get("type");
1268 if(value != null && !value.isEmpty()){
1269 String trimValue=value.substring(value.lastIndexOf('.')+1);
1270 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1271 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYFALSE);
1272 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1274 logger.info("keyValues.get(type) is null/empty");
1278 if(constraints!=null && !constraints.isEmpty()){
1280 listBuffer.append(keySetString.toUpperCase()+"=[");
1281 for(String str:constraints){
1282 listBuffer.append(str+",");
1284 listBuffer.append("]#");
1285 logger.info(listBuffer);
1288 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1289 referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+MANYFALSE);
1290 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1291 constraints.clear();
1295 dataListBuffer.append(listBuffer);
1298 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1299 logger.info("Whole attribute String is:"+attributeStringBuilder);
1300 logger.info("Whole reference String is:"+referenceStringBuilder);
1301 logger.info("List String is:"+listBuffer);
1302 logger.info("Data list buffer is:"+dataListBuffer);
1303 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1305 this.listConstraints=dataListBuffer.toString();
1306 this.referenceAttributes=referenceStringBuilder.toString();
1307 this.attributeString=attributeStringBuilder.toString();
1310 @SuppressWarnings("unchecked")
1311 public void findNode(LinkedHashMap<Object, Object> map) {
1313 map.forEach((key,value) -> {
1314 // if the value is properties and its type is map object, then save all the keys
1315 if(key.equals("properties") && value instanceof Map){
1316 saveNodes((LinkedHashMap<?, ?>)value);
1319 if(!key.equals("policy.nodes.Root") && value instanceof Map){
1320 //value is a Map object, then make a recursive call
1321 findNode((LinkedHashMap<Object, Object>) value);
1327 public void saveNodes(LinkedHashMap<?, ?> map) {
1329 map.forEach((key,value) -> {
1331 orderedElements.add((String)key);
1337 public String getAttributeString() {
1338 return attributeString;
1340 public void setAttributeString(String attributeString) {
1341 this.attributeString = attributeString;
1344 public LinkedHashMap<String, Object> getRetmap() {
1348 public void setRetmap(LinkedHashMap<String, Object> retmap) {
1349 this.retmap = retmap;
1351 public Map<String, String> getMatchableValues() {
1352 return matchableValues;
1355 public void setMatchableValues(Map<String, String> matchableValues) {
1356 this.matchableValues = matchableValues;
1358 public String getReferenceAttributes() {
1359 return referenceAttributes;
1362 public void setReferenceAttributes(String referenceAttributes) {
1363 this.referenceAttributes = referenceAttributes;
1365 public String getListConstraints() {
1366 return listConstraints;
1369 public void setListConstraints(String listConstraints) {
1370 this.listConstraints = listConstraints;
1372 public String getDataOrderInfo() {
1373 return dataOrderInfo;
1376 public void setDataOrderInfo(String dataOrderInfo) {
1377 this.dataOrderInfo = dataOrderInfo;