re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / services / utils / CandidateServiceValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.services.utils;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
31
32 import java.util.*;
33
34 /**
35  * Created by Talio on 12/6/2016.
36  */
37 public class CandidateServiceValidator {
38   public Optional<List<ErrorMessage>> validateFileDataStructure(
39       FilesDataStructure filesDataStructure) {
40     if (Objects.isNull(filesDataStructure)) {
41       return Optional.empty();
42     }
43     if (validateAtLeaseOneModuleExist(filesDataStructure)) {
44       return Optional.of(Arrays.asList(new ErrorMessage(ErrorLevel.ERROR, Messages
45           .NO_MODULES_IN_MANIFEST.getErrorMessage())));
46     }
47
48     List<ErrorMessage> errors = new ArrayList<>();
49     for (Module module : filesDataStructure.getModules()) {
50       validateModuleHaveYaml(errors, module);
51       validateNoVolEnvWithoutVol(errors, module);
52     }
53     return Optional.of(errors);
54   }
55
56
57   private boolean validateAtLeaseOneModuleExist(FilesDataStructure filesDataStructure) {
58     return CollectionUtils.isEmpty(filesDataStructure.getModules());
59   }
60
61   private void validateNoVolEnvWithoutVol(List<ErrorMessage> errors, Module module) {
62     if (StringUtils.isEmpty(module.getVol()) && StringUtils.isNotEmpty(module.getVolEnv())) {
63       errors.add(new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
64           .getErrorWithParameters(Messages.MODULE_IN_MANIFEST_VOL_ENV_NO_VOL.getErrorMessage(),
65               module.getName())));
66     }
67   }
68
69   private void validateModuleHaveYaml(List<ErrorMessage> errors, Module module) {
70     if (StringUtils.isEmpty(module.getYaml())) {
71       errors.add(new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
72           .getErrorWithParameters(Messages.MODULE_IN_MANIFEST_NO_YAML.getErrorMessage(),
73               module.getName())));
74     }
75   }
76 }