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