aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/validation/SOL004MetaDirectoryValidator.java
blob: a76acf51777d418db3ebec79db22f39cc4171be1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;

import org.openecomp.core.converter.ServiceTemplateReaderService;
import org.openecomp.core.impl.services.ServiceTemplateReaderServiceImpl;
import org.openecomp.core.utilities.file.FileContentHandler;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.datatypes.error.ErrorMessage;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.tosca.csar.Manifest;
import org.openecomp.sdc.tosca.csar.OnboardingToscaMetadata;
import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding;
import org.openecomp.sdc.tosca.csar.ToscaMetadata;
import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.exceptions.InvalidManifestMetadataException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.openecomp.sdc.tosca.csar.CSARConstants.CSAR_VERSION_1_0;
import static org.openecomp.sdc.tosca.csar.CSARConstants.CSAR_VERSION_1_1;
import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_METADATA_LIMIT;
import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA;
import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_VNF_METADATA;
import static org.openecomp.sdc.tosca.csar.CSARConstants.NON_FILE_IMPORT_ATTRIBUTES;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_MANIFEST_FILE_EXT;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_FILE_VERSION_ENTRY;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_CREATED_BY_ENTRY;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_CSAR_VERSION_ENTRY;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_CHANGE_LOG;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_DEFINITIONS;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_LICENSES;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_MANIFEST;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_ENTRY_TESTS;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_FILE_VERSION;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_META_PATH_FILE_NAME;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_TYPE_PNF;
import static org.openecomp.sdc.tosca.csar.CSARConstants.TOSCA_TYPE_VNF;

/**
 * Validates the contents of the package to ensure it complies with the "CSAR with TOSCA-Metadata directory" structure
 * as defined in ETSI GS NFV-SOL 004 v2.5.1.
 *
 */

class SOL004MetaDirectoryValidator implements Validator{

    private static final Logger LOGGER = LoggerFactory.getLogger(SOL004MetaDirectoryValidator.class);

    private final List<ErrorMessage> errorsByFile = new ArrayList<>();
    private final Set<String> verifiedImports = new HashSet<>();

    @Override
    public Map<String, List<ErrorMessage>> validateContent(FileContentHandler contentHandler, List<String> folderList) {
            validateMetaFile(contentHandler, folderList);
            return Collections.unmodifiableMap(getAnyValidationErrors());
    }

    private void validateMetaFile(FileContentHandler contentHandler, List<String> folderList) {
        try {
            ToscaMetadata toscaMetadata = OnboardingToscaMetadata.parseToscaMetadataFile(contentHandler.getFileContent(TOSCA_META_PATH_FILE_NAME));
            if(toscaMetadata.isValid() && hasETSIMetadata(toscaMetadata)) {
                verifyManifestNameAndExtension(toscaMetadata);
                handleMetadataEntries(contentHandler, folderList, toscaMetadata);
            }else {
                errorsByFile.addAll(toscaMetadata.getErrors());
            }
        }catch (IOException e){
            reportError(ErrorLevel.ERROR, Messages.METADATA_PARSER_INTERNAL.getErrorMessage());
            LOGGER.error(Messages.METADATA_PARSER_INTERNAL.getErrorMessage(), e.getMessage(), e);
        }
    }

    private void verifyManifestNameAndExtension(ToscaMetadata toscaMetadata) {
        Map<String, String> entries = toscaMetadata.getMetaEntries();
        String manifestFileName = getFileName(entries.get(TOSCA_META_ENTRY_MANIFEST));
        String manifestExtension = getFileExtension(entries.get(TOSCA_META_ENTRY_MANIFEST));
        String mainDefinitionFileName= getFileName(entries.get(TOSCA_META_ENTRY_DEFINITIONS));
        if(!(TOSCA_MANIFEST_FILE_EXT).equals(manifestExtension)){
            reportError(ErrorLevel.ERROR, Messages.MANIFEST_INVALID_EXT.getErrorMessage());
        }
        if(!mainDefinitionFileName.equals(manifestFileName)){
            reportError(ErrorLevel.ERROR, Messages.MANIFEST_INVALID_NAME.getErrorMessage());
        }
    }

