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.Collection;
26 import java.util.Optional;
28 import java.util.stream.Collectors;
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.concepts.PdpSubGroup;
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.onap.policy.pap.main.comm.PolicyUndeployer;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Implementation of policy undeployer.
43 public class PolicyUndeployerImpl extends ProviderBase implements PolicyUndeployer {
44 private static final Logger logger = LoggerFactory.getLogger(PolicyUndeployerImpl.class);
47 * Constructs the object.
49 public PolicyUndeployerImpl() {
54 public void undeploy(String group, String subgroup, Collection<ToscaConceptIdentifier> policies)
55 throws PfModelException {
57 process(new Info(group, subgroup, policies), this::undeployPolicies);
61 * Undeploys policies from all PDPs within a subgroup.
63 * @param data session data
64 * @param policyInfo information about the policies to be undeployed
65 * @throws PfModelException if an error occurs
67 private void undeployPolicies(SessionData data, Info policyInfo) throws PfModelException {
68 // get group and subgroup
69 PdpGroup group = data.getGroup(policyInfo.group);
74 Optional<PdpSubGroup> optsub = group.getPdpSubgroups().stream()
75 .filter(subgroup -> subgroup.getPdpType().equals(policyInfo.subgroup)).findAny();
76 if (!optsub.isPresent()) {
77 logger.warn("subgroup {} {} not found", policyInfo.group, policyInfo.subgroup);
81 PdpSubGroup subgroup = optsub.get();
83 // remove the policies
85 Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
87 for (ToscaConceptIdentifier ident : policyInfo.policies) {
88 if (!subgroup.getPolicies().remove(ident)) {
92 logger.info("remove policy {} from subgroup {} {} count={}", ident, group.getName(),
93 subgroup.getPdpType(), subgroup.getPolicies().size());
96 data.trackUndeploy(ident, pdps, policyInfo.group, policyInfo.subgroup);
101 makeUpdates(data, group, subgroup);
107 protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
108 ToscaConceptIdentifierOptVersion desiredPolicy) {
109 throw new UnsupportedOperationException("makeUpdater should not be invoked");
112 private static class Info {
113 private String group;
114 private String subgroup;
115 private Collection<ToscaConceptIdentifier> policies;
117 public Info(String group, String subgroup, Collection<ToscaConceptIdentifier> policies) {
119 this.subgroup = subgroup;
120 this.policies = policies;