Merge "Add DecisionException"
[policy/models.git] / models-provider / src / main / java / org / onap / policy / models / provider / impl / DummyPolicyModelsProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
31
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.models.base.PfModelException;
36 import org.onap.policy.models.base.PfModelRuntimeException;
37 import org.onap.policy.models.pdp.concepts.PdpGroup;
38 import org.onap.policy.models.pdp.concepts.PdpStatistics;
39 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
40 import org.onap.policy.models.provider.PolicyModelsProvider;
41 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
46 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
47 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
48
49 /**
50  * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework.
51  *
52  * @author Liam Fallon (liam.fallon@est.tech)
53  * @author Chenfei Gao (cgao@research.att.com)
54  */
55 public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider {
56     /**
57      * Constructor that takes the parameters.
58      *
59      * @param parameters the parameters for the provider
60      */
61     public DummyPolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {}
62
63     @Override
64     public void init() throws PfModelException {
65         // Not required on the dummy provider
66     }
67
68     @Override
69     public void close() {
70         // Not required on the dummy provider
71     }
72
73     @Override
74     public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException {
75         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
76     }
77
78     @Override
79     public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException {
80         return new ArrayList<>();
81     }
82
83     @Override
84     public ToscaServiceTemplate getLatestPolicyTypes(final String name) throws PfModelException {
85         return null;
86     }
87
88     @Override
89     public List<ToscaPolicyType> getLatestPolicyTypeList(final String name) throws PfModelException {
90         return new ArrayList<>();
91     }
92
93     @Override
94     public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
95             throws PfModelException {
96         return serviceTemplate;
97     }
98
99     @Override
100     public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
101             throws PfModelException {
102         return serviceTemplate;
103     }
104
105     @Override
106     public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version)
107             throws PfModelException {
108         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeDeleteResponse.json");
109     }
110
111     @Override
112     public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException {
113         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
114     }
115
116     @Override
117     public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException {
118         return new ArrayList<>();
119     }
120
121     @Override
122     public List<ToscaPolicy> getPolicyList4PolicyType(@NonNull final String policyTypeName,
123             final String policyTypeVersion) throws PfModelException {
124         return new ArrayList<>();
125     }
126
127     @Override
128     public ToscaServiceTemplate getLatestPolicies(final String name) throws PfModelException {
129         return null;
130     }
131
132     @Override
133     public List<ToscaPolicy> getLatestPolicyList(final String name) throws PfModelException {
134         return new ArrayList<>();
135     }
136
137     @Override
138     public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
139             throws PfModelException {
140         return serviceTemplate;
141     }
142
143     @Override
144     public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
145             throws PfModelException {
146         return serviceTemplate;
147     }
148
149     @Override
150     public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version)
151             throws PfModelException {
152         return getDummyResponse("dummyimpl/DummyToscaPolicyDeleteResponse.json");
153     }
154
155     @Override
156
157     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
158         return new LegacyOperationalPolicy();
159     }
160
161     @Override
162     public LegacyOperationalPolicy createOperationalPolicy(
163             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
164         return legacyOperationalPolicy;
165     }
166
167     @Override
168     public LegacyOperationalPolicy updateOperationalPolicy(
169             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
170         return legacyOperationalPolicy;
171     }
172
173     @Override
174     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
175         return new LegacyOperationalPolicy();
176     }
177
178     @Override
179     public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
180         return new HashMap<>();
181     }
182
183     @Override
184     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
185             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
186         return new HashMap<>();
187     }
188
189     @Override
190     public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
191             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
192         return new HashMap<>();
193     }
194
195     @Override
196     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
197             throws PfModelException {
198         return new HashMap<>();
199     }
200
201     @Override
202     public List<PdpGroup> getPdpGroups(final String name, final String version) throws PfModelException {
203         return new ArrayList<>();
204     }
205
206     @Override
207     public List<PdpGroup> getLatestPdpGroups(final String name) throws PfModelException {
208         return new ArrayList<>();
209     }
210
211     @Override
212     public List<PdpGroup> getFilteredPdpGroups(final String pdpType,
213             @NonNull final List<Pair<String, String>> supportedPolicyTypes) {
214         return new ArrayList<>();
215     }
216
217     @Override
218     public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
219         return new ArrayList<>();
220     }
221
222     @Override
223     public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException {
224         return new ArrayList<>();
225     }
226
227     @Override
228     public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
229             @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException {
230         // Not implemented
231     }
232
233     @Override
234     public PdpGroup deletePdpGroup(@NonNull final String name, @NonNull final String version) throws PfModelException {
235         return null;
236     }
237
238     @Override
239     public List<PdpStatistics> getPdpStatistics(final String name, final String version) throws PfModelException {
240         return new ArrayList<>();
241     }
242
243     @Override
244     public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpGroupVersion,
245             @NonNull final String pdpType, @NonNull final String pdpInstanceId,
246             @NonNull final PdpStatistics pdppStatistics) throws PfModelException {
247         // Not implemented
248     }
249
250     @Override
251     public Map<Pair<String, String>, List<ToscaPolicy>> getDeployedPolicyList(final String name)
252             throws PfModelException {
253         return null;
254     }
255
256     /**
257      * Return a ToscaServicetemplate dummy response.
258      *
259      * @param fileName the file name containing the dummy response
260      * @return the ToscaServiceTemplate with the dummy response
261      */
262     protected ToscaServiceTemplate getDummyResponse(@NonNull final String fileName) {
263         StandardCoder standardCoder = new StandardCoder();
264         ToscaServiceTemplate serviceTemplate;
265
266         try {
267             serviceTemplate =
268                     standardCoder.decode(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
269             if (serviceTemplate == null) {
270                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
271             }
272         } catch (Exception exc) {
273             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
274         }
275
276         return serviceTemplate;
277     }
278 }