Added oparent to sdc main
[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
37 @org.springframework.stereotype.Component
38 public class InputConverter {
39     private PropertyConvertor propertyConvertor;
40     private static final Logger log = Logger.getLogger(ToscaExportHandler.class);
41
42
43     public InputConverter() {
44         this.propertyConvertor = PropertyConvertor.getInstance();
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
61         Map<String, ToscaProperty> inputs = new HashMap<>();
62         if (inputDef != null) {
63             inputDef.forEach(i -> {
64                 //Extract input the same as property
65                 ToscaProperty toscaProperty = propertyConvertor.convertProperty(dataTypes, i,
66                     PropertyConvertor.PropertyType.INPUT);
67                 //now that we have Tosca property we create new object called tosca input which drives from it
68                 ToscaInput toscaInput = new ToscaInput(toscaProperty);
69                 List<Annotation> annotations = i.getAnnotations();
70                 extractAnnotations(dataTypes, toscaInput, annotations);
71                 inputs.put(i.getName(), toscaInput);
72             });
73         }
74         return inputs;
75     }
76
77     private void extractAnnotations(Map<String, DataTypeDefinition> dataTypes, ToscaInput toscaInput, List<Annotation> annotationsList) {
78         if (annotationsList != null) {
79             annotationsList.forEach(inputAnnotation -> {
80                 ToscaAnnotation annotation = new ToscaAnnotation();
81                 if ((inputAnnotation.getType()) != null) {
82                     annotation.setType(inputAnnotation.getType());
83                 }
84                 if (inputAnnotation.getDescription() != null) {
85                     annotation.setDescription(inputAnnotation.getDescription());
86                 }
87                 if (inputAnnotation.getProperties() != null) {
88                     Map<String, Object> properties = new HashMap<>();
89                     inputAnnotation.getProperties().forEach(k -> {
90                         propertyConvertor.convertAndAddValue(dataTypes,properties,k, k::getValue);
91                     });
92                     annotation.setProperties(properties);
93                 }
94                 toscaInput.addAnnotation(inputAnnotation.getName(), annotation);
95             });
96         }
97     }
98 }
99
100
101