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=========================================================
23 package org.openecomp.core.impl;
25 import static org.openecomp.core.converter.datatypes.Constants.globalStName;
26 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
27 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
29 import java.io.IOException;
30 import java.util.HashMap;
31 import java.util.HashSet;
34 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
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;
43 public abstract class AbstractToscaSolConverter extends AbstractToscaConverter {
45 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractToscaSolConverter.class);
46 private final Set<String> handledDefinitionFilesList = new HashSet<>();
49 public ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException {
50 Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles());
51 ToscaServiceModel toscaServiceModel = new ToscaServiceModel();
52 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
53 FileContentHandler artifacts = new FileContentHandler();
54 GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate();
55 String mServiceDefinitionPath = getMainServiceDefinitionFileName(fileContentHandler);
56 handleMainServiceTemplate(csarFiles, serviceTemplates, gsst, mServiceDefinitionPath);
57 handleExternalArtifacts(csarFiles, serviceTemplates, artifacts);
58 handleMetadataFile(csarFiles);
59 updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, getSimpleName(mServiceDefinitionPath));
60 return toscaServiceModel;
63 private void handleMainServiceTemplate(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates,
64 GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) {
65 if (mServiceDefinitionFileName != null) {
66 handleServiceTemplate(getSimpleName(mServiceDefinitionFileName), mServiceDefinitionFileName, csarFiles, serviceTemplates);
67 handleImportDefinitions(mServiceDefinitionFileName, csarFiles, gsst);
71 private void handleExternalArtifacts(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, FileContentHandler artifacts) {
72 for (Map.Entry<String, byte[]> fileEntry : csarFiles.entrySet()) {
73 if (!handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey())) {
74 if (isGlobalServiceTemplate(fileEntry.getKey())) {
75 handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates);
78 getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
84 private void handleImportDefinitions(final String fileName, final Map<String, byte[]> csarFiles
85 , final GlobalSubstitutionServiceTemplate gsst) {
86 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(csarFiles, fileName);
87 if (toscaDefinitionImportHandler.hasError()) {
88 throw new InvalidToscaDefinitionImportException(toscaDefinitionImportHandler.getErrors());
90 handledDefinitionFilesList.addAll(toscaDefinitionImportHandler.getHandledDefinitionFilesList());
91 for (final String file : handledDefinitionFilesList) {
92 handleDefintionTemplate(file, csarFiles, gsst);
96 private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException {
98 ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(
99 contentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME));
100 return toscaMetadata.getMetaEntries().get(ENTRY_DEFINITIONS.getName());
101 } catch (IOException e) {
102 LOGGER.error(e.getMessage(), e);
103 throw new IOException(e.getMessage());
107 private String getSimpleName(String path) {
108 if (path != null && path.contains("/")) {
109 path = path.substring(path.lastIndexOf('/') + 1);