0a9ea723fc5e2d29eacc739c82418a3a1317a77b
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DummyPolicyModelsProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 Nordix Foundation.
4  *  Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.models.provider.impl;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Date;
28 import java.util.List;
29 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.common.utils.resources.ResourceUtils;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.pdp.concepts.Pdp;
36 import org.onap.policy.models.pdp.concepts.PdpGroup;
37 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
38 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
41 import org.onap.policy.models.provider.PolicyModelsProvider;
42 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplateFilter;
50
51 /**
52  * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework.
53  *
54  * @author Liam Fallon (liam.fallon@est.tech)
55  * @author Chenfei Gao (cgao@research.att.com)
56  */
57 public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider {
58     /**
59      * Constructor that takes the parameters.
60      *
61      * @param parameters the parameters for the provider
62      */
63     public DummyPolicyModelsProviderImpl(final PolicyModelsProviderParameters parameters) {
64         // Default constructor
65     }
66
67     @Override
68     public void init() throws PfModelException {
69         // Not required on the dummy provider
70     }
71
72     @Override
73     public void close() {
74         // Not required on the dummy provider
75     }
76
77
78     @Override
79     public List<ToscaServiceTemplate> getServiceTemplateList(String name, String version) throws PfModelException {
80         return new ArrayList<>();
81     }
82
83     @Override
84     public List<ToscaServiceTemplate> getFilteredServiceTemplateList(@NonNull ToscaServiceTemplateFilter filter)
85             throws PfModelException {
86         return new ArrayList<>();
87     }
88
89     @Override
90     public ToscaServiceTemplate createServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate)
91             throws PfModelException {
92         return serviceTemplate;
93     }
94
95     @Override
96     public ToscaServiceTemplate updateServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate)
97             throws PfModelException {
98         return serviceTemplate;
99     }
100
101     @Override
102     public ToscaServiceTemplate deleteServiceTemplate(@NonNull String name, @NonNull String version)
103             throws PfModelException {
104         return getDummyResponse("dummyimpl/DummyToscaServiceTemplateDeleteResponse.json");
105     }
106
107     @Override
108     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
109         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
110     }
111
112     @Override
113     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
114         return new ArrayList<>();
115     }
116
117     @Override
118     public ToscaServiceTemplate getFilteredPolicyTypes(ToscaPolicyTypeFilter filter) throws PfModelException {
119         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
120     }
121
122     @Override
123     public List<ToscaPolicyType> getFilteredPolicyTypeList(ToscaPolicyTypeFilter filter) {
124         return new ArrayList<>();
125     }
126
127     @Override
128     public ToscaServiceTemplate createPolicyTypes(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
129         return serviceTemplate;
130     }
131
132     @Override
133     public ToscaServiceTemplate updatePolicyTypes(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
134         return serviceTemplate;
135     }
136
137     @Override
138     public ToscaServiceTemplate deletePolicyType(final String name, final String version) throws PfModelException {
139         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeDeleteResponse.json");
140     }
141
142     @Override
143     public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
144         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
145     }
146
147     @Override
148     public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
149         return new ArrayList<>();
150     }
151
152     @Override
153     public ToscaServiceTemplate getFilteredPolicies(ToscaPolicyFilter filter) throws PfModelException {
154         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
155     }
156
157     @Override
158     public List<ToscaPolicy> getFilteredPolicyList(ToscaPolicyFilter filter) throws PfModelException {
159         return new ArrayList<>();
160     }
161
162     @Override
163     public ToscaServiceTemplate createPolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
164         return serviceTemplate;
165     }
166
167     @Override
168     public ToscaServiceTemplate updatePolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
169         return serviceTemplate;
170     }
171
172     @Override
173     public ToscaServiceTemplate deletePolicy(final String name, final String version) throws PfModelException {
174         return getDummyResponse("dummyimpl/DummyToscaPolicyDeleteResponse.json");
175     }
176
177     @Override
178     public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
179         return new ArrayList<>();
180     }
181
182     @Override
183     public List<PdpGroup> getFilteredPdpGroups(PdpGroupFilter filter) throws PfModelException {
184         return new ArrayList<>();
185     }
186
187     @Override
188     public List<PdpGroup> createPdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException {
189         return new ArrayList<>();
190     }
191
192     @Override
193     public List<PdpGroup> updatePdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException {
194         return new ArrayList<>();
195     }
196
197     @Override
198     public void updatePdpSubGroup(final String pdpGroupName, final PdpSubGroup pdpSubGroup) throws PfModelException {
199         // Not implemented
200     }
201
202     @Override
203     public void updatePdp(String pdpGroupName, String pdpSubGroup, Pdp pdp) throws PfModelException {
204         // Not implemented
205     }
206
207     @Override
208     public PdpGroup deletePdpGroup(final String name) throws PfModelException {
209         return null;
210     }
211
212     @Override
213     public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException {
214         return new ArrayList<>();
215     }
216
217     @Override
218     public List<PdpStatistics> getFilteredPdpStatistics(String name, String pdpGroupName, String pdpSubGroup,
219             Date startTimeStamp, Date endTimeStamp, String sortOrder, int getRecordNum) {
220         // Not implemented
221         return new ArrayList<>();
222     }
223
224     @Override
225     public List<PdpStatistics> createPdpStatistics(final List<PdpStatistics> pdpStatisticsList)
226             throws PfModelException {
227         // Not implemented
228         return new ArrayList<>();
229     }
230
231     @Override
232     public List<PdpStatistics> updatePdpStatistics(final List<PdpStatistics> pdpStatisticsList)
233             throws PfModelException {
234         // Not implemented
235         return new ArrayList<>();
236     }
237
238     @Override
239     public List<PdpStatistics> deletePdpStatistics(final String name, final Date timestamp) {
240         // Not implemented
241         return new ArrayList<>();
242     }
243
244     @Override
245     public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
246         // Not implemented
247         return new ArrayList<>();
248     }
249
250     @Override
251     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
252                     throws PfModelException {
253         // Not implemented
254         return new ArrayList<>();
255     }
256
257     @Override
258     public List<PdpPolicyStatus> getGroupPolicyStatus(String groupName) throws PfModelException {
259         // Not implemented
260         return new ArrayList<>();
261     }
262
263     @Override
264     public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
265                     Collection<PdpPolicyStatus> deleteObjs) {
266         // Not implemented
267     }
268
269     /**
270      * Return a ToscaServicetemplate dummy response.
271      *
272      * @param fileName the file name containing the dummy response
273      * @return the ToscaServiceTemplate with the dummy response
274      */
275     protected ToscaServiceTemplate getDummyResponse(final String fileName) {
276         StandardCoder standardCoder = new StandardCoder();
277         ToscaServiceTemplate serviceTemplate;
278
279         try {
280             serviceTemplate =
281                     standardCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
282             if (serviceTemplate == null) {
283                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
284             }
285         } catch (Exception exc) {
286             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
287         }
288
289         return serviceTemplate;
290     }
291 }