[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / main / java / org / openecomp / sdcrests / vsp / rest / mapping / MapValidationResponseToDto.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.sdcrests.vsp.rest.mapping;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26 import org.openecomp.sdc.datatypes.error.ErrorMessage;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.ComponentValidationResult;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.DeploymentFlavorValidationResult;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
32 import org.openecomp.sdcrests.common.mapping.MapErrorCodeToDto;
33 import org.openecomp.sdcrests.common.mapping.MapErrorMessageToDto;
34 import org.openecomp.sdcrests.common.types.ErrorCodeDto;
35 import org.openecomp.sdcrests.common.types.ErrorMessageDto;
36 import org.openecomp.sdcrests.mapping.MappingBase;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentValidationResultDto;
38 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.DeploymentFlavorValidationResultDto;
40 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireValidationResultDto;
41 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
42
43 import java.util.Collection;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Objects;
48 import java.util.Set;
49 import java.util.stream.Collectors;
50
51 public class MapValidationResponseToDto
52     extends MappingBase<ValidationResponse, ValidationResponseDto> {
53   private static Map<String, List<ErrorMessageDto>> mapUploadDataErrors(
54       Map<String, List<ErrorMessage>> uploadDataErrors) {
55     if (MapUtils.isEmpty(uploadDataErrors)) {
56       return null;
57     }
58     return uploadDataErrors.entrySet().stream().collect(
59         Collectors.toMap(entry -> entry.getKey(), entry -> mapErrorMessages(entry.getValue())));
60   }
61
62   private static QuestionnaireValidationResultDto mapQuestionnaireValidationResult(
63       QuestionnaireValidationResult questionnaireValidationResult) {
64     if (Objects.isNull(questionnaireValidationResult)
65         || Objects.isNull(questionnaireValidationResult.getValidationData())) {
66       return null;
67     }
68     QuestionnaireValidationResultDto questionnaireValidationResultDto =
69         new QuestionnaireValidationResultDto();
70     questionnaireValidationResultDto.setValid(questionnaireValidationResult.isValid());
71
72     Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
73     for(CompositionEntityValidationData validationData : questionnaireValidationResult.getValidationData()){
74       validationDataDto.add(new MapCompositionEntityValidationDataToDto().applyMapping
75           (validationData, CompositionEntityValidationDataDto.class));
76     }
77
78     questionnaireValidationResultDto.setValidationData(validationDataDto);
79     return questionnaireValidationResultDto;
80   }
81
82   private static ComponentValidationResultDto mapcomponentValidationResult(
83       ComponentValidationResult componentValidationResult) {
84     if (componentValidationResult == null) {
85       return null;
86     }
87     ComponentValidationResultDto componentValidationResultDto =
88         new ComponentValidationResultDto();
89     componentValidationResultDto.setValid(componentValidationResult.isValid());
90
91     Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
92     for(CompositionEntityValidationData validationData : componentValidationResult.getValidationData()){
93       validationDataDto.add(new MapCompositionEntityValidationDataToDto().applyMapping
94           (validationData, CompositionEntityValidationDataDto.class));
95     }
96
97     componentValidationResultDto.setValidationData(validationDataDto);
98     return componentValidationResultDto;
99   }
100
101   private static DeploymentFlavorValidationResultDto mapdeploymentFlavorValidationResult(
102       DeploymentFlavorValidationResult deploymentFlavorValidationResult) {
103     if (deploymentFlavorValidationResult == null) {
104       return null;
105     }
106     DeploymentFlavorValidationResultDto deploymentFlavorValidationResultDto =
107         new DeploymentFlavorValidationResultDto();
108     deploymentFlavorValidationResultDto.setValid(deploymentFlavorValidationResult.isValid());
109
110     Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
111     for(CompositionEntityValidationData validationData : deploymentFlavorValidationResult.getValidationData()){
112       validationDataDto.add(new MapCompositionEntityValidationDataToDto().applyMapping
113           (validationData, CompositionEntityValidationDataDto.class));
114     }
115
116     deploymentFlavorValidationResultDto.setValidationData(validationDataDto);
117     return deploymentFlavorValidationResultDto;
118   }
119
120   private static List<ErrorMessageDto> mapErrorMessages(List<ErrorMessage> errorMessages) {
121     return errorMessages == null ? null : errorMessages.stream().map(
122         errorMessage -> new MapErrorMessageToDto()
123             .applyMapping(errorMessage, ErrorMessageDto.class)).collect(Collectors.toList());
124   }
125
126   private static Collection<ErrorCodeDto> mapErrorCodes(Collection<ErrorCode> errorCodes) {
127     return CollectionUtils.isEmpty(errorCodes) ? null : errorCodes.stream()
128         .map(errorCode -> new MapErrorCodeToDto().applyMapping(errorCode, ErrorCodeDto.class))
129         .collect(Collectors.toList());
130   }
131
132   @Override
133   public void doMapping(ValidationResponse source, ValidationResponseDto target) {
134     target.setValid(source.isValid());
135     target.setVspErrors(mapErrorCodes(source.getVspErrors()));
136     target.setLicensingDataErrors(mapErrorCodes(source.getLicensingDataErrors()));
137     target.setUploadDataErrors(mapUploadDataErrors(source.getUploadDataErrors()));
138     target.setQuestionnaireValidationResult(
139         mapQuestionnaireValidationResult(source.getQuestionnaireValidationResult()));
140     target.setComponentValidationResult(mapcomponentValidationResult(source.getComponentValidationResult()));
141     target.setDeploymentFlavorValidationResult(mapdeploymentFlavorValidationResult(source.getDeploymentFlavorValidationResult()));
142   }
143 }