2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.rest;
23 import java.util.Collection;
24 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import org.onap.policy.models.base.PfModelException;
28 import org.onap.policy.models.pdp.concepts.Pdp;
29 import org.onap.policy.models.pdp.concepts.PdpGroup;
30 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
34 import org.onap.policy.pap.main.comm.PolicyUndeployer;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Implementation of policy undeployer.
41 public class PolicyUndeployerImpl extends ProviderBase implements PolicyUndeployer {
42 private static final Logger logger = LoggerFactory.getLogger(PolicyUndeployerImpl.class);
46 * Constructs the object.
48 public PolicyUndeployerImpl() {
53 public void undeploy(String group, String subgroup, Collection<ToscaPolicyIdentifier> policies)
54 throws PfModelException {
56 process(new Info(group, subgroup, policies), this::undeployPolicies);
60 * Undeploys policies from all PDPs within a subgroup.
62 * @param data session data
63 * @param policyInfo information about the policies to be undeployed
64 * @throws PfModelException if an error occurs
66 private void undeployPolicies(SessionData data, Info policyInfo) throws PfModelException {
67 // get group and subgroup
68 PdpGroup group = data.getGroup(policyInfo.group);
73 Optional<PdpSubGroup> optsub = group.getPdpSubgroups().stream()
74 .filter(subgroup -> subgroup.getPdpType().equals(policyInfo.subgroup)).findAny();
75 if (!optsub.isPresent()) {
76 logger.warn("subgroup {} {} not found", policyInfo.group, policyInfo.subgroup);
80 PdpSubGroup subgroup = optsub.get();
82 // remove the policies
83 boolean updated = false;
84 Set<String> pdps = subgroup.getPdpInstances().stream().map(Pdp::getInstanceId).collect(Collectors.toSet());
86 for (ToscaPolicyIdentifier ident : policyInfo.policies) {
87 if (!subgroup.getPolicies().remove(ident)) {
91 logger.info("remove policy {} {} from subgroup {} {} count={}", ident.getName(), ident.getVersion(),
92 group.getName(), subgroup.getPdpType(), subgroup.getPolicies().size());
95 data.trackUndeploy(ident, pdps);
100 makeUpdates(data, group, subgroup);
106 protected Updater makeUpdater(SessionData data, ToscaPolicy policy, ToscaPolicyIdentifierOptVersion desiredPolicy) {
107 throw new UnsupportedOperationException("makeUpdater should not be invoked");
110 private static class Info {
111 private String group;
112 private String subgroup;
113 private Collection<ToscaPolicyIdentifier> policies;
115 public Info(String group, String subgroup, Collection<ToscaPolicyIdentifier> policies) {
117 this.subgroup = subgroup;
118 this.policies = policies;