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