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