2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.tosca.csar;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import java.io.IOException;
29 import java.io.InputStream;
31 import static org.openecomp.sdc.tosca.csar.CSARConstants.MAIN_SERVICE_TEMPLATE_MF_FILE_NAME;
32 import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ORIG_PATH_FILE_NAME;
33 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion251.ENTRY_MANIFEST;
34 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntryVersion261.ETSI_ENTRY_MANIFEST;
35 import static org.openecomp.sdc.tosca.csar.ToscaMetadataFileInfo.TOSCA_META_PATH_FILE_NAME;
38 * Offers method utils dealing with the manifest
40 public class ManifestUtils {
42 public ManifestUtils() {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ManifestUtils.class);
48 * Loads a manifest given the file handler and the type to manifest to load.
50 * @param fileContentHandler The package file handler
51 * @param manifestHandler The type of abstract manifest to load
52 * @return The loaded Manifest.
54 public <T extends AbstractOnboardingManifest> Manifest loadManifest(final FileContentHandler fileContentHandler, final T manifestHandler) throws IOException {
55 final Manifest manifest;
57 manifest = getManifest(fileContentHandler, manifestHandler);
58 } catch (final IOException ex) {
59 LOGGER.error("An error occurred while getting the manifest file", ex);
66 * Retrieves the manifest file from the CSAR
68 * @param fileContentHandler contains csar artifacts
69 * @param manifestHandler The type of abstract manifest to load
70 * @return The retrieved Manifest
71 * @throws IOException when TOSCA.meta file or manifest file is invalid
73 public <T extends AbstractOnboardingManifest> Manifest getManifest(FileContentHandler fileContentHandler, T manifestHandler) throws IOException {
74 ToscaMetadata metadata = getMetadata(fileContentHandler);
75 return getManifest(fileContentHandler, getEntryManifestLocation(metadata), manifestHandler);
79 * Retrieves the metadata from the CSAR
81 * @param fileContentHandler contains csar artifacts
82 * @return The retrieved metadata
83 * @throws IOException when TOSCA.meta file or manifest file is invalid
85 public ToscaMetadata getMetadata(FileContentHandler fileContentHandler) throws IOException {
86 ToscaMetadata metadata;
87 if (fileContentHandler.containsFile(TOSCA_META_PATH_FILE_NAME)) {
88 metadata = OnboardingToscaMetadata.parseToscaMetadataFile(fileContentHandler.getFileContentAsStream(TOSCA_META_PATH_FILE_NAME));
89 } else if (fileContentHandler.containsFile(TOSCA_META_ORIG_PATH_FILE_NAME)) {
90 metadata = OnboardingToscaMetadata.parseToscaMetadataFile(fileContentHandler.getFileContentAsStream(TOSCA_META_ORIG_PATH_FILE_NAME));
92 throw new IOException("TOSCA.meta file not found!");
98 * Retrieves the manifest location present in the metadata within the CSAR
100 * @param metadata the CSAR metadata
101 * @return The path of the location of the manifest within the CSAR
103 public String getEntryManifestLocation(final ToscaMetadata metadata) {
104 return metadata.getMetaEntries().containsKey(ETSI_ENTRY_MANIFEST.getName()) ?
105 metadata.getMetaEntries().get(ETSI_ENTRY_MANIFEST.getName()):
106 metadata.getMetaEntries().get(ENTRY_MANIFEST.getName());
110 * Retrieves the manifest given the file handler, the manifest location within the CSAR,
111 * and the type to manifest to load.
113 * @param fileContentHandler The package file handler
114 * @param manifestLocation The path of the location of the manifest within the CSAR
115 * @param manifestHandler The type of abstract manifest to load
116 * @return The loaded Manifest.
118 public <T extends AbstractOnboardingManifest> Manifest getManifest(FileContentHandler fileContentHandler,
119 String manifestLocation, T manifestHandler) throws IOException {
120 try (InputStream manifestInputStream = getManifestInputStream(fileContentHandler, manifestLocation)) {
121 manifestHandler.parse(manifestInputStream);
122 return manifestHandler;
126 private InputStream getManifestInputStream(FileContentHandler handler, String manifestLocation) throws IOException {
128 if (manifestLocation == null || !handler.containsFile(manifestLocation)) {
129 io = handler.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME);
131 io = handler.getFileContentAsStream(manifestLocation);
134 throw new IOException("Manifest file not found!");