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.impl.policy;
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;
28 import java.util.List;
30 import java.util.function.UnaryOperator;
32 import static java.util.Collections.emptyList;
33 import static org.apache.commons.collections.CollectionUtils.isEmpty;
36 * A Helper class which handles altering the targets state of a policy
39 public class PolicyTargetsUpdater {
41 public void removeTarget(List<PolicyDefinition> policies, String targetId, PolicyTargetType targetType) {
42 policies.forEach(policy -> removePolicyTarget(policy, targetId, targetType));
45 public void replaceTarget(List<PolicyDefinition> policies, String oldTargetId, String newTargetId, PolicyTargetType targetType) {
46 policies.forEach(policy -> replacePolicyTarget(policy, targetType, oldTargetId, newTargetId));
49 private void replacePolicyTarget(PolicyDefinition policyDefinition, PolicyTargetType targetType, String oldTargetId, String newTargetId) {
50 List<String> policyTargets = getTargetsList(policyDefinition, targetType);
51 if (isEmpty(policyTargets)) {
54 policyTargets.replaceAll(prevInstanceIdByNewInstanceId(oldTargetId, newTargetId));
57 private void removePolicyTarget(PolicyDefinition policy, String targetId, PolicyTargetType targetType) {
58 List<String> policyTargets = getTargetsList(policy, targetType);
59 if (isEmpty(policyTargets)) {
62 policyTargets.remove(targetId);
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))) {
70 return targets.get(targetType);
73 private UnaryOperator<String> prevInstanceIdByNewInstanceId(String prevInstanceId, String newInstanceId) {
74 return instId -> instId.equals(prevInstanceId) ? newInstanceId: instId;