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;
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.List;
41 import static org.openecomp.core.converter.datatypes.Constants.globalStName;
42 import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_FILE_IMPORT_ATTRIBUTES;
43 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
44 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
46 public class ToscaSolConverterImpl extends AbstractToscaConverter {
48 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaSolConverterImpl.class);
49 private final Set<String> handledDefinitionFilesList = new HashSet<>();
52 public ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException {
53 Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles());
54 ToscaServiceModel toscaServiceModel = new ToscaServiceModel();
55 Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
56 FileContentHandler artifacts = new FileContentHandler();
57 GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate();
58 String mServiceDefinitionPath = getMainServiceDefinitionFileName(fileContentHandler);
59 handleMainServiceTemplate(csarFiles, serviceTemplates, gsst, mServiceDefinitionPath);
60 handleExternalArtifacts(csarFiles, serviceTemplates, artifacts);
61 handleMetadataFile(csarFiles);
62 updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, getSimpleName(mServiceDefinitionPath));
63 return toscaServiceModel;
66 private void handleMainServiceTemplate(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates,
67 GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) {
68 if(mServiceDefinitionFileName != null){
69 handleServiceTemplate(getSimpleName(mServiceDefinitionFileName), mServiceDefinitionFileName, csarFiles, serviceTemplates);
70 String parentDir = mServiceDefinitionFileName.substring(0, mServiceDefinitionFileName.lastIndexOf("/"));
71 handleImportDefinitions(mServiceDefinitionFileName, csarFiles, parentDir, gsst);
75 private void handleExternalArtifacts(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, FileContentHandler artifacts) {
76 for(Map.Entry<String, byte[]> fileEntry: csarFiles.entrySet()){
77 if(!handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey())){
78 if(isGlobalServiceTemplate(fileEntry.getKey())){
79 handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates);
82 getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
88 private void handleImportDefinitions(String fileName, Map<String,byte[]> csarFiles, String parentDir, GlobalSubstitutionServiceTemplate gsst) {
89 handledDefinitionFilesList.add(fileName);
90 ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(fileName));
91 List<Object> imports = (readerService).getImports();
92 for (Object o : imports) {
93 String importPath = getImportedFilePath(o, parentDir);
94 if (importPath != null && !handledDefinitionFilesList.contains(importPath)) {
95 handleDefintionTemplate(importPath, csarFiles, gsst);
96 if (importPath.contains("/")) {
97 parentDir = importPath.substring(0, importPath.lastIndexOf("/"));
99 handleImportDefinitions(importPath, csarFiles, parentDir, gsst);
105 private String getImportedFilePath(Object o, String parentDir){
106 if(o instanceof String){
107 String fileName = (String) o;
108 if(!fileName.contains("/")){
109 fileName = parentDir + "/" + fileName;
112 }else if(o instanceof Map){
113 Map<String, Object> o1 = (Map) o;
114 for(Map.Entry<String, Object> entry: o1.entrySet()){
115 if(NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))){
116 getImportedFilePath(entry.getValue(), parentDir);
123 private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException {
125 ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(
126 contentHandler.getFileContent(TOSCA_META_PATH_FILE_NAME));
127 return toscaMetadata.getMetaEntries().get(TOSCA_META_ENTRY_DEFINITIONS);
128 }catch (IOException e){
129 LOGGER.error(e.getMessage(), e);
130 throw new IOException(e.getMessage());
134 private String getSimpleName(String path){
135 if(path != null && path.contains("/")){
136 path = path.substring(path.lastIndexOf("/") + 1);