aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/main/java/org/onap/policy/pap/main/rest/depundep/PdpGroupDeleteProvider.java
blob: 6df713d35a6aa33191083bbb2296bdaa5ea975be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.rest.depundep;

import java.util.function.BiFunction;
import javax.ws.rs.core.Response.Status;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifierOptVersion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Provider for PAP component to delete PDP groups.
 */
public class PdpGroupDeleteProvider extends ProviderBase {
    private static final Logger logger = LoggerFactory.getLogger(PdpGroupDeleteProvider.class);


    /**
     * Constructs the object.
     */
    public PdpGroupDeleteProvider() {
        super();
    }

    /**
     * Deletes a PDP group.
     *
     * @param groupName name of the PDP group to be deleted
     * @throws PfModelException if an error occurred
     */
    public void deleteGroup(String groupName) throws PfModelException {
        process(groupName, this::deleteGroup);
    }

    /**
     * Deletes a PDP group.
     *
     * @param data session data
     * @param groupName name of the PDP group to be deleted
     * @throws PfModelException if an error occurred
     */
    private void deleteGroup(SessionData data, String groupName) throws PfModelException {
        try {
            PdpGroup group = data.getGroup(groupName);
            if (group == null) {
                throw new PfModelException(Status.NOT_FOUND, "group not found");
            }

            if (group.getPdpGroupState() == PdpState.ACTIVE) {
                throw new PfModelException(Status.BAD_REQUEST, "group is still " + PdpState.ACTIVE);
            }

            data.deleteGroupFromDb(group);

        } catch (PfModelException | RuntimeException e) {
            // no need to log the error object here, as it will be logged by the invoker
            logger.warn("failed to delete group: {}", groupName);
            throw e;
        }
    }

    /**
     * Undeploys a policy.
     *
     * @param policyIdent identifier of the policy to be undeployed
     * @throws PfModelException if an error occurred
     */
    public void undeploy(ToscaPolicyIdentifierOptVersion policyIdent) throws PfModelException {
        process(policyIdent, this::undeployPolicy);
    }

    /**
     * Undeploys a policy from its groups.
     *
     * @param data session data
     * @param ident identifier of the policy to be deleted
     * @throws PfModelException if an error occurred
     */
    private void undeployPolicy(SessionData data, ToscaPolicyIdentifierOptVersion ident) throws PfModelException {
        try {
            processPolicy(data, ident);

        } catch (PfModelException | RuntimeException e) {
            // no need to log the error object here, as it will be logged by the invoker
            logger.warn("failed to undeploy policy: {}", ident);
            throw e;
        }
    }

    /**
     * Returns a function that will remove the specified policy from a subgroup.
     */
    @Override
    protected BiFunction<PdpGroup, PdpSubGroup, Boolean> makeUpdater(ToscaPolicy policy) {
        ToscaPolicyIdentifier desiredIdent = policy.getIdentifier();

        // remove the policy from the subgroup
        return (group, subgroup) -> subgroup.getPolicies().remove(desiredIdent);
    }
}