f1cc9e6e4e84c4eec5cf4954375e1015c518fd98
[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 "PolicyScopeService":
121                                 dictionary.getPSServiceDictionary(response);
122                                 break;
123                         case "PolicyScopeResource":
124                                 dictionary.getPSResourceDictionary(response);
125                                 break;
126                         case "PolicyScopeType":
127                                 dictionary.getPSTypeDictionary(response);
128                                 break;
129                         case "PolicyScopeClosedLoop":
130                                 dictionary.getPSClosedLoopDictionary(response);
131                                 break;
132                         case "GroupPolicyScopeList":
133                                 dictionary.getPSGroupScopeDictionary(response);
134                                 break;
135                         case "RiskType":
136                                 dictionary.getRiskTypeDictionary(response);
137                                 break;
138                         case "SafePolicyWarning":
139                                 dictionary.getSafePolicyWarningDictionary(response);
140                                 break;
141                         case "MicroServiceDictionary":
142                                 dictionary.getMicroServiceDictionary(response);
143                                 break;
144                         default:
145                                 extendedOptions(dictionaryType, request, response, true);
146                                 return;
147                         }
148                 } catch (Exception e) {
149                         String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Querying the Database: " + e.getMessage();
150                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "DictionaryHandler", " Error Querying the Database.");
151                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
152                         response.addHeader("error", "dictionaryDBQuery");
153                         response.addHeader("error", message);
154                         return;
155                 }
156         }
157         
158         /**
159          * Can be used to extend the services.
160          * 
161          * getflag=true indicates Get Request.
162          * getflag=false indicates Put Request.  
163          * @return 
164          */
165         public String extendedOptions(String dictionaryType, HttpServletRequest request, HttpServletResponse response, boolean getflag) {
166                 // Default code
167                 String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Invalid Dictionary in Request.";
168                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, "DictionaryHandler", " Invalid Dictionary in Request.");
169                 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
170                 response.setHeader("error", "dictionary");
171                 response.addHeader("error", message);
172                 return null;
173         }
174
175         public void doDictionaryAPIPut(HttpServletRequest request, HttpServletResponse response) {
176                 String result = null;
177                 String dictionaryType = request.getParameter("dictionaryType");
178                 String operation = request.getParameter("operation");         
179                 try {
180                         DictionaryService dictionary = new DictionaryService();
181                         switch (dictionaryType) {
182                         case "OnapName":
183                                 result = dictionary.saveOnapDictionary(request, response);
184                                 break;
185                         case "Attribute":
186                                 result = dictionary.saveAttributeDictionary(request, response);
187                                 break;
188                         case "Action":
189                                 result = dictionary.saveActionPolicyDictionary(request, response);
190                                 break;
191                         case "BRMSParamTemplate":
192                                 result = dictionary.saveBRMSParamDictionary(request, response);
193                                 break;
194                         case "VSCLAction":
195                                 result = dictionary.saveVSCLAction(request, response);
196                                 break;
197                         case "VNFType":
198                                 result = dictionary.saveVnfType(request, response);
199                                 break;
200                         case "PEPOptions":
201                                 result = dictionary.savePEPOptions(request, response);
202                                 break;
203                         case "Varbind":
204                                 result = dictionary.saveVarbind(request, response);
205                                 break;
206                         case "Service":
207                                 result = dictionary.saveServiceType(request, response);
208                                 break;
209                         case "Site":
210                                 result = dictionary.saveSiteType(request, response);
211                                 break;
212                         case "Settings":
213                                 result = dictionary.saveSettingsDictionary(request, response);
214                                 break;
215                         case "RainyDayTreatments":
216                                 result = dictionary.saveRainyDayDictionary(request, response);
217                                 break;
218                         case "DescriptiveScope":
219                                 result = dictionary.saveDescriptiveDictionary(request, response);
220                                 break;
221                         case "ActionList":
222                                 result = dictionary.saveActionListDictionary(request, response);
223                                 break;
224                         case "ProtocolList":
225                                 result = dictionary.saveProtocolListDictionary(request, response);
226                                 break;
227                         case "Zone":
228                                 result = dictionary.saveZoneDictionary(request, response);
229                                 break;
230                         case "SecurityZone":
231                                 result = dictionary.saveSecurityZoneDictionary(request, response);
232                                 break;
233                         case "PrefixList":
234                                 result = dictionary.savePrefixListDictionary(request, response);
235                                 break;
236                         case "AddressGroup":
237                                 result = dictionary.saveAddressGroupDictionary(request, response);
238                                 break;
239                         case "ServiceGroup":
240                                 result = dictionary.saveServiceGroupDictionary(request, response);
241                                 break;
242                         case "ServiceList":
243                                 result = dictionary.saveServiceListDictionary(request, response);
244                                 break;
245                         case "TermList":
246                         case "RuleList":
247                         case "FirewallRuleList":
248                         case "Term":
249                                 result = dictionary.saveTermListDictionary(request, response);
250                                 break;
251                         case "MicroServiceLocation":
252                                 result = dictionary.saveMicroServiceLocationDictionary(request, response);
253                                 break;
254                         case "MicroServiceConfigName":
255                                 result = dictionary.saveMicroServiceConfigNameDictionary(request, response);
256                                 break;
257                         case "DCAEUUID":
258                                 result = dictionary.saveDCAEUUIDDictionary(request, response);
259                                 break;
260                         case "MicroServiceModels":
261                                 result = dictionary.saveMicroServiceModelsDictionary(request, response);
262                                 break;
263                         case "PolicyScopeService":
264                                 result = dictionary.savePSServiceDictionary(request, response);
265                                 break;
266                         case "PolicyScopeResource":
267                                 result = dictionary.savePSResourceDictionary(request, response);
268                                 break;
269                         case "PolicyScopeType":
270                                 result = dictionary.savePSTypeDictionary(request, response);
271                                 break;
272                         case "PolicyScopeClosedLoop":
273                                 result = dictionary.savePSClosedLoopDictionary(request, response);
274                                 break;
275                         case "GroupPolicyScopeList":
276                                 result = dictionary.savePSGroupScopeDictionary(request, response);
277                                 break;
278                         case "RiskType":
279                                 result = dictionary.saveRiskTypeDictionary(request, response);
280                                 break;
281                         case "SafePolicyWarning":
282                                 result = dictionary.saveSafePolicyWarningDictionary(request, response);
283                                 break;
284                         case "MicroServiceDictionary":
285                                 result = dictionary.saveMicroServiceDictionary(request, response);
286                                 break;
287                         default:
288                                 result = extendedOptions(dictionaryType, request, response, false);
289                                 if(result==null){
290                                         return;
291                                 }else{
292                                         break;
293                                 }
294                         }
295                 } catch (Exception e) {
296                         String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Updating the Database: " + e.getMessage();
297                         PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " Error Updating the Database.");
298                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
299                         response.addHeader("error", message);
300                         return;
301                 }
302                 if (result.equalsIgnoreCase("Success")) {
303                         response.setStatus(HttpServletResponse.SC_OK);                              
304                         response.addHeader("successMapKey", "success");
305                         if (operation.equalsIgnoreCase("update")) {
306                                 response.addHeader("operation",  "updateDictionary");
307                         } else {
308                                 response.addHeader("operation", "createDictionary");
309                         }
310                 } else if (result.equalsIgnoreCase("Duplicate")) {
311                         response.setStatus(HttpServletResponse.SC_CONFLICT);
312                         response.addHeader("error", "dictionaryItemExists");
313                 } else if (result.equalsIgnoreCase("DuplicateGroup")) {
314                         response.setStatus(HttpServletResponse.SC_CONFLICT);
315                         response.addHeader("error", "duplicateGroup");
316                 } else {
317                         String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Updating the Database.";
318                 PolicyLogger.error(message);
319                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST); 
320                         response.addHeader("error", message);
321                 }
322         }
323 }