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 DEFAULT=".default";
100 public static final String MANYFALSE=":MANY-false";
101 public static final String MANYTRUE=":MANY-true";
102 public static final String DEFAULTVALUE=":defaultValue-";
103 public static final String REQUIREDVALUE=":required-";
104 public static final String MATCHABLEKEY="matchable";
105 public static final String REQUIREDFALSE=":required-false";
106 public static final String REQUIREDTRUE=":required-true";
107 public static final String MATCHINGTRUE="matching-true";
109 private StringBuilder dataListBuffer=new StringBuilder();
110 private List<String> dataConstraints= new ArrayList <>();
111 private String attributeString = null;
113 public MSModelUtils(){
116 public MSModelUtils(String onap, String policy){
118 this.policy = policy;
121 private enum ANNOTATION_TYPE{
122 MATCHING, VALIDATION, DICTIONARY
125 public enum MODEL_TYPE {
130 public Map<String, MSAttributeObject> processEpackage(String file, MODEL_TYPE model){
131 if (model == MODEL_TYPE.XMI ){
132 processXMIEpackage(file);
138 private void processXMIEpackage(String xmiFile){
139 EPackage root = getEpackage(xmiFile);
140 TreeIterator<EObject> treeItr = root.eAllContents();
144 // Pulling out dependency from file
145 while (treeItr.hasNext()) {
146 EObject obj = treeItr.next();
147 if (obj instanceof EClassifier) {
148 EClassifier eClassifier = (EClassifier) obj;
149 className = eClassifier.getName();
151 if (obj instanceof EEnum) {
152 enumMap.putAll(getEEnum(obj));
153 }else if (obj instanceof EClass) {
154 String temp = getDependencyList(eClassifier).toString();
155 returnValue = StringUtils.replaceEach(temp, new String[]{"[", "]"}, new String[]{"", ""});
156 getAttributes(className, returnValue, root);
161 if (!enumMap.isEmpty()){
164 if (!matchingClass.isEmpty()){
165 CheckForMatchingClass();
169 private void CheckForMatchingClass() {
170 HashMap<String, String> tempAttribute = new HashMap<>();
172 for (Entry<String, String> set : matchingClass.entrySet()){
173 String key = set.getKey();
174 if (classMap.containsKey(key)){
175 Map<String, String> listAttributes = classMap.get(key).getAttribute();
176 Map<String, String> listRef = classMap.get(key).getRefAttribute();
177 for ( Entry<String, String> eSet : listAttributes.entrySet()){
178 String key2 = eSet.getKey();
179 tempAttribute.put(key2, MATCHINGTRUE);
181 for ( Entry<String, String> eSet : listRef.entrySet()){
182 String key3 = eSet.getKey();
183 tempAttribute.put(key3, MATCHINGTRUE);
187 UpdateMatching(tempAttribute, key);
194 private void UpdateMatching(HashMap<String, String> tempAttribute, String key) {
195 Map<String, MSAttributeObject> newClass = classMap;
197 for (Entry<String, MSAttributeObject> updateClass : newClass.entrySet()){
198 Map<String, String> valueMap = updateClass.getValue().getMatchingSet();
199 String keymap = updateClass.getKey();
200 if (valueMap.containsKey(key)){
201 Map<String, String> modifyMap = classMap.get(keymap).getMatchingSet();
202 modifyMap.remove(key);
203 modifyMap.putAll(tempAttribute);
204 classMap.get(keymap).setMatchingSet(modifyMap);
210 private void addEnumClassMap() {
211 for (Entry<String, MSAttributeObject> value :classMap.entrySet()){
212 value.getValue().setEnumType(enumMap);
216 private EPackage getEpackage(String xmiFile) {
217 ResourceSet resSet = new ResourceSetImpl();
218 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
219 Map<String, Object> m = reg.getExtensionToFactoryMap();
220 m.put("xmi", new XMIResourceFactoryImpl());
221 Resource resource = resSet.getResource(URI.createFileURI(xmiFile), true);
223 resource.load(Collections.emptyMap());
224 } catch (IOException e) {
225 logger.error("Error loading Encore Resource for new Model" + e);
228 return (EPackage) resource.getContents().get(0);
231 private HashMap<String, String> getEEnum(EObject obj) {
232 List<String> valueList = new ArrayList<>();
233 HashMap<String, String> returnMap = new HashMap<>();
234 EEnum eenum = (EEnum)obj;
236 String name = eenum.getName();
237 for (EEnumLiteral eEnumLiteral : eenum.getELiterals())
239 Enumerator instance = eEnumLiteral.getInstance();
240 String value = instance.getLiteral();
241 valueList.add(value);
243 returnMap.put(name, valueList.toString());
247 public void getAttributes(String className, String dependency, EPackage root) {
248 List<String> dpendList = new ArrayList<>();
249 if (dependency!=null){
250 dpendList = new ArrayList<>(Arrays.asList(dependency.split(",")));
252 MSAttributeObject msAttributeObject = new MSAttributeObject();
253 msAttributeObject.setClassName(className);
254 String extendClass = getSubTypes(root, className);
255 Map<String, String> returnRefList = getRefAttributeList(root, className, extendClass);
256 Map<String, String> returnAttributeList = getAttributeList(root, className, extendClass);
257 Map<String, Object> returnSubList = getSubAttributeList(root, className, extendClass);
258 HashMap<String, String> returnAnnotation = getAnnotation(root, className, extendClass);
259 msAttributeObject.setAttribute(returnAttributeList);
260 msAttributeObject.setRefAttribute(returnRefList);
261 msAttributeObject.setSubClass(returnSubList);
262 msAttributeObject.setDependency(dpendList.toString());
263 msAttributeObject.addMatchingSet(returnAnnotation);
264 msAttributeObject.setPolicyTempalate(isPolicyTemplate(root, className));
266 this.classMap.put(className, msAttributeObject);
269 private HashMap<String, String> getAnnotation(EPackage root, String className, String extendClass) {
270 TreeIterator<EObject> treeItr = root.eAllContents();
271 boolean requiredAttribute = false;
272 boolean requiredMatchAttribute = false;
273 HashMap<String, String> annotationSet = new HashMap<>();
276 String annotationDict;
278 // Pulling out dependency from file
279 while (treeItr.hasNext()) {
280 EObject obj = treeItr.next();
281 if (obj instanceof EClassifier) {
282 requiredAttribute = isRequiredAttribute(obj, className );
283 requiredMatchAttribute = isRequiredAttribute(obj, extendClass );
286 if (requiredAttribute){
287 if (obj instanceof EStructuralFeature) {
288 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
289 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
290 matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
292 annotationSet.put(eStrucClassifier.getName(), matching);
294 range = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
296 annotationSet.put(eStrucClassifier.getName(), range);
298 annotationDict = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
299 if (annotationDict!=null){
300 annotationSet.put(eStrucClassifier.getName(), annotationDict);
304 } else if (requiredMatchAttribute && (obj instanceof EStructuralFeature)) {
305 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
306 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
307 matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
309 if (obj instanceof EReference){
310 EClass refType = ((EReference) obj).getEReferenceType();
311 annotationSet.put(refType.getName(), matching);
312 matchingClass.put(refType.getName(), matching);
314 annotationSet.put(eStrucClassifier.getName(), matching);
320 return annotationSet;
323 private Map<String, Object> getSubAttributeList(EPackage root, String className , String superClass) {
324 TreeIterator<EObject> treeItr = root.eAllContents();
325 boolean requiredAttribute = false;
326 Map<String, Object> subAttribute = new HashMap<>();
327 int rollingCount = 0;
328 int processClass = 0;
331 // Pulling out dependency from file
332 while (treeItr.hasNext() && rollingCount < 2) {
334 EObject obj = treeItr.next();
335 if (obj instanceof EClassifier) {
336 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
337 requiredAttribute = true;
339 requiredAttribute = false;
341 if (requiredAttribute){
344 rollingCount = rollingCount+processClass;
347 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
348 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
349 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
350 annotation = annotationTest(eStrucClassifier, configuration, onap);
351 if (annotation && obj instanceof EReference) {
352 EClass refType = ((EReference) obj).getEReferenceType();
353 if(!refType.toString().contains(eProxyURI)){
354 String required = REQUIREDFALSE;
355 if(eStrucClassifier.getLowerBound() == 1){
356 required = REQUIREDTRUE;
358 subAttribute.put(eStrucClassifier.getName(), refType.getName() + required);
367 public String checkDefultValue(String defultValue) {
368 if (defultValue!=null){
369 return DEFAULTVALUE+ defultValue;
371 return ":defaultValue-NA";
375 public String checkRequiredPattern(int upper, int lower) {
377 String pattern = XACMLProperties.getProperty(XACMLRestProperties.PROP_XCORE_REQUIRED_PATTERN);
380 if (upper == Integer.parseInt(pattern.split(",")[1]) && lower==Integer.parseInt(pattern.split(",")[0])){
385 return REQUIREDFALSE;
388 public JSONObject buildJavaObject(Map<String, String> map){
390 return new JSONObject(map);
393 public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass){
395 TreeIterator<EObject> treeItr = root.eAllContents();
396 boolean requiredAttribute = false;
397 HashMap<String, String> refAttribute = new HashMap<>();
398 int rollingCount = 0;
399 int processClass = 0;
401 // Pulling out dependency from file
402 while (treeItr.hasNext()) {
403 EObject obj = treeItr.next();
404 if (obj instanceof EClassifier) {
405 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
406 requiredAttribute = true;
408 requiredAttribute = false;
410 if (requiredAttribute){
413 rollingCount = rollingCount+processClass;
416 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
417 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
418 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
419 annotation = annotationTest(eStrucClassifier, configuration, onap);
420 if ( annotation && obj instanceof EReference) {
421 EClass refType = ((EReference) obj).getEReferenceType();
422 if(refType.toString().contains(eProxyURI)){
423 String one = refType.toString().split(eProxyURI)[1];
424 String refValue = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
425 refAttribute.put(eStrucClassifier.getName(), refValue);
427 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
428 String required = REQUIREDFALSE;
429 if(((EStructuralFeature) obj).getLowerBound() == 1){
430 required = REQUIREDTRUE;
432 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
434 } else if (annotation && obj instanceof EAttributeImpl){
435 EClassifier refType = ((EAttributeImpl) obj).getEType();
436 if (refType instanceof EEnumImpl){
437 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
438 String required = REQUIREDFALSE;
439 if(((EStructuralFeature) obj).getLowerBound() == 1){
440 required = REQUIREDTRUE;
442 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
452 private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
453 String annotationType;
454 EAnnotation eAnnotation;
458 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
460 for (int i = 0; i < value.size(); i++){
461 annotationType = value.get(i).getSource();
462 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
463 onapType = eAnnotation.getDetails().get(0).getValue();
464 onapValue = eAnnotation.getDetails().get(0).getKey();
466 if (annotationType.contains(type) && onapType.contains(annotation)){
470 if (annotationType.contains(type) && onapValue.contains(annotation)){
479 private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
480 String annotationType;
481 EAnnotation eAnnotation;
483 String onapValue = null;
485 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
487 for (int i = 0; i < value.size(); i++){
488 annotationType = value.get(i).getSource();
489 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
490 onapType = eAnnotation.getDetails().get(0).getKey();
491 if (annotationType.contains(type) && onapType.compareToIgnoreCase(annotation.toString())==0){
492 onapValue = eAnnotation.getDetails().get(0).getValue();
493 if (annotation == ANNOTATION_TYPE.VALIDATION){
496 return onapType + "-" + onapValue;
503 public boolean isRequiredAttribute(EObject obj, String className){
504 EClassifier eClassifier = (EClassifier) obj;
505 String workingClass = eClassifier.getName().trim();
506 if (workingClass.equalsIgnoreCase(className)){
513 private boolean isPolicyTemplate(EPackage root, String className){
515 for (EClassifier classifier : root.getEClassifiers()){
516 if (classifier instanceof EClass) {
517 EClass eClass = (EClass)classifier;
518 if (eClass.getName().contentEquals(className)){
519 EList<EAnnotation> value = eClass.getEAnnotations();
520 for (EAnnotation workingValue : value){
521 EMap<String, String> keyMap = workingValue.getDetails();
522 if (keyMap.containsKey("policyTemplate")){
531 private String getSubTypes(EPackage root, String className) {
532 String returnSubTypes = null;
533 for (EClassifier classifier : root.getEClassifiers()){
534 if (classifier instanceof EClass) {
535 EClass eClass = (EClass)classifier;
537 for (EClass eSuperType : eClass.getEAllSuperTypes())
539 if (eClass.getName().contentEquals(className)){
540 returnSubTypes = eSuperType.getName();
545 return returnSubTypes;
548 public Map<String, String> getAttributeList(EPackage root, String className, String superClass){
550 TreeIterator<EObject> treeItr = root.eAllContents();
551 boolean requiredAttribute = false;
552 HashMap<String, String> refAttribute = new HashMap<>();
554 boolean dictionaryTest;
558 // Pulling out dependency from file
559 while (treeItr.hasNext()) {
560 EObject obj = treeItr.next();
561 if (obj instanceof EClassifier) {
562 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
563 requiredAttribute = true;
565 requiredAttribute = false;
570 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
571 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
572 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
573 annotation = annotationTest(eStrucClassifier, configuration, onap);
574 dictionaryTest = annotationTest(eStrucClassifier, dictionary, policy);
575 EClassifier refType = ((EStructuralFeature) obj).getEType();
576 if (annotation && !(obj instanceof EReference) && !(refType instanceof EEnumImpl)) {
577 String name = eStrucClassifier.getName();
579 eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
581 eType = eStrucClassifier.getEType().getInstanceClassName();
583 defaultValue = checkDefultValue(((EStructuralFeature) obj).getDefaultValueLiteral());
585 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
586 String required = checkRequiredPattern(((EStructuralFeature) obj).getUpperBound(), ((EStructuralFeature) obj).getLowerBound());
587 String attributeValue = eType + defaultValue + required + array;
588 refAttribute.put(name, attributeValue);
597 public String arrayCheck(int upperBound) {
599 if (upperBound == -1){
606 public List<String> getDependencyList(EClassifier eClassifier){
607 List<String> returnValue = new ArrayList<>();;
608 EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes();
609 if (somelist.isEmpty()){
612 for(EClass depend: somelist){
613 if (depend.toString().contains(eProxyURI)){
614 String one = depend.toString().split(eProxyURI)[1];
615 String value = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
616 returnValue.add(value);
623 public Map<String, String> buildSubList(Map<String, String> subClassAttributes, Map<String, MSAttributeObject> classMap, String className){
624 Map<String, String> missingValues = new HashMap<>();
625 Map<String, String> workingMap;
628 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
629 String value = map.getValue().split(":")[0];
631 classMap.get(className).getEnumType();
632 enumType = classMap.get(className).getEnumType().containsKey(value);
634 workingMap = classMap.get(value).getRefAttribute();
635 for ( Entry<String, String> subMab : workingMap.entrySet()){
636 String value2 = subMab.getValue().split(":")[0];
637 if (!subClassAttributes.containsValue(value2)){
638 missingValues.put(subMab.getKey(), subMab.getValue());
646 return missingValues;
649 public Map<String, Map<String, String>> recursiveReference(Map<String, MSAttributeObject> classMap, String className){
651 Map<String, Map<String, String>> returnObject = new HashMap<>();
652 Map<String, String> returnClass = getRefclass(classMap, className);
653 returnObject.put(className, returnClass);
654 for (Entry<String, String> reAttribute :returnClass.entrySet()){
655 if (reAttribute.getValue().split(":")[1].contains("MANY") &&
656 classMap.get(reAttribute.getValue().split(":")[0]) != null){
657 returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0]));
666 public String createJson(Map<String, MSAttributeObject> classMap, String className) {
668 Map<String, Map<String, String>> myObject = new HashMap<>();
669 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
670 String value = map.getValue().split(":")[0];
672 enumType = classMap.get(className).getEnumType().containsKey(value);
673 if (!enumType && map.getValue().split(":")[1].contains("MANY")){
674 Map<String, Map<String, String>> testRecursive = recursiveReference(classMap, map.getValue().split(":")[0] );
675 myObject.putAll(testRecursive);
680 Gson gson = new Gson();
681 return gson.toJson(myObject);
684 public Map<String, String> getRefclass(Map<String, MSAttributeObject> classMap, String className){
685 HashMap<String, String> missingValues = new HashMap<>();
687 if (classMap.get(className).getAttribute()!=null || !classMap.get(className).getAttribute().isEmpty()){
688 missingValues.putAll(classMap.get(className).getAttribute());
691 if (classMap.get(className).getRefAttribute()!=null || !classMap.get(className).getRefAttribute().isEmpty()){
692 missingValues.putAll(classMap.get(className).getRefAttribute());
695 return missingValues;
698 public String createSubAttributes(List<String> dependency, Map<String, MSAttributeObject> classMap, String modelName) {
700 HashMap <String, Object> workingMap = new HashMap<>();
701 MSAttributeObject tempObject;
702 if (dependency!=null){
703 if (dependency.isEmpty()){
706 dependency.add(modelName);
707 for (String element: dependency){
708 tempObject = classMap.get(element);
709 if (tempObject!=null){
710 workingMap.putAll(classMap.get(element).getSubClass());
715 return createJson(classMap, modelName);
718 public List<String> getFullDependencyList(List<String> dependency, Map<String,MSAttributeObject > classMap) {
719 ArrayList<String> returnList = new ArrayList<>();
720 ArrayList<String> workingList;
721 returnList.addAll(dependency);
722 for (String element : dependency ){
723 if (classMap.containsKey(element)){
724 MSAttributeObject value = classMap.get(element);
725 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
726 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
727 for(String depend : workingList){
728 if (!returnList.contains(depend) && !depend.isEmpty()){
729 returnList.add(depend.trim());
741 public String parseTosca (String fileName){
742 LinkedHashMap<String,String> map= new LinkedHashMap<>();
748 if(map.get("error") != null){
749 return map.get("error");
753 parseDataAndPolicyNodes(map);
755 LinkedHashMap<String,String> dataMapForJson=parseDataNodes(map);
757 constructJsonForDataFields(dataMapForJson);
759 LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= parsePolicyNodes(map);
761 createAttributes(mapKey);
763 } catch (IOException e) {
770 @SuppressWarnings("unchecked")
771 public LinkedHashMap<String, String> load(String fileName) throws IOException {
772 File newConfiguration = new File(fileName);
773 StringBuilder orderInfo = new StringBuilder("[");
774 Yaml yaml = new Yaml();
775 LinkedHashMap<Object, Object> yamlMap = null;
776 try(InputStream is = new FileInputStream(newConfiguration)){
777 yamlMap = (LinkedHashMap<Object, Object>) yaml.load(is);
778 } catch (FileNotFoundException e) {
782 StringBuilder sb = new StringBuilder();
783 LinkedHashMap<String, String> settings = new LinkedHashMap<>();
784 if (yamlMap == null) {
788 String message = validations(yamlMap);
791 settings.put("error", message);
797 orderedElements.stream().forEach((string) -> {
798 orderInfo.append(string);
799 orderInfo.append(",");
800 logger.info("Content: " + string);
803 orderInfo.append("]");
805 dataOrderInfo = orderInfo.toString();
806 dataOrderInfo = dataOrderInfo.replace(",]", "]");
808 logger.info("dataOrderInfo :" + dataOrderInfo);
810 List<String> path = new ArrayList <>();
811 serializeMap(settings, sb, path, yamlMap);
815 @SuppressWarnings("unchecked")
816 private String validations(@SuppressWarnings("rawtypes") LinkedHashMap yamlMap){
818 boolean isNoteTypeFound = false;
819 boolean isDataTypeFound = false;
820 boolean isToscaVersionKeyFound = false;
821 boolean isToscaVersionValueFound = false;
822 @SuppressWarnings("rawtypes")
823 Map m1 = new HashMap();
826 // Get a set of the entries
827 @SuppressWarnings("rawtypes")
828 Set set = yamlMap.entrySet();
830 @SuppressWarnings("rawtypes")
831 Iterator i = set.iterator();
834 @SuppressWarnings("rawtypes")
835 Map.Entry me = (Map.Entry)i.next();
837 if("tosca_definitions_version".equals(me.getKey())){
838 isToscaVersionKeyFound = true;
840 m1.put("tosca_definitions_version", order);
843 if("tosca_simple_yaml_1_0_0".equals(me.getValue())){
844 isToscaVersionValueFound = true;
847 if("node_types".equals(me.getKey())){
848 isNoteTypeFound = true;
850 m1.put("node_types", order);
853 if("data_types".equals(me.getKey())){
854 isDataTypeFound = true;
856 m1.put("data_types", order);
862 if(!isDataTypeFound){
863 return "data_types are missing or invalid.";
866 if(!isToscaVersionKeyFound || !isToscaVersionValueFound){
867 return "tosca_definitions_version is missing or invalid.";
870 if(!isNoteTypeFound){
871 return "node_types are missing or invalid.";
874 short version = (short) m1.get("tosca_definitions_version");
877 return "tosca_definitions_version should be defined first.";
880 short data = (short) m1.get("data_types");
881 short node = (short) m1.get("node_types");
883 return "node_types should be defined before data_types.";
891 @SuppressWarnings({ "unchecked", "rawtypes" })
892 private void serializeMap(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
893 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
895 if (entry.getValue() instanceof Map) {
896 path.add((String) entry.getKey());
897 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
898 path.remove(path.size() - 1);
899 } else if (entry.getValue() instanceof List) {
900 path.add((String) entry.getKey());
901 serializeList(settings, sb, path, (List) entry.getValue());
902 path.remove(path.size() - 1);
904 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
909 @SuppressWarnings("unchecked")
910 private void serializeList(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) {
912 for (Object listEle : yamlList) {
913 if (listEle instanceof Map) {
914 path.add(Integer.toString(counter));
915 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
916 path.remove(path.size() - 1);
917 } else if (listEle instanceof List) {
918 path.add(Integer.toString(counter));
919 serializeList(settings, sb, path, (List<String>) listEle);
920 path.remove(path.size() - 1);
922 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
928 private void serializeValue(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) {
933 for (String pathEle : path) {
934 sb.append(pathEle).append('.');
937 settings.put(sb.toString(), value.toString());
941 void parseDataAndPolicyNodes(LinkedHashMap<String,String> map){
942 for(String key:map.keySet()){
943 if(key.contains("policy.nodes.Root"))
947 else if(key.contains("policy.nodes")){
948 String wordToFind = "policy.nodes.";
949 int indexForPolicyNode=key.indexOf(wordToFind);
950 String subNodeString= key.substring(indexForPolicyNode+13, key.length());
952 stringBetweenDots(subNodeString);
954 else if(key.contains("policy.data")){
955 String wordToFind="policy.data.";
956 int indexForPolicyNode=key.indexOf(wordToFind);
957 String subNodeString= key.substring(indexForPolicyNode+12, key.length());
959 stringBetweenDotsForDataFields(subNodeString);
964 // Second index of dot should be returned.
965 public int stringBetweenDots(String str){
966 String stringToSearch=str;
967 String[]ss=stringToSearch.split("\\.");
971 uniqueKeys.add(ss[2]);
975 return uniqueKeys.size();
979 public void stringBetweenDotsForDataFields(String str){
980 String stringToSearch=str;
981 String[]ss=stringToSearch.split("\\.");
986 uniqueDataKeys.add(ss[0]+"%"+ss[2]);
991 void constructJsonForDataFields(LinkedHashMap<String,String> dataMapForJson){
992 LinkedHashMap<String,LinkedHashMap<String,String>> dataMapKey= new LinkedHashMap <>();
993 LinkedHashMap<String, String> hmSub;
994 for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
995 String uniqueDataKey= entry.getKey();
996 String[] uniqueDataKeySplit=uniqueDataKey.split("%");
997 String value= dataMapForJson.get(uniqueDataKey);
998 if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
999 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
1000 hmSub.put(uniqueDataKeySplit[1], value);
1003 hmSub=new LinkedHashMap <>();
1004 hmSub.put(uniqueDataKeySplit[1], value);
1007 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
1010 JSONObject mainObject= new JSONObject();
1012 for(Map.Entry<String,LinkedHashMap<String,String>> entry: dataMapKey.entrySet()){
1013 String s=entry.getKey();
1014 json= new JSONObject();
1015 HashMap<String,String> jsonHm=dataMapKey.get(s);
1016 for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
1017 String key=entryMap.getKey();
1018 json.put(key, jsonHm.get(key));
1020 mainObject.put(s,json);
1022 Iterator<String> keysItr = mainObject.keys();
1023 while(keysItr.hasNext()) {
1024 String key = keysItr.next();
1025 String value = mainObject.get(key).toString();
1026 retmap.put(key, value);
1029 logger.info("#############################################################################");
1030 logger.info(mainObject);
1031 logger.info("###############################################################################");
1034 LinkedHashMap<String,String> parseDataNodes(LinkedHashMap<String,String> map){
1035 LinkedHashMap<String,String> dataMapForJson=new LinkedHashMap <>();
1036 matchableValues = new HashMap <>();
1037 for(String uniqueDataKey: uniqueDataKeys){
1038 if(uniqueDataKey.contains("%")){
1039 String[] uniqueDataKeySplit= uniqueDataKey.split("%");
1040 String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE;
1041 String typeValue=map.get(findType);
1042 logger.info(typeValue);
1044 String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED;
1045 String requiredValue= map.get(findRequired);
1047 String matchable =DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+MATCHABLE;
1049 String matchableValue= map.get(matchable);
1051 if(matchableValue != null && matchableValue.equalsIgnoreCase("true")){
1052 if(uniqueDataKey.contains("%")){
1053 String[] keys= uniqueDataKey.split("%");
1054 String key=keys[keys.length -1];
1055 matchableValues.put(key, MATCHINGTRUE);
1057 matchableValues.put(uniqueDataKey, MATCHINGTRUE);
1061 if(requiredValue == null || requiredValue.isEmpty()){
1062 requiredValue = "false";
1064 if(typeValue != null && (typeValue.equalsIgnoreCase(STRING)||
1065 typeValue.equalsIgnoreCase(INTEGER))){
1067 String findDefault=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+DEFAULT;
1068 String defaultValue= map.get(findDefault);
1069 logger.info("defaultValue is:"+ defaultValue);
1070 logger.info("requiredValue is:"+ requiredValue);
1072 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
1073 attributeIndividualStringBuilder.append(typeValue+DEFAULTVALUE);
1074 attributeIndividualStringBuilder.append(defaultValue+REQUIREDVALUE);
1075 attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
1076 dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
1078 else if(typeValue != null && typeValue.equalsIgnoreCase(LIST)){
1079 logger.info("requiredValue is:"+ requiredValue);
1080 String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
1081 String listValue=map.get(findList);
1082 if(listValue!=null){
1083 logger.info("Type of list is:"+ listValue);
1085 if(listValue.contains(".")){
1086 String trimValue=listValue.substring(listValue.lastIndexOf('.')+1);
1087 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1088 referenceIndividualStringBuilder.append(trimValue+REQUIREDVALUE);
1089 referenceIndividualStringBuilder.append(requiredValue+MANYTRUE);
1090 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1093 StringBuilder stringListItems= new StringBuilder();
1094 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+REQUIREDVALUE+requiredValue +MANYFALSE);
1095 dataMapForJson.put(uniqueDataKey, stringListItems.toString());
1096 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
1097 for(int i=0;i<10;i++){
1098 String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i;
1099 logger.info("findConstraints => " + findConstraints);
1100 String constraintsValue=map.get(findConstraints);
1101 logger.info("constraintsValue => " + constraintsValue);
1102 if(constraintsValue==null){
1106 logger.info("constraintsValue => " + constraintsValue);
1107 if(constraintsValue.contains("=")){
1108 constraintsValue = constraintsValue.replace("=", "equal-sign");
1110 dataConstraints.add(constraintsValue);
1111 dataListBuffer.append(constraintsValue+",");
1114 dataListBuffer.append("]#");
1115 logger.info(dataListBuffer);
1120 String findUserDefined=DATATYPE+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+TYPE;
1121 String userDefinedValue=map.get(findUserDefined);
1122 String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
1123 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1124 referenceIndividualStringBuilder.append(trimValue+REQUIREDVALUE);
1125 referenceIndividualStringBuilder.append(requiredValue+MANYFALSE);
1126 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1132 return dataMapForJson;
1136 LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String,String> map){
1137 LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= new LinkedHashMap <>();
1138 for(String uniqueKey: uniqueKeys){
1139 LinkedHashMap<String,String> hm;
1141 for(Map.Entry<String,String> entry:map.entrySet()){
1142 String key=entry.getKey();
1143 if(key.contains(uniqueKey) && key.contains("policy.nodes")){
1144 if(mapKey.containsKey(uniqueKey)){
1145 hm = mapKey.get(uniqueKey);
1146 String keyStr= key.substring(key.lastIndexOf('.')+1);
1147 String valueStr= map.get(key);
1148 if(("type").equals(keyStr)){
1149 if(!key.contains("entry_schema"))
1151 hm.put(keyStr,valueStr);
1154 hm.put(keyStr,valueStr);
1158 hm = new LinkedHashMap <>();
1159 String keyStr= key.substring(key.lastIndexOf('.')+1);
1160 String valueStr= map.get(key);
1161 if(("type").equals(keyStr)){
1162 if(!key.contains("entry_schema"))
1164 hm.put(keyStr,valueStr);
1167 hm.put(keyStr,valueStr);
1169 mapKey.put(uniqueKey, hm);
1177 void createAttributes(LinkedHashMap<String,LinkedHashMap<String,String>> mapKey){
1178 StringBuilder attributeStringBuilder= new StringBuilder();
1179 StringBuilder referenceStringBuilder= new StringBuilder();
1180 StringBuilder listBuffer= new StringBuilder();
1181 List<String> constraints= new ArrayList<>();
1182 for(Map.Entry<String,LinkedHashMap<String,String>> entry: mapKey.entrySet()){
1183 String keySetString= entry.getKey();
1184 LinkedHashMap<String,String> keyValues=mapKey.get(keySetString);
1185 if(STRING.equalsIgnoreCase(keyValues.get("type"))||
1186 INTEGER.equalsIgnoreCase(keyValues.get("type"))){
1187 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
1188 attributeIndividualStringBuilder.append(keySetString+"=");
1189 attributeIndividualStringBuilder.append(keyValues.get("type")+DEFAULTVALUE);
1190 attributeIndividualStringBuilder.append(keyValues.get("default")+REQUIREDVALUE);
1191 attributeIndividualStringBuilder.append(keyValues.get("required")+MANYFALSE);
1192 attributeStringBuilder.append(attributeIndividualStringBuilder+",");
1193 if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1194 matchableValues.put(keySetString, MATCHINGTRUE);
1197 else if(LIST.equalsIgnoreCase(keyValues.get("type"))){
1199 if(("true").equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1200 matchableValues.put(keySetString, MATCHINGTRUE);
1203 Set<String> keys= keyValues.keySet();
1204 Iterator<String> itr=keys.iterator();
1205 boolean isDefinedType = false;
1206 while(itr.hasNext()){
1207 String key= itr.next();
1208 if((!("type").equals(key) ||("required").equals(key)))
1210 String value= keyValues.get(key);
1211 //The "." in the value determines if its a string or a user defined type.
1212 if (!value.contains(".")){
1214 if(StringUtils.isNumeric(key) ){ //only integer key for the value of Constrains
1215 constraints.add(keyValues.get(key));
1218 //This is user defined type
1219 String trimValue=value.substring(value.lastIndexOf('.')+1);
1220 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1221 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYTRUE);
1222 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1223 isDefinedType = true;
1229 if(!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))){ //type is not user defined and is a list but no constraints defined.
1230 if(constraints == null || constraints.isEmpty()){
1231 referenceStringBuilder.append(keySetString+"=MANY-true"+",");
1235 //User defined Datatype.
1236 if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1237 matchableValues.put(keySetString, MATCHINGTRUE);
1239 String value=keyValues.get("type");
1240 if(value != null && !value.isEmpty()){
1241 String trimValue=value.substring(value.lastIndexOf('.')+1);
1242 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1243 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYFALSE);
1244 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1246 logger.info("keyValues.get(type) is null/empty");
1250 if(constraints!=null && !constraints.isEmpty()){
1252 listBuffer.append(keySetString.toUpperCase()+"=[");
1253 for(String str:constraints){
1254 listBuffer.append(str+",");
1256 listBuffer.append("]#");
1257 logger.info(listBuffer);
1260 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1261 referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+MANYFALSE);
1262 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1263 constraints.clear();
1267 dataListBuffer.append(listBuffer);
1270 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1271 logger.info("Whole attribute String is:"+attributeStringBuilder);
1272 logger.info("Whole reference String is:"+referenceStringBuilder);
1273 logger.info("List String is:"+listBuffer);
1274 logger.info("Data list buffer is:"+dataListBuffer);
1275 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1277 this.listConstraints=dataListBuffer.toString();
1278 this.referenceAttributes=referenceStringBuilder.toString();
1279 this.attributeString=attributeStringBuilder.toString();
1282 @SuppressWarnings("unchecked")
1283 public void findNode(LinkedHashMap<Object, Object> map) {
1285 map.forEach((key,value) -> {
1286 // if the value is properties and its type is map object, then save all the keys
1287 if(key.equals("properties") && value instanceof Map){
1288 saveNodes((LinkedHashMap<?, ?>)value);
1291 if(!key.equals("policy.nodes.Root") && value instanceof Map){
1292 //value is a Map object, then make a recursive call
1293 findNode((LinkedHashMap<Object, Object>) value);
1299 public void saveNodes(LinkedHashMap<?, ?> map) {
1301 map.forEach((key,value) -> {
1303 orderedElements.add((String)key);
1309 public String getAttributeString() {
1310 return attributeString;
1312 public void setAttributeString(String attributeString) {
1313 this.attributeString = attributeString;
1316 public LinkedHashMap<String, Object> getRetmap() {
1320 public void setRetmap(LinkedHashMap<String, Object> retmap) {
1321 this.retmap = retmap;
1323 public Map<String, String> getMatchableValues() {
1324 return matchableValues;
1327 public void setMatchableValues(Map<String, String> matchableValues) {
1328 this.matchableValues = matchableValues;
1330 public String getReferenceAttributes() {
1331 return referenceAttributes;
1334 public void setReferenceAttributes(String referenceAttributes) {
1335 this.referenceAttributes = referenceAttributes;
1337 public String getListConstraints() {
1338 return listConstraints;
1341 public void setListConstraints(String listConstraints) {
1342 this.listConstraints = listConstraints;
1344 public String getDataOrderInfo() {
1345 return dataOrderInfo;
1348 public void setDataOrderInfo(String dataOrderInfo) {
1349 this.dataOrderInfo = dataOrderInfo;