Add a CSAR validator for model ETSI SOL001 2.5.1
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / main / java / org / openecomp / core / impl / AbstractToscaSolConverter.java
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 package org.openecomp.core.impl;
23
24 import static org.openecomp.core.converter.datatypes.Constants.GLOBAL_ST_NAME;
25 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ENTRY_DEFINITIONS;
26 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
27
28 import java.io.IOException;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Map;
32 import java.util.Set;
33 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.core.converter.ServiceTemplateReaderService;
35 import org.openecomp.core.utilities.file.FileContentHandler;
36 import org.openecomp.sdc.logging.api.Logger;
37 import org.openecomp.sdc.logging.api.LoggerFactory;
38 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
39 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
40 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
41
42 public abstract class AbstractToscaSolConverter extends AbstractToscaConverter {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(AbstractToscaSolConverter.class);
45     private final Set<String> handledDefinitionFilesList = new HashSet<>();
46
47     @Override
48     public ToscaServiceModel convert(FileContentHandler fileContentHandler) throws IOException {
49         Map<String, byte[]> csarFiles = new HashMap<>(fileContentHandler.getFiles());
50         ToscaServiceModel toscaServiceModel = new ToscaServiceModel();
51         Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
52         FileContentHandler artifacts = new FileContentHandler();
53         GlobalSubstitutionServiceTemplate gsst = new GlobalSubstitutionServiceTemplate();
54         String mServiceDefinitionPath = getMainServiceDefinitionFileName(fileContentHandler);
55         handleMainServiceTemplate(csarFiles, serviceTemplates, gsst, mServiceDefinitionPath);
56         handleExternalArtifacts(csarFiles, serviceTemplates, artifacts);
57         handleMetadataFile(csarFiles);
58         updateToscaServiceModel(toscaServiceModel, serviceTemplates, artifacts, gsst, csarFiles, getSimpleName(mServiceDefinitionPath));
59         return toscaServiceModel;
60     }
61
62     private void handleMainServiceTemplate(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates,
63                                            GlobalSubstitutionServiceTemplate gsst, String mServiceDefinitionFileName) {
64         if (mServiceDefinitionFileName != null) {
65             handleServiceTemplate(getSimpleName(mServiceDefinitionFileName), mServiceDefinitionFileName, csarFiles, serviceTemplates);
66             handleImportDefinitions(mServiceDefinitionFileName, csarFiles, gsst);
67         }
68     }
69
70     private void handleExternalArtifacts(Map<String, byte[]> csarFiles, Map<String, ServiceTemplate> serviceTemplates, FileContentHandler artifacts) {
71         for (Map.Entry<String, byte[]> fileEntry : csarFiles.entrySet()) {
72             if (!handledDefinitionFilesList.contains(fileEntry.getKey()) && !isMetadataFile(fileEntry.getKey())) {
73                 if (isGlobalServiceTemplate(fileEntry.getKey())) {
74                     handleServiceTemplate(GLOBAL_ST_NAME, fileEntry.getKey(), csarFiles, serviceTemplates);
75                 } else {
76                     artifacts.addFile(getConcreteArtifactFileName(fileEntry.getKey()), fileEntry.getValue());
77                 }
78             }
79         }
80     }
81
82     private void handleImportDefinitions(final String fileName, final Map<String, byte[]> csarFiles, final GlobalSubstitutionServiceTemplate gsst) {
83         final ToscaDefinitionImportHandler toscaDefinitionImportHandler = new ToscaDefinitionImportHandler(csarFiles, fileName);
84         if (toscaDefinitionImportHandler.hasError()) {
85             throw new InvalidToscaDefinitionImportException(toscaDefinitionImportHandler.getErrors());
86         }
87         final Map<String, ServiceTemplateReaderService> handledImportDefinitionFileMap = toscaDefinitionImportHandler
88             .getHandledImportDefinitionFileMap();
89         handledDefinitionFilesList.addAll(handledImportDefinitionFileMap.keySet());
90         for (final String file : handledDefinitionFilesList) {
91             handleDefinitionTemplate(file, csarFiles, gsst);
92         }
93     }
94
95     private String getMainServiceDefinitionFileName(FileContentHandler contentHandler) throws IOException {
96         try {
97             ToscaMetadata toscaMetadata = OnboardingToscaMetadata
98                 .parseToscaMetadataFile(contentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME));
99             return toscaMetadata.getMetaEntries().get(ENTRY_DEFINITIONS.getName());
100         } catch (IOException e) {
101             LOGGER.error(e.getMessage(), e);
102             throw new IOException(e.getMessage());
103         }
104     }
105
106     private String getSimpleName(String path) {
107         if (path != null && path.contains("/")) {
108             path = path.substring(path.lastIndexOf('/') + 1);
109         }
110         return path;
111     }
112 }