Merge "Send pdp-update if PDP response doesn't match DB"
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PdpGroupDeleteProvider.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  * ================================================================================
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.Iterator;
25 import java.util.Set;
26 import java.util.function.Predicate;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response.Status;
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.enums.PdpState;
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.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Provider for PAP component to delete PDP groups.
41  */
42 public class PdpGroupDeleteProvider extends ProviderBase {
43     private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteProvider.class);
44
45
46     /**
47      * Constructs the object.
48      */
49     public PdpGroupDeleteProvider() {
50         super();
51     }
52
53     /**
54      * Deletes a PDP group.
55      *
56      * @param groupName name of the PDP group to be deleted
57      * @throws PfModelException if an error occurred
58      */
59     public void deleteGroup(String groupName) throws PfModelException {
60         process(groupName, this::deleteGroup);
61     }
62
63     /**
64      * Deletes a PDP group.
65      *
66      * @param data session data
67      * @param groupName name of the PDP group to be deleted
68      * @throws PfModelException if an error occurred
69      */
70     private void deleteGroup(SessionData data, String groupName) throws PfModelException {
71         try {
72             PdpGroup group = data.getGroup(groupName);
73             if (group == null) {
74                 throw new PfModelException(Status.NOT_FOUND, "group not found");
75             }
76
77             if (group.getPdpGroupState() == PdpState.ACTIVE) {
78                 throw new PfModelException(Status.BAD_REQUEST, "group is still " + PdpState.ACTIVE);
79             }
80
81             data.deleteGroupFromDb(group);
82
83         } catch (PfModelException | RuntimeException e) {
84             // no need to log the error object here, as it will be logged by the invoker
85             logger.warn("failed to delete group: {}", groupName);
86             throw e;
87         }
88     }
89
90     /**
91      * Undeploys a policy.
92      *
93      * @param policyIdent identifier of the policy to be undeployed
94      * @throws PfModelException if an error occurred
95      */
96     public void undeploy(ToscaConceptIdentifierOptVersion policyIdent) throws PfModelException {
97         process(policyIdent, this::undeployPolicy);
98     }
99
100     /**
101      * Undeploys a policy from its groups.
102      *
103      * @param data session data
104      * @param ident identifier of the policy to be deleted
105      * @throws PfModelException if an error occurred
106      */
107     private void undeployPolicy(SessionData data, ToscaConceptIdentifierOptVersion ident) throws PfModelException {
108         try {
109             processPolicy(data, ident);
110
111             if (data.isUnchanged()) {
112                 throw new PfModelException(Status.BAD_REQUEST, "policy does not appear in any PDP group: "
113                                 + ident.getName() + " " + ident.getVersion());
114             }
115
116         } catch (PfModelException | RuntimeException e) {
117             // no need to log the error object here, as it will be logged by the invoker
118             logger.warn("failed to undeploy policy: {}", ident);
119             throw e;
120         }
121     }
122
123     /**
124      * Returns a function that will remove the desired policy from a subgroup.
125      */
126     @Override
127     protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
128                     ToscaConceptIdentifierOptVersion desiredIdent) {
129
130         // construct a matcher based on whether or not the version was specified
131         Predicate<ToscaConceptIdentifier> matcher;
132
133         if (desiredIdent.getVersion() != null) {
134             // version was specified - match the whole identifier
135             matcher = policy.getIdentifier()::equals;
136
137         } else {
138             // version was not specified - match the name only
139             String desnm = desiredIdent.getName();
140             matcher = ident -> ident.getName().equals(desnm);
141         }
142
143
144         // return a function that will remove the policy from the subgroup
145         return (group, subgroup) -> {
146
147             Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
148
149             var result = false;
150
151             Iterator<ToscaConceptIdentifier> iter = subgroup.getPolicies().iterator();
152             while (iter.hasNext()) {
153                 ToscaConceptIdentifier ident = iter.next();
154
155                 if (matcher.test(ident)) {
156                     result = true;
157                     iter.remove();
158                     logger.info("remove policy {} from subgroup {} {} count={}", ident,
159                                     group.getName(), subgroup.getPdpType(), subgroup.getPolicies().size());
160
161                     data.trackUndeploy(ident, pdps, group.getName(), subgroup.getPdpType());
162                 }
163             }
164
165             return result;
166         };
167     }
168 }