Changes for cascaded get in policy models
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyProvider.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy API\r
4  * ================================================================================\r
5  * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.\r
6  * Modifications Copyright (C) 2020 Nordix Foundation.\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  *\r
20  * SPDX-License-Identifier: Apache-2.0\r
21  * ============LICENSE_END=========================================================\r
22  */\r
23 \r
24 package org.onap.policy.api.main.rest.provider;\r
25 \r
26 import java.util.ArrayList;\r
27 import java.util.List;\r
28 import java.util.Map;\r
29 \r
30 import org.apache.commons.lang3.tuple.Pair;\r
31 import org.onap.policy.models.base.PfConceptKey;\r
32 import org.onap.policy.models.base.PfModelException;\r
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;\r
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
36 \r
37 /**\r
38  * Class to provide all kinds of policy operations.\r
39  *\r
40  * @author Chenfei Gao (cgao@research.att.com)\r
41  */\r
42 public class PolicyProvider extends CommonModelProvider {\r
43 \r
44     /**\r
45      * Default constructor.\r
46      */\r
47     public PolicyProvider() throws PfModelException {\r
48         super();\r
49     }\r
50 \r
51     /**\r
52      * Retrieves a list of policies matching specified ID and version of both policy type and policy.\r
53      *\r
54      * @param policyTypeId the ID of policy type\r
55      * @param policyTypeVersion the version of policy type\r
56      * @param policyId the ID of policy\r
57      * @param policyVersion the version of policy\r
58      *\r
59      * @return the ToscaServiceTemplate object\r
60      *\r
61      * @throws PfModelException the PfModel parsing exception\r
62      */\r
63     public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,\r
64             String policyVersion) throws PfModelException {\r
65 \r
66         return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
67     }\r
68 \r
69     /**\r
70      * Retrieves a list of policies with the latest versions that match specified policy type id and version.\r
71      *\r
72      * @param policyTypeId the ID of policy type\r
73      * @param policyTypeVersion the version of policy type\r
74      * @param policyId the ID of the policy\r
75      *\r
76      * @return the ToscaServiceTemplate object\r
77      *\r
78      * @throws PfModelException the PfModel parsing exception\r
79      */\r
80     public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion, String policyId)\r
81             throws PfModelException {\r
82 \r
83         return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);\r
84     }\r
85 \r
86     /**\r
87      * Retrieves a list of deployed policies in each pdp group.\r
88      *\r
89      * @param policyTypeId the ID of policy type\r
90      * @param policyTypeVersion the version of policy type\r
91      * @param policyId the ID of the policy\r
92      *\r
93      * @return a list of deployed policies in each pdp group\r
94      *\r
95      * @throws PfModelException the PfModel parsing exception\r
96      */\r
97     public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(String policyTypeId,\r
98             String policyTypeVersion, String policyId) throws PfModelException {\r
99 \r
100         return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),\r
101                 modelsProvider::getPolicyList, List::addAll, new ArrayList<>(5));\r
102     }\r
103 \r
104     /**\r
105      * Creates one or more new policies for the same policy type ID and version.\r
106      *\r
107      * @param policyTypeId the ID of policy type\r
108      * @param policyTypeVersion the version of policy type\r
109      * @param body the entity body of polic(ies)\r
110      *\r
111      * @return the ToscaServiceTemplate object\r
112      *\r
113      * @throws PfModelException the PfModel parsing exception\r
114      */\r
115     public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
116             throws PfModelException {\r
117 \r
118         return modelsProvider.createPolicies(body);\r
119     }\r
120 \r
121     /**\r
122      * Creates one or more new policies.\r
123      *\r
124      * @param body the entity body of policy\r
125      *\r
126      * @return the ToscaServiceTemplate object\r
127      *\r
128      * @throws PfModelException the PfModel parsing exception\r
129      */\r
130     public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {\r
131         return modelsProvider.createPolicies(body);\r
132     }\r
133 \r
134     /**\r
135      * Deletes the policy matching specified ID and version of both policy type and policy.\r
136      *\r
137      * @param policyTypeId the ID of policy type\r
138      * @param policyTypeVersion the version of policy type\r
139      * @param policyId the ID of policy\r
140      * @param policyVersion the version of policy\r
141      *\r
142      * @return the ToscaServiceTemplate object\r
143      *\r
144      * @throws PfModelException the PfModel parsing exception\r
145      */\r
146     public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,\r
147             String policyVersion) throws PfModelException {\r
148 \r
149         return modelsProvider.deletePolicy(policyId, policyVersion);\r
150     }\r
151 \r
152     /**\r
153      * Retrieves the specified version of the policy.\r
154      *\r
155      * @param policyTypeName the name of the policy type\r
156      * @param policyTypeVersion the version of the policy type\r
157      * @param policyName the name of the policy\r
158      * @param policyVersion the version of the policy\r
159      *\r
160      * @return the TOSCA service template containing the specified version of the policy\r
161      *\r
162      * @throws PfModelException the PfModel parsing exception\r
163      */\r
164     private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,\r
165             String policyVersion) throws PfModelException {\r
166 \r
167         ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)\r
168                 .type(policyTypeName).typeVersion(policyTypeVersion).build();\r
169         return modelsProvider.getFilteredPolicies(policyFilter);\r
170     }\r
171 }\r