    public String getFileExtension(String filePath){
        return filePath.substring(filePath.lastIndexOf(".") + 1);
    }

    private String getFileName(String filePath){
        return filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
    }

    private boolean hasETSIMetadata(ToscaMetadata toscaMetadata){
        Map<String, String> entries = toscaMetadata.getMetaEntries();
        return hasEntry(entries, TOSCA_META_FILE_VERSION_ENTRY)
                && hasEntry(entries, TOSCA_META_CSAR_VERSION_ENTRY)
                && hasEntry(entries, TOSCA_META_CREATED_BY_ENTRY);
    }

    private boolean hasEntry(Map<String, String> entries, String mandatoryEntry) {
        if (!entries.containsKey(mandatoryEntry)) {
            reportError(ErrorLevel.ERROR, String.format(Messages.METADATA_MISSING_ENTRY.getErrorMessage(),mandatoryEntry));
            return false;
        }
        return true;
    }

    private void handleMetadataEntries(FileContentHandler contentHandler, List<String> folderList, ToscaMetadata toscaMetadata) {
        for(Map.Entry entry: toscaMetadata.getMetaEntries().entrySet()){
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            switch (key){
                case TOSCA_META_FILE_VERSION_ENTRY:
                case TOSCA_META_CSAR_VERSION_ENTRY:
                case TOSCA_META_CREATED_BY_ENTRY:
                    verifyMetadataEntryVersions(key, value);
                    break;
                case TOSCA_META_ENTRY_DEFINITIONS:
                    validateDefinitionFile(contentHandler, value);
                    break;
                case TOSCA_META_ENTRY_MANIFEST:
                    validateManifestFile(contentHandler, value);
                    break;
                case TOSCA_META_ENTRY_CHANGE_LOG:
                    validateChangeLog(contentHandler, value);
                    break;
                case TOSCA_META_ENTRY_TESTS:
                case TOSCA_META_ENTRY_LICENSES:
                    validateOtherEntries(folderList, value);
                    break;
                default:
                    errorsByFile.add(new ErrorMessage(ErrorLevel.ERROR, String.format(Messages.METADATA_UNSUPPORTED_ENTRY.getErrorMessage(), entry)));
                    LOGGER.warn(Messages.METADATA_UNSUPPORTED_ENTRY.getErrorMessage(), entry);
                    break;
            }

        }
    }


    private void verifyMetadataEntryVersions(String key, String version) {
        if(!(isValidTOSCAVersion(key,version) || isValidCSARVersion(key, version) || TOSCA_META_CREATED_BY_ENTRY.equals(key))) {
            errorsByFile.add(new ErrorMessage(ErrorLevel.ERROR, String.format(Messages.METADATA_INVALID_VERSION.getErrorMessage(), key, version)));
            LOGGER.error("{}: key {} - value {} ", Messages.METADATA_INVALID_VERSION.getErrorMessage(), key, version);
        }
    }

    private boolean isValidTOSCAVersion(String key, String version){
        return TOSCA_META_FILE_VERSION_ENTRY.equals(key) && TOSCA_META_FILE_VERSION.equals(version);
    }

    private boolean isValidCSARVersion(String value, String version){
        return TOSCA_META_CSAR_VERSION_ENTRY.equals(value) && (CSAR_VERSION_1_1.equals(version)
                || CSAR_VERSION_1_0.equals(version));
    }

    private void validateDefinitionFile(FileContentHandler contentHandler, String filePath) {
        Set<String> existingFiles = contentHandler.getFileList();

        if (verifyFileExists(existingFiles, filePath)) {
            byte[] definitionFile = getFileContent(filePath, contentHandler);
            handleImports(contentHandler, filePath, existingFiles, definitionFile);
        }else{
            reportError(ErrorLevel.ERROR, String.format(Messages.MISSING_DEFINITION_FILE.getErrorMessage(), filePath));
        }
    }

