1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/*
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
* Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.pap.main.rest;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.pap.main.comm.PolicyUndeployer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Implementation of policy undeployer.
*/
public class PolicyUndeployerImpl extends ProviderBase implements PolicyUndeployer {
private static final Logger logger = LoggerFactory.getLogger(PolicyUndeployerImpl.class);
/**
* Constructs the object.
*/
public PolicyUndeployerImpl() {
super();
}
@Override
public void undeploy(String group, String subgroup, Collection<ToscaConceptIdentifier> policies)
throws PfModelException {
process(new Info(group, subgroup, policies), this::undeployPolicies);
}
/**
* Undeploys policies from all PDPs within a subgroup.
*
* @param data session data
* @param policyInfo information about the policies to be undeployed
* @throws PfModelException if an error occurs
*/
private void undeployPolicies(SessionData data, Info policyInfo) throws PfModelException {
// get group and subgroup
PdpGroup group = data.getGroup(policyInfo.group);
if (group == null) {
return;
}
Optional<PdpSubGroup> optsub = group.getPdpSubgroups().stream()
.filter(subgroup -> subgroup.getPdpType().equals(policyInfo.subgroup)).findAny();
if (!optsub.isPresent()) {
logger.warn("subgroup {} {} not found", policyInfo.group, policyInfo.subgroup);
return;
}
PdpSubGroup subgroup = optsub.get();
// remove the policies
var updated = false;
Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
for (ToscaConceptIdentifier ident : policyInfo.policies) {
if (!subgroup.getPolicies().remove(ident)) {
continue;
}
logger.info("remove policy {} from subgroup {} {} count={}", ident, group.getName(),
subgroup.getPdpType(), subgroup.getPolicies().size());
updated = true;
data.trackUndeploy(ident, pdps, policyInfo.group, policyInfo.subgroup);
}
// push the updates
if (updated) {
makeUpdates(data, group, subgroup);
data.update(group);
}
}
@Override
protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
ToscaConceptIdentifierOptVersion desiredPolicy) {
throw new UnsupportedOperationException("makeUpdater should not be invoked");
}
private static class Info {
private String group;
private String subgroup;
private Collection<ToscaConceptIdentifier> policies;
public Info(String group, String subgroup, Collection<ToscaConceptIdentifier> policies) {
this.group = group;
this.subgroup = subgroup;
this.policies = policies;
}
}
}
|