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 mServiceDefinitionFileName = getMainServiceDefinitionFileName(fileContentHandler);
59 handleMainServiceTemplate(csarFiles, serviceTemplates, gsst, mServiceDefinitionFileName);
60 handleExternalArtifacts(csarFiles, serviceTemplates, artifacts);
61 handleMetadataFile(csarFiles);
62 updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, mServiceDefinitionFileName);
63 return toscaServiceModel;
66 private void handleMainServiceTemplate(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates,
67 GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) {
68 handleServiceTemplate(mServiceDefinitionFileName, mServiceDefinitionFileName, csarFiles, serviceTemplates);
69 handleImportDefinitions(mServiceDefinitionFileName, csarFiles, mServiceDefinitionFileName.substring(0,
70 mServiceDefinitionFileName.lastIndexOf("/")), gsst);
73 private void handleExternalArtifacts(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, FileContentHandler artifacts) {
74 for(Map.Entry<String, byte[]> fileEntry: csarFiles.entrySet()){
75 if(!handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey())){
76 if(isGlobalServiceTemplate(fileEntry.getKey())){
77 handleServiceTemplate(globalStName, fileEntry.getKey(), csarFiles, serviceTemplates);
80 getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
86 private void handleImportDefinitions(String fileName, Map<String,byte[]> csarFiles, String parentDir, GlobalSubstitutionServiceTemplate gsst) {
87 handledDefinitionFilesList.add(fileName);
88 ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(csarFiles.get(fileName));
89 List<Object> imports = (readerService).getImports();
90 for (Object o : imports) {
91 String importPath = getImportedFilePath(o, parentDir);
92 if (importPath != null && !handledDefinitionFilesList.contains(importPath)) {
93 handleDefintionTemplate(importPath, csarFiles, gsst);
94 if (importPath.contains("/")) {
95 parentDir = importPath.substring(0, importPath.lastIndexOf("/"));
97 handleImportDefinitions(importPath, csarFiles, parentDir, gsst);
103 private String getImportedFilePath(Object o, String parentDir){
104 if(o instanceof String){
105 String fileName = (String) o;
106 if(!fileName.contains("/")){
107 fileName = parentDir + "/" + fileName;
110 }else if(o instanceof Map){
111 Map<String, Object> o1 = (Map) o;
112 for(Map.Entry<String, Object> entry: o1.entrySet()){
113 if(NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))){
114 getImportedFilePath(entry.getValue(), parentDir);
121 private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException {
123 ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(
124 contentHandler.getFileContent(TOSCA_META_PATH_FILE_NAME));
125 return toscaMetadata.getMetaEntries().get(TOSCA_META_ENTRY_DEFINITIONS);
126 }catch (IOException e){
127 LOGGER.error(e.getMessage(), e);
128 throw new IOException(e.getMessage());