Implement Database provider
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.provider.impl;
22
23 import com.google.gson.Gson;
24
25 import javax.ws.rs.core.Response;
26
27 import lombok.NonNull;
28
29 import org.onap.policy.common.utils.resources.TextFileUtils;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.pap.concepts.PdpGroups;
34 import org.onap.policy.models.provider.PolicyModelsProvider;
35 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
36 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy;
37 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
39 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
40
41 /**
42  * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider {
47     /**
48      * Constructor that takes the parameters.
49      *
50      * @param parameters the parameters for the provider
51      */
52     public DummyPolicyModelsProviderImpl(@NonNull final PolicyModelsProviderParameters parameters) {
53     }
54
55     @Override
56     public void init() throws PfModelException {
57         // Not required on the dummy provider
58     }
59
60     @Override
61     public void close() {
62         // Not required on the dummy provider
63     }
64
65     @Override
66     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException {
67         return getDummyResponse("src/main/resources/dummyimpl/DummyToscaPolicyTypeGetResponse.json");
68     }
69
70     @Override
71     public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
72             throws PfModelException {
73         return serviceTemplate;
74     }
75
76     @Override
77     public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
78             throws PfModelException {
79         return serviceTemplate;
80     }
81
82     @Override
83     public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException {
84         return getDummyResponse("src/main/resources/dummyimpl/DummyToscaPolicyTypeDeleteResponse.json");
85     }
86
87     @Override
88     public ToscaServiceTemplate getPolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
89         return getDummyResponse("src/main/resources/dummyimpl/DummyToscaPolicyGetResponse.json");
90     }
91
92     @Override
93     public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
94             throws PfModelException {
95         return serviceTemplate;
96     }
97
98     @Override
99     public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
100             throws PfModelException {
101         return serviceTemplate;
102     }
103
104     @Override
105     public ToscaServiceTemplate deletePolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
106         return getDummyResponse("src/main/resources/dummyimpl/DummyToscaPolicyDeleteResponse.json");
107     }
108
109     @Override
110
111     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
112         return new LegacyOperationalPolicy();
113     }
114
115     @Override
116     public LegacyOperationalPolicy createOperationalPolicy(
117             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
118         return legacyOperationalPolicy;
119     }
120
121     @Override
122     public LegacyOperationalPolicy updateOperationalPolicy(
123             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
124         return legacyOperationalPolicy;
125     }
126
127     @Override
128     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
129         return new LegacyOperationalPolicy();
130     }
131
132     @Override
133     public LegacyGuardPolicy getGuardPolicy(@NonNull final String policyId) throws PfModelException {
134         return new LegacyGuardPolicy();
135     }
136
137     @Override
138     public LegacyGuardPolicy createGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy)
139             throws PfModelException {
140         return legacyGuardPolicy;
141     }
142
143     @Override
144     public LegacyGuardPolicy updateGuardPolicy(@NonNull final LegacyGuardPolicy legacyGuardPolicy)
145             throws PfModelException {
146         return legacyGuardPolicy;
147     }
148
149     @Override
150     public LegacyGuardPolicy deleteGuardPolicy(@NonNull final String policyId) throws PfModelException {
151         return new LegacyGuardPolicy();
152     }
153
154     @Override
155     public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
156         return new PdpGroups();
157     }
158
159     @Override
160     public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
161         return new PdpGroups();
162     }
163
164     @Override
165     public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
166         return new PdpGroups();
167     }
168
169     @Override
170     public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
171         return new PdpGroups();
172     }
173
174     /**
175      * Return a ToscaServicetemplate dummy response.
176      *
177      * @param fileName the file name containing the dummy response
178      * @return the ToscaServiceTemplate with the dummy response
179      */
180     protected ToscaServiceTemplate getDummyResponse(@NonNull final String fileName) {
181         Gson gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
182         ToscaServiceTemplate serviceTemplate;
183
184         try {
185             serviceTemplate = gson.fromJson(TextFileUtils.getTextFileAsString(fileName), ToscaServiceTemplate.class);
186         } catch (Exception exc) {
187             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
188         }
189
190         return serviceTemplate;
191     }
192 }