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