Clean up dependencies for London Release
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpGroup.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020,2023 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.concepts;
23
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30 import lombok.Data;
31 import lombok.NoArgsConstructor;
32 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
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;
40
41 /**
42  * Class to represent a PDPGroup, which groups multiple PDPSubGroup entities together for a particular domain.
43  *
44  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
45  */
46 @Data
47 @NoArgsConstructor
48 public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
49     private static final String SUBGROUP_FIELD = "pdpSubgroups";
50
51     private String name;
52     private String description;
53     private PdpState pdpGroupState;
54     private Map<String, String> properties;
55     private List<PdpSubGroup> pdpSubgroups;
56
57     /*
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.
60      */
61
62     /**
63      * Constructs the object, making a deep copy from the source.
64      *
65      * @param source source from which to copy fields
66      */
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));
73     }
74
75     @Override
76     public int compareTo(final PdpGroup other) {
77         return compareNameVersion(this, other);
78     }
79
80     /**
81      * Validates that appropriate fields are populated for an incoming call to the PAP REST API.
82      *
83      * @param updateGroupFlow if the operation is pdp group update
84      * @return the validation result
85      */
86     public ValidationResult validatePapRest(boolean updateGroupFlow) {
87         var result = new BeanValidationResult("group", this);
88
89         /*
90          * Don't care about state, because we override it. Ok if description is null.
91          */
92
93         result.validateNotNull("name", name);
94         result.validateNotNullList(SUBGROUP_FIELD, pdpSubgroups,
95             (PdpSubGroup pdpSubGroup) -> pdpSubGroup.validatePapRest(updateGroupFlow));
96
97         if (pdpSubgroups != null && pdpSubgroups.isEmpty()) {
98             result.addResult(SUBGROUP_FIELD, pdpSubgroups, ValidationStatus.INVALID, "is empty");
99         }
100
101         checkDuplicateSubgroups(result);
102
103         return result;
104     }
105
106     /**
107      * Checks for duplicate subgroups.
108      *
109      * @param result where to place validation results
110      */
111     private void checkDuplicateSubgroups(BeanValidationResult result) {
112         if (pdpSubgroups == null || !result.isValid()) {
113             return;
114         }
115
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()) {
119             return;
120         }
121
122         // different sizes implies duplicates
123         result.addResult(SUBGROUP_FIELD, pdpTypes, ValidationStatus.INVALID, "duplicate subgroups");
124     }
125
126     @Override
127     @GsonJsonIgnore
128     public String getVersion() {
129         // We need to pass a version for keying in the database
130         return PfKey.NULL_KEY_VERSION;
131     }
132
133     @Override
134     @GsonJsonIgnore
135     public void setVersion(String version) {
136         // Just ignore any version that is set
137     }
138 }