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;
26 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
27 import org.openecomp.sdc.tosca.csar.Manifest;
28 import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
29 import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding;
30 import org.openecomp.sdc.tosca.csar.ToscaMetadata;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.util.List;
38 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
39 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA;
40 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
41 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_CHANGE_LOG;
42 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ETSI_ENTRY_MANIFEST;
43 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME;
44 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
46 public class ETSIServiceImpl implements ETSIService {
48 private Configuration configuration;
50 public ETSIServiceImpl() throws IOException {
51 InputStream io = getClass().getClassLoader().getResourceAsStream("nonManoConfig.yaml");
53 throw new IOException("Non Mano configuration not found");
55 String data = IOUtils.toString(io, StandardCharsets.UTF_8);
56 YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
57 configuration = yamlToObjectConverter.convertFromString(data, Configuration.class);
60 public ETSIServiceImpl(Configuration configuration) {
61 this.configuration = configuration;
65 public boolean isSol004WithToscaMetaDirectory(FileContentHandler handler) throws IOException {
66 Map<String, byte[]> templates = handler.getFiles();
67 return isMetaFilePresent(templates) && hasMetaMandatoryEntries(getMetadata(handler));
71 public void moveNonManoFileToArtifactFolder(FileContentHandler handler, Manifest manifest) {
72 for (Map.Entry<String, List<String>> entry : manifest.getNonManoSources().entrySet()) {
73 String e = entry.getKey();
74 List<String> k = entry.getValue();
75 updateNonManoLocation(handler, e, k);
80 private void updateNonManoLocation(FileContentHandler handler, String nonManoKey, List<String> sources) {
81 Map<String, byte[]> files = handler.getFiles();
82 for (String key : sources) {
83 if (files.containsKey(key)) {
84 updateLocation(key, nonManoKey, files);
89 private void updateLocation(String key, String nonManoKey, Map<String, byte[]> files) {
90 if (nonManoKey == null || nonManoKey.isEmpty()) {
93 Map<String, NonManoType> map = configuration.getNonManoKeyFolderMapping();
94 if (map.containsKey(nonManoKey)) {
95 NonManoType nonManoPair = map.get(nonManoKey);
96 String newLocation = nonManoPair.getType() + "/" +
97 nonManoPair.getLocation() + "/" + getFileName(key);
98 if (!files.containsKey(newLocation)) {
99 files.put(newLocation, files.remove(key));
104 private String getFileName(String key) {
105 return key.substring(key.lastIndexOf('/') + 1);
108 private boolean hasMetaMandatoryEntries(ToscaMetadata toscaMetadata) {
109 Map<String, String> metaDataEntries = toscaMetadata.getMetaEntries();
110 return metaDataEntries.containsKey(TOSCA_META_ENTRY_DEFINITIONS) && metaDataEntries.containsKey(TOSCA_META_ETSI_ENTRY_MANIFEST)
111 && metaDataEntries.containsKey(TOSCA_META_ETSI_ENTRY_CHANGE_LOG);
114 private boolean isMetaFilePresent(Map<String, byte[]> handler) {
115 return handler.containsKey(TOSCA_META_PATH_FILE_NAME) || handler.containsKey(TOSCA_META_ORIG_PATH_FILE_NAME);
118 public ResourceTypeEnum getResourceType(FileContentHandler handler) throws IOException {
119 ToscaMetadata metadata = getMetadata(handler);
120 Manifest manifest = getManifest(handler, metadata.getMetaEntries().get(TOSCA_META_ETSI_ENTRY_MANIFEST));
121 return getResourceType(manifest);
124 public ResourceTypeEnum getResourceType(Manifest manifest) {
125 // Valid manifest should contain whether vnf or pnf related metadata data exclusively in SOL004 standard,
126 // validation of manifest done during package upload stage
127 if (manifest != null && !manifest.getMetadata().isEmpty()
128 && MANIFEST_PNF_METADATA.stream().anyMatch(e -> manifest.getMetadata().containsKey(e))) {
129 return ResourceTypeEnum.PNF;
131 // VNF is default resource type
132 return ResourceTypeEnum.VF;
135 public Manifest getManifest(FileContentHandler handler) throws IOException {
136 ToscaMetadata metadata = getMetadata(handler);
137 return getManifest(handler, metadata.getMetaEntries().get(TOSCA_META_ETSI_ENTRY_MANIFEST));
140 private Manifest getManifest(FileContentHandler handler, String manifestLocation) throws IOException {
141 try(InputStream manifestInputStream = getManifestInputStream(handler, manifestLocation)) {
142 Manifest onboardingManifest = new SOL004ManifestOnboarding();
143 onboardingManifest.parse(manifestInputStream);
144 return onboardingManifest;
148 private ToscaMetadata getMetadata(FileContentHandler handler) throws IOException {
149 ToscaMetadata metadata;
150 if (handler.containsFile(TOSCA_META_PATH_FILE_NAME)) {
151 metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContent(TOSCA_META_PATH_FILE_NAME));
152 } else if (handler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) {
153 metadata = OnboardingToscaMetadata.parseToscaMetadataFile(handler.getFileContent(TOSCA_META_ORIG_PATH_FILE_NAME));
155 throw new IOException("TOSCA.meta file not found!");
160 private InputStream getManifestInputStream(FileContentHandler handler, String manifestLocation) throws IOException {
162 if (manifestLocation == null || !handler.containsFile(manifestLocation)) {
163 io = handler.getFileContent(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME);
165 io = handler.getFileContent(manifestLocation);
169 throw new IOException("Manifest file not found!");