Add support for legacy guard policies
[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.ToscaServiceTemplate;
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 ToscaServiceTemplate getPolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException {
70         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeGetResponse.json");
71     }
72
73     @Override
74     public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
75             throws PfModelException {
76         return serviceTemplate;
77     }
78
79     @Override
80     public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate)
81             throws PfModelException {
82         return serviceTemplate;
83     }
84
85     @Override
86     public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfConceptKey policyTypeKey) throws PfModelException {
87         return getDummyResponse("dummyimpl/DummyToscaPolicyTypeDeleteResponse.json");
88     }
89
90     @Override
91     public ToscaServiceTemplate getPolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
92         return getDummyResponse("dummyimpl/DummyToscaPolicyGetResponse.json");
93     }
94
95     @Override
96     public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
97             throws PfModelException {
98         return serviceTemplate;
99     }
100
101     @Override
102     public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate)
103             throws PfModelException {
104         return serviceTemplate;
105     }
106
107     @Override
108     public ToscaServiceTemplate deletePolicies(@NonNull final PfConceptKey policyKey) throws PfModelException {
109         return getDummyResponse("dummyimpl/DummyToscaPolicyDeleteResponse.json");
110     }
111
112     @Override
113
114     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId) throws PfModelException {
115         return new LegacyOperationalPolicy();
116     }
117
118     @Override
119     public LegacyOperationalPolicy createOperationalPolicy(
120             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
121         return legacyOperationalPolicy;
122     }
123
124     @Override
125     public LegacyOperationalPolicy updateOperationalPolicy(
126             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
127         return legacyOperationalPolicy;
128     }
129
130     @Override
131     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId) throws PfModelException {
132         return new LegacyOperationalPolicy();
133     }
134
135     @Override
136     public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId) throws PfModelException {
137         return new HashMap<>();
138     }
139
140     @Override
141     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(
142             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
143         return new HashMap<>();
144     }
145
146     @Override
147     public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(
148             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
149         return new HashMap<>();
150     }
151
152     @Override
153     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId)
154             throws PfModelException {
155         return new HashMap<>();
156     }
157
158     @Override
159     public PdpGroups getPdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
160         return new PdpGroups();
161     }
162
163     @Override
164     public PdpGroups createPdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
165         return new PdpGroups();
166     }
167
168     @Override
169     public PdpGroups updatePdpGroups(@NonNull PdpGroups pdpGroups) throws PfModelException {
170         return new PdpGroups();
171     }
172
173     @Override
174     public PdpGroups deletePdpGroups(@NonNull String pdpGroupFilter) throws PfModelException {
175         return new PdpGroups();
176     }
177
178     /**
179      * Return a ToscaServicetemplate dummy response.
180      *
181      * @param fileName the file name containing the dummy response
182      * @return the ToscaServiceTemplate with the dummy response
183      */
184     protected ToscaServiceTemplate getDummyResponse(@NonNull final String fileName) {
185         Gson gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
186         ToscaServiceTemplate serviceTemplate;
187
188         try {
189             serviceTemplate = gson.fromJson(ResourceUtils.getResourceAsString(fileName), ToscaServiceTemplate.class);
190             if (serviceTemplate == null) {
191                 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, "error reading specified file");
192             }
193         } catch (Exception exc) {
194             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "error serializing object", exc);
195         }
196
197         return serviceTemplate;
198     }
199 }