Fix PropertyConvertor initialization (use spring)
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / utils / InputConverter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.tosca.utils;
22
23 import org.openecomp.sdc.be.datatypes.elements.Annotation;
24 import org.openecomp.sdc.be.model.DataTypeDefinition;
25 import org.openecomp.sdc.be.model.InputDefinition;
26 import org.openecomp.sdc.be.tosca.PropertyConvertor;
27 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
28 import org.openecomp.sdc.be.tosca.model.ToscaAnnotation;
29 import org.openecomp.sdc.be.tosca.model.ToscaInput;
30 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
36 import org.springframework.beans.factory.annotation.Autowired;
37
38 @org.springframework.stereotype.Component
39 public class InputConverter {
40     private PropertyConvertor propertyConvertor;
41     private static final Logger log = Logger.getLogger(ToscaExportHandler.class);
42
43     @Autowired
44     public InputConverter(PropertyConvertor propertyConvertor) {
45         this.propertyConvertor = propertyConvertor;
46     }
47
48     /**
49      * This is the converter made for input
50      * input is derived from properties and  is similar to properties
51      * now that it was added annotations , we created a new convertetor for it
52      * Input
53      *    List of annotation
54      *          Annotation:
55      *              name
56      *              type
57      *              description
58      *              list of properties */
59     public Map<String, ToscaProperty>  convertInputs( List<InputDefinition> inputDef,Map<String, DataTypeDefinition> dataTypes) {
60         log.debug("convert inputs to to tosca  ");
61
62         Map<String, ToscaProperty> inputs = new HashMap<>();
63         if (inputDef != null) {
64             inputDef.forEach(i -> {
65                 //Extract input the same as property
66                 ToscaProperty toscaProperty = propertyConvertor.convertProperty(dataTypes, i,
67                     PropertyConvertor.PropertyType.INPUT);
68                 //now that we have Tosca property we create new object called tosca input which drives from it
69                 ToscaInput toscaInput = new ToscaInput(toscaProperty);
70                 List<Annotation> annotations = i.getAnnotations();
71                 extractAnnotations(dataTypes, toscaInput, annotations);
72                 inputs.put(i.getName(), toscaInput);
73             });
74         }
75         return inputs;
76     }
77
78     private void extractAnnotations(Map<String, DataTypeDefinition> dataTypes, ToscaInput toscaInput, List<Annotation> annotationsList) {
79         if (annotationsList != null) {
80             annotationsList.forEach(inputAnnotation -> {
81                 ToscaAnnotation annotation = new ToscaAnnotation();
82                 if ((inputAnnotation.getType()) != null) {
83                     annotation.setType(inputAnnotation.getType());
84                 }
85                 if (inputAnnotation.getDescription() != null) {
86                     annotation.setDescription(inputAnnotation.getDescription());
87                 }
88                 if (inputAnnotation.getProperties() != null) {
89                     Map<String, Object> properties = new HashMap<>();
90                     inputAnnotation.getProperties().forEach(k -> {
91                         propertyConvertor.convertAndAddValue(dataTypes,properties,k, k::getValue);
92                     });
93                     annotation.setProperties(properties);
94                 }
95                 toscaInput.addAnnotation(inputAnnotation.getName(), annotation);
96             });
97         }
98     }
99 }
100
101
102