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 * ================================================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.rest;
24 import java.util.Iterator;
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;
40 * Provider for PAP component to delete PDP groups.
42 public class PdpGroupDeleteProvider extends ProviderBase {
43 private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteProvider.class);
47 * Constructs the object.
49 public PdpGroupDeleteProvider() {
54 * Deletes a PDP group.
56 * @param groupName name of the PDP group to be deleted
57 * @throws PfModelException if an error occurred
59 public void deleteGroup(String groupName) throws PfModelException {
60 process(groupName, this::deleteGroup);
64 * Deletes a PDP group.
66 * @param data session data
67 * @param groupName name of the PDP group to be deleted
68 * @throws PfModelException if an error occurred
70 private void deleteGroup(SessionData data, String groupName) throws PfModelException {
72 PdpGroup group = data.getGroup(groupName);
74 throw new PfModelException(Status.NOT_FOUND, "group not found");
77 if (group.getPdpGroupState() == PdpState.ACTIVE) {
78 throw new PfModelException(Status.BAD_REQUEST, "group is still " + PdpState.ACTIVE);
81 data.deleteGroupFromDb(group);
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);
93 * @param policyIdent identifier of the policy to be undeployed
94 * @throws PfModelException if an error occurred
96 public void undeploy(ToscaConceptIdentifierOptVersion policyIdent) throws PfModelException {
97 process(policyIdent, this::undeployPolicy);
101 * Undeploys a policy from its groups.
103 * @param data session data
104 * @param ident identifier of the policy to be deleted
105 * @throws PfModelException if an error occurred
107 private void undeployPolicy(SessionData data, ToscaConceptIdentifierOptVersion ident) throws PfModelException {
109 processPolicy(data, ident);
111 if (data.isUnchanged()) {
112 throw new PfModelException(Status.BAD_REQUEST, "policy does not appear in any PDP group: "
113 + ident.getName() + " " + ident.getVersion());
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);
124 * Returns a function that will remove the desired policy from a subgroup.
127 protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
128 ToscaConceptIdentifierOptVersion desiredIdent) {
130 // construct a matcher based on whether or not the version was specified
131 Predicate<ToscaConceptIdentifier> matcher;
133 if (desiredIdent.getVersion() != null) {
134 // version was specified - match the whole identifier
135 matcher = policy.getIdentifier()::equals;
138 // version was not specified - match the name only
139 String desnm = desiredIdent.getName();
140 matcher = ident -> ident.getName().equals(desnm);
144 // return a function that will remove the policy from the subgroup
145 return (group, subgroup) -> {
147 Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
151 Iterator<ToscaConceptIdentifier> iter = subgroup.getPolicies().iterator();
152 while (iter.hasNext()) {
153 ToscaConceptIdentifier ident = iter.next();
155 if (matcher.test(ident)) {
158 logger.info("remove policy {} from subgroup {} {} count={}", ident,
159 group.getName(), subgroup.getPdpType(), subgroup.getPolicies().size());
161 data.trackUndeploy(ident, pdps, group.getName(), subgroup.getPdpType());