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;
34 import org.openecomp.sdc.tosca.csar.Manifest;
35 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
36 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
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;
44 public class ETSIServiceImpl implements ETSIService {
46 private Configuration configuration;
48 public ETSIServiceImpl() throws IOException {
49 InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml");
51 throw new IOException("Non Mano configuration not found");
53 String data = IOUtils.toString(io, StandardCharsets.UTF_8);
54 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
55 configuration = yamlToObjectConverter.convertFromString(data, Configuration.class);
58 public ETSIServiceImpl(Configuration configuration) {
59 this.configuration = configuration;
63 public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) throws IOException {
64 Map<String, byte[]> templates = handler.getFiles();
65 return isMetaFilePresent(templates) && hasMetaMandatoryEntries(getMetadata(handler));
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);
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);
83 throw new IOException("TOSCA.meta file does not exist");
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);
95 private void updateLocation(String key, String nonManoKey, Map<String, byte[]> files){
96 if (nonManoKey == null || nonManoKey.isEmpty()) {
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));
111 private String getFileName(String key) {
112 return key.substring(key.lastIndexOf('/') + 1);
115 private boolean hasMetaMandatoryEntries(InputStream metadataInputStream) throws IOException {
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);
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);