2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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 java.util.HashSet;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.stream.Collectors;
30 import lombok.NoArgsConstructor;
31 import org.onap.policy.common.parameters.BeanValidationResult;
32 import org.onap.policy.common.parameters.ObjectValidationResult;
33 import org.onap.policy.common.parameters.ValidationResult;
34 import org.onap.policy.common.parameters.ValidationStatus;
35 import org.onap.policy.models.base.PfKey;
36 import org.onap.policy.models.base.PfNameVersion;
37 import org.onap.policy.models.base.PfUtils;
38 import org.onap.policy.models.pdp.enums.PdpState;
41 * Class to represent a PDPGroup, which groups multiple PDPSubGroup entities together for a particular domain.
43 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
47 public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
49 private String description;
50 private PdpState pdpGroupState;
51 private Map<String, String> properties;
52 private List<PdpSubGroup> pdpSubgroups;
55 * Note: removed "@NotNull" annotation from the constructor argument, because it cannot be covered by a junit test,
56 * as the superclass does the check and throws an exception first.
60 * Constructs the object, making a deep copy from the source.
62 * @param source source from which to copy fields
64 public PdpGroup(PdpGroup source) {
65 this.name = source.name;
66 this.description = source.description;
67 this.pdpGroupState = source.pdpGroupState;
68 this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties));
69 this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new);
73 public int compareTo(final PdpGroup other) {
74 return compareNameVersion(this, other);
78 * Validates that appropriate fields are populated for an incoming call to the PAP REST API.
80 * @return the validation result
82 public ValidationResult validatePapRest() {
83 BeanValidationResult result = new BeanValidationResult("group", this);
86 * Don't care about state, because we override it. Ok if description is null.
89 result.validateNotNull("name", name);
90 result.validateNotNullList("pdpSubgroups", pdpSubgroups, PdpSubGroup::validatePapRest);
92 if (pdpSubgroups != null && pdpSubgroups.isEmpty()) {
93 result.addResult(new ObjectValidationResult("pdpSubgroups", pdpSubgroups, ValidationStatus.INVALID,
97 checkDuplicateSubgroups(result);
103 * Checks for duplicate subgroups.
105 * @param result where to place validation results
107 private void checkDuplicateSubgroups(BeanValidationResult result) {
108 if (pdpSubgroups == null || !result.isValid()) {
112 // verify that the same subgroup doesn't appear more than once
113 List<String> pdpTypes = pdpSubgroups.stream().map(PdpSubGroup::getPdpType).collect(Collectors.toList());
114 if (pdpSubgroups.size() == new HashSet<>(pdpTypes).size()) {
118 // different sizes implies duplicates
119 result.addResult(new ObjectValidationResult("pdpSubgroups", pdpTypes, ValidationStatus.INVALID,
120 "duplicate subgroups"));
124 public String getVersion() {
125 // We need to pass a version for keying in the database
126 return PfKey.NULL_KEY_VERSION;
130 public void setVersion(String version) {
131 // Just ignore any version that is set