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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.pdp.concepts;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
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;
37 * Batch modification of a deployment group, which groups multiple DeploymentSubGroup
38 * entities together for a particular domain.
42 public class DeploymentGroup {
43 private static final String SUBGROUP_FIELD = "deploymentSubgroups";
46 private List<DeploymentSubGroup> deploymentSubgroups;
49 * Constructs the object, making a deep copy from the source.
51 * @param source source from which to copy fields
53 public DeploymentGroup(@NonNull DeploymentGroup source) {
54 this.name = source.name;
55 this.deploymentSubgroups =
56 PfUtils.mapList(source.deploymentSubgroups, DeploymentSubGroup::new, new ArrayList<>(0));
60 * Validates that appropriate fields are populated for an incoming call to the PAP
63 * @return the validation result
65 public ValidationResult validatePapRest() {
66 BeanValidationResult result = new BeanValidationResult("group", this);
68 result.validateNotNull("name", name);
69 result.validateNotNullList(SUBGROUP_FIELD, deploymentSubgroups, DeploymentSubGroup::validatePapRest);
71 if (deploymentSubgroups != null && deploymentSubgroups.isEmpty()) {
72 result.addResult(SUBGROUP_FIELD, deploymentSubgroups, ValidationStatus.INVALID, "is empty");
75 checkDuplicateSubgroups(result);
81 * Checks for duplicate subgroups.
83 * @param result where to place validation results
85 private void checkDuplicateSubgroups(BeanValidationResult result) {
86 if (deploymentSubgroups == null || !result.isValid()) {
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.
94 Map<String, Action> pdpType2action = new HashMap<>();
96 for (DeploymentSubGroup subgrp : deploymentSubgroups) {
97 Action action = subgrp.getAction();
99 pdpType2action.compute(subgrp.getPdpType(), (pdpType, curact) -> {
101 if (curact != null && action == Action.PATCH) {
102 BeanValidationResult subResult = new BeanValidationResult(pdpType, pdpType);
103 subResult.addResult("action", action, ValidationStatus.INVALID,
104 "incompatible with previous action: " + curact);
105 BeanValidationResult subResult2 = new BeanValidationResult(SUBGROUP_FIELD, subgrp);
106 subResult2.addResult(subResult);
107 result.addResult(subResult2);