Migrate pap startup & controllers to spring boot
[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 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
40 /**
41  * Implementation of policy undeployer.
42  */
43 public class PolicyUndeployerImpl extends ProviderBase implements PolicyUndeployer {
44     private static final Logger logger = LoggerFactory.getLogger(PolicyUndeployerImpl.class);
45
46     /**
47      * Constructs the object.
48      */
49     public PolicyUndeployerImpl() {
50         super.initialize();
51     }
52
53     @Override
54     public void undeploy(String group, String subgroup, Collection<ToscaConceptIdentifier> 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         var updated = false;
85         Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
86
87         for (ToscaConceptIdentifier 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, policyInfo.group, policyInfo.subgroup);
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,
108             ToscaConceptIdentifierOptVersion desiredPolicy) {
109         throw new UnsupportedOperationException("makeUpdater should not be invoked");
110     }
111
112     private static class Info {
113         private String group;
114         private String subgroup;
115         private Collection<ToscaConceptIdentifier> policies;
116
117         public Info(String group, String subgroup, Collection<ToscaConceptIdentifier> policies) {
118             this.group = group;
119             this.subgroup = subgroup;
120             this.policies = policies;
121         }
122     }
123 }