c09c6c2338a915673b933db759ee19c9f8c45ff8
[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 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.provider.impl;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.Response;
31
32 import org.onap.policy.common.utils.coder.StandardCoder;
33 import org.onap.policy.common.utils.resources.ResourceUtils;
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.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.ToscaPolicy;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
49 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
50 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
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     @Override
79     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
80         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
81     }
82
83     @Override
84     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
85         return new ArrayList<>();
86     }
87
88     @Override
89     public ToscaServiceTemplate getFilteredPolicyTypes(ToscaPolicyTypeFilter filter) throws PfModelException {
90         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
91     }
92
93     @Override
94     public List<ToscaPolicyType> getFilteredPolicyTypeList(ToscaPolicyTypeFilter filter) {
95         return new ArrayList<>();
96     }
97
98     @Override
99     public ToscaServiceTemplate createPolicyTypes(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
100         return serviceTemplate;
101     }
102
103     @Override
104     public ToscaServiceTemplate updatePolicyTypes(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
105         return serviceTemplate;
106     }
107
108     @Override
109     public ToscaServiceTemplate deletePolicyType(final String name, final String version) throws PfModelException {
110         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeDeleteResponse.json");
111     }
112
113     @Override
114     public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
115         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
116     }
117
118     @Override
119     public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
120         return new ArrayList<>();
121     }
122
123     @Override
124     public ToscaServiceTemplate getFilteredPolicies(ToscaPolicyFilter filter) throws PfModelException {
125         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
126     }
127
128     @Override
129     public List<ToscaPolicy> getFilteredPolicyList(ToscaPolicyFilter filter) throws PfModelException {
130         return new ArrayList<>();
131     }
132
133     @Override
134     public ToscaServiceTemplate createPolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
135         return serviceTemplate;
136     }
137
138     @Override
139     public ToscaServiceTemplate updatePolicies(final ToscaServiceTemplate serviceTemplate) throws PfModelException {
140         return serviceTemplate;
141     }
142
143     @Override
144     public ToscaServiceTemplate deletePolicy(final String name, final String version) throws PfModelException {
145         return getDummyResponse("dummyimpl/DummyToscaPolicyDeleteResponse.json");
146     }
147
148     @Override
149
150     public LegacyOperationalPolicy getOperationalPolicy(final String policyId, final String policyVersion)
151             throws PfModelException {
152         return new LegacyOperationalPolicy();
153     }
154
155     @Override
156     public LegacyOperationalPolicy createOperationalPolicy(final LegacyOperationalPolicy legacyOperationalPolicy)
157             throws PfModelException {
158         return legacyOperationalPolicy;
159     }
160
161     @Override
162     public LegacyOperationalPolicy updateOperationalPolicy(final LegacyOperationalPolicy legacyOperationalPolicy)
163             throws PfModelException {
164         return legacyOperationalPolicy;
165     }
166
167     @Override
168     public LegacyOperationalPolicy deleteOperationalPolicy(final String policyId, final String policyVersion)
169             throws PfModelException {
170         return new LegacyOperationalPolicy();
171     }
172
173     @Override
174     public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(final String policyId, final String policyVersion)
175             throws PfModelException {
176         return new HashMap<>();
177     }
178
179     @Override
180     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(final LegacyGuardPolicyInput legacyGuardPolicy)
181             throws PfModelException {
182         return new HashMap<>();
183     }
184
185     @Override
186     public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(final LegacyGuardPolicyInput legacyGuardPolicy)
187             throws PfModelException {
188         return new HashMap<>();
189     }
190
191     @Override
192     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(final String policyId, final String policyVersion)
193             throws PfModelException {
194         return new HashMap<>();
195     }
196
197     @Override
198     public List<PdpGroup> getPdpGroups(final String name) throws PfModelException {
199         return new ArrayList<>();
200     }
201
202     @Override
203     public List<PdpGroup> getFilteredPdpGroups(PdpGroupFilter filter) throws PfModelException {
204         return new ArrayList<>();
205     }
206
207     @Override
208     public List<PdpGroup> createPdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException {
209         return new ArrayList<>();
210     }
211
212     @Override
213     public List<PdpGroup> updatePdpGroups(final List<PdpGroup> pdpGroups) throws PfModelException {
214         return new ArrayList<>();
215     }
216
217     @Override
218     public void updatePdpSubGroup(final String pdpGroupName, final PdpSubGroup pdpSubGroup) throws PfModelException {
219         // Not implemented
220     }
221
222     @Override
223     public void updatePdp(String pdpGroupName, String pdpSubGroup, Pdp pdp) throws PfModelException {
224         // Not implemented
225     }
226
227     @Override
228     public PdpGroup deletePdpGroup(final String name) throws PfModelException {
229         return null;
230     }
231
232     @Override
233     public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException {
234         return new ArrayList<>();
235     }
236
237     @Override
238     public List<PdpStatistics> getFilteredPdpStatistics(String name, String pdpGroupName, String pdpSubGroup,
239             Date startTimeStamp, Date endTimeStamp, String sortOrder, int getRecordNum) {
240         // Not implemented
241         return new ArrayList<>();
242     }
243
244     @Override
245     public List<PdpStatistics> createPdpStatistics(final List<PdpStatistics> pdpStatisticsList)
246             throws PfModelException {
247         // Not implemented
248         return new ArrayList<>();
249     }
250
251     @Override
252     public List<PdpStatistics> updatePdpStatistics(final List<PdpStatistics> pdpStatisticsList)
253             throws PfModelException {
254         // Not implemented
255         return new ArrayList<>();
256     }
257
258     @Override
259     public List<PdpStatistics> deletePdpStatistics(final String name, final Date timestamp) {
260         // Not implemented
261         return new ArrayList<>();
262     }
263
264     /**
265      * Return a ToscaServicetemplate dummy response.
266      *
267      * @param fileName the file name containing the dummy response
268      * @return the ToscaServiceTemplate with the dummy response
269      */
270     protected ToscaServiceTemplate getDummyResponse(final String fileName) {
271         StandardCoder standardCoder = new StandardCoder();
272         ToscaServiceTemplate serviceTemplate;
273
274         try {
275             serviceTemplate =
276                     standardCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
277             if (serviceTemplate == null) {
278                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
279             }
280         } catch (Exception exc) {
281             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
282         }
283
284         return serviceTemplate;
285     }
286 }