Merge "Refactor optimization policies"
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / DeploymentGroup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.ObjectValidationResult;
32 import org.onap.policy.common.parameters.ValidationResult;
33 import org.onap.policy.common.parameters.ValidationStatus;
34 import org.onap.policy.models.base.PfUtils;
35 import org.onap.policy.models.pdp.concepts.DeploymentSubGroup.Action;
36
37 /**
38  * Batch modification of a deployment group, which groups multiple DeploymentSubGroup
39  * entities together for a particular domain.
40  */
41 @Data
42 @NoArgsConstructor
43 public class DeploymentGroup {
44     private static final String SUBGROUP_FIELD = "deploymentSubgroups";
45
46     private String name;
47     private List<DeploymentSubGroup> deploymentSubgroups;
48
49     /**
50      * Constructs the object, making a deep copy from the source.
51      *
52      * @param source source from which to copy fields
53      */
54     public DeploymentGroup(@NonNull DeploymentGroup source) {
55         this.name = source.name;
56         this.deploymentSubgroups =
57                         PfUtils.mapList(source.deploymentSubgroups, DeploymentSubGroup::new, new ArrayList<>(0));
58     }
59
60     /**
61      * Validates that appropriate fields are populated for an incoming call to the PAP
62      * REST API.
63      *
64      * @return the validation result
65      */
66     public ValidationResult validatePapRest() {
67         BeanValidationResult result = new BeanValidationResult("group", this);
68
69         result.validateNotNull("name", name);
70         result.validateNotNullList(SUBGROUP_FIELD, deploymentSubgroups, DeploymentSubGroup::validatePapRest);
71
72         if (deploymentSubgroups != null && deploymentSubgroups.isEmpty()) {
73             result.addResult(new ObjectValidationResult(SUBGROUP_FIELD, deploymentSubgroups, ValidationStatus.INVALID,
74                             "is empty"));
75         }
76
77         checkDuplicateSubgroups(result);
78
79         return result;
80     }
81
82     /**
83      * Checks for duplicate subgroups.
84      *
85      * @param result where to place validation results
86      */
87     private void checkDuplicateSubgroups(BeanValidationResult result) {
88         if (deploymentSubgroups == null || !result.isValid()) {
89             return;
90         }
91
92         /*
93          * Verify that if a subgroup appears more than once, then the second appearance is
94          * not a PATCH, as that would overwrite anything that has appeared before.
95          */
96         Map<String, Action> pdpType2action = new HashMap<>();
97
98         for (DeploymentSubGroup subgrp : deploymentSubgroups) {
99             Action action = subgrp.getAction();
100
101             pdpType2action.compute(subgrp.getPdpType(), (pdpType, curact) -> {
102
103                 if (curact != null && action == Action.PATCH) {
104                     BeanValidationResult subResult = new BeanValidationResult(pdpType, pdpType);
105                     subResult.addResult(new ObjectValidationResult("action", action, ValidationStatus.INVALID,
106                                     "incompatible with previous action: " + curact));
107                     BeanValidationResult subResult2 = new BeanValidationResult(SUBGROUP_FIELD, subgrp);
108                     subResult2.addResult(subResult);
109                     result.addResult(subResult2);
110                 }
111
112                 return action;
113             });
114         }
115     }
116 }