    private void handleImports(FileContentHandler contentHandler, String filePath, Set<String> existingFiles,
                               byte[] definitionFile) {
        try {
            ServiceTemplateReaderService readerService = new ServiceTemplateReaderServiceImpl(definitionFile);
            List<Object> imports = (readerService).getImports();
            for (Object o : imports) {
                String rootDir = "/";
                if (filePath.contains("/")) {
                    rootDir = filePath.substring(0, filePath.lastIndexOf("/"));
                }
                String verifiedFile = verifyImport(existingFiles, o, rootDir);
                if (verifiedFile != null && !verifiedImports.contains(verifiedFile)) {
                    verifiedImports.add(verifiedFile);
                    handleImports(contentHandler, verifiedFile, existingFiles, getFileContent(verifiedFile,
                            contentHandler));
                }
            }
        }
        catch (Exception  e){
            reportError(ErrorLevel.ERROR, String.format(Messages.INVALID_YAML_FORMAT.getErrorMessage(), e.getMessage()));
            LOGGER.error("{}", Messages.INVALID_YAML_FORMAT_REASON, e.getMessage(), e);
        }
    }

    private String verifyImport(Set<String> existingFiles, Object o, String parentDir) {
        if(o instanceof String){
            String filePath = ((String) o);
            if(!filePath.contains("/")){
                filePath = parentDir + "/" + filePath;
            }
            if(!verifyFileExists(existingFiles, filePath)){
                reportError(ErrorLevel.ERROR, String.format(Messages.MISSING_IMPORT_FILE.getErrorMessage(), (String) o));
                return null;
            }
            return filePath;
        } else if(o instanceof Map){
            Map<String, Object> o1 = (Map)o;
            for(Map.Entry<String, Object> entry: o1.entrySet()){
                if(NON_FILE_IMPORT_ATTRIBUTES.stream().noneMatch(attr -> entry.getKey().equals(attr))){
                    verifyImport(existingFiles, entry.getValue(), parentDir);
                }
            }
        }else {
            reportError(ErrorLevel.ERROR, Messages.INVALID_IMPORT_STATEMENT.getErrorMessage());
        }
        return null;
    }

    private boolean verifyFileExists(Set<String> existingFiles, String filePath){
        return existingFiles.contains(filePath);
    }

    private byte[] getFileContent(String filename, FileContentHandler contentHandler){
        Map<String, byte[]> files = contentHandler.getFiles();
        return files.get(filename);
    }

