53bf3c1c63848704aef59d3e96622225f90b29e6
[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. All rights reserved.
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.Set;
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.PfNameVersion;
36 import org.onap.policy.models.base.PfUtils;
37 import org.onap.policy.models.pdp.enums.PdpState;
38
39 /**
40  * Class to represent a PDPGroup, which groups multiple PDPSubGroup entities together for
41  * 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     /**
49      * In the future, we'll eliminate the "version" field. Until then, the version of
50      * every group should always be this fixed value.
51      */
52     public static final String VERSION = "1.0.0";
53
54     private String name;
55     private String version;
56     private String description;
57     private PdpState pdpGroupState;
58     private Map<String, String> properties;
59     private List<PdpSubGroup> pdpSubgroups;
60
61     /*
62      * Note: removed "@NotNull" annotation from the constructor argument, because it
63      * cannot be covered by a junit test, as the superclass does the check and throws an
64      * exception first.
65      */
66
67     /**
68      * Constructs the object, making a deep copy from the source.
69      *
70      * @param source source from which to copy fields
71      */
72     public PdpGroup(PdpGroup source) {
73         this.name = source.name;
74         this.version = source.version;
75         this.description = source.description;
76         this.pdpGroupState = source.pdpGroupState;
77         this.properties = (source.properties == null ? null : new LinkedHashMap<>(source.properties));
78         this.pdpSubgroups = PfUtils.mapList(source.pdpSubgroups, PdpSubGroup::new);
79     }
80
81     @Override
82     public int compareTo(final PdpGroup other) {
83         return compareNameVersion(this, other);
84     }
85
86     /**
87      * Validates that appropriate fields are populated for an incoming call to the PAP
88      * REST API.
89      *
90      * @return the validation result
91      */
92     public ValidationResult validatePapRest() {
93         BeanValidationResult result = new BeanValidationResult("group", this);
94
95         /*
96          * Don't care about version or state, because we override them. Ok if description
97          * is null.
98          */
99
100         result.validateNotNull("name", name);
101         result.validateNotNullList("pdpSubgroups", pdpSubgroups, PdpSubGroup::validatePapRest);
102
103         checkDuplicateSubgroups(result);
104
105         return result;
106     }
107
108     /**
109      * Checks for duplicate subgroups.
110      *
111      * @param result where to place validation results
112      */
113     private void checkDuplicateSubgroups(BeanValidationResult result) {
114         if (pdpSubgroups == null) {
115             return;
116         }
117
118         Set<String> set = new HashSet<>();
119
120         for (PdpSubGroup subgrp : pdpSubgroups) {
121             if (subgrp == null) {
122                 continue;
123             }
124
125             String pdpType = subgrp.getPdpType();
126             if (pdpType == null) {
127                 continue;
128             }
129
130             if (!set.add(pdpType)) {
131                 result.addResult(new ObjectValidationResult("subgroups", pdpType, ValidationStatus.INVALID,
132                                 "duplicate subgroup"));
133             }
134         }
135     }
136 }