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