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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.core.impl;
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;
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.HashSet;
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;
43 public class ToscaModelConverter extends AbstractToscaConverter {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaModelConverter.class);
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;
57 public ToscaServiceModel convert(final FileContentHandler fileContentHandler) throws IOException {
58 init(fileContentHandler);
59 handleMainServiceTemplate();
60 handleExternalArtifacts();
61 handleMetadataFile(csarFileMap);
62 updateToscaServiceModel();
63 return toscaServiceModel;
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();
77 public void convertTopologyTemplate(final ServiceTemplate serviceTemplate, final ServiceTemplateReaderService readerService) {
78 new VnfTopologyTemplateConverter().convertTopologyTemplate(serviceTemplate, readerService);
81 private void handleMainServiceTemplate() {
82 if (mainDefinitionFilePath == null) {
85 final String mainServiceTemplateFileName = FilenameUtils.getName(mainDefinitionFilePath);
86 handleServiceTemplate(mainServiceTemplateFileName, mainDefinitionFilePath, csarFileMap, serviceTemplateMap);
87 handleImportDefinitions(mainDefinitionFilePath);
90 private void handleExternalArtifacts() {
91 if (MapUtils.isEmpty(csarFileMap)) {
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);
100 artifactFileContentHandler.addFile(getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
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());
110 final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap =
111 toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
112 handledDefinitionFilesList.addAll(handledImportDefinitionFileMap.keySet());
113 handledDefinitionFilesList.forEach(file -> handleDefinitionTemplate(file, csarFileMap, globalSubstitutionServiceTemplate));
116 private String getMainServiceDefinitionFileName() throws IOException {
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());
127 private void updateToscaServiceModel() {
128 final String mainDefinitionSimpleName = FilenameUtils.getName(mainDefinitionFilePath);
129 updateToscaServiceModel(toscaServiceModel, serviceTemplateMap, artifactFileContentHandler, globalSubstitutionServiceTemplate, csarFileMap,
130 mainDefinitionSimpleName);