Add changes for safe delete in policy-models
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyOperationalPolicyProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.rest.provider;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
34
35 /**
36  * Class to provide all kinds of legacy operational policy operations.
37  *
38  * @author Chenfei Gao (cgao@research.att.com)
39  */
40 public class LegacyOperationalPolicyProvider extends CommonModelProvider {
41
42     private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
43             new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
44
45     /**
46      * Default constructor.
47      */
48     public LegacyOperationalPolicyProvider() throws PfModelException {
49         super();
50     }
51
52     /**
53      * Retrieves a list of operational policies matching specified ID and version.
54      *
55      * @param policyId the ID of policy
56      * @param policyVersion the version of policy
57      *
58      * @return the LegacyOperationalPolicy object
59      */
60     public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
61             throws PfModelException {
62
63         return modelsProvider.getOperationalPolicy(policyId, policyVersion);
64     }
65
66     /**
67      * Retrieves a list of deployed operational policies in each pdp group.
68      *
69      * @param policyId the ID of the policy
70      *
71      * @return a list of deployed policies in each pdp group
72      *
73      * @throws PfModelException the PfModel parsing exception
74      */
75     public Map<Pair<String, String>, List<LegacyOperationalPolicy>> fetchDeployedOperationalPolicies(String policyId)
76             throws PfModelException {
77
78         return collectDeployedPolicies(policyId, LEGACY_OPERATIONAL_TYPE, modelsProvider::getOperationalPolicy,
79                 List::add, new ArrayList<>(5));
80     }
81
82     /**
83      * Creates a new operational policy.
84      *
85      * @param body the entity body of policy
86      *
87      * @return the LegacyOperationalPolicy object
88      */
89     public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
90
91         return modelsProvider.createOperationalPolicy(body);
92     }
93
94     /**
95      * Deletes the operational policies matching specified ID and version.
96      *
97      * @param policyId the ID of policy
98      * @param policyVersion the version of policy
99      *
100      * @return the LegacyOperationalPolicy object
101      */
102     public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
103             throws PfModelException {
104
105         return modelsProvider.deleteOperationalPolicy(policyId, policyVersion);
106     }
107 }