0d810d2100be534ef1164b875233148eebddbecd
[policy/models.git] / models-pdp / src / main / java / org / onap / policy / models / pdp / concepts / DeploymentGroups.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.pdp.concepts;
22
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 import lombok.Getter;
27 import lombok.Setter;
28 import lombok.ToString;
29 import org.onap.policy.common.parameters.BeanValidationResult;
30 import org.onap.policy.common.parameters.ObjectValidationResult;
31 import org.onap.policy.common.parameters.ValidationResult;
32 import org.onap.policy.common.parameters.ValidationStatus;
33
34 /**
35  * Batch modification of a deployment groups via the PDP Group deployment REST API.
36  */
37 @Getter
38 @Setter
39 @ToString
40 public class DeploymentGroups {
41     private static final String GROUPS_FIELD = "groups";
42
43     private List<DeploymentGroup> groups;
44
45     /**
46      * Validates that appropriate fields are populated for an incoming call to the PAP
47      * REST API.
48      *
49      * @return the validation result
50      */
51     public ValidationResult validatePapRest() {
52         BeanValidationResult result = new BeanValidationResult(GROUPS_FIELD, this);
53
54         result.validateNotNullList(GROUPS_FIELD, groups, DeploymentGroup::validatePapRest);
55         if (!result.isValid()) {
56             return result;
57         }
58
59         // verify that the same group doesn't appear more than once
60         Set<String> sawGroup = new HashSet<>();
61         for (DeploymentGroup group : groups) {
62             String name = group.getName();
63             if (sawGroup.contains(name)) {
64                 return new ObjectValidationResult(GROUPS_FIELD, name, ValidationStatus.INVALID, "duplicate group name");
65
66             } else {
67                 sawGroup.add(name);
68             }
69         }
70
71         return result;
72     }
73 }