Added oparent to sdc main
[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.*;
29
30 public class PolicyDefinitionBuilder {
31
32     private PolicyDefinition policyDefinition;
33
34     private PolicyDefinitionBuilder() {
35         this.policyDefinition = new PolicyDefinition();
36     }
37
38     public static PolicyDefinitionBuilder create() {
39         return new PolicyDefinitionBuilder();
40     }
41
42     public PolicyDefinitionBuilder setName(String name) {
43         policyDefinition.setName(name);
44         return this;
45     }
46
47     public PolicyDefinitionBuilder setUniqueId(String uid) {
48         policyDefinition.setUniqueId(uid);
49         return this;
50     }
51
52     public PolicyDefinitionBuilder setProperties(PropertyDataDefinition ... props) {
53         policyDefinition.setProperties(Arrays.asList(props));
54         return this;
55     }
56
57     public PolicyDefinitionBuilder setTargets(Map<PolicyTargetType, List<String>> targets ){
58         policyDefinition.setTargets(targets);
59         return this;
60     }
61
62     public PolicyDefinitionBuilder addComponentInstanceTarget(String instId) {
63         Map<PolicyTargetType, List<String>> targets = getTargets();
64         targets.computeIfAbsent(PolicyTargetType.COMPONENT_INSTANCES, k -> new ArrayList<>())
65                .add(instId);
66         return this;
67     }
68
69     public PolicyDefinitionBuilder addGroupTarget(String groupId) {
70         Map<PolicyTargetType, List<String>> targets = getTargets();
71         targets.computeIfAbsent(PolicyTargetType.GROUPS, k -> new ArrayList<>()).add(groupId);
72         return this;
73     }
74
75      public PolicyDefinitionBuilder addProperty(String propName) {
76         initPolicyProperties();
77         PropertyDefinition prop = new PropertyDataDefinitionBuilder()
78                 .setName(propName)
79                 .build();
80         policyDefinition.getProperties().add(prop);
81         return this;
82     }
83
84     public PolicyDefinition build() {
85         return policyDefinition;
86     }
87
88     private Map<PolicyTargetType, List<String>> getTargets() {
89         Map<PolicyTargetType, List<String>> targets = policyDefinition.getTargets();
90         if (targets == null) {
91             targets = new HashMap<>();
92             policyDefinition.setTargets(targets);
93         }
94         return targets;
95     }
96
97     private void initPolicyProperties() {
98         if (policyDefinition.getProperties() == null) {
99             policyDefinition.setProperties(new ArrayList<>());
100         }
101     }
102
103     public PolicyDefinitionBuilder setType(String type) {
104         policyDefinition.setPolicyTypeName(type);
105         return this;
106     }
107 }