2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.utils;
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;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.List;
34 public class PolicyDefinitionBuilder {
36 private PolicyDefinition policyDefinition;
38 private PolicyDefinitionBuilder() {
39 this.policyDefinition = new PolicyDefinition();
42 public static PolicyDefinitionBuilder create() {
43 return new PolicyDefinitionBuilder();
46 public PolicyDefinitionBuilder setName(String name) {
47 policyDefinition.setName(name);
51 public PolicyDefinitionBuilder setUniqueId(String uid) {
52 policyDefinition.setUniqueId(uid);
56 public PolicyDefinitionBuilder setProperties(PropertyDataDefinition ... props) {
57 policyDefinition.setProperties(Arrays.asList(props));
61 public PolicyDefinitionBuilder setTargets(Map<PolicyTargetType, List<String>> targets ){
62 policyDefinition.setTargets(targets);
66 public PolicyDefinitionBuilder addComponentInstanceTarget(String instId) {
67 Map<PolicyTargetType, List<String>> targets = getTargets();
68 targets.computeIfAbsent(PolicyTargetType.COMPONENT_INSTANCES, k -> new ArrayList<>())
73 public PolicyDefinitionBuilder addGroupTarget(String groupId) {
74 Map<PolicyTargetType, List<String>> targets = getTargets();
75 targets.computeIfAbsent(PolicyTargetType.GROUPS, k -> new ArrayList<>()).add(groupId);
79 public PolicyDefinitionBuilder addProperty(String propName) {
80 initPolicyProperties();
81 PropertyDefinition prop = new PropertyDataDefinitionBuilder()
84 policyDefinition.getProperties().add(prop);
88 public PolicyDefinition build() {
89 return policyDefinition;
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);
101 private void initPolicyProperties() {
102 if (policyDefinition.getProperties() == null) {
103 policyDefinition.setProperties(new ArrayList<>());
107 public PolicyDefinitionBuilder setType(String type) {
108 policyDefinition.setPolicyTypeName(type);