Fix sonars in policy models
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DummyPolicyModelsProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 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.time.Instant;
26 import java.util.ArrayList;
27 import java.util.Collection;
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.ToscaEntityFilter;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaTypedEntityFilter;
49
50 /**
51  * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework.
52  *
53  * @author Liam Fallon (liam.fallon@est.tech)
54  * @author Chenfei Gao (cgao@research.att.com)
55  */
56 public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider {
57     /**
58      * Constructor that takes the parameters.
59      *
60      * @param parameters the parameters for the provider
61      */
62     public DummyPolicyModelsProviderImpl(final PolicyModelsProviderParameters parameters) {
63         // Default constructor
64     }
65
66     @Override
67     public void init() throws PfModelException {
68         // Not required on the dummy provider
69     }
70
71     @Override
72     public void close() {
73         // Not required on the dummy provider
74     }
75
76
77     @Override
78     public List<ToscaServiceTemplate> getServiceTemplateList(String name, String version) throws PfModelException {
79         return new ArrayList<>();
80     }
81
82     @Override
83     public List<ToscaServiceTemplate> getFilteredServiceTemplateList(
84             @NonNull ToscaEntityFilter<ToscaServiceTemplate> filter) throws PfModelException {
85         return new ArrayList<>();
86     }
87
88     @Override
89     public ToscaServiceTemplate createServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate)
90             throws PfModelException {
91         return serviceTemplate;
92     }
93
94     @Override
95     public ToscaServiceTemplate updateServiceTemplate(@NonNull ToscaServiceTemplate serviceTemplate)
96             throws PfModelException {
97         return serviceTemplate;
98     }
99
100     @Override
101     public ToscaServiceTemplate deleteServiceTemplate(@NonNull String name, @NonNull String version)
102             throws PfModelException {
103         return getDummyResponse("dummyimpl/DummyToscaServiceTemplateDeleteResponse.json");
104     }
105
106     @Override
107     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
108         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
109     }
110
111     @Override
112     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
113         return new ArrayList<>();
114     }
115
116     @Override
117     public ToscaServiceTemplate getFilteredPolicyTypes(ToscaEntityFilter<ToscaPolicyType> filter)
118             throws PfModelException {
119         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
120     }
121
122     @Override
123     public List<ToscaPolicyType> getFilteredPolicyTypeList(ToscaEntityFilter<ToscaPolicyType> 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(ToscaTypedEntityFilter<ToscaPolicy> filter)
154             throws PfModelException {
155         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
156     }
157
158     @Override
159     public List<ToscaPolicy> getFilteredPolicyList(ToscaTypedEntityFilter<ToscaPolicy> filter) throws PfModelException {
160         return new ArrayList<>();
161     }
162
163     @Override
164     public ToscaServiceTemplate createPolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
165         return serviceTemplate;
166     }
167
168     @Override
169     public ToscaServiceTemplate updatePolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
170         return serviceTemplate;
171     }
172
173     @Override
174     public ToscaServiceTemplate deletePolicy(final String name, final String version) throws PfModelException {
175         return getDummyResponse("dummyimpl/DummyToscaPolicyDeleteResponse.json");
176     }
177
178     @Override
179     public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
180         return new ArrayList<>();
181     }
182
183     @Override
184     public List<PdpGroup> getFilteredPdpGroups(PdpGroupFilter filter) throws PfModelException {
185         return new ArrayList<>();
186     }
187
188     @Override
189     public List<PdpGroup> createPdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException {
190         return new ArrayList<>();
191     }
192
193     @Override
194     public List<PdpGroup> updatePdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException {
195         return new ArrayList<>();
196     }
197
198     @Override
199     public void updatePdpSubGroup(final String pdpGroupName, final PdpSubGroup pdpSubGroup) throws PfModelException {
200         // Not implemented
201     }
202
203     @Override
204     public void updatePdp(String pdpGroupName, String pdpSubGroup, Pdp pdp) throws PfModelException {
205         // Not implemented
206     }
207
208     @Override
209     public PdpGroup deletePdpGroup(final String name) throws PfModelException {
210         return null;
211     }
212
213     @Override
214     public List<PdpStatistics> getPdpStatistics(final String name, final Instant timestamp) throws PfModelException {
215         return new ArrayList<>();
216     }
217
218     @Override
219     public List<PdpStatistics> getFilteredPdpStatistics(String name, String pdpGroupName, String pdpSubGroup,
220             Instant startTimeStamp, Instant endTimeStamp, String sortOrder, int getRecordNum) {
221         // Not implemented
222         return new ArrayList<>();
223     }
224
225     @Override
226     public List<PdpStatistics> createPdpStatistics(final List<PdpStatistics> pdpStatisticsList)
227             throws PfModelException {
228         // Not implemented
229         return new ArrayList<>();
230     }
231
232     @Override
233     public List<PdpStatistics> updatePdpStatistics(final List<PdpStatistics> pdpStatisticsList)
234             throws PfModelException {
235         // Not implemented
236         return new ArrayList<>();
237     }
238
239     @Override
240     public List<PdpStatistics> deletePdpStatistics(final String name, final Instant timestamp) {
241         // Not implemented
242         return new ArrayList<>();
243     }
244
245     @Override
246     public List<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
247         // Not implemented
248         return new ArrayList<>();
249     }
250
251     @Override
252     public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
253             throws PfModelException {
254         // Not implemented
255         return new ArrayList<>();
256     }
257
258     @Override
259     public List<PdpPolicyStatus> getGroupPolicyStatus(String groupName) throws PfModelException {
260         // Not implemented
261         return new ArrayList<>();
262     }
263
264     @Override
265     public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs,
266             Collection<PdpPolicyStatus> deleteObjs) {
267         // Not implemented
268     }
269
270     /**
271      * Return a ToscaServicetemplate dummy response.
272      *
273      * @param fileName the file name containing the dummy response
274      * @return the ToscaServiceTemplate with the dummy response
275      */
276     protected ToscaServiceTemplate getDummyResponse(final String fileName) {
277         var standardCoder = new StandardCoder();
278         ToscaServiceTemplate serviceTemplate;
279
280         try {
281             serviceTemplate =
282                     standardCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
283             if (serviceTemplate == null) {
284                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
285             }
286         } catch (Exception exc) {
287             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
288         }
289
290         return serviceTemplate;
291     }
292 }