9984df1cccd7c1cd05d159bf12d592db6fdebca7
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019, Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi;
22
23 import org.apache.commons.io.IOUtils;
24 import org.onap.sdc.tosca.parser.utils.YamlToObjectConverter;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.openecomp.sdc.tosca.csar.Manifest;
35 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
36 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
37
38 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_CHANGE_LOG;
39 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
40 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_MANIFEST;
41 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME;
42 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
43
44 public class ETSIServiceImpl implements ETSIService {
45
46     private Configuration configuration;
47
48     public ETSIServiceImpl() throws IOException {
49         InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml");
50         if(io == null){
51             throw new IOException("Non Mano configuration not found");
52         }
53         String data = IOUtils.toString(io, StandardCharsets.UTF_8);
54         YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
55         configuration = yamlToObjectConverter.convertFromString(data, Configuration.class);
56     }
57
58     public ETSIServiceImpl(Configuration configuration) {
59         this.configuration = configuration;
60     }
61
62     @Override
63     public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) throws IOException {
64         Map<String, byte[]> templates = handler.getFiles();
65         return isMetaFilePresent(templates) && hasMetaMandatoryEntries(getMetadata(handler));
66     }
67
68     @Override
69     public void moveNonManoFileToArtifactFolder(FileContentHandler handler, Manifest manifest) {
70         for (Map.Entry<String, List<String>> entry : manifest.getNonManoSources().entrySet()) {
71             String e = entry.getKey();
72             List<String> k = entry.getValue();
73             updateNonManoLocation(handler, e, k);
74         }
75     }
76
77     private InputStream getMetadata(FileContentHandler contentHandler) throws IOException{
78         if(contentHandler.containsFile(TOSCA_META_PATH_FILE_NAME)){
79             return contentHandler.getFileContent(TOSCA_META_PATH_FILE_NAME);
80         }else if(contentHandler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)){
81             return contentHandler.getFileContent(TOSCA_META_ORIG_PATH_FILE_NAME);
82         }
83         throw new IOException("TOSCA.meta file does not exist");
84     }
85
86     private void updateNonManoLocation(FileContentHandler handler, String nonManoKey, List<String> sources) {
87         Map<String, byte[]> files = handler.getFiles();
88         for (String key : sources) {
89             if (files.containsKey(key)) {
90                 updateLocation(key, nonManoKey, files);
91             }
92         }
93     }
94
95     private void updateLocation(String key, String nonManoKey, Map<String, byte[]> files){
96         if (nonManoKey == null || nonManoKey.isEmpty()) {
97             return;
98         }
99         Map<String, NonManoType> map = configuration.getNonManoKeyFolderMapping();
100         if (map.containsKey(nonManoKey)) {
101             NonManoType nonManoPair = map.get(nonManoKey);
102             String newLocation = nonManoPair.getType() + "/" +
103                     nonManoPair.getLocation() + "/" + getFileName(key);
104             if (!files.containsKey(newLocation)) {
105                 files.put(newLocation, files.remove(key));
106             }
107         }
108     }
109
110
111     private String getFileName(String key) {
112         return key.substring(key.lastIndexOf('/') + 1);
113     }
114
115     private boolean hasMetaMandatoryEntries(InputStream metadataInputStream) throws IOException {
116
117         ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(metadataInputStream);
118         Map<String, String> metaDataEntries = toscaMetadata.getMetaEntries();
119         return metaDataEntries.containsKey(TOSCA_META_ENTRY_DEFINITIONS) && metaDataEntries.containsKey(TOSCA_META_ENTRY_MANIFEST)
120                 && metaDataEntries.containsKey(TOSCA_META_ENTRY_CHANGE_LOG);
121     }
122
123     private boolean isMetaFilePresent(Map<String, byte[]> handler) {
124         return handler.containsKey(TOSCA_META_PATH_FILE_NAME) || handler.containsKey(TOSCA_META_ORIG_PATH_FILE_NAME);
125     }
126 }