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
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.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;
38 * Batch modification of a deployment group, which groups multiple DeploymentSubGroup
39 * entities together for a particular domain.
43 public class DeploymentGroup {
44 private static final String SUBGROUP_FIELD = "deploymentSubgroups";
47 private List<DeploymentSubGroup> deploymentSubgroups;
50 * Constructs the object, making a deep copy from the source.
52 * @param source source from which to copy fields
54 public DeploymentGroup(@NonNull DeploymentGroup source) {
55 this.name = source.name;
56 this.deploymentSubgroups =
57 PfUtils.mapList(source.deploymentSubgroups, DeploymentSubGroup::new, new ArrayList<>(0));
61 * Validates that appropriate fields are populated for an incoming call to the PAP
64 * @return the validation result
66 public ValidationResult validatePapRest() {
67 BeanValidationResult result = new BeanValidationResult("group", this);
69 result.validateNotNull("name", name);
70 result.validateNotNullList(SUBGROUP_FIELD, deploymentSubgroups, DeploymentSubGroup::validatePapRest);
72 if (deploymentSubgroups != null && deploymentSubgroups.isEmpty()) {
73 result.addResult(new ObjectValidationResult(SUBGROUP_FIELD, deploymentSubgroups, ValidationStatus.INVALID,
77 checkDuplicateSubgroups(result);
83 * Checks for duplicate subgroups.
85 * @param result where to place validation results
87 private void checkDuplicateSubgroups(BeanValidationResult result) {
88 if (deploymentSubgroups == null || !result.isValid()) {
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.
96 Map<String, Action> pdpType2action = new HashMap<>();
98 for (DeploymentSubGroup subgrp : deploymentSubgroups) {
99 Action action = subgrp.getAction();
101 pdpType2action.compute(subgrp.getPdpType(), (pdpType, curact) -> {
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);