2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.pdp.concepts;
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.LinkedHashMap;
28 import java.util.List;
30 import java.util.stream.Collectors;
32 import lombok.NoArgsConstructor;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.common.parameters.ValidationResult;
35 import org.onap.policy.common.parameters.ValidationStatus;
36 import org.onap.policy.models.base.PfKey;
37 import org.onap.policy.models.base.PfNameVersion;
38 import org.onap.policy.models.base.PfUtils;
39 import org.onap.policy.models.pdp.enums.PdpState;
42 * Class to represent a PDPGroup, which groups multiple PDPSubGroup entities together for a particular domain.
44 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
48 public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
49 private static final String SUBGROUP_FIELD = "pdpSubgroups";
52 private String description;
53 private PdpState pdpGroupState;
54 private Map<String, String> properties;
55 private List<PdpSubGroup> pdpSubgroups;
58 * Note: removed "@NotNull" annotation from the constructor argument, because it cannot be covered by a junit test,
59 * as the superclass does the check and throws an exception first.
63 * Constructs the object, making a deep copy from the source.
65 * @param source source from which to copy fields
67 public PdpGroup(PdpGroup source) {
68 this.name = source.name;
69 this.description = source.description;
70 this.pdpGroupState = source.pdpGroupState;
71 this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties));
72 this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new, new ArrayList<>(0));
76 public int compareTo(final PdpGroup other) {
77 return compareNameVersion(this, other);
81 * Validates that appropriate fields are populated for an incoming call to the PAP REST API.
83 * @param updateGroupFlow if the operation is pdp group update
84 * @return the validation result
86 public ValidationResult validatePapRest(boolean updateGroupFlow) {
87 var result = new BeanValidationResult("group", this);
90 * Don't care about state, because we override it. Ok if description is null.
93 result.validateNotNull("name", name);
94 result.validateNotNullList(SUBGROUP_FIELD, pdpSubgroups,
95 (PdpSubGroup pdpSubGroup) -> pdpSubGroup.validatePapRest(updateGroupFlow));
97 if (pdpSubgroups != null && pdpSubgroups.isEmpty()) {
98 result.addResult(SUBGROUP_FIELD, pdpSubgroups, ValidationStatus.INVALID, "is empty");
101 checkDuplicateSubgroups(result);
107 * Checks for duplicate subgroups.
109 * @param result where to place validation results
111 private void checkDuplicateSubgroups(BeanValidationResult result) {
112 if (pdpSubgroups == null || !result.isValid()) {
116 // verify that the same subgroup doesn't appear more than once
117 List<String> pdpTypes = pdpSubgroups.stream().map(PdpSubGroup::getPdpType).collect(Collectors.toList());
118 if (pdpSubgroups.size() == new HashSet<>(pdpTypes).size()) {
122 // different sizes implies duplicates
123 result.addResult(SUBGROUP_FIELD, pdpTypes, ValidationStatus.INVALID, "duplicate subgroups");
128 public String getVersion() {
129 // We need to pass a version for keying in the database
130 return PfKey.NULL_KEY_VERSION;
135 public void setVersion(String version) {
136 // Just ignore any version that is set