2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi;
23 import org.apache.commons.io.IOUtils;
24 import org.onap.sdc.tosca.parser.utils.YamlToObjectConverter;
25 import org.openecomp.core.utilities.file.FileContentHandler;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.nio.charset.StandardCharsets;
31 import java.util.List;
33 import java.util.Optional;
35 import org.openecomp.sdc.tosca.csar.Manifest;
37 import static org.openecomp.sdc.tosca.csar.CSARConstants.*;
39 public class ETSIServiceImpl implements ETSIService {
41 private Configuration configuration;
43 public ETSIServiceImpl() throws IOException {
44 InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml");
46 throw new IOException("Non Mano configuration not found");
48 String data = IOUtils.toString(io, StandardCharsets.UTF_8);
49 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
50 configuration = yamlToObjectConverter.convertFromString(data, Configuration.class);
53 public ETSIServiceImpl(Configuration configuration) {
54 this.configuration = configuration;
58 public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) {
59 Map<String, byte[]> templates = handler.getFiles();
60 return isMetaFilePresent(templates) && hasMetaMandatoryEntries(templates);
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);
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);
81 private void updateLocation(String key, String nonManoKey, Map<String, byte[]> files){
82 if (nonManoKey == null || nonManoKey.isEmpty()) {
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));
97 private String getFileName(String key) {
98 return key.substring(key.lastIndexOf('/') + 1);
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()) {
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);
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);