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