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