Merge "Added VFModule count"
[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 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;
29 import java.util.Map;
30 import java.util.stream.Collectors;
31 import lombok.Data;
32 import lombok.NoArgsConstructor;
33 import org.onap.policy.common.parameters.BeanValidationResult;
34 import org.onap.policy.common.parameters.ObjectValidationResult;
35 import org.onap.policy.common.parameters.ValidationResult;
36 import org.onap.policy.common.parameters.ValidationStatus;
37 import org.onap.policy.models.base.PfKey;
38 import org.onap.policy.models.base.PfNameVersion;
39 import org.onap.policy.models.base.PfUtils;
40 import org.onap.policy.models.pdp.enums.PdpState;
41
42 /**
43  * Class to represent a PDPGroup, which groups multiple PDPSubGroup entities together for a particular domain.
44  *
45  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
46  */
47 @Data
48 @NoArgsConstructor
49 public class PdpGroup implements PfNameVersion, Comparable<PdpGroup> {
50     private static final String SUBGROUP_FIELD = "pdpSubgroups";
51
52     private String name;
53     private String description;
54     private PdpState pdpGroupState;
55     private Map<String, String> properties;
56     private List<PdpSubGroup> pdpSubgroups;
57
58     /*
59      * Note: removed "@NotNull" annotation from the constructor argument, because it cannot be covered by a junit test,
60      * as the superclass does the check and throws an exception first.
61      */
62
63     /**
64      * Constructs the object, making a deep copy from the source.
65      *
66      * @param source source from which to copy fields
67      */
68     public PdpGroup(PdpGroup source) {
69         this.name = source.name;
70         this.description = source.description;
71         this.pdpGroupState = source.pdpGroupState;
72         this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties));
73         this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new, new ArrayList<>(0));
74     }
75
76     @Override
77     public int compareTo(final PdpGroup other) {
78         return compareNameVersion(this, other);
79     }
80
81     /**
82      * Validates that appropriate fields are populated for an incoming call to the PAP REST API.
83      *
84      * @return the validation result
85      */
86     public ValidationResult validatePapRest() {
87         BeanValidationResult 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, PdpSubGroup::validatePapRest);
95
96         if (pdpSubgroups != null && pdpSubgroups.isEmpty()) {
97             result.addResult(new ObjectValidationResult(SUBGROUP_FIELD, pdpSubgroups, ValidationStatus.INVALID,
98                             "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(new ObjectValidationResult(SUBGROUP_FIELD, pdpTypes, ValidationStatus.INVALID,
124                         "duplicate subgroups"));
125     }
126
127     @Override
128     @JsonIgnore
129     public String getVersion() {
130         // We need to pass a version for keying in the database
131         return PfKey.NULL_KEY_VERSION;
132     }
133
134     @Override
135     @JsonIgnore
136     public void setVersion(String version) {
137         // Just ignore any version that is set
138     }
139 }