Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / handler / DictionaryHandlerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.handler;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.onap.policy.common.logging.eelf.MessageCodes;
27 import org.onap.policy.common.logging.eelf.PolicyLogger;
28 import org.onap.policy.pap.xacml.rest.service.DictionaryService;
29 import org.onap.policy.xacml.api.XACMLErrorConstants;
30
31 public class DictionaryHandlerImpl implements DictionaryHandler {
32     /*
33      * Get Equivalent for Dictionary Services.
34      */
35     @Override
36     public void doDictionaryAPIGet(HttpServletRequest request, HttpServletResponse response) {
37         String dictionaryType = request.getParameter("dictionaryType");
38         try {
39             DictionaryService dictionary = new DictionaryService();
40             switch (dictionaryType) {
41                 case "OnapName":
42                     dictionary.getOnapDictionary(response);
43                     break;
44                 case "Attribute":
45                     dictionary.getAttributeDictionary(response);
46                     break;
47                 case "Action":
48                     dictionary.getActionPolicyDictionary(response);
49                     break;
50                 case "BRMSParamTemplate":
51                     dictionary.getBRMSParamDictionary(response);
52                     break;
53                 case "VSCLAction":
54                     dictionary.getVSCLAction(response);
55                     break;
56                 case "VNFType":
57                     dictionary.getVnfType(response);
58                     break;
59                 case "PEPOptions":
60                     dictionary.getPEPOptions(response);
61                     break;
62                 case "Varbind":
63                     dictionary.getVarbind(response);
64                     break;
65                 case "Service":
66                     dictionary.getServiceType(response);
67                     break;
68                 case "Site":
69                     dictionary.getSiteType(response);
70                     break;
71                 case "Settings":
72                     dictionary.getSettingsDictionary(response);
73                     break;
74                 case "RainyDayTreatments":
75                     dictionary.getRainyDayDictionary(response);
76                     break;
77                 case "DescriptiveScope":
78                     dictionary.getDescriptiveDictionary(response);
79                     break;
80                 case "ActionList":
81                     dictionary.getActionListDictionary(response);
82                     break;
83                 case "ProtocolList":
84                     dictionary.getProtocolListDictionary(response);
85                     break;
86                 case "Zone":
87                     dictionary.getZoneDictionary(response);
88                     break;
89                 case "SecurityZone":
90                     dictionary.getSecurityZoneDictionary(response);
91                     break;
92                 case "PrefixList":
93                     dictionary.getPrefixListDictionary(response);
94                     break;
95                 case "AddressGroup":
96                     dictionary.getAddressGroupDictionary(response);
97                     break;
98                 case "ServiceGroup":
99                     dictionary.getServiceGroupDictionary(response);
100                     break;
101                 case "ServiceList":
102                     dictionary.getServiceListDictionary(response);
103                     break;
104                 case "TermList":
105                 case "RuleList":
106                 case "FirewallRuleList":
107                 case "Term":
108                     dictionary.getTermListDictionary(response);
109                     break;
110                 case "MicroServiceLocation":
111                     dictionary.getMicroServiceLocationDictionary(response);
112                     break;
113                 case "MicroServiceConfigName":
114                     dictionary.getMicroServiceConfigNameDictionary(response);
115                     break;
116                 case "MicroServiceDictionaryData":
117                     dictionary.getMicroServiceDictionaryData(response);
118                     break;
119                 case "DCAEUUID":
120                     dictionary.getDCAEUUIDDictionary(response);
121                     break;
122                 case "MicroServiceModels":
123                     dictionary.getMicroServiceModelsDictionary(response);
124                     break;
125                 case "MicroServiceDictionary":
126                     dictionary.getMicroServiceDictionary(response);
127                     break;
128                 case "OptimizationModels":
129                     dictionary.getOptimizationModelsDictionary(response);
130                     break;
131                 case "PolicyScopeService":
132                     dictionary.getPSServiceDictionary(response);
133                     break;
134                 case "PolicyScopeResource":
135                     dictionary.getPSResourceDictionary(response);
136                     break;
137                 case "PolicyScopeType":
138                     dictionary.getPSTypeDictionary(response);
139                     break;
140                 case "PolicyScopeClosedLoop":
141                     dictionary.getPSClosedLoopDictionary(response);
142                     break;
143                 case "GroupPolicyScopeList":
144                     dictionary.getPSGroupScopeDictionary(response);
145                     break;
146                 case "RiskType":
147                     dictionary.getRiskTypeDictionary(response);
148                     break;
149                 case "SafePolicyWarning":
150                     dictionary.getSafePolicyWarningDictionary(response);
151                     break;
152                 default:
153                     extendedOptions(dictionaryType, request, response, true);
154             }
155         } catch (Exception e) {
156             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Querying the Database: " + e.getMessage();
157             PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "DictionaryHandler", " Error Querying the Database.");
158             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
159             response.addHeader("error", "dictionaryDBQuery");
160             response.addHeader("error", message);
161         }
162     }
163
164     /**
165      * Can be used to extend the services.
166      * <p>
167      * getflag=true indicates Get Request. getflag=false indicates Put Request.
168      *
169      * @return
170      */
171     @Override
172     public String extendedOptions(String dictionaryType, HttpServletRequest request, HttpServletResponse response,
173             boolean getflag) {
174         // Default code
175         String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Invalid Dictionary in Request.";
176         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "DictionaryHandler", " Invalid Dictionary in Request.");
177         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
178         response.setHeader("error", "dictionary");
179         response.addHeader("error", message);
180         return null;
181     }
182
183     @Override
184     public void doDictionaryAPIPut(HttpServletRequest request, HttpServletResponse response) {
185         String result = null;
186         String dictionaryType = request.getParameter("dictionaryType");
187         String operation = request.getParameter("operation");
188         try {
189             DictionaryService dictionary = new DictionaryService();
190             switch (dictionaryType) {
191                 case "OnapName":
192                     result = dictionary.saveOnapDictionary(request, response);
193                     break;
194                 case "Attribute":
195                     result = dictionary.saveAttributeDictionary(request, response);
196                     break;
197                 case "Action":
198                     result = dictionary.saveActionPolicyDictionary(request, response);
199                     break;
200                 case "BRMSParamTemplate":
201                     result = dictionary.saveBRMSParamDictionary(request, response);
202                     break;
203                 case "VSCLAction":
204                     result = dictionary.saveVSCLAction(request, response);
205                     break;
206                 case "VNFType":
207                     result = dictionary.saveVnfType(request, response);
208                     break;
209                 case "PEPOptions":
210                     result = dictionary.savePEPOptions(request, response);
211                     break;
212                 case "Varbind":
213                     result = dictionary.saveVarbind(request, response);
214                     break;
215                 case "Service":
216                     result = dictionary.saveServiceType(request, response);
217                     break;
218                 case "Site":
219                     result = dictionary.saveSiteType(request, response);
220                     break;
221                 case "Settings":
222                     result = dictionary.saveSettingsDictionary(request, response);
223                     break;
224                 case "RainyDayTreatments":
225                     result = dictionary.saveRainyDayDictionary(request, response);
226                     break;
227                 case "DescriptiveScope":
228                     result = dictionary.saveDescriptiveDictionary(request, response);
229                     break;
230                 case "ActionList":
231                     result = dictionary.saveActionListDictionary(request, response);
232                     break;
233                 case "ProtocolList":
234                     result = dictionary.saveProtocolListDictionary(request, response);
235                     break;
236                 case "Zone":
237                     result = dictionary.saveZoneDictionary(request, response);
238                     break;
239                 case "SecurityZone":
240                     result = dictionary.saveSecurityZoneDictionary(request, response);
241                     break;
242                 case "PrefixList":
243                     result = dictionary.savePrefixListDictionary(request, response);
244                     break;
245                 case "AddressGroup":
246                     result = dictionary.saveAddressGroupDictionary(request, response);
247                     break;
248                 case "ServiceGroup":
249                     result = dictionary.saveServiceGroupDictionary(request, response);
250                     break;
251                 case "ServiceList":
252                     result = dictionary.saveServiceListDictionary(request, response);
253                     break;
254                 case "TermList":
255                 case "RuleList":
256                 case "FirewallRuleList":
257                 case "Term":
258                     result = dictionary.saveTermListDictionary(request, response);
259                     break;
260                 case "MicroServiceLocation":
261                     result = dictionary.saveMicroServiceLocationDictionary(request, response);
262                     break;
263                 case "MicroServiceConfigName":
264                     result = dictionary.saveMicroServiceConfigNameDictionary(request, response);
265                     break;
266                 case "MicroServiceDictionaryData":
267                     dictionary.saveMicroServiceDictionaryData(request, response);
268                     break;
269                 case "DCAEUUID":
270                     result = dictionary.saveDCAEUUIDDictionary(request, response);
271                     break;
272                 case "MicroServiceModels":
273                     result = dictionary.saveMicroServiceModelsDictionary(request, response);
274                     break;
275                 case "MicroServiceDictionary":
276                     result = dictionary.saveMicroServiceDictionary(request, response);
277                     break;
278                 case "OptimizationModels":
279                     result = dictionary.saveOptimizationModelsDictionary(request, response);
280                     break;
281                 case "PolicyScopeService":
282                     result = dictionary.savePSServiceDictionary(request, response);
283                     break;
284                 case "PolicyScopeResource":
285                     result = dictionary.savePSResourceDictionary(request, response);
286                     break;
287                 case "PolicyScopeType":
288                     result = dictionary.savePSTypeDictionary(request, response);
289                     break;
290                 case "PolicyScopeClosedLoop":
291                     result = dictionary.savePSClosedLoopDictionary(request, response);
292                     break;
293                 case "GroupPolicyScopeList":
294                     result = dictionary.savePSGroupScopeDictionary(request, response);
295                     break;
296                 case "RiskType":
297                     result = dictionary.saveRiskTypeDictionary(request, response);
298                     break;
299                 case "SafePolicyWarning":
300                     result = dictionary.saveSafePolicyWarningDictionary(request, response);
301                     break;
302                 default:
303                     result = extendedOptions(dictionaryType, request, response, false);
304                     if (result == null) {
305                         return;
306                     } else {
307                         break;
308                     }
309             }
310         } catch (Exception e) {
311             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Updating the Database: " + e.getMessage();
312             PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " Error Updating the Database.");
313             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
314             response.addHeader("error", message);
315             return;
316         }
317         if (("Success").equalsIgnoreCase(result)) {
318             response.setStatus(HttpServletResponse.SC_OK);
319             response.addHeader("successMapKey", "success");
320             if (operation.equalsIgnoreCase("update")) {
321                 response.addHeader("operation", "updateDictionary");
322             } else {
323                 response.addHeader("operation", "createDictionary");
324             }
325         } else if (("Duplicate").equalsIgnoreCase(result)) {
326             response.setStatus(HttpServletResponse.SC_CONFLICT);
327             response.addHeader("error", "dictionaryItemExists");
328         } else if (("DuplicateGroup").equalsIgnoreCase(result)) {
329             response.setStatus(HttpServletResponse.SC_CONFLICT);
330             response.addHeader("error", "duplicateGroup");
331         } else {
332             String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Updating the Database.";
333             PolicyLogger.error(message);
334             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
335             response.addHeader("error", message);
336         }
337     }
338 }