Initial OpenECOMP policy/drools-pdp commit
[policy/drools-pdp.git] / policy-management / src / main / java / org / openecomp / policy / drools / server / restful / RestManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-management
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.policy.drools.server.restful;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
26 import java.util.UUID;
27 import java.util.regex.Pattern;
28
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.DefaultValue;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.QueryParam;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.Response.Status;
42
43 import org.openecomp.policy.common.logging.eelf.MessageCodes;
44 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
45 import org.openecomp.policy.common.logging.flexlogger.Logger;
46 import org.openecomp.policy.drools.controller.DroolsController;
47 import org.openecomp.policy.drools.event.comm.TopicEndpoint;
48 import org.openecomp.policy.drools.event.comm.TopicSink;
49 import org.openecomp.policy.drools.event.comm.TopicSource;
50 import org.openecomp.policy.drools.event.comm.bus.DmaapTopicSink;
51 import org.openecomp.policy.drools.event.comm.bus.DmaapTopicSource;
52 import org.openecomp.policy.drools.event.comm.bus.UebTopicSink;
53 import org.openecomp.policy.drools.event.comm.bus.UebTopicSource;
54 import org.openecomp.policy.drools.properties.PolicyProperties;
55 import org.openecomp.policy.drools.protocol.coders.EventProtocolCoder;
56 import org.openecomp.policy.drools.protocol.coders.EventProtocolCoder.CoderFilters;
57 import org.openecomp.policy.drools.protocol.coders.JsonProtocolFilter;
58 import org.openecomp.policy.drools.protocol.coders.JsonProtocolFilter.FilterRule;
59 import org.openecomp.policy.drools.protocol.coders.ProtocolCoderToolset;
60 import org.openecomp.policy.drools.protocol.configuration.ControllerConfiguration;
61 import org.openecomp.policy.drools.protocol.configuration.PdpdConfiguration;
62 import org.openecomp.policy.drools.system.PolicyController;
63 import org.openecomp.policy.drools.system.PolicyEngine;
64
65
66 /**
67  * REST Endpoint for management of the Drools PDP
68  */
69 @Path("/policy/pdp")
70 public class RestManager {
71         /**
72          * Logger
73          */
74         private static Logger  logger = FlexLogger.getLogger(RestManager.class);  
75         
76         /**
77          * gets the Policy Engine
78          * 
79          * @return the Policy Engine
80          */
81     @GET
82     @Path("engine")
83     @Produces(MediaType.APPLICATION_JSON)
84     public PolicyEngine engine() {      
85         return PolicyEngine.manager;
86     }
87     
88     
89     /**
90      * Updates the Policy Engine
91      * 
92      * @param configuration configuration
93      * @return Policy Engine
94      */
95     @PUT
96     @Path("engine")
97     @Produces(MediaType.APPLICATION_JSON)
98     @Consumes(MediaType.APPLICATION_JSON)
99     public Response updateEngine(PdpdConfiguration configuration) {
100         PolicyController controller = null;
101         boolean success = true;
102                 try {
103                         success = PolicyEngine.manager.configure(configuration);
104                 } catch (Exception e) {
105                         success = false;
106                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
107                               "PolicyEngine", this.toString());
108                 }
109         
110                 if (!success)
111                         return Response.status(Response.Status.NOT_ACCEPTABLE).
112                     entity(new Error("cannot perform operation")).build();
113                 else
114                         return Response.status(Response.Status.OK).entity(controller).build();
115     }
116     
117     /**
118      * Activates the Policy Engine
119      * 
120      * @param configuration configuration
121      * @return Policy Engine
122      */
123     @PUT
124     @Path("engine/activation")
125     @Produces(MediaType.APPLICATION_JSON)
126     public Response activateEngine() {
127         boolean success = true;
128                 try {
129                         PolicyEngine.manager.activate();
130                 } catch (Exception e) {
131                         success = false;
132                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
133                               "PolicyEngine", this.toString());
134                 }
135         
136                 if (!success)
137                         return Response.status(Response.Status.NOT_ACCEPTABLE).
138                     entity(new Error("cannot perform operation")).build();
139                 else
140                         return Response.status(Response.Status.OK).entity(PolicyEngine.manager).build();
141     }
142     
143     /**
144      * Activates the Policy Engine
145      * 
146      * @param configuration configuration
147      * @return Policy Engine
148      */
149     @PUT
150     @Path("engine/deactivation")
151     @Produces(MediaType.APPLICATION_JSON)
152     public Response deactivateEngine() {
153         boolean success = true;
154                 try {
155                         PolicyEngine.manager.deactivate();
156                 } catch (Exception e) {
157                         success = false;
158                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
159                               "PolicyEngine", this.toString());
160                 }
161         
162                 if (!success)
163                         return Response.status(Response.Status.NOT_ACCEPTABLE).
164                     entity(new Error("cannot perform operation")).build();
165                 else
166                         return Response.status(Response.Status.OK).entity(PolicyEngine.manager).build();
167     }
168     
169     @DELETE
170     @Path("engine")
171     @Produces(MediaType.APPLICATION_JSON)
172     public Response engineShutdown() { 
173         try {
174                         PolicyEngine.manager.shutdown();
175                 } catch (IllegalStateException e) {
176                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
177                                                   "shutdown: " + PolicyEngine.manager);
178                 return Response.status(Response.Status.BAD_REQUEST).
179                                 entity(PolicyEngine.manager).
180                                 build();
181                 }
182         
183                 return Response.status(Response.Status.OK).
184                                 entity(PolicyEngine.manager).
185                                 build();
186     }   
187     
188     @PUT
189     @Path("engine/lock")
190     @Produces(MediaType.APPLICATION_JSON)
191     @Consumes(MediaType.APPLICATION_JSON)
192     public Response lockEngine() {
193         boolean success = PolicyEngine.manager.lock();
194         if (success)
195                 return Response.status(Status.OK).
196                                         entity("Policy Engine is locked").
197                                         build();
198         else
199                 return Response.status(Status.SERVICE_UNAVAILABLE).
200                                         entity("Policy Engine cannot be locked").
201                                         build();
202     }
203     
204     @DELETE
205     @Path("engine/unlock")
206     @Produces(MediaType.APPLICATION_JSON)
207     @Consumes(MediaType.APPLICATION_JSON)
208     public Response unlockEngine() {
209         boolean success = PolicyEngine.manager.unlock();
210         if (success)
211                 return Response.status(Status.OK).
212                                         entity("Policy Engine is unlocked").
213                                         build();
214         else
215                 return Response.status(Status.SERVICE_UNAVAILABLE).
216                                         entity("Policy Engine cannot be unlocked").
217                                         build();
218     }
219     
220     @GET
221     @Path("engine/controllers")
222     @Produces(MediaType.APPLICATION_JSON)
223     public List<PolicyController> controllers() {
224         return PolicyEngine.manager.getPolicyControllers();
225     }
226     
227     @POST
228     @Path("engine/controllers")
229     @Produces(MediaType.APPLICATION_JSON)
230     @Consumes(MediaType.APPLICATION_JSON)
231     public Response addController(Properties config) {
232         if (config == null)
233                 return Response.status(Response.Status.BAD_REQUEST).
234                                         entity(new Error("A configuration must be provided")).
235                                         build();
236         
237         String controllerName = config.getProperty(PolicyProperties.PROPERTY_CONTROLLER_NAME);
238         if (controllerName == null || controllerName.isEmpty())
239                 return Response.status(Response.Status.BAD_REQUEST).
240                                         entity(new Error
241                                                                 ("Configuration must have an entry for " + 
242                                                      PolicyProperties.PROPERTY_CONTROLLER_NAME)).
243                                         build();
244         
245         PolicyController controller;
246                 try {
247                         controller = PolicyController.factory.get(controllerName);
248                         if (controller != null)
249                                 return Response.status(Response.Status.NOT_MODIFIED).
250                                                         entity(controller).
251                                                         build();
252                 } catch (IllegalArgumentException e) {
253                         // This is OK
254                 } catch (IllegalStateException e) {
255                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
256                               controllerName, this.toString());
257                         return Response.status(Response.Status.NOT_ACCEPTABLE).
258                             entity(new Error(controllerName + " not found")).build();
259                 }
260         
261         try {
262                         controller = PolicyEngine.manager.createPolicyController
263                                         (config.getProperty(PolicyProperties.PROPERTY_CONTROLLER_NAME), config);
264                 } catch (IllegalArgumentException | IllegalStateException e) {
265                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
266                                           controllerName, this.toString());
267                 return Response.status(Response.Status.BAD_REQUEST).
268                                                                 entity(new Error(e.getMessage())).
269                                                                 build();
270                 }
271         
272         try {
273                         boolean success = controller.start();
274                         if (!success) {
275                                 logger.warn("Can't start " + controllerName + ": " + controller.toString());
276                                 return Response.status(Response.Status.PARTIAL_CONTENT).
277                                        entity(new Error(controllerName + " can't be started")).build();
278                         }
279                 } catch (IllegalStateException e) {
280                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
281                     controllerName, this.toString());
282                         return Response.status(Response.Status.PARTIAL_CONTENT).
283                                    entity(controller).build();
284                 }
285         
286                 return Response.status(Response.Status.CREATED).
287                 entity(controller).
288                 build();
289     }    
290     
291     @GET
292     @Path("engine/controllers/{controllerName}")
293     @Produces(MediaType.APPLICATION_JSON)
294     public Response controller(@PathParam("controllerName") String controllerName) {
295         PolicyController controller = null;
296                 try {
297                         controller = PolicyController.factory.get(controllerName);
298                 } catch (IllegalArgumentException e) {
299                         logger.info("Can't retrieve controller " + controllerName + 
300                                                   ".  Reason: " + e.getMessage());
301                 } catch (IllegalStateException e) {
302                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
303                               controllerName, this.toString());
304                         return Response.status(Response.Status.NOT_ACCEPTABLE).
305                             entity(new Error(controllerName + " not acceptable")).build();
306                 }
307         
308                 if (controller != null)
309                 return Response.status(Response.Status.OK).
310                                 entity(controller).build();
311                 else
312                         return Response.status(Response.Status.NOT_FOUND).
313                                            entity(new Error(controllerName + " not found")).build();
314     }
315     
316     @DELETE
317     @Path("engine/controllers/{controllerName}")
318     @Produces(MediaType.APPLICATION_JSON)
319     @Consumes(MediaType.APPLICATION_JSON)
320     public Response deleteController(@PathParam("controllerName") String controllerName) {
321         
322         if (controllerName == null || controllerName.isEmpty())
323                 return Response.status(Response.Status.BAD_REQUEST).
324                                         entity("A controller name must be provided").
325                                         build();
326         
327         PolicyController controller;
328         try {
329                 controller =
330                                 PolicyController.factory.get(controllerName);
331                 if (controller == null)
332                         return Response.status(Response.Status.BAD_REQUEST).
333                                                 entity(new Error(controllerName + "  does not exist")).
334                                                 build();
335                 } catch (IllegalArgumentException e) {
336                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
337                                           controllerName, this.toString());
338                         return Response.status(Response.Status.BAD_REQUEST).
339                                                                 entity(new Error(controllerName +  " not found: " + e.getMessage())).
340                                                                 build();
341                 } catch (IllegalStateException e) {
342                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
343                               controllerName, this.toString());
344                         return Response.status(Response.Status.NOT_ACCEPTABLE).
345                                    entity(new Error(controllerName + " not acceptable")).build();
346                 }
347         
348         try {
349                         PolicyEngine.manager.removePolicyController(controllerName);
350                 } catch (IllegalArgumentException | IllegalStateException e) {
351                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
352                                           controllerName + controller);
353                 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).
354                                                                entity(new Error(e.getMessage())).
355                                                                build();
356                 }
357         
358                 return Response.status(Response.Status.OK).
359                 entity(controller).
360                 build();
361     }
362     
363     /**
364      * Updates the Policy Engine
365      * 
366      * @param configuration configuration
367      * @return Policy Engine
368      */
369     @PUT
370     @Path("engine/controllers/{controllerName}")
371     @Produces(MediaType.APPLICATION_JSON)
372     @Consumes(MediaType.APPLICATION_JSON)
373     public Response updateController(@PathParam("controllerName") String controllerName,
374                                          ControllerConfiguration controllerConfiguration) {
375         
376         if (controllerName == null || controllerName.isEmpty() || 
377             controllerConfiguration == null || 
378             controllerConfiguration.getName().intern() != controllerName)
379                 return Response.status(Response.Status.BAD_REQUEST).
380                                         entity("A valid or matching controller names must be provided").
381                                         build();
382         
383         PolicyController controller;
384         try {
385                 controller = PolicyEngine.manager.updatePolicyController(controllerConfiguration);
386                 if (controller == null)
387                         return Response.status(Response.Status.BAD_REQUEST).
388                                                 entity(new Error(controllerName + "  does not exist")).
389                                                 build();
390                 } catch (IllegalArgumentException e) {
391                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
392                                           controllerName, this.toString());
393                         return Response.status(Response.Status.BAD_REQUEST).
394                                                                 entity(new Error(controllerName +  " not found: " + e.getMessage())).
395                                                                 build();
396                 } catch (Exception e) {
397                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
398                               controllerName, this.toString());
399                         return Response.status(Response.Status.NOT_ACCEPTABLE).
400                                    entity(new Error(controllerName + " not acceptable")).build();
401                 }
402         
403                 return Response.status(Response.Status.OK).
404                 entity(controller).
405                 build();
406     }
407     
408     public DroolsController getDroolsController(String controllerName) throws IllegalArgumentException {
409                 PolicyController controller = PolicyController.factory.get(controllerName);
410         if (controller == null)
411                 throw new IllegalArgumentException(controllerName + "  does not exist");
412
413                 DroolsController drools = controller.getDrools();
414         if (drools == null)
415                 throw new IllegalArgumentException(controllerName + "  has no drools configuration");
416         
417         return drools;
418     }
419     
420     @GET
421     @Path("engine/controllers/{controllerName}/decoders")
422     @Produces(MediaType.APPLICATION_JSON)
423     public Response decoders(@PathParam("controllerName") String controllerName) {
424                 try {
425                         DroolsController drools = getDroolsController(controllerName);
426                         List<ProtocolCoderToolset> decoders = EventProtocolCoder.manager.getDecoders
427                                                                                                                 (drools.getGroupId(), drools.getArtifactId());                  
428                         return Response.status(Response.Status.OK).
429                                        entity(decoders).
430                                        build();
431                 } catch (IllegalArgumentException | IllegalStateException e) {
432                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
433                           controllerName, this.toString());
434                         return Response.status(Response.Status.BAD_REQUEST).
435                                                        entity(new Error(e.getMessage())).
436                                                        build();
437                 }
438     }
439     
440     @GET
441     @Path("engine/controllers/{controllerName}/decoders/filters")
442     @Produces(MediaType.APPLICATION_JSON)
443     public Response decoderFilters(@PathParam("controllerName") String controllerName) {
444                 try {
445                         DroolsController drools = getDroolsController(controllerName);
446                         List<CoderFilters> filters = EventProtocolCoder.manager.getDecoderFilters
447                                                         (drools.getGroupId(), drools.getArtifactId());
448                         return Response.status(Response.Status.OK).
449                                     entity(filters).
450                                     build();
451                 } catch (IllegalArgumentException | IllegalStateException e) {
452                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
453                           controllerName, this.toString());
454                         return Response.status(Response.Status.BAD_REQUEST).
455                                                        entity(new Error(e.getMessage())).
456                                                        build();
457                 }
458     }
459     
460     @GET
461     @Path("engine/controllers/{controllerName}/decoders/{topicName}")
462     @Produces(MediaType.APPLICATION_JSON)
463     public Response decoder(@PathParam("controllerName") String controllerName,
464                                  @PathParam("topicName") String topicName) {
465                 try {
466                         DroolsController drools = getDroolsController(controllerName);
467                         ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
468                                                         (drools.getGroupId(), drools.getArtifactId(), topicName);
469                         return Response.status(Response.Status.OK).
470                                     entity(decoder).
471                                     build();
472                 } catch (IllegalArgumentException | IllegalStateException e) {
473                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
474                           controllerName, this.toString());
475                         return Response.status(Response.Status.BAD_REQUEST).
476                                                        entity(new Error(e.getMessage())).
477                                                        build();
478                 }
479     }    
480     
481     @GET
482     @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters")
483     @Produces(MediaType.APPLICATION_JSON)
484     public Response decoderFilter(@PathParam("controllerName") String controllerName,
485                                        @PathParam("topicName") String topicName) {
486                 try {
487                         DroolsController drools = getDroolsController(controllerName);
488                         ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
489                                                                                                 (drools.getGroupId(), drools.getArtifactId(), topicName);
490                         if (decoder == null)
491                         return Response.status(Response.Status.BAD_REQUEST).
492                                         entity(new Error(topicName + "  does not exist")).
493                                         build();
494                         else
495                                 return Response.status(Response.Status.OK).
496                             entity(decoder.getCoders()).
497                             build();
498                 } catch (IllegalArgumentException | IllegalStateException e) {
499                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
500                           controllerName, this.toString());
501                         return Response.status(Response.Status.BAD_REQUEST).
502                                                        entity(new Error(e.getMessage())).
503                                                        build();
504                 }
505     }
506     
507     @GET
508     @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}")
509     @Produces(MediaType.APPLICATION_JSON)
510     public Response decoderFilter(@PathParam("controllerName") String controllerName,
511                                        @PathParam("topicName") String topicName,
512                                        @PathParam("factClassName") String factClass) {
513                 try {
514                         DroolsController drools = getDroolsController(controllerName);
515                 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
516                                                                                         (drools.getGroupId(), drools.getArtifactId(), topicName);
517                         CoderFilters filters = decoder.getCoder(factClass);
518                         if (filters == null)
519                         return Response.status(Response.Status.BAD_REQUEST).
520                                         entity(new Error(topicName + ":" + factClass + "  does not exist")).
521                                         build();
522                         else
523                                 return Response.status(Response.Status.OK).
524                         entity(filters).
525                         build();
526                 } catch (IllegalArgumentException | IllegalStateException e) {
527                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
528                           controllerName, this.toString());
529                         return Response.status(Response.Status.BAD_REQUEST).
530                                                        entity(new Error(e.getMessage())).
531                                                        build();
532                 }
533     }
534     
535     @POST
536     @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}")
537     @Consumes(MediaType.APPLICATION_JSON)
538     @Produces(MediaType.APPLICATION_JSON)
539     public Response decoderFilter(@PathParam("controllerName") String controllerName,
540                                       @PathParam("topicName") String topicName,
541                                       @PathParam("factClassName") String factClass,
542                                       JsonProtocolFilter configFilters) {
543         
544         if (configFilters == null) {
545                 return Response.status(Response.Status.BAD_REQUEST).
546                                         entity(new Error("Configuration Filters not provided")).
547                                         build();
548         }
549         
550                 try {
551                         DroolsController drools = getDroolsController(controllerName);
552                 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
553                                                                                         (drools.getGroupId(), drools.getArtifactId(), topicName);
554                 CoderFilters filters = decoder.getCoder(factClass);
555                         if (filters == null)
556                         return Response.status(Response.Status.BAD_REQUEST).
557                                         entity(new Error(topicName + ":" + factClass + "  does not exist")).
558                                         build();
559                         filters.setFilter(configFilters);
560                         return Response.status(Response.Status.OK).
561                                     entity(filters).
562                                     build();
563                 } catch (IllegalArgumentException | IllegalStateException e) {
564                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
565                           controllerName, this.toString());
566                         return Response.status(Response.Status.BAD_REQUEST).
567                                                        entity(new Error(e.getMessage())).
568                                                        build();
569                 }
570     }
571     
572     @GET
573     @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}/rules")
574     @Produces(MediaType.APPLICATION_JSON)
575     public Response decoderFilterRules(@PathParam("controllerName") String controllerName,
576                                           @PathParam("topicName") String topicName,
577                                           @PathParam("factClassName") String factClass) {
578                 try {
579                         DroolsController drools = getDroolsController(controllerName);
580                 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
581                                                                                         (drools.getGroupId(), drools.getArtifactId(), topicName);
582                 
583                 CoderFilters filters = decoder.getCoder(factClass);
584                         if (filters == null)
585                         return Response.status(Response.Status.BAD_REQUEST).
586                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + "  does not exist")).
587                                         build();
588                         
589                         JsonProtocolFilter filter = filters.getFilter();
590                         if (filter == null)
591                         return Response.status(Response.Status.BAD_REQUEST).
592                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + "  no filters")).
593                                         build();
594                         
595                         return Response.status(Response.Status.OK).
596                                     entity(filter.getRules()).
597                                     build();
598                 } catch (IllegalArgumentException | IllegalStateException e) {
599                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
600                           controllerName, this.toString());
601                         return Response.status(Response.Status.BAD_REQUEST).
602                                                        entity(new Error(e.getMessage())).
603                                                        build();
604                 }
605     }
606     
607     @GET
608     @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}/rules/{ruleName}")
609     @Produces(MediaType.APPLICATION_JSON)
610     public Response decoderFilterRules(@PathParam("controllerName") String controllerName,
611                                           @PathParam("topicName") String topicName,
612                                           @PathParam("factClassName") String factClass,
613                                           @PathParam("ruleName") String ruleName) {
614                 try {
615                         DroolsController drools = getDroolsController(controllerName);
616                 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
617                                                                                         (drools.getGroupId(), drools.getArtifactId(), topicName);
618                 
619                 CoderFilters filters = decoder.getCoder(factClass);
620                         if (filters == null)
621                         return Response.status(Response.Status.BAD_REQUEST).
622                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + "  does not exist")).
623                                         build();
624                         
625                         JsonProtocolFilter filter = filters.getFilter();
626                         if (filter == null)
627                         return Response.status(Response.Status.BAD_REQUEST).
628                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + "  no filters")).
629                                         build();
630                         
631                         return Response.status(Response.Status.OK).
632                                     entity(filter.getRules(ruleName)).
633                                     build();
634                 } catch (IllegalArgumentException | IllegalStateException e) {
635                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
636                           controllerName, this.toString());
637                         return Response.status(Response.Status.BAD_REQUEST).
638                                                        entity(new Error(e.getMessage())).
639                                                        build();
640                 }
641     }
642     
643     @DELETE
644     @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}/rules/{ruleName}")
645     @Consumes(MediaType.APPLICATION_JSON)
646     @Produces(MediaType.APPLICATION_JSON)
647     public Response deleteDecoderFilterRule(@PathParam("controllerName") String controllerName,
648                                                   @PathParam("topicName") String topicName,
649                                                   @PathParam("factClassName") String factClass,
650                                                   @PathParam("ruleName") String ruleName,
651                                                   FilterRule rule) {
652                 
653                 try {
654                         DroolsController drools = getDroolsController(controllerName);
655                 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
656                                                                                         (drools.getGroupId(), drools.getArtifactId(), topicName);
657                 
658                 CoderFilters filters = decoder.getCoder(factClass);
659                         if (filters == null)
660                         return Response.status(Response.Status.BAD_REQUEST).
661                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + "  does not exist")).
662                                         build();
663                         
664                         JsonProtocolFilter filter = filters.getFilter();
665                         if (filter == null)
666                         return Response.status(Response.Status.BAD_REQUEST).
667                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + "  no filters")).
668                                         build();
669                         
670                         if (rule == null) {
671                                 filter.deleteRules(ruleName);
672                                 return Response.status(Response.Status.OK).
673                             entity(filter.getRules()).
674                             build();            
675                         }
676                         
677                         if (rule.getName() == null || !rule.getName().equals(ruleName))
678                         return Response.status(Response.Status.BAD_REQUEST).
679                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + ":" + ruleName + 
680                                                                  " rule name request inconsistencies (" + rule.getName() + ")")).
681                                         build();
682                         
683                         filter.deleteRule(ruleName, rule.getRegex());
684                         return Response.status(Response.Status.OK).
685                                     entity(filter.getRules()).
686                                     build();
687                 } catch (IllegalArgumentException | IllegalStateException e) {
688                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
689                           controllerName, this.toString());
690                         return Response.status(Response.Status.BAD_REQUEST).
691                                                        entity(new Error(e.getMessage())).
692                                                        build();
693                 }
694     }
695     
696     @PUT
697     @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}/rules")
698     @Produces(MediaType.APPLICATION_JSON)
699     public Response decoderFilterRule(@PathParam("controllerName") String controllerName,
700                                               @PathParam("topicName") String topicName,
701                                               @PathParam("factClassName") String factClass,
702                                               JsonProtocolFilter.FilterRule rule) {
703                 
704                 try {
705                         DroolsController drools = getDroolsController(controllerName);
706                 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
707                                                                                         (drools.getGroupId(), drools.getArtifactId(), topicName);
708                 
709                 CoderFilters filters = decoder.getCoder(factClass);
710                         if (filters == null)
711                         return Response.status(Response.Status.BAD_REQUEST).
712                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + "  does not exist")).
713                                         build();
714                         
715                         JsonProtocolFilter filter = filters.getFilter();
716                         if (filter == null)
717                         return Response.status(Response.Status.BAD_REQUEST).
718                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass + "  no filters")).
719                                         build();
720                         
721                         if (rule.getName() == null)
722                         return Response.status(Response.Status.BAD_REQUEST).
723                                         entity(new Error(controllerName + ":" + topicName + ":" + factClass +  
724                                                                  " rule name request inconsistencies (" + rule.getName() + ")")).
725                                         build();
726                         
727                         filter.addRule(rule.getName(), rule.getRegex());
728                         return Response.status(Response.Status.OK).
729                                     entity(filter.getRules()).
730                                     build();
731                 } catch (IllegalArgumentException | IllegalStateException e) {
732                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
733                           controllerName, this.toString());
734                         return Response.status(Response.Status.BAD_REQUEST).
735                                                        entity(new Error(e.getMessage())).
736                                                        build();
737                 }
738     }
739     
740     @GET
741     @Path("engine/controllers/{controllerName}/encoders")
742     @Produces(MediaType.APPLICATION_JSON)
743     public Response encoderFilters(@PathParam("controllerName") String controllerName) {        
744                 List<CoderFilters> encoders;
745                 try {
746                         PolicyController controller = PolicyController.factory.get(controllerName);
747                 if (controller == null)
748                         return Response.status(Response.Status.BAD_REQUEST).
749                                                 entity(new Error(controllerName + "  does not exist")).
750                                                 build();
751                         DroolsController drools = controller.getDrools();
752                 if (drools == null)
753                         return Response.status(Response.Status.INTERNAL_SERVER_ERROR).
754                                                 entity(new Error(controllerName + "  has not drools component")).
755                                                 build();
756                         encoders = EventProtocolCoder.manager.getEncoderFilters
757                                                         (drools.getGroupId(), drools.getArtifactId());
758                 } catch (IllegalArgumentException e) {
759                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
760                           controllerName, this.toString());
761                         return Response.status(Response.Status.BAD_REQUEST).
762                                                        entity(new Error(controllerName +  " not found: " + e.getMessage())).
763                                                        build();
764                 } catch (IllegalStateException e) {
765                         logger.warn(MessageCodes.EXCEPTION_ERROR, e, 
766                     controllerName, this.toString());
767                         return Response.status(Response.Status.NOT_ACCEPTABLE).
768                             entity(new Error(controllerName + " is not accepting the request")).build();
769                 }
770                 
771                 return Response.status(Response.Status.OK).
772                                entity(encoders).
773                                build();
774     }
775     
776     @POST
777     @Path("engine/controllers/{controllerName}/decoders/{topic}")
778     @Produces(MediaType.APPLICATION_JSON)
779     public Response decode(@PathParam("controllerName") String controllerName,
780                                    @PathParam("topic") String topic,
781                                    String json) {
782         
783         PolicyController policyController = PolicyController.factory.get(controllerName);
784         
785         CodingResult result = new CodingResult();
786                 result.decoding = false;
787                 result.encoding = false;
788                 result.jsonEncoding = null;
789
790                 Object event;
791         try {
792                 event = EventProtocolCoder.manager.decode
793                                         (policyController.getDrools().getGroupId(), 
794                                          policyController.getDrools().getArtifactId(), 
795                                          topic, 
796                                          json);
797                 result.decoding = true;
798         } catch (Exception e) {
799                 return Response.status(Response.Status.BAD_REQUEST).
800                                 entity(new Error(e.getMessage())).
801                                 build();
802         }
803         
804         try {
805                 result.jsonEncoding = EventProtocolCoder.manager.encode(topic, event);
806                 result.encoding = true;
807         } catch (Exception e) {
808                 return Response.status(Response.Status.OK).
809                                 entity(result).
810                                 build();
811         } 
812         
813                 return Response.status(Response.Status.OK).
814                 entity(result).
815                 build();
816     }
817     
818         @GET
819     @Path("engine/topics")
820     @Produces(MediaType.APPLICATION_JSON)
821     public TopicEndpoint topics() {
822         return TopicEndpoint.manager;
823     }
824     
825         @SuppressWarnings("unchecked")
826         @GET
827     @Path("engine/topics/sources")
828     @Produces(MediaType.APPLICATION_JSON)
829     public List<TopicSource> sources() {
830         return (List<TopicSource>) TopicEndpoint.manager.getTopicSources();
831     }
832     
833     @SuppressWarnings("unchecked")
834         @GET
835     @Path("engine/topics/sinks")
836     @Produces(MediaType.APPLICATION_JSON)
837     public List<TopicSink> sinks() {
838         return (List<TopicSink>) TopicEndpoint.manager.getTopicSinks();
839     }
840     
841         @GET
842     @Path("engine/topics/sources/ueb")
843     @Produces(MediaType.APPLICATION_JSON)
844     public List<UebTopicSource> uebSources() {
845         return TopicEndpoint.manager.getUebTopicSources();
846     }
847     
848         @GET
849     @Path("engine/topics/sinks/ueb")
850     @Produces(MediaType.APPLICATION_JSON)
851     public List<UebTopicSink> uebSinks() {
852         return (List<UebTopicSink>) TopicEndpoint.manager.getUebTopicSinks();
853     }
854     
855         @GET
856     @Path("engine/topics/sources/dmaap")
857     @Produces(MediaType.APPLICATION_JSON)
858     public List<DmaapTopicSource> dmaapSources() {
859         return TopicEndpoint.manager.getDmaapTopicSources();
860     }
861     
862         @GET
863     @Path("engine/topics/sinks/dmaap")
864     @Produces(MediaType.APPLICATION_JSON)
865     public List<DmaapTopicSink> dmaapSinks() {
866         return (List<DmaapTopicSink>) TopicEndpoint.manager.getDmaapTopicSinks();
867     }
868     
869     @SuppressWarnings("unchecked")
870     @GET
871     @Path("engine/topics/{topic}/sources")
872     @Produces(MediaType.APPLICATION_JSON)
873     public List<TopicSource> sourceTopic(@PathParam("topic") String topic) {
874         List<String> topics = new ArrayList<String>();
875         topics.add(topic);
876         
877         return (List<TopicSource>) TopicEndpoint.manager.getTopicSources(topics);
878     }
879     
880     @SuppressWarnings("unchecked")
881     @GET
882     @Path("engine/topics/{topic}/sinks")
883     @Produces(MediaType.APPLICATION_JSON)
884     public List<TopicSink> sinkTopic(@PathParam("topic") String topic) {
885         List<String> topics = new ArrayList<String>();
886         topics.add(topic);
887         
888         return (List<TopicSink>) TopicEndpoint.manager.getTopicSinks(topics);
889     }
890     
891     
892     @GET
893     @Path("engine/topics/{topic}/ueb/source")
894     @Produces(MediaType.APPLICATION_JSON)
895     public UebTopicSource uebSourceTopic(@PathParam("topic") String topic) {
896         return TopicEndpoint.manager.getUebTopicSource(topic);
897     }
898     
899     @GET
900     @Path("engine/topics/{topic}/ueb/sink")
901     @Produces(MediaType.APPLICATION_JSON)
902     public UebTopicSink uebSinkTopic(@PathParam("topic") String topic) {
903         return TopicEndpoint.manager.getUebTopicSink(topic);
904     }
905     
906     @GET
907     @Path("engine/topics/{topic}/dmaap/source")
908     @Produces(MediaType.APPLICATION_JSON)
909     public DmaapTopicSource dmaapSourceTopic(@PathParam("topic") String topic) {
910         return TopicEndpoint.manager.getDmaapTopicSource(topic);
911     }
912     
913     @GET
914     @Path("engine/topics/{topic}/dmaap/sink")
915     @Produces(MediaType.APPLICATION_JSON)
916     public DmaapTopicSink dmaapSinkTopic(@PathParam("topic") String topic) {
917         return TopicEndpoint.manager.getDmaapTopicSink(topic);
918     }
919     
920     @GET
921     @Path("engine/topics/{topic}/ueb/source/events")
922     @Produces(MediaType.APPLICATION_JSON)
923     public Response uebSourceEvent(@PathParam("topic") String topicName) {
924         
925         UebTopicSource uebReader = TopicEndpoint.manager.getUebTopicSource(topicName);
926         String[] events = uebReader.getRecentEvents();
927                 return Response.status(Status.OK).
928                         entity(events).
929                         build();
930     }
931     
932     @GET
933     @Path("engine/topics/{topic}/ueb/sink/events")
934     @Produces(MediaType.APPLICATION_JSON)
935     public Response uebSinkEvent(@PathParam("topic") String topicName) {
936         
937         UebTopicSink uebSink = TopicEndpoint.manager.getUebTopicSink(topicName);
938         String[] events = uebSink.getRecentEvents();
939                 return Response.status(Status.OK).
940                         entity(events).
941                         build();
942     }
943     
944     @GET
945     @Path("engine/topics/{topic}/dmaap/source/events")
946     @Produces(MediaType.APPLICATION_JSON)
947     public Response dmaapSourcevent(@PathParam("topic") String topicName) {
948         
949         DmaapTopicSource uebReader = TopicEndpoint.manager.getDmaapTopicSource(topicName);
950         String[] events = uebReader.getRecentEvents();
951                 return Response.status(Status.OK).
952                         entity(events).
953                         build();
954     }
955     
956     @GET
957     @Path("engine/topics/{topic}/dmaap/sink/events")
958     @Produces(MediaType.APPLICATION_JSON)
959     public Response dmaapSinkEvent(@PathParam("topic") String topicName) {
960         
961         DmaapTopicSink uebSink = TopicEndpoint.manager.getDmaapTopicSink(topicName);
962         String[] events = uebSink.getRecentEvents();
963                 return Response.status(Status.OK).
964                         entity(events).
965                         build();
966     }
967     
968     @PUT
969     @Path("engine/topics/{topic}/ueb/sources/events")
970     @Consumes(MediaType.TEXT_PLAIN)
971     @Produces(MediaType.APPLICATION_JSON)
972     public Response uebOffer(@PathParam("topic") String topicName,
973                                  String json) {
974         try {
975                         UebTopicSource uebReader = TopicEndpoint.manager.getUebTopicSource(topicName);
976                         boolean success = uebReader.offer(json);
977                         if (success)
978                                 return Response.status(Status.OK).
979                                                         entity("Successfully injected event over " + topicName).
980                                                         build();
981                         else
982                                 return Response.status(Status.NOT_ACCEPTABLE).
983                                                         entity("Failure to inject event over " + topicName).
984                                                         build();
985                 } catch (Exception e) {
986                 return Response.status(Response.Status.BAD_REQUEST).
987                                 entity(new Error(e.getMessage())).
988                                 build();
989                 } 
990     }
991     
992     @PUT
993     @Path("engine/topics/{topic}/dmaap/sources/events")
994     @Consumes(MediaType.TEXT_PLAIN)
995     @Produces(MediaType.APPLICATION_JSON)
996     public Response dmaapOffer(@PathParam("topic") String topicName,
997                                    String json) {
998         try {
999                         DmaapTopicSource dmaapReader = TopicEndpoint.manager.getDmaapTopicSource(topicName);
1000                         boolean success = dmaapReader.offer(json);
1001                         if (success)
1002                                 return Response.status(Status.OK).
1003                                                         entity("Successfully injected event over " + topicName).
1004                                                         build();
1005                         else
1006                                 return Response.status(Status.NOT_ACCEPTABLE).
1007                                                         entity("Failure to inject event over " + topicName).
1008                                                         build();
1009                 } catch (Exception e) {
1010                 return Response.status(Response.Status.BAD_REQUEST).
1011                                 entity(new Error(e.getMessage())).
1012                                 build();
1013                 } 
1014     }
1015     
1016     @PUT
1017     @Path("engine/topics/lock")
1018     @Produces(MediaType.APPLICATION_JSON)
1019     @Consumes(MediaType.APPLICATION_JSON)
1020     public Response lockTopics() {
1021         boolean success = TopicEndpoint.manager.lock();
1022         if (success)
1023                 return Response.status(Status.OK).
1024                                         entity("Endpoints are locked").
1025                                         build();
1026         else
1027                 return Response.status(Status.SERVICE_UNAVAILABLE).
1028                                         entity("Endpoints cannot be locked").
1029                                         build();
1030     }
1031     
1032     @DELETE
1033     @Path("engine/topics/lock")
1034     @Produces(MediaType.APPLICATION_JSON)
1035     @Consumes(MediaType.APPLICATION_JSON)
1036     public Response unlockTopics() {
1037         boolean success = TopicEndpoint.manager.unlock();
1038         if (success)
1039                 return Response.status(Status.OK).
1040                                         entity("Endpoints are unlocked").
1041                                         build();
1042         else
1043                 return Response.status(Status.SERVICE_UNAVAILABLE).
1044                                         entity("Endpoints cannot be unlocked").
1045                                         build();
1046     }
1047     
1048     @PUT
1049     @Path("engine/topics/{topic}/ueb/sources/lock")
1050     @Produces(MediaType.APPLICATION_JSON)
1051     @Consumes(MediaType.APPLICATION_JSON)
1052     public Response lockTopic(@PathParam("topic") String topicName) {
1053         UebTopicSource reader = TopicEndpoint.manager.getUebTopicSource(topicName);     
1054         boolean success = reader.lock();
1055         if (success)
1056                 return Response.status(Status.OK).
1057                                         entity("Endpoints are unlocked").
1058                                         build();
1059         else
1060                 return Response.status(Status.SERVICE_UNAVAILABLE).
1061                                         entity("Endpoints cannot be unlocked").
1062                                         build();
1063     }
1064     
1065     @PUT
1066     @Path("engine/topics/{topic}/ueb/sources/unlock")
1067     @Produces(MediaType.APPLICATION_JSON)
1068     @Consumes(MediaType.APPLICATION_JSON)
1069     public Response unlockTopic(@PathParam("topic") String topicName) {
1070         UebTopicSource reader = TopicEndpoint.manager.getUebTopicSource(topicName);     
1071         boolean success = reader.unlock();
1072         if (success)
1073                 return Response.status(Status.OK).
1074                                         entity("Endpoints are unlocked").
1075                                         build();
1076         else
1077                 return Response.status(Status.SERVICE_UNAVAILABLE).
1078                                         entity("Endpoints cannot be unlocked").
1079                                         build();
1080     }
1081     
1082     @PUT
1083     @Path("engine/controllers/{controllerName}/lock")
1084     @Produces(MediaType.APPLICATION_JSON)
1085     public Response lockController(@PathParam("controllerName") String controllerName) {
1086         PolicyController policyController = PolicyController.factory.get(controllerName);
1087         boolean success = policyController.lock();
1088         if (success)
1089                 return Response.status(Status.OK).
1090                                         entity("Controller " + controllerName + " is now locked").
1091                                         build();
1092         else
1093                 return Response.status(Status.SERVICE_UNAVAILABLE).
1094                                         entity("Controller " + controllerName + " cannot be locked").
1095                                         build();
1096     }  
1097     
1098     @DELETE
1099     @Path("engine/controllers/{controllerName}/lock")
1100     @Produces(MediaType.APPLICATION_JSON)
1101     public Response unlockController(@PathParam("controllerName") String controllerName) {
1102         PolicyController policyController = PolicyController.factory.get(controllerName);
1103         boolean success = policyController.unlock();
1104         if (success)
1105                 return Response.status(Status.OK).
1106                                         entity("Controller " + controllerName + " is now unlocked").
1107                                         build();
1108         else
1109                 return Response.status(Status.SERVICE_UNAVAILABLE).
1110                                         entity("Controller " + controllerName + " cannot be unlocked").
1111                                         build();
1112     }
1113     
1114     @POST
1115     @Path("engine/util/coders/filters/rules/{ruleName}")
1116     @Produces(MediaType.APPLICATION_JSON)
1117     public Response rules(@DefaultValue("false") @QueryParam("negate") boolean negate,
1118                               @PathParam("ruleName") String name,
1119                               String regex) {           
1120         String literalRegex = Pattern.quote(regex);
1121         if (negate)
1122                 literalRegex = "^(?!" + literalRegex + "$).*";
1123         
1124                 return Response.status(Status.OK).
1125                                         entity(new JsonProtocolFilter.FilterRule(name,literalRegex)).
1126                                         build();
1127     }
1128     
1129     @GET
1130     @Path("engine/util/uuid")
1131     public Response uuid() {    
1132                 return Response.status(Status.OK).
1133                         entity(UUID.randomUUID().toString()).
1134                         build();
1135     }
1136     
1137     /*
1138      * Helper classes for aggregation of results
1139      */
1140     
1141     
1142         public static class Endpoints {
1143                 public List<TopicSource> sources;
1144                 public List<TopicSink> sinks;
1145                 
1146                 public Endpoints(List<TopicSource> sources,
1147                                          List<TopicSink> sinks) {
1148                         this.sources = sources;
1149                         this.sinks = sinks;
1150                 }
1151         }
1152         
1153         public static class Endpoint {
1154                 public TopicSource source;
1155                 public TopicSink sink;
1156                 
1157                 public Endpoint(TopicSource source,
1158                                            TopicSink sink) {
1159                         this.source = source;
1160                         this.sink = sink;
1161                 }
1162         }
1163         
1164         public static class CodingResult {
1165                 public String jsonEncoding;
1166                 public Boolean encoding;
1167                 public Boolean decoding;
1168         }
1169         
1170         public static class Error {
1171                 public String error;
1172
1173                 /**
1174                  * @param error
1175                  */
1176                 public Error(String error) {
1177                         this.error = error;
1178                 }
1179         }
1180 }
1181