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