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;
39 import java.util.Map.Entry;
41 import org.apache.commons.lang.StringUtils;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.eclipse.emf.common.util.EList;
45 import org.eclipse.emf.common.util.EMap;
46 import org.eclipse.emf.common.util.Enumerator;
47 import org.eclipse.emf.common.util.TreeIterator;
48 import org.eclipse.emf.common.util.URI;
49 import org.eclipse.emf.ecore.EAnnotation;
50 import org.eclipse.emf.ecore.EClass;
51 import org.eclipse.emf.ecore.EClassifier;
52 import org.eclipse.emf.ecore.EEnum;
53 import org.eclipse.emf.ecore.EEnumLiteral;
54 import org.eclipse.emf.ecore.EObject;
55 import org.eclipse.emf.ecore.EPackage;
56 import org.eclipse.emf.ecore.EReference;
57 import org.eclipse.emf.ecore.EStructuralFeature;
58 import org.eclipse.emf.ecore.impl.EAttributeImpl;
59 import org.eclipse.emf.ecore.impl.EEnumImpl;
60 import org.eclipse.emf.ecore.resource.Resource;
61 import org.eclipse.emf.ecore.resource.ResourceSet;
62 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
63 import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
64 import org.json.JSONObject;
65 import org.onap.policy.rest.XACMLRestProperties;
66 import org.yaml.snakeyaml.Yaml;
68 import com.att.research.xacml.util.XACMLProperties;
69 import com.google.gson.Gson;
72 public class MSModelUtils {
74 private static final Log logger = LogFactory.getLog(MSModelUtils.class);
76 private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
77 private HashMap<String, String> enumMap = new HashMap<>();
78 private HashMap<String, String> matchingClass = new HashMap<>();
79 private String configuration = "configuration";
80 private String dictionary = "dictionary";
81 private String onap = "";
82 private String policy = "";
83 private String eProxyURI = "eProxyURI:";
84 private List<String> orderedElements = new ArrayList<>();
85 private String dataOrderInfo = null;
86 private Set<String> uniqueDataKeys= new HashSet<>();
87 private Set<String> uniqueKeys= new HashSet<>();
88 private String listConstraints = null;
89 private String referenceAttributes;
90 private LinkedHashMap<String, Object> retmap = new LinkedHashMap<>();
91 private Map<String, String> matchableValues;
92 public static final String PROPERTIES=".properties.";
93 public static final String DATATYPE = "data_types.policy.data.";
94 public static final String TYPE=".type";
95 public static final String REQUIRED=".required";
96 public static final String MATCHABLE=".matchable";
97 public static final String STRING="string";
98 public static final String INTEGER="integer";
99 public static final String LIST="list";
100 public static final String MAP="map";
101 public static final String DEFAULT=".default";
102 public static final String MANYFALSE=":MANY-false";
103 public static final String MANYTRUE=":MANY-true";
104 public static final String DEFAULTVALUE=":defaultValue-";
105 public static final String REQUIREDVALUE=":required-";
106 public static final String MATCHABLEKEY="matchable";
107 public static final String REQUIREDFALSE=":required-false";
108 public static final String REQUIREDTRUE=":required-true";
109 public static final String MATCHINGTRUE="matching-true";
111 private StringBuilder dataListBuffer=new StringBuilder();
112 private List<String> dataConstraints= new ArrayList <>();
113 private String attributeString = null;
115 public MSModelUtils(){
118 public MSModelUtils(String onap, String policy){
120 this.policy = policy;
123 private enum ANNOTATION_TYPE{
124 MATCHING, VALIDATION, DICTIONARY
127 public enum MODEL_TYPE {
132 public Map<String, MSAttributeObject> processEpackage(String file, MODEL_TYPE model){
133 if (model == MODEL_TYPE.XMI ){
134 processXMIEpackage(file);
140 private void processXMIEpackage(String xmiFile){
141 EPackage root = getEpackage(xmiFile);
142 TreeIterator<EObject> treeItr = root.eAllContents();
146 // Pulling out dependency from file
147 while (treeItr.hasNext()) {
148 EObject obj = treeItr.next();
149 if (obj instanceof EClassifier) {
150 EClassifier eClassifier = (EClassifier) obj;
151 className = eClassifier.getName();
153 if (obj instanceof EEnum) {
154 enumMap.putAll(getEEnum(obj));
155 }else if (obj instanceof EClass) {
156 String temp = getDependencyList(eClassifier).toString();
157 returnValue = StringUtils.replaceEach(temp, new String[]{"[", "]"}, new String[]{"", ""});
158 getAttributes(className, returnValue, root);
163 if (!enumMap.isEmpty()){
166 if (!matchingClass.isEmpty()){
167 CheckForMatchingClass();
171 private void CheckForMatchingClass() {
172 HashMap<String, String> tempAttribute = new HashMap<>();
174 for (Entry<String, String> set : matchingClass.entrySet()){
175 String key = set.getKey();
176 if (classMap.containsKey(key)){
177 Map<String, String> listAttributes = classMap.get(key).getAttribute();
178 Map<String, String> listRef = classMap.get(key).getRefAttribute();
179 for ( Entry<String, String> eSet : listAttributes.entrySet()){
180 String key2 = eSet.getKey();
181 tempAttribute.put(key2, MATCHINGTRUE);
183 for ( Entry<String, String> eSet : listRef.entrySet()){
184 String key3 = eSet.getKey();
185 tempAttribute.put(key3, MATCHINGTRUE);
189 UpdateMatching(tempAttribute, key);
196 private void UpdateMatching(HashMap<String, String> tempAttribute, String key) {
197 Map<String, MSAttributeObject> newClass = classMap;
199 for (Entry<String, MSAttributeObject> updateClass : newClass.entrySet()){
200 Map<String, String> valueMap = updateClass.getValue().getMatchingSet();
201 String keymap = updateClass.getKey();
202 if (valueMap.containsKey(key)){
203 Map<String, String> modifyMap = classMap.get(keymap).getMatchingSet();
204 modifyMap.remove(key);
205 modifyMap.putAll(tempAttribute);
206 classMap.get(keymap).setMatchingSet(modifyMap);
212 private void addEnumClassMap() {
213 for (Entry<String, MSAttributeObject> value :classMap.entrySet()){
214 value.getValue().setEnumType(enumMap);
218 private EPackage getEpackage(String xmiFile) {
219 ResourceSet resSet = new ResourceSetImpl();
220 Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
221 Map<String, Object> m = reg.getExtensionToFactoryMap();
222 m.put("xmi", new XMIResourceFactoryImpl());
223 Resource resource = resSet.getResource(URI.createFileURI(xmiFile), true);
225 resource.load(Collections.emptyMap());
226 } catch (IOException e) {
227 logger.error("Error loading Encore Resource for new Model" + e);
230 return (EPackage) resource.getContents().get(0);
233 private HashMap<String, String> getEEnum(EObject obj) {
234 List<String> valueList = new ArrayList<>();
235 HashMap<String, String> returnMap = new HashMap<>();
236 EEnum eenum = (EEnum)obj;
238 String name = eenum.getName();
239 for (EEnumLiteral eEnumLiteral : eenum.getELiterals())
241 Enumerator instance = eEnumLiteral.getInstance();
242 String value = instance.getLiteral();
243 valueList.add(value);
245 returnMap.put(name, valueList.toString());
249 public void getAttributes(String className, String dependency, EPackage root) {
250 List<String> dpendList = new ArrayList<>();
251 if (dependency!=null){
252 dpendList = new ArrayList<>(Arrays.asList(dependency.split(",")));
254 MSAttributeObject msAttributeObject = new MSAttributeObject();
255 msAttributeObject.setClassName(className);
256 String extendClass = getSubTypes(root, className);
257 Map<String, String> returnRefList = getRefAttributeList(root, className, extendClass);
258 Map<String, String> returnAttributeList = getAttributeList(root, className, extendClass);
259 Map<String, Object> returnSubList = getSubAttributeList(root, className, extendClass);
260 HashMap<String, String> returnAnnotation = getAnnotation(root, className, extendClass);
261 msAttributeObject.setAttribute(returnAttributeList);
262 msAttributeObject.setRefAttribute(returnRefList);
263 msAttributeObject.setSubClass(returnSubList);
264 msAttributeObject.setDependency(dpendList.toString());
265 msAttributeObject.addMatchingSet(returnAnnotation);
266 msAttributeObject.setPolicyTempalate(isPolicyTemplate(root, className));
268 this.classMap.put(className, msAttributeObject);
271 private HashMap<String, String> getAnnotation(EPackage root, String className, String extendClass) {
272 TreeIterator<EObject> treeItr = root.eAllContents();
273 boolean requiredAttribute = false;
274 boolean requiredMatchAttribute = false;
275 HashMap<String, String> annotationSet = new HashMap<>();
278 String annotationDict;
280 // Pulling out dependency from file
281 while (treeItr.hasNext()) {
282 EObject obj = treeItr.next();
283 if (obj instanceof EClassifier) {
284 requiredAttribute = isRequiredAttribute(obj, className );
285 requiredMatchAttribute = isRequiredAttribute(obj, extendClass );
288 if (requiredAttribute){
289 if (obj instanceof EStructuralFeature) {
290 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
291 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
292 matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
294 annotationSet.put(eStrucClassifier.getName(), matching);
296 range = annotationValue(eStrucClassifier, ANNOTATION_TYPE.VALIDATION, policy);
298 annotationSet.put(eStrucClassifier.getName(), range);
300 annotationDict = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
301 if (annotationDict!=null){
302 annotationSet.put(eStrucClassifier.getName(), annotationDict);
306 } else if (requiredMatchAttribute && (obj instanceof EStructuralFeature)) {
307 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
308 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
309 matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy);
311 if (obj instanceof EReference){
312 EClass refType = ((EReference) obj).getEReferenceType();
313 annotationSet.put(refType.getName(), matching);
314 matchingClass.put(refType.getName(), matching);
316 annotationSet.put(eStrucClassifier.getName(), matching);
322 return annotationSet;
325 private Map<String, Object> getSubAttributeList(EPackage root, String className , String superClass) {
326 TreeIterator<EObject> treeItr = root.eAllContents();
327 boolean requiredAttribute = false;
328 Map<String, Object> subAttribute = new HashMap<>();
329 int rollingCount = 0;
330 int processClass = 0;
333 // Pulling out dependency from file
334 while (treeItr.hasNext() && rollingCount < 2) {
336 EObject obj = treeItr.next();
337 if (obj instanceof EClassifier) {
338 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
339 requiredAttribute = true;
341 requiredAttribute = false;
343 if (requiredAttribute){
346 rollingCount = rollingCount+processClass;
349 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
350 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
351 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
352 annotation = annotationTest(eStrucClassifier, configuration, onap);
353 if (annotation && obj instanceof EReference) {
354 EClass refType = ((EReference) obj).getEReferenceType();
355 if(!refType.toString().contains(eProxyURI)){
356 String required = REQUIREDFALSE;
357 if(eStrucClassifier.getLowerBound() == 1){
358 required = REQUIREDTRUE;
360 subAttribute.put(eStrucClassifier.getName(), refType.getName() + required);
369 public String checkDefultValue(String defultValue) {
370 if (defultValue!=null){
371 return DEFAULTVALUE+ defultValue;
373 return ":defaultValue-NA";
377 public String checkRequiredPattern(int upper, int lower) {
379 String pattern = XACMLProperties.getProperty(XACMLRestProperties.PROP_XCORE_REQUIRED_PATTERN);
382 if (upper == Integer.parseInt(pattern.split(",")[1]) && lower==Integer.parseInt(pattern.split(",")[0])){
387 return REQUIREDFALSE;
390 public JSONObject buildJavaObject(Map<String, String> map){
392 return new JSONObject(map);
395 public Map<String, String> getRefAttributeList(EPackage root, String className, String superClass){
397 TreeIterator<EObject> treeItr = root.eAllContents();
398 boolean requiredAttribute = false;
399 HashMap<String, String> refAttribute = new HashMap<>();
400 int rollingCount = 0;
401 int processClass = 0;
403 // Pulling out dependency from file
404 while (treeItr.hasNext()) {
405 EObject obj = treeItr.next();
406 if (obj instanceof EClassifier) {
407 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
408 requiredAttribute = true;
410 requiredAttribute = false;
412 if (requiredAttribute){
415 rollingCount = rollingCount+processClass;
418 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
419 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
420 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
421 annotation = annotationTest(eStrucClassifier, configuration, onap);
422 if ( annotation && obj instanceof EReference) {
423 EClass refType = ((EReference) obj).getEReferenceType();
424 if(refType.toString().contains(eProxyURI)){
425 String one = refType.toString().split(eProxyURI)[1];
426 String refValue = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
427 refAttribute.put(eStrucClassifier.getName(), refValue);
429 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
430 String required = REQUIREDFALSE;
431 if(((EStructuralFeature) obj).getLowerBound() == 1){
432 required = REQUIREDTRUE;
434 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
436 } else if (annotation && obj instanceof EAttributeImpl){
437 EClassifier refType = ((EAttributeImpl) obj).getEType();
438 if (refType instanceof EEnumImpl){
439 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
440 String required = REQUIREDFALSE;
441 if(((EStructuralFeature) obj).getLowerBound() == 1){
442 required = REQUIREDTRUE;
444 refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required);
454 private boolean annotationTest(EStructuralFeature eStrucClassifier, String annotation, String type) {
455 String annotationType;
456 EAnnotation eAnnotation;
460 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
462 for (int i = 0; i < value.size(); i++){
463 annotationType = value.get(i).getSource();
464 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
465 onapType = eAnnotation.getDetails().get(0).getValue();
466 onapValue = eAnnotation.getDetails().get(0).getKey();
468 if (annotationType.contains(type) && onapType.contains(annotation)){
472 if (annotationType.contains(type) && onapValue.contains(annotation)){
481 private String annotationValue(EStructuralFeature eStrucClassifier, ANNOTATION_TYPE annotation, String type) {
482 String annotationType;
483 EAnnotation eAnnotation;
485 String onapValue = null;
487 EList<EAnnotation> value = eStrucClassifier.getEAnnotations();
489 for (int i = 0; i < value.size(); i++){
490 annotationType = value.get(i).getSource();
491 eAnnotation = eStrucClassifier.getEAnnotations().get(i);
492 onapType = eAnnotation.getDetails().get(0).getKey();
493 if (annotationType.contains(type) && onapType.compareToIgnoreCase(annotation.toString())==0){
494 onapValue = eAnnotation.getDetails().get(0).getValue();
495 if (annotation == ANNOTATION_TYPE.VALIDATION){
498 return onapType + "-" + onapValue;
505 public boolean isRequiredAttribute(EObject obj, String className){
506 EClassifier eClassifier = (EClassifier) obj;
507 String workingClass = eClassifier.getName().trim();
508 if (workingClass.equalsIgnoreCase(className)){
515 private boolean isPolicyTemplate(EPackage root, String className){
517 for (EClassifier classifier : root.getEClassifiers()){
518 if (classifier instanceof EClass) {
519 EClass eClass = (EClass)classifier;
520 if (eClass.getName().contentEquals(className)){
521 EList<EAnnotation> value = eClass.getEAnnotations();
522 for (EAnnotation workingValue : value){
523 EMap<String, String> keyMap = workingValue.getDetails();
524 if (keyMap.containsKey("policyTemplate")){
533 private String getSubTypes(EPackage root, String className) {
534 String returnSubTypes = null;
535 for (EClassifier classifier : root.getEClassifiers()){
536 if (classifier instanceof EClass) {
537 EClass eClass = (EClass)classifier;
539 for (EClass eSuperType : eClass.getEAllSuperTypes())
541 if (eClass.getName().contentEquals(className)){
542 returnSubTypes = eSuperType.getName();
547 return returnSubTypes;
550 public Map<String, String> getAttributeList(EPackage root, String className, String superClass){
552 TreeIterator<EObject> treeItr = root.eAllContents();
553 boolean requiredAttribute = false;
554 HashMap<String, String> refAttribute = new HashMap<>();
556 boolean dictionaryTest;
560 // Pulling out dependency from file
561 while (treeItr.hasNext()) {
562 EObject obj = treeItr.next();
563 if (obj instanceof EClassifier) {
564 if (isRequiredAttribute(obj, className ) || isRequiredAttribute(obj, superClass )){
565 requiredAttribute = true;
567 requiredAttribute = false;
572 if (requiredAttribute && (obj instanceof EStructuralFeature)) {
573 EStructuralFeature eStrucClassifier = (EStructuralFeature) obj;
574 if (!eStrucClassifier.getEAnnotations().isEmpty()) {
575 annotation = annotationTest(eStrucClassifier, configuration, onap);
576 dictionaryTest = annotationTest(eStrucClassifier, dictionary, policy);
577 EClassifier refType = ((EStructuralFeature) obj).getEType();
578 if (annotation && !(obj instanceof EReference) && !(refType instanceof EEnumImpl)) {
579 String name = eStrucClassifier.getName();
581 eType = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy);
583 eType = eStrucClassifier.getEType().getInstanceClassName();
585 defaultValue = checkDefultValue(((EStructuralFeature) obj).getDefaultValueLiteral());
587 String array = arrayCheck(((EStructuralFeature) obj).getUpperBound());
588 String required = checkRequiredPattern(((EStructuralFeature) obj).getUpperBound(), ((EStructuralFeature) obj).getLowerBound());
589 String attributeValue = eType + defaultValue + required + array;
590 refAttribute.put(name, attributeValue);
599 public String arrayCheck(int upperBound) {
601 if (upperBound == -1){
608 public List<String> getDependencyList(EClassifier eClassifier){
609 List<String> returnValue = new ArrayList<>();;
610 EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes();
611 if (somelist.isEmpty()){
614 for(EClass depend: somelist){
615 if (depend.toString().contains(eProxyURI)){
616 String one = depend.toString().split(eProxyURI)[1];
617 String value = StringUtils.replaceEach(one.split("#")[1], new String[]{"//", ")"}, new String[]{"", ""});
618 returnValue.add(value);
625 public Map<String, String> buildSubList(Map<String, String> subClassAttributes, Map<String, MSAttributeObject> classMap, String className){
626 Map<String, String> missingValues = new HashMap<>();
627 Map<String, String> workingMap;
630 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
631 String value = map.getValue().split(":")[0];
633 classMap.get(className).getEnumType();
634 enumType = classMap.get(className).getEnumType().containsKey(value);
636 workingMap = classMap.get(value).getRefAttribute();
637 for ( Entry<String, String> subMab : workingMap.entrySet()){
638 String value2 = subMab.getValue().split(":")[0];
639 if (!subClassAttributes.containsValue(value2)){
640 missingValues.put(subMab.getKey(), subMab.getValue());
648 return missingValues;
651 public Map<String, Map<String, String>> recursiveReference(Map<String, MSAttributeObject> classMap, String className){
653 Map<String, Map<String, String>> returnObject = new HashMap<>();
654 Map<String, String> returnClass = getRefclass(classMap, className);
655 returnObject.put(className, returnClass);
656 for (Entry<String, String> reAttribute :returnClass.entrySet()){
657 if (reAttribute.getValue().split(":")[1].contains("MANY") &&
658 classMap.get(reAttribute.getValue().split(":")[0]) != null){
659 returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0]));
668 public String createJson(Map<String, MSAttributeObject> classMap, String className) {
670 Map<String, Map<String, String>> myObject = new HashMap<>();
671 for ( Entry<String, String> map : classMap.get(className).getRefAttribute().entrySet()){
672 String value = map.getValue().split(":")[0];
674 enumType = classMap.get(className).getEnumType().containsKey(value);
675 if (!enumType && map.getValue().split(":")[1].contains("MANY")){
676 Map<String, Map<String, String>> testRecursive = recursiveReference(classMap, map.getValue().split(":")[0] );
677 myObject.putAll(testRecursive);
682 Gson gson = new Gson();
683 return gson.toJson(myObject);
686 public Map<String, String> getRefclass(Map<String, MSAttributeObject> classMap, String className){
687 HashMap<String, String> missingValues = new HashMap<>();
689 if (classMap.get(className).getAttribute()!=null || !classMap.get(className).getAttribute().isEmpty()){
690 missingValues.putAll(classMap.get(className).getAttribute());
693 if (classMap.get(className).getRefAttribute()!=null || !classMap.get(className).getRefAttribute().isEmpty()){
694 missingValues.putAll(classMap.get(className).getRefAttribute());
697 return missingValues;
700 public String createSubAttributes(List<String> dependency, Map<String, MSAttributeObject> classMap, String modelName) {
702 HashMap <String, Object> workingMap = new HashMap<>();
703 MSAttributeObject tempObject;
704 if (dependency!=null){
705 if (dependency.isEmpty()){
708 dependency.add(modelName);
709 for (String element: dependency){
710 tempObject = classMap.get(element);
711 if (tempObject!=null){
712 workingMap.putAll(classMap.get(element).getSubClass());
717 return createJson(classMap, modelName);
720 public List<String> getFullDependencyList(List<String> dependency, Map<String,MSAttributeObject > classMap) {
721 ArrayList<String> returnList = new ArrayList<>();
722 ArrayList<String> workingList;
723 returnList.addAll(dependency);
724 for (String element : dependency ){
725 if (classMap.containsKey(element)){
726 MSAttributeObject value = classMap.get(element);
727 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
728 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
729 for(String depend : workingList){
730 if (!returnList.contains(depend) && !depend.isEmpty()){
731 returnList.add(depend.trim());
743 public String parseTosca (String fileName){
744 LinkedHashMap<String,String> map= new LinkedHashMap<>();
750 if(map.get("error") != null){
751 return map.get("error");
755 parseDataAndPolicyNodes(map);
757 LinkedHashMap<String,String> dataMapForJson=parseDataNodes(map);
759 constructJsonForDataFields(dataMapForJson);
761 LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= parsePolicyNodes(map);
763 createAttributes(mapKey);
765 } catch (IOException e) {
767 }catch(ParserException e){
769 return e.getMessage();
775 @SuppressWarnings("unchecked")
776 public LinkedHashMap<String, String> load(String fileName) throws IOException,ParserException {
777 File newConfiguration = new File(fileName);
778 StringBuilder orderInfo = new StringBuilder("[");
779 Yaml yaml = new Yaml();
780 LinkedHashMap<Object, Object> yamlMap = null;
781 try(InputStream is = new FileInputStream(newConfiguration)){
782 yamlMap = (LinkedHashMap<Object, Object>) yaml.load(is);
783 } catch (FileNotFoundException e) {
786 throw new ParserException("Invalid TOSCA Model format. Please make sure it is a valid YAML file");
789 StringBuilder sb = new StringBuilder();
790 LinkedHashMap<String, String> settings = new LinkedHashMap<>();
791 if (yamlMap == null) {
795 String message = validations(yamlMap);
798 settings.put("error", message);
804 orderedElements.stream().forEach((string) -> {
805 orderInfo.append(string);
806 orderInfo.append(",");
807 logger.info("Content: " + string);
810 orderInfo.append("]");
812 dataOrderInfo = orderInfo.toString();
813 dataOrderInfo = dataOrderInfo.replace(",]", "]");
815 logger.info("dataOrderInfo :" + dataOrderInfo);
817 List<String> path = new ArrayList <>();
818 serializeMap(settings, sb, path, yamlMap);
822 @SuppressWarnings("unchecked")
823 private String validations(@SuppressWarnings("rawtypes") LinkedHashMap yamlMap){
825 boolean isNoteTypeFound = false;
826 boolean isDataTypeFound = false;
827 boolean isToscaVersionKeyFound = false;
828 boolean isToscaVersionValueFound = false;
829 @SuppressWarnings("rawtypes")
830 Map m1 = new HashMap();
833 // Get a set of the entries
834 @SuppressWarnings("rawtypes")
835 Set set = yamlMap.entrySet();
837 @SuppressWarnings("rawtypes")
838 Iterator i = set.iterator();
841 @SuppressWarnings("rawtypes")
842 Map.Entry me = (Map.Entry)i.next();
844 if("tosca_definitions_version".equals(me.getKey())){
845 isToscaVersionKeyFound = true;
847 m1.put("tosca_definitions_version", order);
850 if("tosca_simple_yaml_1_0_0".equals(me.getValue())){
851 isToscaVersionValueFound = true;
854 if("node_types".equals(me.getKey())){
855 isNoteTypeFound = true;
857 m1.put("node_types", order);
860 if("data_types".equals(me.getKey())){
861 isDataTypeFound = true;
863 m1.put("data_types", order);
869 if(!isDataTypeFound){
870 return "data_types are missing or invalid.";
873 if(!isToscaVersionKeyFound || !isToscaVersionValueFound){
874 return "tosca_definitions_version is missing or invalid.";
877 if(!isNoteTypeFound){
878 return "node_types are missing or invalid.";
881 short version = (short) m1.get("tosca_definitions_version");
884 return "tosca_definitions_version should be defined first.";
887 short data = (short) m1.get("data_types");
888 short node = (short) m1.get("node_types");
890 return "node_types should be defined before data_types.";
898 @SuppressWarnings({ "unchecked", "rawtypes" })
899 private void serializeMap(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
900 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
902 if (entry.getValue() instanceof Map) {
903 path.add((String) entry.getKey());
904 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue());
905 path.remove(path.size() - 1);
906 } else if (entry.getValue() instanceof List) {
907 path.add((String) entry.getKey());
908 serializeList(settings, sb, path, (List) entry.getValue());
909 path.remove(path.size() - 1);
911 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue());
916 @SuppressWarnings("unchecked")
917 private void serializeList(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) {
919 for (Object listEle : yamlList) {
920 if (listEle instanceof Map) {
921 path.add(Integer.toString(counter));
922 serializeMap(settings, sb, path, (Map<Object, Object>) listEle);
923 path.remove(path.size() - 1);
924 } else if (listEle instanceof List) {
925 path.add(Integer.toString(counter));
926 serializeList(settings, sb, path, (List<String>) listEle);
927 path.remove(path.size() - 1);
929 serializeValue(settings, sb, path, Integer.toString(counter), listEle);
935 private void serializeValue(LinkedHashMap<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) {
940 for (String pathEle : path) {
941 sb.append(pathEle).append('.');
944 settings.put(sb.toString(), value.toString());
948 void parseDataAndPolicyNodes(LinkedHashMap<String,String> map){
949 for(String key:map.keySet()){
950 if(key.contains("policy.nodes.Root"))
954 else if(key.contains("policy.nodes")){
955 String wordToFind = "policy.nodes.";
956 int indexForPolicyNode=key.indexOf(wordToFind);
957 String subNodeString= key.substring(indexForPolicyNode+13, key.length());
959 stringBetweenDots(subNodeString);
961 else if(key.contains("policy.data")){
962 String wordToFind="policy.data.";
963 int indexForPolicyNode=key.indexOf(wordToFind);
964 String subNodeString= key.substring(indexForPolicyNode+12, key.length());
966 stringBetweenDotsForDataFields(subNodeString);
971 // Second index of dot should be returned.
972 public int stringBetweenDots(String str){
973 String stringToSearch=str;
974 String[]ss=stringToSearch.split("\\.");
978 uniqueKeys.add(ss[2]);
982 return uniqueKeys.size();
986 public void stringBetweenDotsForDataFields(String str){
987 String stringToSearch=str;
988 String[]ss=stringToSearch.split("\\.");
993 uniqueDataKeys.add(ss[0]+"%"+ss[2]);
998 void constructJsonForDataFields(LinkedHashMap<String,String> dataMapForJson){
999 LinkedHashMap<String,LinkedHashMap<String,String>> dataMapKey= new LinkedHashMap <>();
1000 LinkedHashMap<String, String> hmSub;
1001 for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
1002 String uniqueDataKey= entry.getKey();
1003 String[] uniqueDataKeySplit=uniqueDataKey.split("%");
1004 String value= dataMapForJson.get(uniqueDataKey);
1005 if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
1006 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
1007 hmSub.put(uniqueDataKeySplit[1], value);
1010 hmSub=new LinkedHashMap <>();
1011 hmSub.put(uniqueDataKeySplit[1], value);
1014 dataMapKey.put(uniqueDataKeySplit[0], hmSub);
1017 JSONObject mainObject= new JSONObject();
1019 for(Map.Entry<String,LinkedHashMap<String,String>> entry: dataMapKey.entrySet()){
1020 String s=entry.getKey();
1021 json= new JSONObject();
1022 HashMap<String,String> jsonHm=dataMapKey.get(s);
1023 for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
1024 String key=entryMap.getKey();
1025 json.put(key, jsonHm.get(key));
1027 mainObject.put(s,json);
1029 Iterator<String> keysItr = mainObject.keys();
1030 while(keysItr.hasNext()) {
1031 String key = keysItr.next();
1032 String value = mainObject.get(key).toString();
1033 retmap.put(key, value);
1036 logger.info("#############################################################################");
1037 logger.info(mainObject);
1038 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()+REQUIREDVALUE+requiredValue +MANYFALSE);
1102 }else if( MAP.equalsIgnoreCase(typeValue)){
1103 stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+REQUIREDVALUE+requiredValue +MANYTRUE);
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);
1113 if((constraintsValue==null || constraintsValue.isEmpty()) && i==0){ //if no constraints at all ( index i as 0 can tell this )
1114 isConstraintsFound = false;
1115 //if type is list but no constraints
1116 String newValue = dataMapForJson.get(uniqueDataKey).replace("MANY-false", "MANY-true");
1117 newValue = newValue.replace(uniqueDataKeySplit[1].toUpperCase()+":", "");
1118 dataMapForJson.put(uniqueDataKey, newValue);
1121 isConstraintsFound = true;
1122 if(i == 0){ // only need to add one time for the same attribute
1123 dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
1126 if(constraintsValue==null){
1127 constraintsValue = "";
1128 }else if (constraintsValue.contains("=")) {
1129 constraintsValue = constraintsValue.replace("=", "equal-sign");
1132 dataConstraints.add(constraintsValue);
1133 dataListBuffer.append(constraintsValue+",");
1136 if(isConstraintsFound){
1137 dataListBuffer.append("]#");
1141 logger.info("entry_schema.type is not defined correctly");
1145 String findUserDefined=DATATYPE+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+TYPE;
1146 String userDefinedValue=map.get(findUserDefined);
1147 String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
1148 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1149 referenceIndividualStringBuilder.append(trimValue+REQUIREDVALUE);
1150 referenceIndividualStringBuilder.append(requiredValue+MANYFALSE);
1151 dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
1157 return dataMapForJson;
1161 LinkedHashMap<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String,String> map) throws ParserException{
1162 LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= new LinkedHashMap <>();
1163 for(String uniqueKey: uniqueKeys){
1164 LinkedHashMap<String,String> hm;
1166 for(Map.Entry<String,String> entry:map.entrySet()){
1167 String key=entry.getKey();
1168 if(key.contains(uniqueKey) && key.contains("policy.nodes")){
1169 if(mapKey.containsKey(uniqueKey)){
1170 hm = mapKey.get(uniqueKey);
1171 String keyStr= key.substring(key.lastIndexOf('.')+1);
1172 String valueStr= map.get(key);
1173 if("type".equalsIgnoreCase(keyStr) && ((key.contains("entry_schema.0.type") || key.contains("entry_schema.type") && valueStr.contains("policy.data.")))){
1174 throw new ParserException("For using user defined object type, Please make sure no space between 'type:' and object " + valueStr );
1177 if("type".equals(keyStr)){
1178 if(!key.contains("entry_schema"))
1180 hm.put(keyStr,valueStr);
1183 hm.put(keyStr,valueStr);
1187 hm = new LinkedHashMap <>();
1188 String keyStr= key.substring(key.lastIndexOf('.')+1);
1189 String valueStr= map.get(key);
1190 if(key.contains(".objective.")){
1191 throw new ParserException("Attribute objective is a key word. Please use a different name");
1193 if(("type").equals(keyStr)){
1194 if(!key.contains("entry_schema"))
1196 hm.put(keyStr,valueStr);
1199 hm.put(keyStr,valueStr);
1201 mapKey.put(uniqueKey, hm);
1209 void createAttributes(LinkedHashMap<String,LinkedHashMap<String,String>> mapKey){
1210 StringBuilder attributeStringBuilder= new StringBuilder();
1211 StringBuilder referenceStringBuilder= new StringBuilder();
1212 StringBuilder listBuffer= new StringBuilder();
1213 List<String> constraints= new ArrayList<>();
1214 for(Map.Entry<String,LinkedHashMap<String,String>> entry: mapKey.entrySet()){
1215 String keySetString= entry.getKey();
1216 LinkedHashMap<String,String> keyValues=mapKey.get(keySetString);
1217 if(STRING.equalsIgnoreCase(keyValues.get("type"))||
1218 INTEGER.equalsIgnoreCase(keyValues.get("type"))){
1219 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
1220 attributeIndividualStringBuilder.append(keySetString+"=");
1221 attributeIndividualStringBuilder.append(keyValues.get("type")+DEFAULTVALUE);
1222 attributeIndividualStringBuilder.append(keyValues.get("default")+REQUIREDVALUE);
1223 attributeIndividualStringBuilder.append(keyValues.get("required")+MANYFALSE);
1224 attributeStringBuilder.append(attributeIndividualStringBuilder+",");
1225 if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1226 matchableValues.put(keySetString, MATCHINGTRUE);
1229 else if(LIST.equalsIgnoreCase(keyValues.get("type"))){
1231 if(("true").equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1232 matchableValues.put(keySetString, MATCHINGTRUE);
1235 Set<String> keys= keyValues.keySet();
1236 Iterator<String> itr=keys.iterator();
1237 boolean isDefinedType = false;
1238 while(itr.hasNext()){
1239 String key= itr.next();
1240 if((!("type").equals(key) ||("required").equals(key)))
1242 String value= keyValues.get(key);
1243 //The "." in the value determines if its a string or a user defined type.
1244 if (!value.contains(".")){
1246 if(StringUtils.isNumeric(key) ){ //only integer key for the value of Constrains
1247 constraints.add(keyValues.get(key));
1250 //This is user defined type
1251 String trimValue=value.substring(value.lastIndexOf('.')+1);
1252 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1253 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYTRUE);
1254 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1255 isDefinedType = true;
1261 if(!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))){ //type is not user defined and is a list but no constraints defined.
1262 if(constraints == null || constraints.isEmpty()){
1263 referenceStringBuilder.append(keySetString+"=MANY-true"+",");
1267 //User defined Datatype.
1268 if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){
1269 matchableValues.put(keySetString, MATCHINGTRUE);
1271 String value=keyValues.get("type");
1272 if(value != null && !value.isEmpty()){
1273 String trimValue=value.substring(value.lastIndexOf('.')+1);
1274 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1275 referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYFALSE);
1276 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1278 logger.info("keyValues.get(type) is null/empty");
1282 if(constraints!=null && !constraints.isEmpty()){
1284 listBuffer.append(keySetString.toUpperCase()+"=[");
1285 for(String str:constraints){
1286 listBuffer.append(str+",");
1288 listBuffer.append("]#");
1289 logger.info(listBuffer);
1292 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
1293 referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+MANYFALSE);
1294 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
1295 constraints.clear();
1299 dataListBuffer.append(listBuffer);
1302 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1303 logger.info("Whole attribute String is:"+attributeStringBuilder);
1304 logger.info("Whole reference String is:"+referenceStringBuilder);
1305 logger.info("List String is:"+listBuffer);
1306 logger.info("Data list buffer is:"+dataListBuffer);
1307 logger.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
1309 this.listConstraints=dataListBuffer.toString();
1310 this.referenceAttributes=referenceStringBuilder.toString();
1311 this.attributeString=attributeStringBuilder.toString();
1314 @SuppressWarnings("unchecked")
1315 public void findNode(LinkedHashMap<Object, Object> map) {
1317 map.forEach((key,value) -> {
1318 // if the value is properties and its type is map object, then save all the keys
1319 if(key.equals("properties") && value instanceof Map){
1320 saveNodes((LinkedHashMap<?, ?>)value);
1323 if(!key.equals("policy.nodes.Root") && value instanceof Map){
1324 //value is a Map object, then make a recursive call
1325 findNode((LinkedHashMap<Object, Object>) value);
1331 public void saveNodes(LinkedHashMap<?, ?> map) {
1333 map.forEach((key,value) -> {
1335 orderedElements.add((String)key);
1341 public String getAttributeString() {
1342 return attributeString;
1344 public void setAttributeString(String attributeString) {
1345 this.attributeString = attributeString;
1348 public LinkedHashMap<String, Object> getRetmap() {
1352 public void setRetmap(LinkedHashMap<String, Object> retmap) {
1353 this.retmap = retmap;
1355 public Map<String, String> getMatchableValues() {
1356 return matchableValues;
1359 public void setMatchableValues(Map<String, String> matchableValues) {
1360 this.matchableValues = matchableValues;
1362 public String getReferenceAttributes() {
1363 return referenceAttributes;
1366 public void setReferenceAttributes(String referenceAttributes) {
1367 this.referenceAttributes = referenceAttributes;
1369 public String getListConstraints() {
1370 return listConstraints;
1373 public void setListConstraints(String listConstraints) {
1374 this.listConstraints = listConstraints;
1376 public String getDataOrderInfo() {
1377 return dataOrderInfo;
1380 public void setDataOrderInfo(String dataOrderInfo) {
1381 this.dataOrderInfo = dataOrderInfo;