6729d7e88d55a98250b2750faa05aaefc29c5b99
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / PdpGroups.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.pdp.concepts;
23
24 import java.util.Collections;
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.Getter;
31 import lombok.Setter;
32 import lombok.ToString;
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
38 /**
39  * Request deploy or update a set of groups via the PDP Group deployment REST API.
40  */
41 @Getter
42 @Setter
43 @ToString
44 public class PdpGroups {
45     private static final String GROUPS_FIELD = "groups";
46
47     private List<PdpGroup> groups;
48
49     /**
50      * Get the contents of this class as a list of PDP group maps.
51      *
52      * @return the PDP groups in a list of maps
53      */
54     public List<Map<String, PdpGroup>> toMapList() {
55         final Map<String, PdpGroup> pdpGroupMap = new LinkedHashMap<>();
56         for (PdpGroup pdpGroup : groups) {
57             pdpGroupMap.put(pdpGroup.getName() + ':' + pdpGroup.getVersion(), pdpGroup);
58         }
59
60         return Collections.singletonList(pdpGroupMap);
61     }
62
63     /**
64      * Validates that appropriate fields are populated for an incoming call to the PAP
65      * REST API.
66      *
67      * @return the validation result
68      */
69     public ValidationResult validatePapRest() {
70         ValidationResult result = new BeanValidationResult(GROUPS_FIELD, this);
71         ((BeanValidationResult) result).validateNotNullList(GROUPS_FIELD, groups,
72             (PdpGroup pdpGroup) -> pdpGroup.validatePapRest(false));
73         if (!result.isValid()) {
74             return result;
75         }
76
77         // verify that the same group doesn't appear more than once
78         return checkForDuplicateGroups(result);
79     }
80
81     /**
82      * Validates that there are no duplicate PdpGroups with the same name.
83      *
84      * @param result the validation result
85      * @return the validation result
86      */
87     public ValidationResult checkForDuplicateGroups(ValidationResult result) {
88         if (null == groups) {
89             result.setResult(ValidationStatus.INVALID, "is null");
90         } else {
91             List<String> names = groups.stream().map(PdpGroup::getName).collect(Collectors.toList());
92             if (groups.size() != new HashSet<>(names).size()) {
93                 result =
94                     new ObjectValidationResult(GROUPS_FIELD, names, ValidationStatus.INVALID, "duplicate group names");
95             }
96         }
97         return result;
98     }
99 }