4cb0ac54e19aa66740be917c0e16b865658d32cd
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpGroup.java
1 /*-
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
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.HashSet;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.stream.Collectors;
29 import lombok.Data;
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;
39
40 /**
41  * Class to represent a PDPGroup, which groups multiple PDPSubGroup entities together for a particular domain.
42  *
43  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
44  */
45 @Data
46 @NoArgsConstructor
47 public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
48     private String name;
49     private String description;
50     private PdpState pdpGroupState;
51     private Map<String, String> properties;
52     private List<PdpSubGroup> pdpSubgroups;
53
54     /*
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.
57      */
58
59     /**
60      * Constructs the object, making a deep copy from the source.
61      *
62      * @param source source from which to copy fields
63      */
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);
70     }
71
72     @Override
73     public int compareTo(final PdpGroup other) {
74         return compareNameVersion(this, other);
75     }
76
77     /**
78      * Validates that appropriate fields are populated for an incoming call to the PAP REST API.
79      *
80      * @return the validation result
81      */
82     public ValidationResult validatePapRest() {
83         BeanValidationResult result = new BeanValidationResult("group", this);
84
85         /*
86          * Don't care about state, because we override it. Ok if description is null.
87          */
88
89         result.validateNotNull("name", name);
90         result.validateNotNullList("pdpSubgroups", pdpSubgroups, PdpSubGroup::validatePapRest);
91
92         if (pdpSubgroups != null && pdpSubgroups.isEmpty()) {
93             result.addResult(new ObjectValidationResult("pdpSubgroups", pdpSubgroups, ValidationStatus.INVALID,
94                             "is empty"));
95         }
96
97         checkDuplicateSubgroups(result);
98
99         return result;
100     }
101
102     /**
103      * Checks for duplicate subgroups.
104      *
105      * @param result where to place validation results
106      */
107     private void checkDuplicateSubgroups(BeanValidationResult result) {
108         if (pdpSubgroups == null || !result.isValid()) {
109             return;
110         }
111
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()) {
115             return;
116         }
117
118         // different sizes implies duplicates
119         result.addResult(new ObjectValidationResult("pdpSubgroups", pdpTypes, ValidationStatus.INVALID,
120                         "duplicate subgroups"));
121     }
122
123     @Override
124     public String getVersion() {
125         // We need to pass a version for keying in the database
126         return PfKey.NULL_KEY_VERSION;
127     }
128
129     @Override
130     public void setVersion(String version) {
131         // Just ignore any version that is set
132     }
133 }