    private void validateManifestFile(FileContentHandler contentHandler, String filePath){
        final Set<String> exitingFiles = contentHandler.getFileList();
        if(verifyFileExists(exitingFiles, filePath)) {
            Manifest onboardingManifest = new SOL004ManifestOnboarding();
            onboardingManifest.parse(contentHandler.getFileContent(filePath));
            if(onboardingManifest.isValid()){
                try {
                    verifyManifestMetadata(onboardingManifest.getMetadata());
                }catch (InvalidManifestMetadataException e){
                   reportError(ErrorLevel.ERROR, e.getMessage());
                   LOGGER.error(e.getMessage(), e);
                }
                verifySourcesExists(exitingFiles, onboardingManifest);
            }else{
                List<String> manifestErrors = onboardingManifest.getErrors();
                for(String error: manifestErrors){
                    reportError(ErrorLevel.ERROR, error);
                }
            }
        }else {
            reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_NOT_EXIST.getErrorMessage(), filePath));
        }
    }

    private void verifyManifestMetadata(Map<String, String> metadata) {
        if(metadata.size() != MANIFEST_METADATA_LIMIT){
            reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_METADATA_DOES_NOT_MATCH_LIMIT.getErrorMessage(),
                    MANIFEST_METADATA_LIMIT));
        }
        if(isPnfMetadata(metadata)){
            handlePnfMetadataEntries(metadata);
        }else {
            handleVnfMetadataEntries(metadata);
        }
    }

    private boolean isPnfMetadata(Map<String, String> metadata) {
        String metadataType = "";
        for(String key: metadata.keySet()) {
            if(metadataType.isEmpty()){
                 metadataType = key.contains(TOSCA_TYPE_PNF) ? TOSCA_TYPE_PNF : TOSCA_TYPE_VNF;
            }else if(!key.contains(metadataType)){
                throw new InvalidManifestMetadataException(Messages.MANIFEST_METADATA_INVALID_ENTRY.getErrorMessage());
            }
        }
        return TOSCA_TYPE_PNF.equals(metadataType);
    }

    private void handleVnfMetadataEntries(Map<String, String> metadata) {
        for (String requiredVnfEntry : MANIFEST_VNF_METADATA) {
            if (!metadata.containsKey(requiredVnfEntry)) {
                reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_METADATA_MISSING_ENTRY.getErrorMessage(), requiredVnfEntry));
            }
        }
    }

    private void handlePnfMetadataEntries(Map<String, String> metadata) {
        for (String requiredPnfEntry : MANIFEST_PNF_METADATA) {
            if (!metadata.containsKey(requiredPnfEntry)) {
                reportError(ErrorLevel.ERROR, String.format(Messages.MANIFEST_METADATA_MISSING_ENTRY.getErrorMessage(), requiredPnfEntry));
            }
        }
    }

    private void verifySourcesExists(Set<String> exitingFiles, Manifest onboardingManifest) {
        List<String> sources = filterSources(onboardingManifest.getSources());
        Map<String, List<String>> nonManoArtifacts = onboardingManifest.getNonManoSources();
        verifyFilesExist(exitingFiles, sources);
        for (Map.Entry entry : nonManoArtifacts.entrySet()) {
            verifyFilesExist(exitingFiles, filterSources((List)entry.getValue()));
        }
    }

    private List<String> filterSources(List<String> source){
        return source.stream()
                .filter(this::externalFileReferences)
                .collect(Collectors.toList());
    }

    private boolean externalFileReferences(String filePath){
        return !filePath.contains("://");
    }

    private void validateOtherEntries(List<String> folderList, String folderPath){
        if(!verifyFoldersExist(folderList, folderPath))
            reportError(ErrorLevel.ERROR, String.format(Messages.METADATA_MISSING_OPTIONAL_FOLDERS.getErrorMessage(),
                    folderPath));
    }

    private boolean verifyFoldersExist(List<String> folderList, String folderPath){
        return folderList.contains(folderPath + "/");
    }

    private void verifyFilesExist(Set<String> existingFiles, List<String> sources){
        for(String file: sources){
            if(!verifyFileExists(existingFiles, file)){
                reportError(ErrorLevel.ERROR, String.format(Messages.MISSING_ARTIFACT.getErrorMessage(), file));
            }

        }
    }

    private void validateChangeLog(FileContentHandler contentHandler, String filePath){
        if(!verifyFileExists(contentHandler.getFileList(), filePath)){
            reportError(ErrorLevel.ERROR, String.format(Messages.MISSING_ARTIFACT.getErrorMessage(), filePath));
        }
    }

    private void reportError(ErrorLevel errorLevel, String errorMessage){
        errorsByFile.add(new ErrorMessage(errorLevel, errorMessage));
    }

    private Map<String, List<ErrorMessage>> getAnyValidationErrors(){

        if(errorsByFile.isEmpty()){
            return Collections.emptyMap();
        }
        Map<String, List<ErrorMessage>> errors = new HashMap<>();
        errors.put(SdcCommon.UPLOAD_FILE, errorsByFile);
        return errors;
    }
}