2656382972551d4d0d83269292d3a398e776efa5
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / main / java / org / openecomp / core / impl / ToscaModelConverter.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2021 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.core.impl;
23
24 import static org.openecomp.core.converter.datatypes.Constants.GLOBAL_ST_NAME;
25 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
26 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
27
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Map;
32 import java.util.Set;
33 import org.apache.commons.collections4.MapUtils;
34 import org.apache.commons.io.FilenameUtils;
35 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
36 import org.openecomp.core.converter.ServiceTemplateReaderService;
37 import org.openecomp.core.utilities.file.FileContentHandler;
38 import org.openecomp.sdc.logging.api.Logger;
39 import org.openecomp.sdc.logging.api.LoggerFactory;
40 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
41 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
42
43 public class ToscaModelConverter extends AbstractToscaConverter {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaModelConverter.class);
46
47     private final Set<String> handledDefinitionFilesList = new HashSet<>();
48     private ToscaServiceModel toscaServiceModel;
49     private Map<String, ServiceTemplate> serviceTemplateMap;
50     private FileContentHandler csarFileContentHandler;
51     private FileContentHandler artifactFileContentHandler;
52     private Map<String, byte[]> csarFileMap;
53     private GlobalSubstitutionServiceTemplate globalSubstitutionServiceTemplate;
54     private String mainDefinitionFilePath;
55
56     @Override
57     public ToscaServiceModel convert(final FileContentHandler fileContentHandler) throws IOException {
58         init(fileContentHandler);
59         handleMainServiceTemplate();
60         handleExternalArtifacts();
61         handleMetadataFile(csarFileMap);
62         updateToscaServiceModel();
63         return toscaServiceModel;
64     }
65
66     private void init(final FileContentHandler fileContentHandler) throws IOException {
67         csarFileContentHandler = fileContentHandler;
68         csarFileMap = new HashMap<>(fileContentHandler.getFiles());
69         toscaServiceModel = new ToscaServiceModel();
70         serviceTemplateMap = new HashMap<>();
71         artifactFileContentHandler = new FileContentHandler();
72         globalSubstitutionServiceTemplate = new GlobalSubstitutionServiceTemplate();
73         mainDefinitionFilePath = getMainServiceDefinitionFileName();
74     }
75
76     @Override
77     public void convertTopologyTemplate(final ServiceTemplate serviceTemplate, final ServiceTemplateReaderService readerService) {
78         new VnfTopologyTemplateConverter().convertTopologyTemplate(serviceTemplate, readerService);
79     }
80
81     private void handleMainServiceTemplate() {
82         if (mainDefinitionFilePath == null) {
83             return;
84         }
85         final String mainServiceTemplateFileName = FilenameUtils.getName(mainDefinitionFilePath);
86         handleServiceTemplate(mainServiceTemplateFileName, mainDefinitionFilePath, csarFileMap, serviceTemplateMap);
87         handleImportDefinitions(mainDefinitionFilePath);
88     }
89
90     private void handleExternalArtifacts() {
91         if (MapUtils.isEmpty(csarFileMap)) {
92             return;
93         }
94         csarFileMap.entrySet().stream()
95             .filter(fileEntry -> !handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey()))
96             .forEach(fileEntry -> {
97                 if (isGlobalServiceTemplate(fileEntry.getKey())) {
98                     handleServiceTemplate(GLOBAL_ST_NAME, fileEntry.getKey(), csarFileMap, serviceTemplateMap);
99                 } else {
100                     artifactFileContentHandler.addFile(getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
101                 }
102             });
103     }
104
105     private void handleImportDefinitions(final String fileName) {
106         final var toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(csarFileMap, fileName);
107         if (toscaDefinitionImportHandler.hasError()) {
108             throw new InvalidToscaDefinitionImportException(toscaDefinitionImportHandler.getErrors());
109         }
110         final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap =
111             toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
112         handledDefinitionFilesList.addAll(handledImportDefinitionFileMap.keySet());
113         handledDefinitionFilesList.forEach(file -> handleDefinitionTemplate(file, csarFileMap, globalSubstitutionServiceTemplate));
114     }
115
116     private String getMainServiceDefinitionFileName() throws IOException {
117         try {
118             var toscaMetadata = OnboardingToscaMetadata
119                 .parseToscaMetadataFile(csarFileContentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME));
120             return toscaMetadata.getMetaEntries().get(ENTRY_DEFINITIONS.getName());
121         } catch (final IOException e) {
122             LOGGER.error(e.getMessage(), e);
123             throw new IOException(e.getMessage());
124         }
125     }
126
127     private void updateToscaServiceModel() {
128         final String mainDefinitionSimpleName = FilenameUtils.getName(mainDefinitionFilePath);
129         updateToscaServiceModel(toscaServiceModel, serviceTemplateMap, artifactFileContentHandler, globalSubstitutionServiceTemplate, csarFileMap,
130             mainDefinitionSimpleName);
131     }
132
133 }