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 org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
26 import org.openecomp.core.converter.ServiceTemplateReaderService;
27 import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
28 import org.openecomp.core.utilities.file.FileContentHandler;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
32 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
33 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
35 import java.io.IOException;
36 import java.util.HashMap;
37 import java.util.HashSet;
38 import java.util.List;
42 import static org.openecomp.core.converter.datatypes.Constants.globalStName;
43 import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_FILE_IMPORT_ATTRIBUTES;
44 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
45 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
47 public abstract class AbstractToscaSolConverter extends AbstractToscaConverter {
49 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractToscaSolConverter.class);
50 private final Set<String> handledDefinitionFilesList = new HashSet<>();
53 public ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException {
54 Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles());
55 ToscaServiceModel toscaServiceModel = new ToscaServiceModel();
56 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
57 FileContentHandler artifacts = new FileContentHandler();
58 GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate();
59 String mServiceDefinitionPath = getMainServiceDefinitionFileName(fileContentHandler);
60 handleMainServiceTemplate(csarFiles, serviceTemplates, gsst, mServiceDefinitionPath);
61 handleExternalArtifacts(csarFiles, serviceTemplates, artifacts);
62 handleMetadataFile(csarFiles);
63 updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, getSimpleName(mServiceDefinitionPath));
64 return toscaServiceModel;
67 private void handleMainServiceTemplate(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates,
68 GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) {
69 if (mServiceDefinitionFileName != null) {
70 handleServiceTemplate(getSimpleName(mServiceDefinitionFileName), mServiceDefinitionFileName, csarFiles, serviceTemplates);
71 String parentDir = mServiceDefinitionFileName.substring(0, mServiceDefinitionFileName.lastIndexOf("/"));
72 handleImportDefinitions(mServiceDefinitionFileName, csarFiles, parentDir, gsst);
76 private void handleExternalArtifacts(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, FileContentHandler artifacts) {
77 for (Map.Entry<String, byte[]> fileEntry : csarFiles.entrySet()) {
78 if (!handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey())) {
79 if (isGlobalServiceTemplate(fileEntry.getKey())) {
80 handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates);
83 getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
89 private void handleImportDefinitions(String fileName, Map<String, byte[]> csarFiles, String parentDir, GlobalSubstitutionServiceTemplate gsst) {
90 handledDefinitionFilesList.add(fileName);
91 ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(fileName));
92 List<Object> imports = (readerService).getImports();
93 for (Object o : imports) {
94 String importPath = getImportedFilePath(o, parentDir);
95 if (importPath != null && !handledDefinitionFilesList.contains(importPath)) {
96 handleDefintionTemplate(importPath, csarFiles, gsst);
97 if (importPath.contains("/")) {
98 parentDir = importPath.substring(0, importPath.lastIndexOf("/"));
100 handleImportDefinitions(importPath, csarFiles, parentDir, gsst);
106 private String getImportedFilePath(Object o, String parentDir) {
107 if (o instanceof String) {
108 String fileName = (String) o;
109 if (!fileName.contains("/")) {
110 fileName = parentDir + "/" + fileName;
113 } else if (o instanceof Map) {
114 Map<String, Object> o1 = (Map) o;
115 for (Map.Entry<String, Object> entry : o1.entrySet()) {
116 if (NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))) {
117 getImportedFilePath(entry.getValue(), parentDir);
124 private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException {
126 ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(
127 contentHandler.getFileContent(TOSCA_META_PATH_FILE_NAME));
128 return toscaMetadata.getMetaEntries().get(TOSCA_META_ENTRY_DEFINITIONS);
129 } catch (IOException e) {
130 LOGGER.error(e.getMessage(), e);
131 throw new IOException(e.getMessage());
135 private String getSimpleName(String path) {
136 if (path != null && path.contains("/")) {
137 path = path.substring(path.lastIndexOf("/") + 1);