Make clientAuth header optional and log request
[policy/engine.git] / ONAP-PDP-REST / src / main / java / org / onap / policy / pdp / rest / api / controller / PolicyEngineServices.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-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.pdp.rest.api.controller;
22
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiImplicitParam;
25 import io.swagger.annotations.ApiImplicitParams;
26 import io.swagger.annotations.ApiOperation;
27 import java.lang.reflect.InvocationTargetException;
28 import java.util.Collection;
29 import java.util.concurrent.atomic.AtomicLong;
30 import javax.servlet.http.HttpServletRequest;
31 import org.onap.policy.api.ConfigNameRequest;
32 import org.onap.policy.api.ConfigRequestParameters;
33 import org.onap.policy.api.DecisionRequestParameters;
34 import org.onap.policy.api.DecisionResponse;
35 import org.onap.policy.api.DeletePolicyParameters;
36 import org.onap.policy.api.DictionaryParameters;
37 import org.onap.policy.api.DictionaryResponse;
38 import org.onap.policy.api.EventRequestParameters;
39 import org.onap.policy.api.MetricsResponse;
40 import org.onap.policy.api.PolicyParameters;
41 import org.onap.policy.api.PolicyResponse;
42 import org.onap.policy.api.PushPolicyParameters;
43 import org.onap.policy.common.logging.flexlogger.FlexLogger;
44 import org.onap.policy.common.logging.flexlogger.Logger;
45 import org.onap.policy.pdp.rest.XACMLPdpServlet;
46 import org.onap.policy.pdp.rest.api.models.ConfigFirewallPolicyAPIRequest;
47 import org.onap.policy.pdp.rest.api.models.ConfigPolicyAPIRequest;
48 import org.onap.policy.pdp.rest.api.models.PolicyConfig;
49 import org.onap.policy.pdp.rest.api.services.CreateUpdateConfigPolicyService;
50 import org.onap.policy.pdp.rest.api.services.CreateUpdateDictionaryService;
51 import org.onap.policy.pdp.rest.api.services.CreateUpdateFirewallPolicyService;
52 import org.onap.policy.pdp.rest.api.services.CreateUpdatePolicyService;
53 import org.onap.policy.pdp.rest.api.services.DeletePolicyService;
54 import org.onap.policy.pdp.rest.api.services.GetConfigService;
55 import org.onap.policy.pdp.rest.api.services.GetDecisionService;
56 import org.onap.policy.pdp.rest.api.services.GetDictionaryService;
57 import org.onap.policy.pdp.rest.api.services.GetMetricsService;
58 import org.onap.policy.pdp.rest.api.services.ListConfigService;
59 import org.onap.policy.pdp.rest.api.services.ListPolicyService;
60 import org.onap.policy.pdp.rest.api.services.NotificationService;
61 import org.onap.policy.pdp.rest.api.services.NotificationService.NotificationServiceType;
62 import org.onap.policy.pdp.rest.api.services.PolicyEngineImportService;
63 import org.onap.policy.pdp.rest.api.services.PushPolicyService;
64 import org.onap.policy.pdp.rest.api.services.SendEventService;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.http.ResponseEntity;
67 import org.springframework.http.converter.HttpMessageNotReadableException;
68 import org.springframework.web.bind.annotation.DeleteMapping;
69 import org.springframework.web.bind.annotation.ExceptionHandler;
70 import org.springframework.web.bind.annotation.GetMapping;
71 import org.springframework.web.bind.annotation.PostMapping;
72 import org.springframework.web.bind.annotation.PutMapping;
73 import org.springframework.web.bind.annotation.RequestAttribute;
74 import org.springframework.web.bind.annotation.RequestBody;
75 import org.springframework.web.bind.annotation.RequestHeader;
76 import org.springframework.web.bind.annotation.RequestMapping;
77 import org.springframework.web.bind.annotation.RequestParam;
78 import org.springframework.web.bind.annotation.ResponseBody;
79 import org.springframework.web.bind.annotation.RestController;
80 import org.springframework.web.multipart.MultipartFile;
81 import springfox.documentation.annotations.ApiIgnore;
82
83 @RestController
84 @Api(value = "Policy Engine Services")
85 @RequestMapping("/")
86 public class PolicyEngineServices {
87     private static Logger logger = FlexLogger.getLogger(PolicyEngineServices.class.getName());
88
89     private final AtomicLong configCounter = new AtomicLong();
90     private final AtomicLong configNameCounter = new AtomicLong();
91     private final AtomicLong eventCounter = new AtomicLong();
92     private final AtomicLong decisionCounter = new AtomicLong();
93     private final AtomicLong pushCounter = new AtomicLong();
94     private final AtomicLong deleteCounter = new AtomicLong();
95     private final AtomicLong createPolicyCounter = new AtomicLong();
96     private final AtomicLong updatePolicyCounter = new AtomicLong();
97     private final AtomicLong createDictionaryCounter = new AtomicLong();
98     private final AtomicLong updateDictionaryCounter = new AtomicLong();
99     private final AtomicLong getDictionaryCounter = new AtomicLong();
100     private final AtomicLong policyEngineImportCounter = new AtomicLong();
101     private final AtomicLong deprecatedCounter = new AtomicLong();
102     private final AtomicLong metricCounter = new AtomicLong();
103     private final AtomicLong notificationCounter = new AtomicLong();
104
105     /**
106      * Gets the config.
107      *
108      * @param configRequestParameters the config request parameters
109      * @param clientEncoding the client encoding
110      * @param requestId the request ID
111      * @return the config
112      */
113     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
114             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
115     @ApiOperation(value = "Gets the configuration from the PolicyDecisionPoint(PDP)")
116     @PostMapping(value = "/getConfig")
117     @ResponseBody
118     public ResponseEntity<Collection<PolicyConfig>> getConfig(
119             @RequestBody ConfigRequestParameters configRequestParameters,
120             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
121             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
122         Collection<PolicyConfig> policyConfig = null;
123         GetConfigService getConfigService = new GetConfigService(configRequestParameters, requestId);
124         policyConfig = getConfigService.getResult();
125         HttpStatus status = getConfigService.getResponseCode();
126         configCounter.incrementAndGet();
127         return new ResponseEntity<>(policyConfig, status);
128     }
129
130     /**
131      * Gets the config by policy name.
132      *
133      * @param configNameRequest the config name request
134      * @param clientEncoding the client encoding
135      * @param requestId the request id
136      * @return the config by policy name
137      */
138     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
139             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
140     @ApiOperation(value = "Gets the configuration from the PolicyDecisionPoint(PDP) using PolicyName")
141     @PostMapping(value = "/getConfigByPolicyName")
142     @ResponseBody
143     @Deprecated
144     public ResponseEntity<Collection<PolicyConfig>> getConfigByPolicyName(
145             @RequestBody ConfigNameRequest configNameRequest,
146             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
147             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
148         Collection<PolicyConfig> policyConfig = null;
149         logger.info("Operation: getConfigByPolicyName - " + configNameRequest.getPolicyName());
150         ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
151         configRequestParameters.setPolicyName(configNameRequest.getPolicyName());
152
153         GetConfigService getConfigService = new GetConfigService(configRequestParameters, requestId);
154         policyConfig = getConfigService.getResult();
155         HttpStatus status = getConfigService.getResponseCode();
156         configNameCounter.incrementAndGet();
157         return new ResponseEntity<>(policyConfig, status);
158     }
159
160
161     /**
162      * List config.
163      *
164      * @param configRequestParameters the config request parameters
165      * @param clientEncoding the client encoding
166      * @param requestId the request ID
167      * @return the response entity
168      */
169     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
170             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
171     @ApiOperation(value = "Gets the list of configuration policies from the PDP")
172     @PostMapping(value = "/listConfig")
173     @ResponseBody
174     public ResponseEntity<Collection<String>> listConfig(@RequestBody ConfigRequestParameters configRequestParameters,
175             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
176             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
177         Collection<String> results = null;
178         logger.info("Operation: listConfig - " + configRequestParameters);
179         ListConfigService listConfigService = new ListConfigService(configRequestParameters, requestId);
180         results = listConfigService.getResult();
181         HttpStatus status = listConfigService.getResponseCode();
182         configCounter.incrementAndGet();
183         return new ResponseEntity<>(results, status);
184     }
185
186     /**
187      * List Policy.
188      *
189      * @param ConfigNameRequest the config request parameters
190      * @param clientEncoding the client encoding
191      * @param requestId the request ID
192      * @return the response entity
193      */
194     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
195             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
196     @ApiOperation(value = "Gets the list of policies from the PDP")
197     @PostMapping(value = "/listPolicy")
198     @ResponseBody
199     public ResponseEntity<Collection<String>> listPolicy(@RequestBody ConfigNameRequest configNameRequest,
200             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
201             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
202         Collection<String> results = null;
203         logger.info("Operation: listPolicy - " + configNameRequest);
204         ListPolicyService listPolicyService = new ListPolicyService(configNameRequest);
205         results = listPolicyService.getResult();
206         HttpStatus status = listPolicyService.getResponseCode();
207         configCounter.incrementAndGet();
208         return new ResponseEntity<>(results, status);
209     }
210
211     /**
212      * Gets the metrics.
213      *
214      * @param clientEncoding the client encoding
215      * @param requestId the request ID
216      * @return the metrics
217      */
218     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
219             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
220     @ApiOperation(value = "Gets the policy metrics from the PolicyAccessPoint(PAP)")
221     @GetMapping(value = "/getMetrics")
222     @ResponseBody
223     public ResponseEntity<MetricsResponse> getMetrics(
224             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
225             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
226         MetricsResponse response = null;
227         logger.info("Operation: getMetrics");
228
229         GetMetricsService getMetricsService = new GetMetricsService(requestId);
230         response = getMetricsService.getResult();
231         HttpStatus status = getMetricsService.getResponseCode();
232         metricCounter.incrementAndGet();
233         return new ResponseEntity<>(response, status);
234     }
235
236     /**
237      * Gets the notification.
238      *
239      * @param notificationTopic the notification topic
240      * @param clientEncoding the client encoding
241      * @param requestId the request ID
242      * @return the notification
243      */
244     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
245             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
246     @ApiOperation(value = "Registers DMaaP Topic to recieve notification from Policy Engine")
247     @PostMapping(value = "/getNotification")
248     @ResponseBody
249     public ResponseEntity<String> getNotification(@RequestBody String notificationTopic,
250             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
251             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
252         logger.info("Operation: getNotification for Topic: " + notificationTopic);
253
254         NotificationService notificationService =
255                 new NotificationService(notificationTopic, requestId, NotificationServiceType.ADD);
256         String policyResponse = notificationService.getResult();
257         HttpStatus status = notificationService.getResponseCode();
258         notificationCounter.incrementAndGet();
259         return new ResponseEntity<>(policyResponse, status);
260     }
261
262     /**
263      * Stop notification.
264      *
265      * @param notificationTopic the notification topic
266      * @param clientEncoding the client encoding
267      * @param requestId the request ID
268      * @return the response entity
269      */
270     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
271             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
272     @ApiOperation(value = "De-Registers DMaaP Topic to stop recieving notifications from Policy Engine")
273     @PostMapping(value = "/stopNotification")
274     @ResponseBody
275     public ResponseEntity<String> stopNotification(@RequestBody String notificationTopic,
276             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
277             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
278         logger.info("Operation: stopNotification for Topic: " + notificationTopic);
279
280         NotificationService notificationService =
281                 new NotificationService(notificationTopic, requestId, NotificationServiceType.REMOVE);
282         String policyResponse = notificationService.getResult();
283         HttpStatus status = notificationService.getResponseCode();
284
285         notificationCounter.incrementAndGet();
286         return new ResponseEntity<>(policyResponse, status);
287     }
288
289     /**
290      * Send heartbeat.
291      *
292      * @param notificationTopic the notification topic
293      * @param clientEncoding the client encoding
294      * @param requestId the request ID
295      * @return the response entity
296      */
297     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
298             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
299     @ApiOperation(
300             value = "Sends Heartbeat to DMaaP Topic Registry to continue recieving notifications from Policy Engine")
301     @PostMapping(value = "/sendHeartbeat")
302     @ResponseBody
303     public ResponseEntity<String> sendHeartbeat(@RequestBody String notificationTopic,
304             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
305             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
306         logger.info("Operation: sendHeartbeat for topic - " + notificationTopic);
307
308         NotificationService notificationService =
309                 new NotificationService(notificationTopic, requestId, NotificationServiceType.HB);
310         String policyResponse = notificationService.getResult();
311         HttpStatus status = notificationService.getResponseCode();
312
313         return new ResponseEntity<>(policyResponse, status);
314     }
315
316     /**
317      * Send event.
318      *
319      * @param eventRequestParameters the event request parameters
320      * @param clientEncoding the client encoding
321      * @param requestId the request ID
322      * @return the response entity
323      */
324     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
325             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
326     @ApiOperation(value = "Sends the Events specified to the Policy Engine")
327     @PostMapping(value = "/sendEvent")
328     @ResponseBody
329     public ResponseEntity<Collection<PolicyResponse>> sendEvent(
330             @RequestBody EventRequestParameters eventRequestParameters,
331             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
332             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
333         Collection<PolicyResponse> policyResponse = null;
334         logger.info("Operation: sendEvent with EventAttributes - " + eventRequestParameters.getEventAttributes());
335
336         SendEventService sendEventService = new SendEventService(eventRequestParameters, requestId);
337         policyResponse = sendEventService.getResult();
338         HttpStatus status = sendEventService.getResponseCode();
339         eventCounter.incrementAndGet();
340         return new ResponseEntity<>(policyResponse, status);
341     }
342
343     /**
344      * Gets the decision.
345      *
346      * @param decisionRequestParameters the decision request parameters
347      * @param clientEncoding the client encoding
348      * @param requestId the request ID
349      * @return the decision
350      */
351     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
352             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
353     @ApiOperation(value = "Gets the Decision using specified decision parameters")
354     @PostMapping(value = "/getDecision")
355     @ResponseBody
356     public ResponseEntity<DecisionResponse> getDecision(
357             @RequestBody DecisionRequestParameters decisionRequestParameters,
358             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
359             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
360         DecisionResponse decisionResponse = null;
361
362         GetDecisionService getDecisionService = new GetDecisionService(decisionRequestParameters, requestId);
363         decisionResponse = getDecisionService.getResult();
364         HttpStatus status = getDecisionService.getResponseCode();
365         decisionCounter.incrementAndGet();
366         return new ResponseEntity<>(decisionResponse, status);
367     }
368
369     /**
370      * Push policy.
371      *
372      * @param pushPolicyParameters the push policy parameters
373      * @param clientEncoding the client encoding
374      * @param requestId the request ID
375      * @return the response entity
376      */
377     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
378             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
379     @ApiOperation(value = "Pushes the specified policy to the PDP Group.")
380     @PutMapping(value = "/pushPolicy")
381     @ResponseBody
382     public ResponseEntity<String> pushPolicy(@RequestBody PushPolicyParameters pushPolicyParameters,
383             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
384             @RequestAttribute(name = "Mechid") String mechId,
385             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
386         String response = null;
387         logger.info("Operation: pushPolicy - " + pushPolicyParameters + ", Mechid - " + mechId);
388         PushPolicyService pushPolicyService = new PushPolicyService(pushPolicyParameters, requestId);
389         response = pushPolicyService.getResult();
390         HttpStatus status = pushPolicyService.getResponseCode();
391         pushCounter.incrementAndGet();
392         return new ResponseEntity<>(response, status);
393     }
394
395     /**
396      * Delete policy.
397      *
398      * @param deletePolicyParameters the delete policy parameters
399      * @param clientEncoding the client encoding
400      * @param requestId the request ID
401      * @return the response entity
402      */
403     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
404             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
405     @ApiOperation(value = "Deletes the specified policy from the PDP Group or PAP.")
406     @DeleteMapping(value = "/deletePolicy")
407     @ResponseBody
408     public ResponseEntity<String> deletePolicy(@RequestBody DeletePolicyParameters deletePolicyParameters,
409             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
410             @RequestAttribute(name = "Mechid") String mechId,
411             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
412         String response = null;
413         logger.info("Operation: deletePolicy - " + deletePolicyParameters + ", Mechid - " + mechId);
414         DeletePolicyService deletePolicyService = new DeletePolicyService(deletePolicyParameters, requestId);
415         response = deletePolicyService.getResult();
416         HttpStatus status = deletePolicyService.getResponseCode();
417         deleteCounter.incrementAndGet();
418         return new ResponseEntity<>(response, status);
419     }
420
421     /**
422      * Creates the policy.
423      *
424      * @param policyParameters the policy parameters
425      * @param clientEncoding the client encoding
426      * @param requestId the request ID
427      * @return the response entity
428      */
429     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
430             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
431     @ApiOperation(value = "Creates a Policy based on given Policy Parameters.")
432     @PutMapping(value = "/createPolicy")
433     @ResponseBody
434     public ResponseEntity<String> createPolicy(@RequestBody PolicyParameters policyParameters,
435             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
436             @RequestAttribute(name = "Mechid") String mechId,
437             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
438         String response = null;
439         HttpStatus status = HttpStatus.UNAUTHORIZED;
440         logger.info("Operation: createPolicy for " + policyParameters.toString() + ", Mechid : " + mechId);
441
442         CreateUpdatePolicyService createPolicyService;
443         try {
444             createPolicyService = (CreateUpdatePolicyService) XACMLPdpServlet.getCreateUpdatePolicyConstructor()
445                     .newInstance(policyParameters, requestId, false);
446             response = createPolicyService.getResult();
447             status = createPolicyService.getResponseCode();
448         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
449                 | InvocationTargetException e) {
450             logger.error(e.getMessage(), e);
451             response = "Problem with CreateUpdate Policy Service. ";
452             status = HttpStatus.INTERNAL_SERVER_ERROR;
453         }
454         createPolicyCounter.incrementAndGet();
455         return new ResponseEntity<>(response, status);
456     }
457
458     /**
459      * Update policy.
460      *
461      * @param policyParameters the policy parameters
462      * @param clientEncoding the client encoding
463      * @param requestId the request ID
464      * @return the response entity
465      */
466     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
467             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
468     @ApiOperation(value = "Updates a Policy based on given Policy Parameters.")
469     @PutMapping(value = "/updatePolicy")
470     @ResponseBody
471     public ResponseEntity<String> updatePolicy(@RequestBody PolicyParameters policyParameters,
472             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
473             @RequestAttribute(name = "Mechid") String mechId,
474             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
475         String response = null;
476         HttpStatus status = HttpStatus.UNAUTHORIZED;
477         logger.info("Operation: updatePolicy for " + policyParameters.toString() + ", MechId - " + mechId);
478
479         CreateUpdatePolicyService updatePolicyService;
480         try {
481             updatePolicyService = (CreateUpdatePolicyService) XACMLPdpServlet.getCreateUpdatePolicyConstructor()
482                     .newInstance(policyParameters, requestId, true);
483             response = updatePolicyService.getResult();
484             status = updatePolicyService.getResponseCode();
485         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
486                 | InvocationTargetException e) {
487             logger.error(e.getMessage(), e);
488             response = "Problem with CreateUpdate Policy Service. ";
489             status = HttpStatus.INTERNAL_SERVER_ERROR;
490         }
491         updatePolicyCounter.incrementAndGet();
492         return new ResponseEntity<>(response, status);
493     }
494
495     /**
496      * Creates the dictionary item.
497      *
498      * @param dictionaryParameters the dictionary parameters
499      * @param clientEncoding the client encoding
500      * @param requestId the request ID
501      * @return the response entity
502      */
503     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
504             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
505     @ApiOperation(value = "Creates a Dictionary Item for a specific dictionary based on given Parameters.")
506     @PutMapping(value = "/createDictionaryItem")
507     @ResponseBody
508     public ResponseEntity<String> createDictionaryItem(@RequestBody DictionaryParameters dictionaryParameters,
509             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
510             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
511         logger.info("Operation: createDictionaryItem - " + dictionaryParameters);
512
513         CreateUpdateDictionaryService createDictionaryService =
514                 new CreateUpdateDictionaryService(dictionaryParameters, requestId, false);
515         String response = createDictionaryService.getResult();
516         HttpStatus status = createDictionaryService.getResponseCode();
517         createDictionaryCounter.incrementAndGet();
518         return new ResponseEntity<>(response, status);
519     }
520
521     /**
522      * Update dictionary item.
523      *
524      * @param dictionaryParameters the dictionary parameters
525      * @param clientEncoding the client encoding
526      * @param requestId the request ID
527      * @return the response entity
528      */
529     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
530             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
531     @ApiOperation(value = "Updates a Dictionary Item for a specific dictionary based on given Parameters.")
532     @PutMapping(value = "/updateDictionaryItem")
533     @ResponseBody
534     public ResponseEntity<String> updateDictionaryItem(@RequestBody DictionaryParameters dictionaryParameters,
535             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
536             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
537         logger.info("Operation: updateDictionaryItem - " + dictionaryParameters);
538
539         CreateUpdateDictionaryService updateDictionaryService =
540                 new CreateUpdateDictionaryService(dictionaryParameters, requestId, true);
541         String response = updateDictionaryService.getResult();
542         HttpStatus status = updateDictionaryService.getResponseCode();
543         updateDictionaryCounter.incrementAndGet();
544         return new ResponseEntity<>(response, status);
545     }
546
547     /**
548      * Gets the dictionary items.
549      *
550      * @param dictionaryParameters the dictionary parameters
551      * @param clientEncoding the client encoding
552      * @param requestId the request ID
553      * @return the dictionary items
554      */
555     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
556             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
557     @ApiOperation(value = "Gets the dictionary items from the PAP")
558     @PostMapping(value = "/getDictionaryItems")
559     @ResponseBody
560     public ResponseEntity<DictionaryResponse> getDictionaryItems(@RequestBody DictionaryParameters dictionaryParameters,
561             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
562             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
563         DictionaryResponse dictionaryResponse = null;
564         logger.info("Operation: getDictionaryItems - " + dictionaryParameters);
565         GetDictionaryService getDictionaryService = new GetDictionaryService(dictionaryParameters, requestId);
566         dictionaryResponse = getDictionaryService.getResult();
567         HttpStatus status = getDictionaryService.getResponseCode();
568         getDictionaryCounter.incrementAndGet();
569         return new ResponseEntity<>(dictionaryResponse, status);
570     }
571
572     /**
573      * Policy engine import.
574      *
575      * @param importParametersJson the import parameters json
576      * @param file the file
577      * @param clientEncoding the client encoding
578      * @param requestId the request ID
579      * @return the response entity
580      */
581     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
582             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
583     @ApiOperation(value = "Imports models and templates which represent the service used to create a policy.")
584     @PostMapping(value = "/policyEngineImport")
585     @ResponseBody
586     public ResponseEntity<String> policyEngineImport(@RequestParam("importParametersJson") String importParametersJson,
587             @RequestParam("file") MultipartFile file,
588             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
589             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
590         logger.info("Operation: policyEngineImport with importParametersJson: " + importParametersJson);
591         PolicyEngineImportService policyEngineImportService =
592                 new PolicyEngineImportService(importParametersJson, file, requestId);
593         String response = policyEngineImportService.getResult();
594         HttpStatus status = policyEngineImportService.getResponseCode();
595         policyEngineImportCounter.incrementAndGet();
596         return new ResponseEntity<>(response, status);
597     }
598
599     /**
600      * Creates the config.
601      *
602      * @param configPolicyAPIRequest the config policy API request
603      * @param clientEncoding the client encoding
604      * @param requestId the request id
605      * @return the response entity
606      */
607     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
608             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
609     @ApiOperation(value = "Creates a Config Policy based on given Policy Parameters.")
610     @PutMapping(value = "/createConfig")
611     @ResponseBody
612     @Deprecated
613     public ResponseEntity<String> createConfig(@RequestBody ConfigPolicyAPIRequest configPolicyAPIRequest,
614             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
615             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
616         logger.info("Operation: createConfig");
617
618         CreateUpdateConfigPolicyService createPolicyService =
619                 new CreateUpdateConfigPolicyService(configPolicyAPIRequest, requestId, false);
620         String response = createPolicyService.getResult();
621         HttpStatus status = createPolicyService.getResponseCode();
622         deprecatedCounter.incrementAndGet();
623         return new ResponseEntity<>(response, status);
624     }
625
626     /**
627      * Update config.
628      *
629      * @param configPolicyAPIRequest the config policy API request
630      * @param clientEncoding the client encoding
631      * @param requestId the request id
632      * @return the response entity
633      */
634     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
635             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
636     @ApiOperation(value = "Updates a Config Policy based on given Policy Parameters.")
637     @PutMapping(value = "/updateConfig")
638     @ResponseBody
639     @Deprecated
640     public ResponseEntity<String> updateConfig(@RequestBody ConfigPolicyAPIRequest configPolicyAPIRequest,
641             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
642             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
643         logger.info("Operation: updateConfig");
644
645         CreateUpdateConfigPolicyService updatePolicyService =
646                 new CreateUpdateConfigPolicyService(configPolicyAPIRequest, requestId, true);
647         String response = updatePolicyService.getResult();
648         HttpStatus status = updatePolicyService.getResponseCode();
649         deprecatedCounter.incrementAndGet();
650         return new ResponseEntity<>(response, status);
651     }
652
653     /**
654      * Creates the firewall config.
655      *
656      * @param configFirewallPolicyAPIRequest the config firewall policy API request
657      * @param clientEncoding the client encoding
658      * @param requestId the request id
659      * @return the response entity
660      */
661
662     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
663             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
664     @ApiOperation(value = "Creates a Config Firewall Policy")
665     @PutMapping(value = "/createFirewallConfig")
666     @ResponseBody
667     @Deprecated
668     public ResponseEntity<String> createFirewallConfig(
669             @RequestBody ConfigFirewallPolicyAPIRequest configFirewallPolicyAPIRequest,
670             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
671             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
672         logger.info("Operation: createFirewallConfig");
673
674         CreateUpdateFirewallPolicyService createFirewallPolicyService =
675                 new CreateUpdateFirewallPolicyService(configFirewallPolicyAPIRequest, requestId, false);
676         String response = createFirewallPolicyService.getResult();
677         HttpStatus status = createFirewallPolicyService.getResponseCode();
678         deprecatedCounter.incrementAndGet();
679         return new ResponseEntity<>(response, status);
680     }
681
682     /**
683      * Update firewall config.
684      *
685      * @param configFirewallPolicyAPIRequest the config firewall policy API request
686      * @param clientEncoding the client encoding
687      * @param requestId the request id
688      * @return the response entity
689      */
690     @ApiImplicitParams({@ApiImplicitParam(name = "Authorization", required = true, paramType = "Header"),
691             @ApiImplicitParam(name = "Environment", required = true, paramType = "Header")})
692     @ApiOperation(value = "Updates a Config Firewall Policy")
693     @PutMapping(value = "/updateFirewallConfig")
694     @ResponseBody
695     @Deprecated
696     public ResponseEntity<String> updateFirewallConfig(
697             @RequestBody ConfigFirewallPolicyAPIRequest configFirewallPolicyAPIRequest,
698             @RequestHeader(value = "ClientAuth", required = false) String clientEncoding,
699             @RequestHeader(value = "X-ECOMP-RequestID", required = false) String requestId) {
700         logger.info("Operation: updateFirewallConfig");
701
702         CreateUpdateFirewallPolicyService updateFirewallPolicyService =
703                 new CreateUpdateFirewallPolicyService(configFirewallPolicyAPIRequest, requestId, true);
704         String response = updateFirewallPolicyService.getResult();
705         HttpStatus status = updateFirewallPolicyService.getResponseCode();
706         deprecatedCounter.incrementAndGet();
707         return new ResponseEntity<>(response, status);
708     }
709
710     /**
711      * Gets the count.
712      *
713      * @return the count
714      */
715     @ApiOperation(value = "Gets the API Services usage Information")
716     @ApiIgnore
717     @GetMapping(value = "/count")
718     public String getCount() {
719         return "Total Config Calls : " + configCounter + "\n" + "Total Config calls made using Policy File Name: "
720                 + configNameCounter + "\n" + "Total Event Calls : " + eventCounter + "\nTotal Decision Calls: "
721                 + decisionCounter + "\nTotal Push policy Calls: " + pushCounter + "\nTotal Delete Policy Calls: "
722                 + deleteCounter + "\nTotal Create Policy Calls: " + createPolicyCounter
723                 + "\nTotal Update Policy Calls: " + updatePolicyCounter + "\nTotal Create Dictionary Calls: "
724                 + createDictionaryCounter + "\nTotal Update Dictionary Calls: " + updateDictionaryCounter
725                 + "\nTotal Get Dictionary Calls: " + getDictionaryCounter + "\nTotal PolicyEngine Import Calls: "
726                 + policyEngineImportCounter + "\nTotal Deprecated Policy Calls: " + deprecatedCounter
727                 + "\nTotal Metrics Calls:" + metricCounter + "\nTotal Notification Calls:" + notificationCounter;
728     }
729
730     /**
731      * Message not readable exception handler.
732      *
733      * @param req the req
734      * @param exception the exception
735      * @return the response entity
736      */
737     @ExceptionHandler({HttpMessageNotReadableException.class})
738     public ResponseEntity<String> messageNotReadableExceptionHandler(HttpServletRequest req,
739             HttpMessageNotReadableException exception) {
740         logger.error("Request not readable: {}", exception);
741         StringBuilder message = new StringBuilder();
742         message.append(exception.getMessage());
743         if (exception.getCause() != null) {
744             message.append(" Reason Caused: " + exception.getCause().getMessage());
745         }
746         return new ResponseEntity<>(message.toString(), HttpStatus.BAD_REQUEST);
747     }
748 }