3 * * ============LICENSE_START=======================================================
4 * * Copyright (C) 2019 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.onap.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.core.converter.ServiceTemplateReaderService;
35 import org.openecomp.core.utilities.file.FileContentHandler;
36 import org.openecomp.sdc.logging.api.Logger;
37 import org.openecomp.sdc.logging.api.LoggerFactory;
38 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
39 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
40 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
42 public abstract class AbstractToscaSolConverter extends AbstractToscaConverter {
44 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractToscaSolConverter.class);
45 private final Set<String> handledDefinitionFilesList = new HashSet<>();
48 public ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException {
49 Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles());
50 ToscaServiceModel toscaServiceModel = new ToscaServiceModel();
51 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
52 FileContentHandler artifacts = new FileContentHandler();
53 GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate();
54 String mServiceDefinitionPath = getMainServiceDefinitionFileName(fileContentHandler);
55 handleMainServiceTemplate(csarFiles, serviceTemplates, gsst, mServiceDefinitionPath);
56 handleExternalArtifacts(csarFiles, serviceTemplates, artifacts);
57 handleMetadataFile(csarFiles);
58 updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, getSimpleName(mServiceDefinitionPath));
59 return toscaServiceModel;
62 private void handleMainServiceTemplate(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates,
63 GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) {
64 if (mServiceDefinitionFileName != null) {
65 handleServiceTemplate(getSimpleName(mServiceDefinitionFileName), mServiceDefinitionFileName, csarFiles, serviceTemplates);
66 handleImportDefinitions(mServiceDefinitionFileName, csarFiles, gsst);
70 private void handleExternalArtifacts(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, FileContentHandler artifacts) {
71 for (Map.Entry<String, byte[]> fileEntry : csarFiles.entrySet()) {
72 if (!handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey())) {
73 if (isGlobalServiceTemplate(fileEntry.getKey())) {
74 handleServiceTemplate(GLOBAL_ST_NAME, fileEntry.getKey(), csarFiles, serviceTemplates);
76 artifacts.addFile(getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
82 private void handleImportDefinitions(final String fileName, final Map<String, byte[]> csarFiles, final GlobalSubstitutionServiceTemplate gsst) {
83 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(csarFiles, fileName);
84 if (toscaDefinitionImportHandler.hasError()) {
85 throw new InvalidToscaDefinitionImportException(toscaDefinitionImportHandler.getErrors());
87 final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap = toscaDefinitionImportHandler
88 .getHandledImportDefinitionFileMap();
89 handledDefinitionFilesList.addAll(handledImportDefinitionFileMap.keySet());
90 for (final String file : handledDefinitionFilesList) {
91 handleDefinitionTemplate(file, csarFiles, gsst);
95 private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException {
97 ToscaMetadata toscaMetadata = OnboardingToscaMetadata
98 .parseToscaMetadataFile(contentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME));
99 return toscaMetadata.getMetaEntries().get(ENTRY_DEFINITIONS.getName());
100 } catch (IOException e) {
101 LOGGER.error(e.getMessage(), e);
102 throw new IOException(e.getMessage());
106 private String getSimpleName(String path) {
107 if (path != null && path.contains("/")) {
108 path = path.substring(path.lastIndexOf('/') + 1);