0fa327fed1f3d3656cc970ad3c19114f720f487d
[sdc.git] /
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.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.Objects;
36 import java.util.Optional;
37
38 /**
39  * Created by Talio on 12/6/2016.
40  */
41 public class CandidateServiceValidator {
42   public Optional<List<ErrorMessage>> validateFileDataStructure(
43       FilesDataStructure filesDataStructure) {
44     if (Objects.isNull(filesDataStructure)) {
45       return Optional.empty();
46     }
47     if (validateAtLeaseOneModuleExist(filesDataStructure)) {
48       return Optional.of(Arrays.asList(new ErrorMessage(ErrorLevel.ERROR, Messages
49           .NO_MODULES_IN_MANIFEST.getErrorMessage())));
50     }
51
52     List<ErrorMessage> errors = new ArrayList<>();
53     for (Module module : filesDataStructure.getModules()) {
54       validateModuleHaveYaml(errors, module);
55       validateNoVolEnvWithoutVol(errors, module);
56     }
57     return Optional.of(errors);
58   }
59
60
61   private boolean validateAtLeaseOneModuleExist(FilesDataStructure filesDataStructure) {
62     return CollectionUtils.isEmpty(filesDataStructure.getModules());
63   }
64
65   private void validateNoVolEnvWithoutVol(List<ErrorMessage> errors, Module module) {
66     if (StringUtils.isEmpty(module.getVol()) && StringUtils.isNotEmpty(module.getVolEnv())) {
67       errors.add(new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
68           .getErrorWithParameters(Messages.MODULE_IN_MANIFEST_VOL_ENV_NO_VOL.getErrorMessage(),
69               module.getName())));
70     }
71   }
72
73   private void validateModuleHaveYaml(List<ErrorMessage> errors, Module module) {
74     if (StringUtils.isEmpty(module.getYaml())) {
75       errors.add(new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
76           .getErrorWithParameters(Messages.MODULE_IN_MANIFEST_NO_YAML.getErrorMessage(),
77               module.getName())));
78     }
79   }
80 }