Merge "Restructure for authorative 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 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 com.google.gson.Gson;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.ws.rs.core.Response;
30 import lombok.NonNull;
31 import org.onap.policy.common.utils.resources.ResourceUtils;
32 import org.onap.policy.models.base.PfConceptKey;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.pdp.concepts.PdpGroups;
36 import org.onap.policy.models.provider.PolicyModelsProvider;
37 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
38 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
39 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
40 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
42 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
43
44 /**
45  * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  * @author Chenfei Gao (cgao@research.att.com)
49  */
50 public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider {
51     /**
52      * Constructor that takes the parameters.
53      *
54      * @param parameters the parameters for the provider
55      */
56     public DummyPolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {}
57
58     @Override
59     public void init() throws PfModelException {
60         // Not required on the dummy provider
61     }
62
63     @Override
64     public void close() {
65         // Not required on the dummy provider
66     }
67
68     @Override
69     public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException {
70         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
71     }
72
73     @Override
74     public JpaToscaServiceTemplate createPolicyTypes(@NonNull final JpaToscaServiceTemplate serviceTemplate)
75             throws PfModelException {
76         return serviceTemplate;
77     }
78
79     @Override
80     public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final JpaToscaServiceTemplate serviceTemplate)
81             throws PfModelException {
82         return serviceTemplate;
83     }
84
85     @Override
86     public JpaToscaServiceTemplate deletePolicyTypes(@NonNull final PfConceptKey policyTypeKey)
87             throws PfModelException {
88         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeDeleteResponse.json");
89     }
90
91     @Override
92     public JpaToscaServiceTemplate getPolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
93         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
94     }
95
96     @Override
97     public JpaToscaServiceTemplate createPolicies(@NonNull final JpaToscaServiceTemplate serviceTemplate)
98             throws PfModelException {
99         return serviceTemplate;
100     }
101
102     @Override
103     public JpaToscaServiceTemplate updatePolicies(@NonNull final JpaToscaServiceTemplate serviceTemplate)
104             throws PfModelException {
105         return serviceTemplate;
106     }
107
108     @Override
109     public JpaToscaServiceTemplate deletePolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
110         return getDummyResponse("dummyimpl/DummyToscaPolicyDeleteResponse.json");
111     }
112
113     @Override
114
115     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
116         return new LegacyOperationalPolicy();
117     }
118
119     @Override
120     public LegacyOperationalPolicy createOperationalPolicy(
121             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
122         return legacyOperationalPolicy;
123     }
124
125     @Override
126     public LegacyOperationalPolicy updateOperationalPolicy(
127             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
128         return legacyOperationalPolicy;
129     }
130
131     @Override
132     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
133         return new LegacyOperationalPolicy();
134     }
135
136     @Override
137     public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
138         return new HashMap<>();
139     }
140
141     @Override
142     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
143             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
144         return new HashMap<>();
145     }
146
147     @Override
148     public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
149             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
150         return new HashMap<>();
151     }
152
153     @Override
154     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
155             throws PfModelException {
156         return new HashMap<>();
157     }
158
159     @Override
160     public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
161         return new PdpGroups();
162     }
163
164     @Override
165     public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
166         return new PdpGroups();
167     }
168
169     @Override
170     public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
171         return new PdpGroups();
172     }
173
174     @Override
175     public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
176         return new PdpGroups();
177     }
178
179     /**
180      * Return a ToscaServicetemplate dummy response.
181      *
182      * @param fileName the file name containing the dummy response
183      * @return the ToscaServiceTemplate with the dummy response
184      */
185     protected JpaToscaServiceTemplate getDummyResponse(@NonNull final String fileName) {
186         Gson gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
187         JpaToscaServiceTemplate serviceTemplate;
188
189         try {
190             serviceTemplate = gson.fromJson(ResourceUtils.getResourceAsString(fileName), JpaToscaServiceTemplate.class);
191             if (serviceTemplate == null) {
192                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
193             }
194         } catch (Exception exc) {
195             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
196         }
197
198         return serviceTemplate;
199     }
200 }