aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java
blob: b94a32291ea535ccf4ec784585ad97ebf461951d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdc.be.tosca.utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.openecomp.sdc.be.datatypes.elements.Annotation;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.tosca.PropertyConvertor;
import org.openecomp.sdc.be.tosca.ToscaExportHandler;
import org.openecomp.sdc.be.tosca.model.ToscaAnnotation;
import org.openecomp.sdc.be.tosca.model.ToscaInput;
import org.openecomp.sdc.be.tosca.model.ToscaProperty;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.beans.factory.annotation.Autowired;

@org.springframework.stereotype.Component
public class InputConverter {

    private static final Logger log = Logger.getLogger(ToscaExportHandler.class);
    private PropertyConvertor propertyConvertor;

    @Autowired
    public InputConverter(PropertyConvertor propertyConvertor) {
        this.propertyConvertor = propertyConvertor;
    }

    /**
     * This is the converter made for input
     * input is derived from properties and  is similar to properties
     * now that it was added annotations , we created a new convertetor for it
     * Input
     *    List of annotation
     *          Annotation:
     *              name
     *              type
     *              description
     *              list of properties */
    public Map<String, ToscaProperty> convertInputs(List<InputDefinition> inputDef, Map<String, DataTypeDefinition> dataTypes) {
        log.debug("convert inputs to tosca");
        Map<String, ToscaProperty> inputs = new HashMap<>();
        if (inputDef != null) {
            inputDef.forEach(i -> {
                //Extract input the same as property
                ToscaProperty toscaProperty = propertyConvertor.convertProperty(dataTypes, i, PropertyConvertor.PropertyType.INPUT);
                //now that we have Tosca property we create new object called tosca input which drives from it
                ToscaInput toscaInput = new ToscaInput(toscaProperty);
                List<Annotation> annotations = i.getAnnotations();
                extractAnnotations(dataTypes, toscaInput, annotations);
                inputs.put(i.getName(), toscaInput);
            });
        }
        return inputs;
    }

    private void extractAnnotations(Map<String, DataTypeDefinition> dataTypes, ToscaInput toscaInput, List<Annotation> annotationsList) {
        if (annotationsList != null) {
            annotationsList.forEach(inputAnnotation -> {
                ToscaAnnotation annotation = new ToscaAnnotation();
                if ((inputAnnotation.getType()) != null) {
                    annotation.setType(inputAnnotation.getType());
                }
                if (inputAnnotation.getDescription() != null) {
                    annotation.setDescription(inputAnnotation.getDescription());
                }
                if (inputAnnotation.getProperties() != null) {
                    Map<String, Object> properties = new HashMap<>();
                    inputAnnotation.getProperties().forEach(k -> {
                        propertyConvertor.convertAndAddValue(dataTypes, properties, k, k::getValue);
                    });
                    annotation.setProperties(properties);
                }
                toscaInput.addAnnotation(inputAnnotation.getName(), annotation);
            });
        }
    }
}