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