aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-tosca-converter-lib/openecomp-tosca-converter-core/src/main/java/org/openecomp/core/impl/GlobalSubstitutionServiceTemplate.java
blob: 98d8d23751b593b23c8eb0a9fbc007b32c001bd3 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * Copyright © 2016-2017 European Support Limited
 *
 * 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.
 */

package org.openecomp.core.impl;

import static org.openecomp.core.converter.datatypes.Constants.ONAP_INDEX;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.collections4.MapUtils;
import org.onap.sdc.tosca.datatypes.model.DataType;
import org.onap.sdc.tosca.datatypes.model.Import;
import org.onap.sdc.tosca.datatypes.model.NodeType;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;

public class GlobalSubstitutionServiceTemplate extends ServiceTemplate {

    public static final String GLOBAL_SUBSTITUTION_SERVICE_FILE_NAME =
        "GlobalSubstitutionTypesServiceTemplate.yaml";
    public static final String TEMPLATE_NAME_PROPERTY = "template_name";
    public static final String DEFINITION_VERSION = "tosca_simple_yaml_1_0_0";
    public static final String HEAT_INDEX = "openecomp_heat_index";
    public static final String HEAT_INDEX_IMPORT_FILE = "openecomp-heat/_index.yml";
    public static final String ONAP_INDEX_IMPORT_FILE = "onap/_index.yml";

    // transient needed to avoid being parsed as a YAML String. Used parser is reading fields instead of getters,
    // although it ignores static or transient fields.
    private final transient Map<String, ServiceTemplate> globalServiceTemplates;
    private final transient Map<String, DataType> globalDataTypeMap;

    public GlobalSubstitutionServiceTemplate() {
        super();
        init();
        globalServiceTemplates =
            GlobalTypesGenerator.getGlobalTypesServiceTemplate(OnboardingTypesEnum.CSAR);
        globalDataTypeMap = loadGlobalDataTypes();
    }

    public void init()   {
        writeDefinitionSection();
        writeMetadataSection();
        writeImportsSection();
        setNode_types(new HashMap<>());
        setData_types(new HashMap<>());
    }

    public void appendNodes(final Map<String, NodeType> nodes) {
        final Optional<Map<String, NodeType>> nodeTypesToAdd = findNonGlobalTypesNodes(nodes);
        nodeTypesToAdd.ifPresent(nodeTypes -> getNode_types().putAll(nodeTypes));
    }

    public void appendDataTypes(final Map<String, DataType> dataTypeMap) {
        if (MapUtils.isEmpty(dataTypeMap)) {
            return;
        }
        dataTypeMap.entrySet().stream()
            .filter(dataTypeEntry -> !isGlobalDataType(dataTypeEntry.getKey()))
            .forEach(dataTypeEntry -> {
                final Optional<DataType> dataType = parseDataTypeToYamlObject(dataTypeEntry);
                dataType.ifPresent(dataType1 -> getData_types().put(dataTypeEntry.getKey(), dataType1));
            });
    }

    private void writeImportsSection() {
        List<Map<String, Import>> imports = new ArrayList<>();
        Map<String, Import> stringImportMap = new HashMap<>();
        imports.add(stringImportMap);
        setImports(imports);
        Import imprtObj = new Import();
        imprtObj.setFile(HEAT_INDEX_IMPORT_FILE);
        stringImportMap.put(HEAT_INDEX, imprtObj);
        Import onapDefinitionsImport = new Import();
        onapDefinitionsImport.setFile(ONAP_INDEX_IMPORT_FILE);
        stringImportMap.put(ONAP_INDEX, onapDefinitionsImport);
    }


    private void writeMetadataSection() {
        Map<String, String> metadata = new HashMap<>();
        metadata.put(TEMPLATE_NAME_PROPERTY, "GlobalSubstitutionTypes");
        setMetadata(metadata);
    }

    private void writeDefinitionSection() {
        setTosca_definitions_version(DEFINITION_VERSION);
    }

    private Optional<Map<String, NodeType>> findNonGlobalTypesNodes(final Map<String, NodeType> nodes){
        final Map<String, NodeType> globalNodeTypes = getAllGlobalNodeTypes();
        if (MapUtils.isEmpty(globalNodeTypes)) {
            return Optional.of(nodes);
        }

        final Map<String, NodeType> nodeTypesToAdd = new HashMap<>();

        for(Map.Entry<String, NodeType> nodeTypeEntry : nodes.entrySet()){
            if(!globalNodeTypes.containsKey(nodeTypeEntry.getKey())){
                Optional<NodeType> nodeType = parseNodeTypeToYamlObject(nodeTypeEntry);
                nodeType
                    .ifPresent(nodeTypeValue -> nodeTypesToAdd.put(nodeTypeEntry.getKey(), nodeTypeValue));
            }
        }

        return Optional.of(nodeTypesToAdd);
    }

    private boolean isGlobalDataType(final String dataType) {
        if (MapUtils.isEmpty(globalDataTypeMap)) {
            return false;
        }

        return globalDataTypeMap.containsKey(dataType);
    }

    private Optional<NodeType> parseNodeTypeToYamlObject(final Entry<String, NodeType> nodeTypeEntry) {
        return ToscaConverterUtil
            .createObjectFromClass(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(), NodeType.class);
    }

    private Optional<DataType> parseDataTypeToYamlObject(final Entry<String, DataType> dataTypeEntry) {
        return ToscaConverterUtil
            .createObjectFromClass(dataTypeEntry.getKey(), dataTypeEntry.getValue(), DataType.class);
    }

    private Map<String, DataType> loadGlobalDataTypes() {
        return globalServiceTemplates.values().stream()
            .map(ServiceTemplate::getData_types)
            .filter(MapUtils::isNotEmpty)
            .flatMap(stringDataTypeMap -> stringDataTypeMap.entrySet().stream())
            .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (dataType, dataType2) -> dataType));
    }

    private Map<String, NodeType> getAllGlobalNodeTypes(){
        Map<String, NodeType> globalNodeTypes = new HashMap<>();

        for(Map.Entry<String, ServiceTemplate> serviceTemplateEntry : globalServiceTemplates.entrySet()){
            if(isNodesServiceTemplate(serviceTemplateEntry.getKey())){
                globalNodeTypes.putAll(serviceTemplateEntry.getValue().getNode_types());
            }
        }

        return globalNodeTypes;
    }

    private boolean isNodesServiceTemplate(String filename) {
        return filename.endsWith("nodes.yml") || filename.endsWith("nodes.yaml");
    }
}