78a23f43eaf028c245175e21a3d37b4e5bf52a2c
[sdc.git] /
1 /*
2  * -
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
9  *  *
10  *  *      http://www.apache.org/licenses/LICENSE-2.0
11  *  *
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.
17  *  *
18  *  * SPDX-License-Identifier: Apache-2.0
19  *  * ============LICENSE_END=========================================================
20  *
21  */
22
23 package org.openecomp.core.impl;
24
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;
38 import java.util.Map;
39 import java.util.Set;
40
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;
45
46 public class ToscaSolConverterImpl extends AbstractToscaConverter {
47
48     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaSolConverterImpl.class);
49     private final Set<String> handledDefinitionFilesList = new HashSet<>();
50
51     @Override
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;
64     }
65
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);
71     }
72
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);
78                 }else{
79                     artifacts.addFile(
80                             getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
81                 }
82             }
83         }
84     }
85
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("/"));
96                 }
97                 handleImportDefinitions(importPath, csarFiles, parentDir, gsst);
98             }
99         }
100         return;
101     }
102
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;
108             }
109             return 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);
115                 }
116             }
117         }
118         return null;
119     }
120
121     private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException {
122         try {
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());
129         }
130     }
131 }