re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / utils / InputConverter.java
1 package org.openecomp.sdc.be.tosca.utils;
2
3 import org.openecomp.sdc.be.datatypes.elements.Annotation;
4 import org.openecomp.sdc.be.model.DataTypeDefinition;
5 import org.openecomp.sdc.be.model.InputDefinition;
6 import org.openecomp.sdc.be.tosca.PropertyConvertor;
7 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
8 import org.openecomp.sdc.be.tosca.model.ToscaAnnotation;
9 import org.openecomp.sdc.be.tosca.model.ToscaInput;
10 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
11 import org.openecomp.sdc.common.log.wrappers.Logger;
12
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 @org.springframework.stereotype.Component
18 public class InputConverter {
19     private PropertyConvertor propertyConvertor;
20     private static final Logger log = Logger.getLogger(ToscaExportHandler.class);
21
22
23     public InputConverter() {
24         this.propertyConvertor = PropertyConvertor.getInstance();
25
26     }
27     /**
28      * This is the converter made for input
29      * input is derived from properties and  is similar to properties
30      * now that it was added annotations , we created a new convertetor for it
31      * Input
32      *    List of annotation
33      *          Annotation:
34      *              name
35      *              type
36      *              description
37      *              list of properties */
38     public Map<String, ToscaProperty>  convertInputs( List<InputDefinition> inputDef,Map<String, DataTypeDefinition> dataTypes) {
39         log.debug("convert inputs to to tosca  ");
40
41         Map<String, ToscaProperty> inputs = new HashMap<>();
42         if (inputDef != null) {
43             inputDef.forEach(i -> {
44                 //Extract input the same as property
45                 ToscaProperty toscaProperty = propertyConvertor.convertProperty(dataTypes, i, false);
46                 //now that we have Tosca property we create new object called tosca input which drives from it
47                 ToscaInput toscaInput = new ToscaInput(toscaProperty);
48                 List<Annotation> annotations = i.getAnnotations();
49                 extractAnnotations(dataTypes, toscaInput, annotations);
50                 inputs.put(i.getName(), toscaInput);
51             });
52         }
53         return inputs;
54     }
55
56     private void extractAnnotations(Map<String, DataTypeDefinition> dataTypes, ToscaInput toscaInput, List<Annotation> annotationsList) {
57         if (annotationsList != null) {
58             annotationsList.forEach(inputAnnotation -> {
59                 ToscaAnnotation annotation = new ToscaAnnotation();
60                 if ((inputAnnotation.getType()) != null) {
61                     annotation.setType(inputAnnotation.getType());
62                 }
63                 if (inputAnnotation.getDescription() != null) {
64                     annotation.setDescription(inputAnnotation.getDescription());
65                 }
66                 if (inputAnnotation.getProperties() != null) {
67                     Map<String, Object> properties = new HashMap<>();
68                     inputAnnotation.getProperties().forEach(k -> {
69                         propertyConvertor.convertAndAddValue(dataTypes,properties,k, k::getValue);
70                     });
71                     annotation.setProperties(properties);
72                 }
73                 toscaInput.addAnnotation(inputAnnotation.getName(), annotation);
74             });
75         }
76     }
77 }
78
79
80