Merge "Add Legacy Op Policy Persistence"
[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 javax.ws.rs.core.Response;
27 import lombok.NonNull;
28 import org.onap.policy.common.utils.resources.ResourceUtils;
29 import org.onap.policy.models.base.PfConceptKey;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.pap.concepts.PdpGroups;
33 import org.onap.policy.models.provider.PolicyModelsProvider;
34 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
35 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy;
36 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
39
40 /**
41  * This class provides a dummy implementation of the Policy Models Provider for the ONAP Policy Framework.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  * @author Chenfei Gao (cgao@research.att.com)
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("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("dummyimpl/DummyToscaPolicyTypeDeleteResponse.json");
85     }
86
87     @Override
88     public ToscaServiceTemplate getPolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
89         return getDummyResponse("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("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(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
186             if (serviceTemplate == null) {
187                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
188             }
189         } catch (Exception exc) {
190             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
191         }
192
193         return serviceTemplate;
194     }
195 }