Make filters on TOSCA entities generic
[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-2021 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 org.onap.policy.api.main.rest.PolicyFetchMode;\r
27 import org.onap.policy.models.base.PfModelException;\r
28 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;\r
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;\r
31 \r
32 /**\r
33  * Class to provide all kinds of policy operations.\r
34  *\r
35  * @author Chenfei Gao (cgao@research.att.com)\r
36  */\r
37 public class PolicyProvider extends CommonModelProvider {\r
38 \r
39     /**\r
40      * Default constructor.\r
41      */\r
42     public PolicyProvider() throws PfModelException {\r
43         super();\r
44     }\r
45 \r
46     /**\r
47      * Retrieves a list of policies matching specified ID and version of both policy type and policy.\r
48      *\r
49      * @param policyTypeId the ID of policy type\r
50      * @param policyTypeVersion the version of policy type\r
51      * @param policyId the ID of policy\r
52      * @param policyVersion the version of policy\r
53      * @param mode the fetch mode for policies\r
54      *\r
55      * @return the ToscaServiceTemplate object\r
56      *\r
57      * @throws PfModelException the PfModel parsing exception\r
58      */\r
59     public ToscaServiceTemplate fetchPolicies(final String policyTypeId, final String policyTypeVersion,\r
60             final String policyId, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {\r
61 \r
62         return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode);\r
63     }\r
64 \r
65     /**\r
66      * Retrieves a list of policies with the latest versions that match specified policy type id and version.\r
67      *\r
68      * @param policyTypeId the ID of policy type\r
69      * @param policyTypeVersion the version of policy type\r
70      * @param policyId the ID of the policy\r
71      * @param mode the fetch mode for policies\r
72      * @return the ToscaServiceTemplate object\r
73      *\r
74      * @throws PfModelException the PfModel parsing exception\r
75      */\r
76     public ToscaServiceTemplate fetchLatestPolicies(final String policyTypeId, final String policyTypeVersion,\r
77             final String policyId, final PolicyFetchMode mode) throws PfModelException {\r
78 \r
79         return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaTypedEntityFilter.LATEST_VERSION,\r
80                 mode);\r
81     }\r
82 \r
83     /**\r
84      * Creates one or more new policies for the same policy type ID and version.\r
85      *\r
86      * @param policyTypeId the ID of policy type\r
87      * @param policyTypeVersion the version of policy type\r
88      * @param body the entity body of polic(ies)\r
89      *\r
90      * @return the ToscaServiceTemplate object\r
91      *\r
92      * @throws PfModelException the PfModel parsing exception\r
93      */\r
94     public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)\r
95             throws PfModelException {\r
96 \r
97         return modelsProvider.createPolicies(body);\r
98     }\r
99 \r
100     /**\r
101      * Creates one or more new policies.\r
102      *\r
103      * @param body the entity body of policy\r
104      *\r
105      * @return the ToscaServiceTemplate object\r
106      *\r
107      * @throws PfModelException the PfModel parsing exception\r
108      */\r
109     public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {\r
110         return modelsProvider.createPolicies(body);\r
111     }\r
112 \r
113     /**\r
114      * Deletes the policy matching specified ID and version of both policy type and policy.\r
115      *\r
116      * @param policyTypeId the ID of policy type\r
117      * @param policyTypeVersion the version of policy type\r
118      * @param policyId the ID of policy\r
119      * @param policyVersion the version of policy\r
120      *\r
121      * @return the ToscaServiceTemplate object\r
122      *\r
123      * @throws PfModelException the PfModel parsing exception\r
124      */\r
125     public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,\r
126             String policyVersion) throws PfModelException {\r
127 \r
128         return modelsProvider.deletePolicy(policyId, policyVersion);\r
129     }\r
130 \r
131     /**\r
132      * Retrieves the specified version of the policy.\r
133      *\r
134      * @param policyTypeName the name of the policy type\r
135      * @param policyTypeVersion the version of the policy type\r
136      * @param policyName the name of the policy\r
137      * @param policyVersion the version of the policy\r
138      * @param mode the fetch mode for policies\r
139      *\r
140      * @return the TOSCA service template containing the specified version of the policy\r
141      *\r
142      * @throws PfModelException the PfModel parsing exception\r
143      */\r
144     private ToscaServiceTemplate getFilteredPolicies(final String policyTypeName, final String policyTypeVersion,\r
145             final String policyName, final String policyVersion, final PolicyFetchMode mode) throws PfModelException {\r
146 \r
147         ToscaTypedEntityFilter<ToscaPolicy> policyFilter = ToscaTypedEntityFilter.<ToscaPolicy>builder()\r
148                 .name(policyName).version(policyVersion).type(policyTypeName).typeVersion(policyTypeVersion).build();\r
149 \r
150         ToscaServiceTemplate serviceTemplate = modelsProvider.getFilteredPolicies(policyFilter);\r
151 \r
152         if (mode == null || PolicyFetchMode.BARE.equals(mode)) {\r
153             serviceTemplate.setPolicyTypes(null);\r
154             serviceTemplate.setDataTypes(null);\r
155         }\r
156 \r
157         return serviceTemplate;\r
158     }\r
159 }\r