Fix issue with GeneratedValue in PfGeneratedIdKey
[policy/models.git] / models-provider / src / test / java / org / onap / policy / models / provider / impl / DummyPolicyModelsProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2022 Nordix Foundation.
4  *  Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2020, 2022 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.List;
28 import java.util.Map;
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.PfConceptKey;
34 import org.onap.policy.models.base.PfModelException;
35 import org.onap.policy.models.base.PfModelRuntimeException;
36 import org.onap.policy.models.pdp.concepts.Pdp;
37 import org.onap.policy.models.pdp.concepts.PdpGroup;
38 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
39 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
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.ToscaEntityFilter;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntityKey;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
51
52 /**
53  * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework.
54  *
55  * @author Liam Fallon (liam.fallon@est.tech)
56  * @author Chenfei Gao (cgao@research.att.com)
57  */
58 public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider {
59     /**
60      * Constructor that takes the parameters.
61      *
62      * @param parameters the parameters for the provider
63      */
64     public DummyPolicyModelsProviderImpl(final PolicyModelsProviderParameters parameters) {
65         // Default constructor
66     }
67
68     @Override
69     public void init() throws PfModelException {
70         // Not required on the dummy provider
71     }
72
73     @Override
74     public void close() {
75         // Not required on the dummy provider
76     }
77
78
79     @Override
80     public List<ToscaServiceTemplate> getServiceTemplateList(String name, String version) throws PfModelException {
81         return new ArrayList<>();
82     }
83
84     @Override
85     public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
86             @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
87         return new ArrayList<>();
88     }
89
90     @Override
91     public ToscaServiceTemplate createServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate)
92             throws PfModelException {
93         return serviceTemplate;
94     }
95
96     @Override
97     public ToscaServiceTemplate updateServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate)
98             throws PfModelException {
99         return serviceTemplate;
100     }
101
102     @Override
103     public ToscaServiceTemplate deleteServiceTemplate(@NonNull String name, @NonNull String version)
104             throws PfModelException {
105         return getDummyResponse("dummyimpl/DummyToscaServiceTemplateDeleteResponse.json");
106     }
107
108     @Override
109     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
110         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
111     }
112
113     @Override
114     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
115         return new ArrayList<>();
116     }
117
118     @Override
119     public ToscaServiceTemplate getFilteredPolicyTypes(ToscaEntityFilter<ToscaPolicyType> filter)
120             throws PfModelException {
121         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
122     }
123
124     @Override
125     public List<ToscaPolicyType> getFilteredPolicyTypeList(ToscaEntityFilter<ToscaPolicyType> filter) {
126         return new ArrayList<>();
127     }
128
129     @Override
130     public ToscaServiceTemplate createPolicyTypes(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
131         return serviceTemplate;
132     }
133
134     @Override
135     public ToscaServiceTemplate updatePolicyTypes(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
136         return serviceTemplate;
137     }
138
139     @Override
140     public ToscaServiceTemplate deletePolicyType(final String name, final String version) throws PfModelException {
141         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeDeleteResponse.json");
142     }
143
144     @Override
145     public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
146         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
147     }
148
149     @Override
150     public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
151         return new ArrayList<>();
152     }
153
154     @Override
155     public ToscaServiceTemplate getFilteredPolicies(ToscaTypedEntityFilter<ToscaPolicy> filter)
156             throws PfModelException {
157         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
158     }
159
160     @Override
161     public List<ToscaPolicy> getFilteredPolicyList(ToscaTypedEntityFilter<ToscaPolicy> filter) throws PfModelException {
162         return new ArrayList<>();
163     }
164
165     @Override
166     public ToscaServiceTemplate createPolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
167         return serviceTemplate;
168     }
169
170     @Override
171     public ToscaServiceTemplate updatePolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
172         return serviceTemplate;
173     }
174
175     @Override
176     public ToscaServiceTemplate deletePolicy(final String name, final String version) throws PfModelException {
177         return getDummyResponse("dummyimpl/DummyToscaPolicyDeleteResponse.json");
178     }
179
180     @Override
181     public ToscaServiceTemplate createToscaNodeTemplates(@NonNull ToscaServiceTemplate serviceTemplate)
182         throws PfModelException {
183         return getDummyNodeTemplateResponse("dummyimpl/DummyToscaNodeTemplateResponse.json");
184     }
185
186     @Override
187     public ToscaServiceTemplate updateToscaNodeTemplates(@NonNull ToscaServiceTemplate serviceTemplate)
188         throws PfModelRuntimeException, PfModelException {
189         return getDummyNodeTemplateResponse("dummyimpl/DummyToscaNodeTemplateResponse.json");
190     }
191
192     @Override
193     public ToscaServiceTemplate deleteToscaNodeTemplate(@NonNull String name, @NonNull String version)
194         throws PfModelException {
195         return getDummyNodeTemplateResponse("dummyimpl/DummyToscaNodeTemplateResponse.json");
196     }
197
198     @Override
199     public List<Map<ToscaEntityKey, Map<String, Object>>> getNodeTemplateMetadataSets(@NonNull String name,
200                                                                                       @NonNull String version)
201         throws PfModelException {
202         return new ArrayList<>();
203     }
204
205     @Override
206     public List<Map<PfConceptKey, ToscaNodeTemplate>> getToscaNodeTemplate(final String name,
207                                                                            final String version)
208         throws PfModelException {
209         return new ArrayList<>();
210     }
211
212     @Override
213     public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
214         return new ArrayList<>();
215     }
216
217     @Override
218     public List<PdpGroup> getFilteredPdpGroups(PdpGroupFilter filter) throws PfModelException {
219         return new ArrayList<>();
220     }
221
222     @Override
223     public List<PdpGroup> createPdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException {
224         return new ArrayList<>();
225     }
226
227     @Override
228     public List<PdpGroup> updatePdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException {
229         return new ArrayList<>();
230     }
231
232     @Override
233     public void updatePdpSubGroup(final String pdpGroupName, final PdpSubGroup pdpSubGroup) throws PfModelException {
234         // Not implemented
235     }
236
237     @Override
238     public void updatePdp(String pdpGroupName, String pdpSubGroup, Pdp pdp) throws PfModelException {
239         // Not implemented
240     }
241
242     @Override
243     public PdpGroup deletePdpGroup(final String name) throws PfModelException {
244         return null;
245     }
246
247     @Override
248     public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
249         // Not implemented
250         return new ArrayList<>();
251     }
252
253     @Override
254     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
255             throws PfModelException {
256         // Not implemented
257         return new ArrayList<>();
258     }
259
260     @Override
261     public List<PdpPolicyStatus> getGroupPolicyStatus(String groupName) throws PfModelException {
262         // Not implemented
263         return new ArrayList<>();
264     }
265
266     @Override
267     public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
268             Collection<PdpPolicyStatus> deleteObjs) {
269         // Not implemented
270     }
271
272     /**
273      * Return a ToscaServicetemplate dummy response.
274      *
275      * @param fileName the file name containing the dummy response
276      * @return the ToscaServiceTemplate with the dummy response
277      */
278     protected ToscaServiceTemplate getDummyResponse(final String fileName) {
279         var standardCoder = new StandardCoder();
280         ToscaServiceTemplate serviceTemplate;
281
282         try {
283             serviceTemplate =
284                     standardCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
285             if (serviceTemplate == null) {
286                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
287             }
288         } catch (Exception exc) {
289             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
290         }
291
292         return serviceTemplate;
293     }
294
295     /**
296      * Return a tosca node template dummy response.
297      *
298      * @param fileName the file name containing the dummy response
299      * @return the service template with the dummy response
300      */
301     protected ToscaServiceTemplate getDummyNodeTemplateResponse(final String fileName) {
302         var standardCoder = new StandardCoder();
303         ToscaServiceTemplate serviceTemplate;
304
305         try {
306             serviceTemplate =
307                 standardCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
308             if (serviceTemplate == null) {
309                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
310             }
311         } catch (Exception exc) {
312             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
313         }
314
315         return serviceTemplate;
316     }
317 }