[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.logging.context.impl.MdcDataDebugMessage;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Objects;
37 import java.util.Optional;
38
39 /**
40  * Created by Talio on 12/6/2016.
41  */
42 public class CandidateServiceValidator {
43   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
44
45   public Optional<List<ErrorMessage>> validateFileDataStructure(
46       FilesDataStructure filesDataStructure) {
47     if (Objects.isNull(filesDataStructure)) {
48       return Optional.empty();
49     }
50     if (validateAtLeaseOneModuleExist(filesDataStructure)) {
51       return Optional.of(Arrays.asList(new ErrorMessage(ErrorLevel.ERROR, Messages
52           .NO_MODULES_IN_MANIFEST.getErrorMessage())));
53     }
54
55     List<ErrorMessage> errors = new ArrayList<>();
56     for (Module module : filesDataStructure.getModules()) {
57       validateModuleHaveYaml(errors, module);
58       validateNoVolEnvWithoutVol(errors, module);
59     }
60     return Optional.of(errors);
61   }
62
63
64   private boolean validateAtLeaseOneModuleExist(FilesDataStructure filesDataStructure) {
65
66     mdcDataDebugMessage.debugEntryMessage(null, null);
67
68     mdcDataDebugMessage.debugExitMessage(null, null);
69     return CollectionUtils.isEmpty(filesDataStructure.getModules());
70   }
71
72   private void validateNoVolEnvWithoutVol(List<ErrorMessage> errors, Module module) {
73
74     mdcDataDebugMessage.debugEntryMessage(null, null);
75
76     if (StringUtils.isEmpty(module.getVol()) && StringUtils.isNotEmpty(module.getVolEnv())) {
77       errors.add(new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
78           .getErrorWithParameters(Messages.MODULE_IN_MANIFEST_VOL_ENV_NO_VOL.getErrorMessage(),
79               module.getName())));
80     }
81
82     mdcDataDebugMessage.debugExitMessage(null, null);
83   }
84
85   private void validateModuleHaveYaml(List<ErrorMessage> errors, Module module) {
86
87     mdcDataDebugMessage.debugEntryMessage(null, null);
88
89     if (StringUtils.isEmpty(module.getYaml())) {
90       errors.add(new ErrorMessage(ErrorLevel.ERROR, ErrorMessagesFormatBuilder
91           .getErrorWithParameters(Messages.MODULE_IN_MANIFEST_NO_YAML.getErrorMessage(),
92               module.getName())));
93     }
94
95     mdcDataDebugMessage.debugExitMessage(null, null);
96   }
97 }