2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.models.pdp.concepts;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.stream.Collectors;
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;
39 * Request deploy or update a set of groups via the PDP Group deployment REST API.
44 public class PdpGroups {
45 private static final String GROUPS_FIELD = "groups";
47 private List<PdpGroup> groups;
50 * Get the contents of this class as a list of PDP group maps.
52 * @return the PDP groups in a list of maps
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);
60 return Collections.singletonList(pdpGroupMap);
64 * Validates that appropriate fields are populated for an incoming call to the PAP
67 * @return the validation result
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()) {
77 // verify that the same group doesn't appear more than once
78 return checkForDuplicateGroups(result);
82 * Validates that there are no duplicate PdpGroups with the same name.
84 * @param result the validation result
85 * @return the validation result
87 public ValidationResult checkForDuplicateGroups(ValidationResult result) {
89 result.setResult(ValidationStatus.INVALID, "is null");
91 List<String> names = groups.stream().map(PdpGroup::getName).collect(Collectors.toList());
92 if (groups.size() != new HashSet<>(names).size()) {
94 new ObjectValidationResult(GROUPS_FIELD, names, ValidationStatus.INVALID, "duplicate group names");