2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.rest;
25 import java.util.Iterator;
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;
42 * Provider for PAP component to delete PDP groups.
45 public class PdpGroupDeleteProvider extends ProviderBase {
46 private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteProvider.class);
49 * Deletes a PDP group.
51 * @param groupName name of the PDP group to be deleted
52 * @throws PfModelException if an error occurred
54 public void deleteGroup(String groupName) throws PfModelException {
55 process(groupName, this::deleteGroup);
59 * Deletes a PDP group.
61 * @param data session data
62 * @param groupName name of the PDP group to be deleted
63 * @throws PfModelException if an error occurred
65 private void deleteGroup(SessionData data, String groupName) throws PfModelException {
67 PdpGroup group = data.getGroup(groupName);
69 throw new PfModelException(Status.NOT_FOUND, "group not found");
72 if (group.getPdpGroupState() == PdpState.ACTIVE) {
73 throw new PfModelException(Status.BAD_REQUEST, "group is still " + PdpState.ACTIVE);
76 data.deleteGroupFromDb(group);
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);
88 * @param policyIdent identifier of the policy to be undeployed
89 * @throws PfModelException if an error occurred
91 public void undeploy(ToscaConceptIdentifierOptVersion policyIdent, String user) throws PfModelException {
92 process(user, policyIdent, this::undeployPolicy);
96 * Undeploys a policy from its groups.
98 * @param data session data
99 * @param ident identifier of the policy to be deleted
100 * @throws PfModelException if an error occurred
102 private void undeployPolicy(SessionData data, ToscaConceptIdentifierOptVersion ident) throws PfModelException {
104 processPolicy(data, ident);
106 if (data.isUnchanged()) {
107 throw new PfModelException(Status.BAD_REQUEST, "policy does not appear in any PDP group: "
108 + ident.getName() + " " + ident.getVersion());
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);
119 * Returns a function that will remove the desired policy from a subgroup.
122 protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
123 ToscaConceptIdentifierOptVersion desiredIdent) {
125 // construct a matcher based on whether or not the version was specified
126 Predicate<ToscaConceptIdentifier> matcher;
128 if (desiredIdent.getVersion() != null) {
129 // version was specified - match the whole identifier
130 matcher = policy.getIdentifier()::equals;
133 // version was not specified - match the name only
134 String desnm = desiredIdent.getName();
135 matcher = ident -> ident.getName().equals(desnm);
139 // return a function that will remove the policy from the subgroup
140 return (group, subgroup) -> {
142 Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
146 Iterator<ToscaConceptIdentifier> iter = subgroup.getPolicies().iterator();
147 while (iter.hasNext()) {
148 ToscaConceptIdentifier ident = iter.next();
150 if (matcher.test(ident)) {
153 logger.info("remove policy {} from subgroup {} {} count={}", ident,
154 group.getName(), subgroup.getPdpType(), subgroup.getPolicies().size());
156 data.trackUndeploy(ident, pdps, group.getName(), subgroup.getPdpType());