11a6f2dcebb3cdde305f80a79f1ca3622e26740e
[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 import org.apache.commons.lang3.tuple.Pair;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
33
34 /**
35  * Class to provide all kinds of legacy operational policy operations.
36  *
37  * @author Chenfei Gao (cgao@research.att.com)
38  */
39 public class LegacyOperationalPolicyProvider extends CommonModelProvider {
40
41     private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
42             new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
43
44     /**
45      * Default constructor.
46      */
47     public LegacyOperationalPolicyProvider() throws PfModelException {
48         super();
49     }
50
51     /**
52      * Retrieves a list of operational policies matching specified ID and version.
53      *
54      * @param policyId the ID of policy
55      * @param policyVersion the version of policy
56      *
57      * @return the LegacyOperationalPolicy object
58      */
59     public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
60             throws PfModelException {
61
62         return modelsProvider.getOperationalPolicy(policyId, policyVersion);
63     }
64
65     /**
66      * Retrieves a list of deployed operational policies in each pdp group.
67      *
68      * @param policyId the ID of the policy
69      *
70      * @return a list of deployed policies in each pdp group
71      *
72      * @throws PfModelException the PfModel parsing exception
73      */
74     public Map<Pair<String, String>, List<LegacyOperationalPolicy>> fetchDeployedOperationalPolicies(String policyId)
75             throws PfModelException {
76
77         return collectDeployedPolicies(policyId, LEGACY_OPERATIONAL_TYPE, modelsProvider::getOperationalPolicy,
78                 List::add, new ArrayList<>(5));
79     }
80
81     /**
82      * Creates a new operational policy.
83      *
84      * @param body the entity body of policy
85      *
86      * @return the LegacyOperationalPolicy object
87      */
88     public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
89
90         return modelsProvider.createOperationalPolicy(body);
91     }
92
93     /**
94      * Deletes the operational policies matching specified ID and version.
95      *
96      * @param policyId the ID of policy
97      * @param policyVersion the version of policy
98      *
99      * @return the LegacyOperationalPolicy object
100      */
101     public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
102             throws PfModelException {
103
104         return modelsProvider.deleteOperationalPolicy(policyId, policyVersion);
105     }
106 }