Create onboarding validator for ASD VSPs
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / csar / ManifestUtils.java
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  *
19  *
20  */
21
22 package org.openecomp.sdc.tosca.csar;
23
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30
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;
36
37 /**
38  * Offers method utils dealing with the manifest
39  */
40 public class ManifestUtils {
41
42     public ManifestUtils() {
43     }
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ManifestUtils.class);
46
47     /**
48      * Loads a manifest given the file handler and the type to manifest to load.
49      *
50      * @param fileContentHandler The package file handler
51      * @param manifestHandler The type of abstract manifest to load
52      * @return The loaded Manifest.
53      */
54     public <T extends AbstractOnboardingManifest> Manifest loadManifest(final FileContentHandler fileContentHandler, final T manifestHandler) throws IOException {
55         final Manifest manifest;
56         try {
57             manifest = getManifest(fileContentHandler, manifestHandler);
58         } catch (final IOException ex) {
59             LOGGER.error("An error occurred while getting the manifest file", ex);
60             throw ex;
61         }
62         return manifest;
63     }
64
65    /**
66      * Retrieves the manifest file from the CSAR
67      *
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
72      */
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);
76     }
77
78     /**
79      * Retrieves the metadata from the CSAR
80      *
81      * @param fileContentHandler contains csar artifacts
82      * @return The retrieved metadata
83      * @throws IOException when TOSCA.meta file or manifest file is invalid
84      */
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));
91         } else {
92             throw new IOException("TOSCA.meta file not found!");
93         }
94         return metadata;
95     }
96
97     /**
98      * Retrieves the manifest location present in the metadata within the CSAR
99      *
100      * @param metadata the CSAR metadata
101      * @return The path of the location of the manifest within the CSAR
102      */
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());
107     }
108
109     /**
110      * Retrieves the manifest given the file handler, the manifest location within the CSAR,
111      * and the type to manifest to load.
112      *
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.
117      */
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;
123         }
124     }
125
126     private InputStream getManifestInputStream(FileContentHandler handler, String manifestLocation) throws IOException {
127         InputStream io;
128         if (manifestLocation == null || !handler.containsFile(manifestLocation)) {
129             io = handler.getFileContentAsStream(MAIN_SERVICE_TEMPLATE_MF_FILE_NAME);
130         } else {
131             io = handler.getFileContentAsStream(manifestLocation);
132         }
133         if (io == null) {
134             throw new IOException("Manifest file not found!");
135         }
136         return io;
137     }
138 }