Move deploy/undeploy REST classes
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.main.rest;
22
23 import java.util.Collection;
24 import java.util.Optional;
25 import java.util.Set;
26 import java.util.stream.Collectors;
27 import org.onap.policy.models.base.PfModelException;
28 import org.onap.policy.models.pdp.concepts.Pdp;
29 import org.onap.policy.models.pdp.concepts.PdpGroup;
30 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
34 import org.onap.policy.pap.main.comm.PolicyUndeployer;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Implementation of policy undeployer.
40  */
41 public class PolicyUndeployerImpl extends ProviderBase implements PolicyUndeployer {
42     private static final Logger logger = LoggerFactory.getLogger(PolicyUndeployerImpl.class);
43
44
45     /**
46      * Constructs the object.
47      */
48     public PolicyUndeployerImpl() {
49         super();
50     }
51
52     @Override
53     public void undeploy(String group, String subgroup, Collection<ToscaPolicyIdentifier> policies)
54                     throws PfModelException {
55
56         process(new Info(group, subgroup, policies), this::undeployPolicies);
57     }
58
59     /**
60      * Undeploys policies from all PDPs within a subgroup.
61      *
62      * @param data session data
63      * @param policyInfo information about the policies to be undeployed
64      * @throws PfModelException if an error occurs
65      */
66     private void undeployPolicies(SessionData data, Info policyInfo) throws PfModelException {
67         // get group and subgroup
68         PdpGroup group = data.getGroup(policyInfo.group);
69         if (group == null) {
70             return;
71         }
72
73         Optional<PdpSubGroup> optsub = group.getPdpSubgroups().stream()
74                         .filter(subgroup -> subgroup.getPdpType().equals(policyInfo.subgroup)).findAny();
75         if (!optsub.isPresent()) {
76             logger.warn("subgroup {} {} not found", policyInfo.group, policyInfo.subgroup);
77             return;
78         }
79
80         PdpSubGroup subgroup = optsub.get();
81
82         // remove the policies
83         boolean updated = false;
84         Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
85
86         for (ToscaPolicyIdentifier ident : policyInfo.policies) {
87             if (!subgroup.getPolicies().remove(ident)) {
88                 continue;
89             }
90
91             logger.info("remove policy {} {} from subgroup {} {} count={}", ident.getName(), ident.getVersion(),
92                             group.getName(), subgroup.getPdpType(), subgroup.getPolicies().size());
93
94             updated = true;
95             data.trackUndeploy(ident, pdps);
96         }
97
98         // push the updates
99         if (updated) {
100             makeUpdates(data, group, subgroup);
101             data.update(group);
102         }
103     }
104
105     @Override
106     protected Updater makeUpdater(SessionData data, ToscaPolicy policy, ToscaPolicyIdentifierOptVersion desiredPolicy) {
107         throw new UnsupportedOperationException("makeUpdater should not be invoked");
108     }
109
110     private static class Info {
111         private String group;
112         private String subgroup;
113         private Collection<ToscaPolicyIdentifier> policies;
114
115         public Info(String group, String subgroup, Collection<ToscaPolicyIdentifier> policies) {
116             this.group = group;
117             this.subgroup = subgroup;
118             this.policies = policies;
119         }
120     }
121 }