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) {
771 @SuppressWarnings("unchecked")
772 public LinkedHashMap<String, String> load(String fileName) throws IOException {
773 File newConfiguration = new File(fileName);
774 StringBuilder orderInfo = new StringBuilder("[");
775 Yaml yaml = new Yaml();
776 LinkedHashMap<Object, Object> yamlMap = null;
777 try(InputStream is = new FileInputStream(newConfiguration)){
778 yamlMap = (LinkedHashMap<Object, Object>) yaml.load(is);
779 } catch (FileNotFoundException e) {
783 StringBuilder sb = new StringBuilder();
784 LinkedHashMap<String, String> settings = new LinkedHashMap<>();
785 if (yamlMap == null) {
789 String message = validations(yamlMap);
792 settings.put("error", message);
798 orderedElements.stream().forEach((string) -> {
799 orderInfo.append(string);
800 orderInfo.append(",");
801 logger.info("Content: " + string);
804 orderInfo.append("]");
806 dataOrderInfo = orderInfo.toString();
807 dataOrderInfo = dataOrderInfo.replace(",]", "]");
809 logger.info("dataOrderInfo :" + dataOrderInfo);
811 List<String> path = new ArrayList <>();
812 serializeMap(settings, sb, path, yamlMap);
816 @SuppressWarnings("unchecked")
817 private String validations(@SuppressWarnings("rawtypes") LinkedHashMap yamlMap){
819 boolean isNoteTypeFound = false;
820 boolean isDataTypeFound = false;
821 boolean isToscaVersionKeyFound = false;
822 boolean isToscaVersionValueFound = false;
823 @SuppressWarnings("rawtypes")
824 Map m1 = new HashMap();
827 // Get a set of the entries
828 @SuppressWarnings("rawtypes")
829 Set set = yamlMap.entrySet();
831 @SuppressWarnings("rawtypes")
832 Iterator i = set.iterator();
835 @SuppressWarnings("rawtypes")
836 Map.Entry me = (Map.Entry)i.next();
838 if("tosca_definitions_version".equals(me.getKey())){
839 isToscaVersionKeyFound = true;
841 m1.put("tosca_definitions_version", order);
844 if("tosca_simple_yaml_1_0_0".equals(me.getValue())){
845 isToscaVersionValueFound = true;
848 if("node_types".equals(me.getKey())){
849 isNoteTypeFound = true;
851 m1.put("node_types", order);
854 if("data_types".equals(me.getKey())){
855 isDataTypeFound = true;
857 m1.put("data_types", order);
863 if(!isDataTypeFound){
864 return "data_types are missing or invalid.";
867 if(!isToscaVersionKeyFound || !isToscaVersionValueFound){
868 return "tosca_definitions_version is missing or invalid.";
871 if(!isNoteTypeFound){
872 return "node_types are missing or invalid.";
875 short version = (short) m1.get("tosca_definitions_version");
878 return "tosca_definitions_version should be defined first.";
881 short data = (short) m1.get("data_types");
882 short node = (short) m1.get("node_types");
884 return "node_types should be defined before data_types.";
892 @SuppressWarnings({ "unchecked", "rawtypes" })
893 private void serializeMap(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
894 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
896 if (entry.getValue() instanceof Map) {
897 path.add((String) entry.getKey());
898 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
899 path.remove(path.size() - 1);
900 } else if (entry.getValue() instanceof List) {
901 path.add((String) entry.getKey());
902 serializeList(settings, sb, path, (List) entry.getValue());
903 path.remove(path.size() - 1);
905 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
910 @SuppressWarnings("unchecked")
911 private void serializeList(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) {
913 for (Object listEle : yamlList) {
914 if (listEle instanceof Map) {
915 path.add(Integer.toString(counter));
916 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
917 path.remove(path.size() - 1);
918 } else if (listEle instanceof List) {
919 path.add(Integer.toString(counter));
920 serializeList(settings, sb, path, (List<String>) listEle);
921 path.remove(path.size() - 1);
923 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
929 private void serializeValue(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) {
934 for (String pathEle : path) {
935 sb.append(pathEle).append('.');
938 settings.put(sb.toString(), value.toString());
942 void parseDataAndPolicyNodes(LinkedHashMap<String,String> map){
943 for(String key:map.keySet()){
944 if(key.contains("policy.nodes.Root"))
948 else if(key.contains("policy.nodes")){
949 String wordToFind = "policy.nodes.";
950 int indexForPolicyNode=key.indexOf(wordToFind);
951 String subNodeString= key.substring(indexForPolicyNode+13, key.length());
953 stringBetweenDots(subNodeString);
955 else if(key.contains("policy.data")){
956 String wordToFind="policy.data.";
957 int indexForPolicyNode=key.indexOf(wordToFind);
958 String subNodeString= key.substring(indexForPolicyNode+12, key.length());
960 stringBetweenDotsForDataFields(subNodeString);
965 // Second index of dot should be returned.
966 public int stringBetweenDots(String str){
967 String stringToSearch=str;
968 String[]ss=stringToSearch.split("\\.");
972 uniqueKeys.add(ss[2]);
976 return uniqueKeys.size();
980 public void stringBetweenDotsForDataFields(String str){
981 String stringToSearch=str;
982 String[]ss=stringToSearch.split("\\.");
987 uniqueDataKeys.add(ss[0]+"%"+ss[2]);
992 void constructJsonForDataFields(LinkedHashMap<String,String> dataMapForJson){
993 LinkedHashMap<String,LinkedHashMap<String,String>> dataMapKey= new LinkedHashMap <>();
994 LinkedHashMap<String, String> hmSub;
995 for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
996 String uniqueDataKey= entry.getKey();
997 String[] uniqueDataKeySplit=uniqueDataKey.split("%");
998 String value= dataMapForJson.get(uniqueDataKey);
999 if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
1000 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
1001 hmSub.put(uniqueDataKeySplit[1], value);
1004 hmSub=new LinkedHashMap <>();
1005 hmSub.put(uniqueDataKeySplit[1], value);
1008 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
1011 JSONObject mainObject= new JSONObject();
1013 for(Map.Entry<String,LinkedHashMap<String,String>> entry: dataMapKey.entrySet()){
1014 String s=entry.getKey();
1015 json= new JSONObject();
1016 HashMap<String,String> jsonHm=dataMapKey.get(s);
1017 for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
1018 String key=entryMap.getKey();
1019 json.put(key, jsonHm.get(key));
1021 mainObject.put(s,json);
1023 Iterator<String> keysItr = mainObject.keys();
1024 while(keysItr.hasNext()) {
1025 String key = keysItr.next();
1026 String value = mainObject.get(key).toString();
1027 retmap.put(key, value);
1030 logger.info("#############################################################################");
1031 logger.info(mainObject);
1032 logger.info("###############################################################################");
1035 LinkedHashMap<String,String> parseDataNodes(LinkedHashMap<String,String> map){
1036 LinkedHashMap<String,String> dataMapForJson=new LinkedHashMap <>();
1037 matchableValues = new HashMap <>();
1038 for(String uniqueDataKey: uniqueDataKeys){
1039 if(uniqueDataKey.contains("%")){
1040 String[] uniqueDataKeySplit= uniqueDataKey.split("%");
1041 String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE;
1042 String typeValue=map.get(findType);
1043 logger.info(typeValue);
1045 String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED;
1046 String requiredValue= map.get(findRequired);
1048 String matchable =DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+MATCHABLE;
1050 String matchableValue= map.get(matchable);
1052 if(matchableValue != null && matchableValue.equalsIgnoreCase("true")){
1053 if(uniqueDataKey.contains("%")){
1054 String[] keys= uniqueDataKey.split("%");
1055 String key=keys[keys.length -1];
1056 matchableValues.put(key, MATCHINGTRUE);
1058 matchableValues.put(uniqueDataKey, MATCHINGTRUE);
1062 if(requiredValue == null || requiredValue.isEmpty()){
1063 requiredValue = "false";
1065 if(typeValue != null && (typeValue.equalsIgnoreCase(STRING)||
1066 typeValue.equalsIgnoreCase(INTEGER))){
1068 String findDefault=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+DEFAULT;
1069 String defaultValue= map.get(findDefault);
1070 logger.info("defaultValue is:"+ defaultValue);
1071 logger.info("requiredValue is:"+ requiredValue);
1073 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
1074 attributeIndividualStringBuilder.append(typeValue+DEFAULTVALUE);
1075 attributeIndividualStringBuilder.append(defaultValue+REQUIREDVALUE);
1076 attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
1077 dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());
1079 else if(LIST.equalsIgnoreCase(typeValue) || MAP.equalsIgnoreCase(typeValue)){
1080 logger.info("requiredValue is:"+ requiredValue);
1081 String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
1082 String listValue=map.get(findList);
1083 if(listValue!=null){
1084 logger.info("Type of list is:"+ listValue);
1086 if(listValue.contains(".")){
1087 String trimValue=listValue.substring(listValue.lastIndexOf('.')+1);
1088 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1089 referenceIndividualStringBuilder.append(trimValue+REQUIREDVALUE);
1090 referenceIndividualStringBuilder.append(requiredValue+MANYTRUE);
1091 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1094 StringBuilder stringListItems= new StringBuilder();
1095 if(LIST.equalsIgnoreCase(typeValue)){
1096 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":required-"+requiredValue +":MANY-false");
1097 }else if( MAP.equalsIgnoreCase(typeValue)){
1098 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":required-"+requiredValue +":MANY-true");
1100 dataMapForJson.put(uniqueDataKey, stringListItems.toString());
1101 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
1102 for(int i=0;i<10;i++){
1103 String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i;
1104 logger.info("findConstraints => " + findConstraints);
1105 String constraintsValue=map.get(findConstraints);
1106 logger.info("constraintsValue => " + constraintsValue);
1107 if(constraintsValue==null){
1111 logger.info("constraintsValue => " + constraintsValue);
1112 if(constraintsValue.contains("=")){
1113 constraintsValue = constraintsValue.replace("=", "equal-sign");
1115 dataConstraints.add(constraintsValue);
1116 dataListBuffer.append(constraintsValue+",");
1119 dataListBuffer.append("]#");
1120 logger.info(dataListBuffer);
1125 String findUserDefined=DATATYPE+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+TYPE;
1126 String userDefinedValue=map.get(findUserDefined);
1127 String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
1128 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1129 referenceIndividualStringBuilder.append(trimValue+REQUIREDVALUE);
1130 referenceIndividualStringBuilder.append(requiredValue+MANYFALSE);
1131 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1137 return dataMapForJson;
1141 LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String,String> map){
1142 LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= new LinkedHashMap <>();
1143 for(String uniqueKey: uniqueKeys){
1144 LinkedHashMap<String,String> hm;
1146 for(Map.Entry<String,String> entry:map.entrySet()){
1147 String key=entry.getKey();
1148 if(key.contains(uniqueKey) && key.contains("policy.nodes")){
1149 if(mapKey.containsKey(uniqueKey)){
1150 hm = mapKey.get(uniqueKey);
1151 String keyStr= key.substring(key.lastIndexOf('.')+1);
1152 String valueStr= map.get(key);
1153 if(("type").equals(keyStr)){
1154 if(!key.contains("entry_schema"))
1156 hm.put(keyStr,valueStr);
1159 hm.put(keyStr,valueStr);
1163 hm = new LinkedHashMap <>();
1164 String keyStr= key.substring(key.lastIndexOf('.')+1);
1165 String valueStr= map.get(key);
1166 if(("type").equals(keyStr)){
1167 if(!key.contains("entry_schema"))
1169 hm.put(keyStr,valueStr);
1172 hm.put(keyStr,valueStr);
1174 mapKey.put(uniqueKey, hm);
1182 void createAttributes(LinkedHashMap<String,LinkedHashMap<String,String>> mapKey){
1183 StringBuilder attributeStringBuilder= new StringBuilder();
1184 StringBuilder referenceStringBuilder= new StringBuilder();
1185 StringBuilder listBuffer= new StringBuilder();
1186 List<String> constraints= new ArrayList<>();
1187 for(Map.Entry<String,LinkedHashMap<String,String>> entry: mapKey.entrySet()){
1188 String keySetString= entry.getKey();
1189 LinkedHashMap<String,String> keyValues=mapKey.get(keySetString);
1190 if(STRING.equalsIgnoreCase(keyValues.get("type"))||
1191 INTEGER.equalsIgnoreCase(keyValues.get("type"))){
1192 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
1193 attributeIndividualStringBuilder.append(keySetString+"=");
1194 attributeIndividualStringBuilder.append(keyValues.get("type")+DEFAULTVALUE);
1195 attributeIndividualStringBuilder.append(keyValues.get("default")+REQUIREDVALUE);
1196 attributeIndividualStringBuilder.append(keyValues.get("required")+MANYFALSE);
1197 attributeStringBuilder.append(attributeIndividualStringBuilder+",");
1198 if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1199 matchableValues.put(keySetString, MATCHINGTRUE);
1202 else if(LIST.equalsIgnoreCase(keyValues.get("type"))){
1204 if(("true").equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1205 matchableValues.put(keySetString, MATCHINGTRUE);
1208 Set<String> keys= keyValues.keySet();
1209 Iterator<String> itr=keys.iterator();
1210 boolean isDefinedType = false;
1211 while(itr.hasNext()){
1212 String key= itr.next();
1213 if((!("type").equals(key) ||("required").equals(key)))
1215 String value= keyValues.get(key);
1216 //The "." in the value determines if its a string or a user defined type.
1217 if (!value.contains(".")){
1219 if(StringUtils.isNumeric(key) ){ //only integer key for the value of Constrains
1220 constraints.add(keyValues.get(key));
1223 //This is user defined type
1224 String trimValue=value.substring(value.lastIndexOf('.')+1);
1225 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1226 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYTRUE);
1227 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1228 isDefinedType = true;
1234 if(!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))){ //type is not user defined and is a list but no constraints defined.
1235 if(constraints == null || constraints.isEmpty()){
1236 referenceStringBuilder.append(keySetString+"=MANY-true"+",");
1240 //User defined Datatype.
1241 if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1242 matchableValues.put(keySetString, MATCHINGTRUE);
1244 String value=keyValues.get("type");
1245 if(value != null && !value.isEmpty()){
1246 String trimValue=value.substring(value.lastIndexOf('.')+1);
1247 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1248 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYFALSE);
1249 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1251 logger.info("keyValues.get(type) is null/empty");
1255 if(constraints!=null && !constraints.isEmpty()){
1257 listBuffer.append(keySetString.toUpperCase()+"=[");
1258 for(String str:constraints){
1259 listBuffer.append(str+",");
1261 listBuffer.append("]#");
1262 logger.info(listBuffer);
1265 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1266 referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+MANYFALSE);
1267 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1268 constraints.clear();
1272 dataListBuffer.append(listBuffer);
1275 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1276 logger.info("Whole attribute String is:"+attributeStringBuilder);
1277 logger.info("Whole reference String is:"+referenceStringBuilder);
1278 logger.info("List String is:"+listBuffer);
1279 logger.info("Data list buffer is:"+dataListBuffer);
1280 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1282 this.listConstraints=dataListBuffer.toString();
1283 this.referenceAttributes=referenceStringBuilder.toString();
1284 this.attributeString=attributeStringBuilder.toString();
1287 @SuppressWarnings("unchecked")
1288 public void findNode(LinkedHashMap<Object, Object> map) {
1290 map.forEach((key,value) -> {
1291 // if the value is properties and its type is map object, then save all the keys
1292 if(key.equals("properties") && value instanceof Map){
1293 saveNodes((LinkedHashMap<?, ?>)value);
1296 if(!key.equals("policy.nodes.Root") && value instanceof Map){
1297 //value is a Map object, then make a recursive call
1298 findNode((LinkedHashMap<Object, Object>) value);
1304 public void saveNodes(LinkedHashMap<?, ?> map) {
1306 map.forEach((key,value) -> {
1308 orderedElements.add((String)key);
1314 public String getAttributeString() {
1315 return attributeString;
1317 public void setAttributeString(String attributeString) {
1318 this.attributeString = attributeString;
1321 public LinkedHashMap<String, Object> getRetmap() {
1325 public void setRetmap(LinkedHashMap<String, Object> retmap) {
1326 this.retmap = retmap;
1328 public Map<String, String> getMatchableValues() {
1329 return matchableValues;
1332 public void setMatchableValues(Map<String, String> matchableValues) {
1333 this.matchableValues = matchableValues;
1335 public String getReferenceAttributes() {
1336 return referenceAttributes;
1339 public void setReferenceAttributes(String referenceAttributes) {
1340 this.referenceAttributes = referenceAttributes;
1342 public String getListConstraints() {
1343 return listConstraints;
1346 public void setListConstraints(String listConstraints) {
1347 this.listConstraints = listConstraints;
1349 public String getDataOrderInfo() {
1350 return dataOrderInfo;
1353 public void setDataOrderInfo(String dataOrderInfo) {
1354 this.dataOrderInfo = dataOrderInfo;