Convert models to JUnit 5
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / DeploymentGroup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019, 2021 AT&T Intellectual Property.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.concepts;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import lombok.Data;
28 import lombok.NoArgsConstructor;
29 import lombok.NonNull;
30 import org.onap.policy.common.parameters.BeanValidationResult;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.onap.policy.common.parameters.ValidationStatus;
33 import org.onap.policy.models.base.PfUtils;
34 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
35
36 /**
37  * Batch modification of a deployment group, which groups multiple DeploymentSubGroup
38  * entities together for a particular domain.
39  */
40 @Data
41 @NoArgsConstructor
42 public class DeploymentGroup {
43     private static final String SUBGROUP_FIELD = "deploymentSubgroups";
44
45     private String name;
46     private List<DeploymentSubGroup> deploymentSubgroups;
47
48     /**
49      * Constructs the object, making a deep copy from the source.
50      *
51      * @param source source from which to copy fields
52      */
53     public DeploymentGroup(@NonNull DeploymentGroup source) {
54         this.name = source.name;
55         this.deploymentSubgroups =
56                         PfUtils.mapList(source.deploymentSubgroups, DeploymentSubGroup::new, new ArrayList<>(0));
57     }
58
59     /**
60      * Validates that appropriate fields are populated for an incoming call to the PAP
61      * REST API.
62      *
63      * @return the validation result
64      */
65     public ValidationResult validatePapRest() {
66         var result = new BeanValidationResult("group", this);
67
68         result.validateNotNull("name", name);
69         result.validateNotNullList(SUBGROUP_FIELD, deploymentSubgroups, DeploymentSubGroup::validatePapRest);
70
71         if (deploymentSubgroups != null && deploymentSubgroups.isEmpty()) {
72             result.addResult(SUBGROUP_FIELD, deploymentSubgroups, ValidationStatus.INVALID, "is empty");
73         }
74
75         checkDuplicateSubgroups(result);
76
77         return result;
78     }
79
80     /**
81      * Checks for duplicate subgroups.
82      *
83      * @param result where to place validation results
84      */
85     private void checkDuplicateSubgroups(BeanValidationResult result) {
86         if (deploymentSubgroups == null || !result.isValid()) {
87             return;
88         }
89
90         /*
91          * Verify that if a subgroup appears more than once, then the second appearance is
92          * not a PATCH, as that would overwrite anything that has appeared before.
93          */
94         Map<String, Action> pdpType2action = new HashMap<>();
95
96         for (DeploymentSubGroup subgrp : deploymentSubgroups) {
97             var action = subgrp.getAction();
98
99             pdpType2action.compute(subgrp.getPdpType(), (pdpType, curact) -> {
100
101                 if (curact != null && action == Action.PATCH) {
102                     var subResult = new BeanValidationResult(pdpType, pdpType);
103                     subResult.addResult("action", action, ValidationStatus.INVALID,
104                                     "incompatible with previous action: " + curact);
105                     var subResult2 = new BeanValidationResult(SUBGROUP_FIELD, subgrp);
106                     subResult2.addResult(subResult);
107                     result.addResult(subResult2);
108                 }
109
110                 return action;
111             });
112         }
113     }
114 }