Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / policy / PolicyTargetsUpdater.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.impl.policy;
22
23 import org.apache.commons.collections.MapUtils;
24 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
25 import org.openecomp.sdc.be.model.PolicyDefinition;
26 import org.springframework.stereotype.Component;
27
28 import java.util.List;
29 import java.util.Map;
30 import java.util.function.UnaryOperator;
31
32 import static java.util.Collections.emptyList;
33 import static org.apache.commons.collections.CollectionUtils.isEmpty;
34
35 /**
36  * A Helper class which handles altering the targets state of a policy
37  */
38 @Component
39 public class PolicyTargetsUpdater {
40
41     public void removeTarget(List<PolicyDefinition> policies, String targetId, PolicyTargetType targetType) {
42         policies.forEach(policy -> removePolicyTarget(policy, targetId, targetType));
43     }
44
45     public void replaceTarget(List<PolicyDefinition> policies, String oldTargetId, String newTargetId, PolicyTargetType targetType) {
46         policies.forEach(policy -> replacePolicyTarget(policy, targetType, oldTargetId, newTargetId));
47     }
48
49     private void replacePolicyTarget(PolicyDefinition policyDefinition, PolicyTargetType targetType, String oldTargetId, String newTargetId) {
50         List<String> policyTargets = getTargetsList(policyDefinition, targetType);
51         if (isEmpty(policyTargets)) {
52             return;
53         }
54         policyTargets.replaceAll(prevInstanceIdByNewInstanceId(oldTargetId, newTargetId));
55     }
56
57     private void removePolicyTarget(PolicyDefinition policy, String targetId, PolicyTargetType targetType) {
58         List<String> policyTargets = getTargetsList(policy, targetType);
59         if (isEmpty(policyTargets)) {
60             return;
61         }
62         policyTargets.remove(targetId);
63     }
64
65     private List<String> getTargetsList(PolicyDefinition policyDefinition, PolicyTargetType targetType) {
66         Map<PolicyTargetType, List<String>> targets = policyDefinition.getTargets();
67         if(MapUtils.isEmpty(targets) || isEmpty(targets.get(targetType))) {
68             return emptyList();
69         }
70         return targets.get(targetType);
71     }
72
73     private UnaryOperator<String> prevInstanceIdByNewInstanceId(String prevInstanceId, String newInstanceId) {
74         return instId -> instId.equals(prevInstanceId) ? newInstanceId: instId;
75     }
76
77 }