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-2022 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;
39 import org.springframework.stereotype.Component;
42 * Implementation of policy undeployer.
45 public class PolicyUndeployerImpl extends ProviderBase implements PolicyUndeployer {
46 private static final Logger logger = LoggerFactory.getLogger(PolicyUndeployerImpl.class);
49 public void undeploy(String group, String subgroup, Collection<ToscaConceptIdentifier> policies)
50 throws PfModelException {
52 process(new Info(group, subgroup, policies), this::undeployPolicies);
56 * Undeploys policies from all PDPs within a subgroup.
58 * @param data session data
59 * @param policyInfo information about the policies to be undeployed
60 * @throws PfModelException if an error occurs
62 private void undeployPolicies(SessionData data, Info policyInfo) throws PfModelException {
63 // get group and subgroup
64 PdpGroup group = data.getGroup(policyInfo.group);
69 Optional<PdpSubGroup> optsub = group.getPdpSubgroups().stream()
70 .filter(subgroup -> subgroup.getPdpType().equals(policyInfo.subgroup)).findAny();
71 if (!optsub.isPresent()) {
72 logger.warn("subgroup {} {} not found", policyInfo.group, policyInfo.subgroup);
76 PdpSubGroup subgroup = optsub.get();
78 // remove the policies
80 Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
82 for (ToscaConceptIdentifier ident : policyInfo.policies) {
83 if (!subgroup.getPolicies().remove(ident)) {
87 logger.info("remove policy {} from subgroup {} {} count={}", ident, group.getName(),
88 subgroup.getPdpType(), subgroup.getPolicies().size());
91 data.trackUndeploy(ident, pdps, policyInfo.group, policyInfo.subgroup);
96 makeUpdates(data, group, subgroup);
102 protected Updater makeUpdater(SessionData data, ToscaPolicy policy,
103 ToscaConceptIdentifierOptVersion desiredPolicy) {
104 throw new UnsupportedOperationException("makeUpdater should not be invoked");
107 private static class Info {
108 private String group;
109 private String subgroup;
110 private Collection<ToscaConceptIdentifier> policies;
112 public Info(String group, String subgroup, Collection<ToscaConceptIdentifier> policies) {
114 this.subgroup = subgroup;
115 this.policies = policies;