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