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.converter.ServiceTemplateReaderService;
36 import org.openecomp.core.utilities.file.FileContentHandler;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
39 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
40 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
41 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
44 public abstract class AbstractToscaSolConverter extends AbstractToscaConverter {
46 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractToscaSolConverter.class);
47 private final Set<String> handledDefinitionFilesList = new HashSet<>();
50 public ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException {
51 Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles());
52 ToscaServiceModel toscaServiceModel = new ToscaServiceModel();
53 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
54 FileContentHandler artifacts = new FileContentHandler();
55 GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate();
56 String mServiceDefinitionPath = getMainServiceDefinitionFileName(fileContentHandler);
57 handleMainServiceTemplate(csarFiles, serviceTemplates, gsst, mServiceDefinitionPath);
58 handleExternalArtifacts(csarFiles, serviceTemplates, artifacts);
59 handleMetadataFile(csarFiles);
60 updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, getSimpleName(mServiceDefinitionPath));
61 return toscaServiceModel;
64 private void handleMainServiceTemplate(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates,
65 GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) {
66 if (mServiceDefinitionFileName != null) {
67 handleServiceTemplate(getSimpleName(mServiceDefinitionFileName), mServiceDefinitionFileName, csarFiles, serviceTemplates);
68 handleImportDefinitions(mServiceDefinitionFileName, csarFiles, gsst);
72 private void handleExternalArtifacts(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, FileContentHandler artifacts) {
73 for (Map.Entry<String, byte[]> fileEntry : csarFiles.entrySet()) {
74 if (!handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey())) {
75 if (isGlobalServiceTemplate(fileEntry.getKey())) {
76 handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates);
79 getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
85 private void handleImportDefinitions(final String fileName, final Map<String, byte[]> csarFiles
86 , final GlobalSubstitutionServiceTemplate gsst) {
87 final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(csarFiles, fileName);
88 if (toscaDefinitionImportHandler.hasError()) {
89 throw new InvalidToscaDefinitionImportException(toscaDefinitionImportHandler.getErrors());
91 final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap =
92 toscaDefinitionImportHandler.getHandledImportDefinitionFileMap();
93 handledDefinitionFilesList.addAll(handledImportDefinitionFileMap.keySet());
94 for (final String file : handledDefinitionFilesList) {
95 handleDefinitionTemplate(file, csarFiles, gsst);
99 private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException {
101 ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(
102 contentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME));
103 return toscaMetadata.getMetaEntries().get(ENTRY_DEFINITIONS.getName());
104 } catch (IOException e) {
105 LOGGER.error(e.getMessage(), e);
106 throw new IOException(e.getMessage());
110 private String getSimpleName(String path) {
111 if (path != null && path.contains("/")) {
112 path = path.substring(path.lastIndexOf('/') + 1);