cleaner way to handle swagger serialization issue
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / PolicyTypeProvider.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.models.base.PfModelException;\r
28 import org.onap.policy.models.provider.PolicyModelsProvider;\r
29 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityFilter;\r
30 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;\r
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;\r
32 import org.springframework.beans.factory.annotation.Autowired;\r
33 import org.springframework.stereotype.Service;\r
34 \r
35 /**\r
36  * Class to provide all kinds of policy type operations.\r
37  *\r
38  * @author Chenfei Gao (cgao@research.att.com)\r
39  */\r
40 @Service\r
41 public class PolicyTypeProvider extends CommonModelProvider {\r
42 \r
43     /**\r
44      * Default constructor.\r
45      */\r
46     @Autowired\r
47     public PolicyTypeProvider(PolicyModelsProvider modelsProvider) throws PfModelException {\r
48         super(modelsProvider);\r
49     }\r
50 \r
51     /**\r
52      * Retrieves a list of policy types matching specified policy type ID and version.\r
53      *\r
54      * @param policyTypeId the ID of policy type\r
55      * @param policyTypeVersion the version of policy type\r
56      *\r
57      * @return the ToscaServiceTemplate object\r
58      *\r
59      * @throws PfModelException the PfModel parsing exception\r
60      */\r
61     public ToscaServiceTemplate fetchPolicyTypes(String policyTypeId, String policyTypeVersion)\r
62             throws PfModelException {\r
63 \r
64         return getFilteredPolicyTypes(policyTypeId, policyTypeVersion);\r
65     }\r
66 \r
67     /**\r
68      * Retrieves a list of policy types with the latest versions.\r
69      *\r
70      * @param policyTypeId the ID of policy type\r
71      *\r
72      * @return the ToscaServiceTemplate object\r
73      *\r
74      * @throws PfModelException the PfModel parsing exception\r
75      */\r
76     public ToscaServiceTemplate fetchLatestPolicyTypes(String policyTypeId) throws PfModelException {\r
77 \r
78         return getFilteredPolicyTypes(policyTypeId, ToscaEntityFilter.LATEST_VERSION);\r
79     }\r
80 \r
81     /**\r
82      * Creates a new policy type.\r
83      *\r
84      * @param body the entity body of policy type\r
85      *\r
86      * @return the ToscaServiceTemplate object\r
87      * @throws PfModelException the PfModel parsing exception\r
88      */\r
89     public ToscaServiceTemplate createPolicyType(ToscaServiceTemplate body) throws PfModelException {\r
90 \r
91         return modelsProvider.createPolicyTypes(body);\r
92     }\r
93 \r
94     /**\r
95      * Delete the policy type matching specified policy type ID and version.\r
96      *\r
97      * @param policyTypeId the ID of policy type\r
98      * @param policyTypeVersion the version of policy type\r
99      *\r
100      * @return the ToscaServiceTemplate object\r
101      *\r
102      * @throws PfModelException the PfModel parsing exception\r
103      */\r
104     public ToscaServiceTemplate deletePolicyType(String policyTypeId, String policyTypeVersion)\r
105             throws PfModelException {\r
106 \r
107         return modelsProvider.deletePolicyType(policyTypeId, policyTypeVersion);\r
108     }\r
109 \r
110     /**\r
111      * Retrieves the specified version of the policy type.\r
112      *\r
113      * @param policyTypeName the name of the policy type\r
114      * @param policyTypeVersion the version of the policy type\r
115      *\r
116      * @return the TOSCA service template containing the specified version of the policy type\r
117      *\r
118      * @throws PfModelException the PfModel parsing exception\r
119      */\r
120     private ToscaServiceTemplate getFilteredPolicyTypes(String policyTypeName, String policyTypeVersion)\r
121             throws PfModelException {\r
122 \r
123         ToscaEntityFilter<ToscaPolicyType> policyTypeFilter =\r
124                 ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeName).version(policyTypeVersion).build();\r
125         return modelsProvider.getFilteredPolicyTypes(policyTypeFilter);\r
126     }\r
127 }