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