b3002abf6e7b24f6e0330ab0ad7d97c19160b98d
[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 import java.util.Optional;
34
35 import org.openecomp.sdc.tosca.csar.Manifest;
36
37 import static org.openecomp.sdc.tosca.csar.CSARConstants.*;
38
39 public class ETSIServiceImpl implements ETSIService {
40
41     private Configuration configuration;
42
43     public ETSIServiceImpl() throws IOException {
44         InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml");
45         if(io == null){
46             throw new IOException("Non Mano configuration not found");
47         }
48         String data = IOUtils.toString(io, StandardCharsets.UTF_8);
49         YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
50         configuration = yamlToObjectConverter.convertFromString(data, Configuration.class);
51     }
52
53     public ETSIServiceImpl(Configuration configuration) {
54         this.configuration = configuration;
55     }
56
57     @Override
58     public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) {
59         Map<String, byte[]> templates = handler.getFiles();
60         return isMetaFilePresent(templates) && hasMetaMandatoryEntries(templates);
61     }
62
63     @Override
64     public void moveNonManoFileToArtifactFolder(FileContentHandler handler, Manifest manifest) {
65         for (Map.Entry<String, List<String>> entry : manifest.getNonManoSources().entrySet()) {
66             String e = entry.getKey();
67             List<String> k = entry.getValue();
68             updateNonManoLocation(handler, e, k);
69         }
70     }
71
72     private void updateNonManoLocation(FileContentHandler handler, String nonManoKey, List<String> sources) {
73         Map<String, byte[]> files = handler.getFiles();
74         for (String key : sources) {
75             if (files.containsKey(key)) {
76                 updateLocation(key, nonManoKey, files);
77             }
78         }
79     }
80
81     private void updateLocation(String key, String nonManoKey, Map<String, byte[]> files){
82         if (nonManoKey == null || nonManoKey.isEmpty()) {
83             return;
84         }
85         Map<String, NonManoType> map = configuration.getNonManoKeyFolderMapping();
86         if (map.containsKey(nonManoKey)) {
87             NonManoType nonManoPair = map.get(nonManoKey);
88             String newLocation = nonManoPair.getType() + "/" +
89                     nonManoPair.getLocation() + "/" + getFileName(key);
90             if (!files.containsKey(newLocation)) {
91                 files.put(newLocation, files.remove(key));
92             }
93         }
94     }
95
96
97     private String getFileName(String key) {
98         return key.substring(key.lastIndexOf('/') + 1);
99     }
100
101     private boolean hasMetaMandatoryEntries(Map<String, byte[]> templates) {
102         Optional<byte[]> meta = templates.entrySet().stream().filter(e -> e.getKey().equals(TOSCA_META_PATH_FILE_NAME)
103                 || e.getKey().equals(TOSCA_META_ORIG_PATH_FILE_NAME)).findFirst().map(Map.Entry::getValue);
104         if (!meta.isPresent()) {
105             return false;
106         }
107         String metaContent = new String(meta.get(), StandardCharsets.UTF_8);
108         return metaContent.contains(TOSCA_META_ENTRY_DEFINITIONS) && metaContent.contains(TOSCA_META_ENTRY_MANIFEST)
109                 && metaContent.contains(TOSCA_META_ENTRY_CHANGE_LOG);
110     }
111
112     private boolean isMetaFilePresent(Map<String, byte[]> handler) {
113         return handler.containsKey(TOSCA_META_PATH_FILE_NAME) || handler.containsKey(TOSCA_META_ORIG_PATH_FILE_NAME);
114     }
115 }