Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / utils / PolicyDefinitionBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.be.components.utils;
22
23 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
24 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
25 import org.openecomp.sdc.be.model.PolicyDefinition;
26 import org.openecomp.sdc.be.model.PropertyDefinition;
27
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 public class PolicyDefinitionBuilder {
35
36     private PolicyDefinition policyDefinition;
37
38     private PolicyDefinitionBuilder() {
39         this.policyDefinition = new PolicyDefinition();
40     }
41
42     public static PolicyDefinitionBuilder create() {
43         return new PolicyDefinitionBuilder();
44     }
45
46     public PolicyDefinitionBuilder setName(String name) {
47         policyDefinition.setName(name);
48         return this;
49     }
50
51     public PolicyDefinitionBuilder setUniqueId(String uid) {
52         policyDefinition.setUniqueId(uid);
53         return this;
54     }
55
56     public PolicyDefinitionBuilder setProperties(PropertyDataDefinition ... props) {
57         policyDefinition.setProperties(Arrays.asList(props));
58         return this;
59     }
60
61     public PolicyDefinitionBuilder setTargets(Map<PolicyTargetType, List<String>> targets ){
62         policyDefinition.setTargets(targets);
63         return this;
64     }
65
66     public PolicyDefinitionBuilder addComponentInstanceTarget(String instId) {
67         Map<PolicyTargetType, List<String>> targets = getTargets();
68         targets.computeIfAbsent(PolicyTargetType.COMPONENT_INSTANCES, k -> new ArrayList<>())
69                .add(instId);
70         return this;
71     }
72
73     public PolicyDefinitionBuilder addGroupTarget(String groupId) {
74         Map<PolicyTargetType, List<String>> targets = getTargets();
75         targets.computeIfAbsent(PolicyTargetType.GROUPS, k -> new ArrayList<>()).add(groupId);
76         return this;
77     }
78
79      public PolicyDefinitionBuilder addProperty(String propName) {
80         initPolicyProperties();
81         PropertyDefinition prop = new PropertyDataDefinitionBuilder()
82                 .setName(propName)
83                 .build();
84         policyDefinition.getProperties().add(prop);
85         return this;
86     }
87
88     public PolicyDefinition build() {
89         return policyDefinition;
90     }
91
92     private Map<PolicyTargetType, List<String>> getTargets() {
93         Map<PolicyTargetType, List<String>> targets = policyDefinition.getTargets();
94         if (targets == null) {
95             targets = new HashMap<>();
96             policyDefinition.setTargets(targets);
97         }
98         return targets;
99     }
100
101     private void initPolicyProperties() {
102         if (policyDefinition.getProperties() == null) {
103             policyDefinition.setProperties(new ArrayList<>());
104         }
105     }
106
107     public PolicyDefinitionBuilder setType(String type) {
108         policyDefinition.setPolicyTypeName(type);
109         return this;
110     }
111 }