2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.drools.server.restful;
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;
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;
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;
67 * REST Endpoint for management of the Drools PDP
70 public class RestManager {
74 private static Logger logger = FlexLogger.getLogger(RestManager.class);
77 * gets the Policy Engine
79 * @return the Policy Engine
83 @Produces(MediaType.APPLICATION_JSON)
84 public PolicyEngine engine() {
85 return PolicyEngine.manager;
90 * Updates the Policy Engine
92 * @param configuration configuration
93 * @return Policy 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;
103 success = PolicyEngine.manager.configure(configuration);
104 } catch (Exception e) {
106 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
107 "PolicyEngine", this.toString());
111 return Response.status(Response.Status.NOT_ACCEPTABLE).
112 entity(new Error("cannot perform operation")).build();
114 return Response.status(Response.Status.OK).entity(controller).build();
118 * Activates the Policy Engine
120 * @param configuration configuration
121 * @return Policy Engine
124 @Path("engine/activation")
125 @Produces(MediaType.APPLICATION_JSON)
126 public Response activateEngine() {
127 boolean success = true;
129 PolicyEngine.manager.activate();
130 } catch (Exception e) {
132 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
133 "PolicyEngine", this.toString());
137 return Response.status(Response.Status.NOT_ACCEPTABLE).
138 entity(new Error("cannot perform operation")).build();
140 return Response.status(Response.Status.OK).entity(PolicyEngine.manager).build();
144 * Activates the Policy Engine
146 * @param configuration configuration
147 * @return Policy Engine
150 @Path("engine/deactivation")
151 @Produces(MediaType.APPLICATION_JSON)
152 public Response deactivateEngine() {
153 boolean success = true;
155 PolicyEngine.manager.deactivate();
156 } catch (Exception e) {
158 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
159 "PolicyEngine", this.toString());
163 return Response.status(Response.Status.NOT_ACCEPTABLE).
164 entity(new Error("cannot perform operation")).build();
166 return Response.status(Response.Status.OK).entity(PolicyEngine.manager).build();
171 @Produces(MediaType.APPLICATION_JSON)
172 public Response engineShutdown() {
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).
183 return Response.status(Response.Status.OK).
184 entity(PolicyEngine.manager).
190 @Produces(MediaType.APPLICATION_JSON)
191 @Consumes(MediaType.APPLICATION_JSON)
192 public Response lockEngine() {
193 boolean success = PolicyEngine.manager.lock();
195 return Response.status(Status.OK).
196 entity("Policy Engine is locked").
199 return Response.status(Status.SERVICE_UNAVAILABLE).
200 entity("Policy Engine cannot be locked").
205 @Path("engine/unlock")
206 @Produces(MediaType.APPLICATION_JSON)
207 @Consumes(MediaType.APPLICATION_JSON)
208 public Response unlockEngine() {
209 boolean success = PolicyEngine.manager.unlock();
211 return Response.status(Status.OK).
212 entity("Policy Engine is unlocked").
215 return Response.status(Status.SERVICE_UNAVAILABLE).
216 entity("Policy Engine cannot be unlocked").
221 @Path("engine/controllers")
222 @Produces(MediaType.APPLICATION_JSON)
223 public List<PolicyController> controllers() {
224 return PolicyEngine.manager.getPolicyControllers();
228 @Path("engine/controllers")
229 @Produces(MediaType.APPLICATION_JSON)
230 @Consumes(MediaType.APPLICATION_JSON)
231 public Response addController(Properties config) {
233 return Response.status(Response.Status.BAD_REQUEST).
234 entity(new Error("A configuration must be provided")).
237 String controllerName = config.getProperty(PolicyProperties.PROPERTY_CONTROLLER_NAME);
238 if (controllerName == null || controllerName.isEmpty())
239 return Response.status(Response.Status.BAD_REQUEST).
241 ("Configuration must have an entry for " +
242 PolicyProperties.PROPERTY_CONTROLLER_NAME)).
245 PolicyController controller;
247 controller = PolicyController.factory.get(controllerName);
248 if (controller != null)
249 return Response.status(Response.Status.NOT_MODIFIED).
252 } catch (IllegalArgumentException e) {
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();
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())).
273 boolean success = controller.start();
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();
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();
286 return Response.status(Response.Status.CREATED).
292 @Path("engine/controllers/{controllerName}")
293 @Produces(MediaType.APPLICATION_JSON)
294 public Response controller(@PathParam("controllerName") String controllerName) {
295 PolicyController controller = null;
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();
308 if (controller != null)
309 return Response.status(Response.Status.OK).
310 entity(controller).build();
312 return Response.status(Response.Status.NOT_FOUND).
313 entity(new Error(controllerName + " not found")).build();
317 @Path("engine/controllers/{controllerName}")
318 @Produces(MediaType.APPLICATION_JSON)
319 @Consumes(MediaType.APPLICATION_JSON)
320 public Response deleteController(@PathParam("controllerName") String controllerName) {
322 if (controllerName == null || controllerName.isEmpty())
323 return Response.status(Response.Status.BAD_REQUEST).
324 entity("A controller name must be provided").
327 PolicyController 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")).
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())).
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();
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())).
358 return Response.status(Response.Status.OK).
364 * Updates the Policy Engine
366 * @param configuration configuration
367 * @return Policy Engine
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) {
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").
383 PolicyController controller;
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")).
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())).
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();
403 return Response.status(Response.Status.OK).
409 @Path("engine/controllers/{controllerName}/drools")
410 @Produces(MediaType.APPLICATION_JSON)
411 public Response drools(@PathParam("controllerName") String controllerName) {
413 DroolsController drools = getDroolsController(controllerName);
414 return Response.status(Response.Status.OK).
417 } catch (IllegalArgumentException | IllegalStateException e) {
418 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
419 controllerName, this.toString());
420 return Response.status(Response.Status.BAD_REQUEST).
421 entity(new Error(e.getMessage())).
427 @Path("engine/controllers/{controllerName}/drools/facts/{sessionName}")
428 @Produces(MediaType.APPLICATION_JSON)
429 public Response droolsFacts(@DefaultValue("false") @QueryParam("count") boolean count,
430 @PathParam("controllerName") String controllerName,
431 @PathParam("sessionName") String sessionName) {
433 DroolsController drools = getDroolsController(controllerName);
435 return Response.status(Response.Status.OK).
436 entity(drools.factClassNames(sessionName)).
439 return Response.status(Response.Status.OK).
440 entity(new Long(drools.factCount(sessionName))).
442 } catch (IllegalArgumentException | IllegalStateException e) {
443 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
444 controllerName, this.toString());
445 return Response.status(Response.Status.BAD_REQUEST).
446 entity(new Error(e.getMessage())).
452 @Path("engine/controllers/{controllerName}/drools/facts/{sessionName}/{className}")
453 @Produces(MediaType.APPLICATION_JSON)
454 public Response droolsFacts(@DefaultValue("false") @QueryParam("count") boolean count,
455 @PathParam("controllerName") String controllerName,
456 @PathParam("sessionName") String sessionName,
457 @PathParam("className") String className) {
459 DroolsController drools = getDroolsController(controllerName);
460 List<Object> facts = drools.facts(sessionName, className);
462 return Response.status(Response.Status.OK).entity(facts).build();
464 return Response.status(Response.Status.OK).entity(facts.size()).build();
465 } catch (IllegalArgumentException | IllegalStateException e) {
466 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
467 controllerName, this.toString());
468 return Response.status(Response.Status.BAD_REQUEST).
469 entity(new Error(e.getMessage())).
475 @Path("engine/controllers/{controllerName}/drools/facts/{sessionName}/{queryName}/{queriedEntity}")
476 @Produces(MediaType.APPLICATION_JSON)
477 public Response droolsFacts(@DefaultValue("false") @QueryParam("count") boolean count,
478 @PathParam("controllerName") String controllerName,
479 @PathParam("sessionName") String sessionName,
480 @PathParam("queryName") String queryName,
481 @PathParam("queriedEntity") String queriedEntity) {
483 DroolsController drools = getDroolsController(controllerName);
484 List<Object> facts = drools.factQuery(sessionName, queryName, queriedEntity);
486 return Response.status(Response.Status.OK).entity(facts).build();
488 return Response.status(Response.Status.OK).entity(facts.size()).build();
489 } catch (IllegalArgumentException | IllegalStateException e) {
490 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
491 controllerName, this.toString());
492 return Response.status(Response.Status.BAD_REQUEST).
493 entity(new Error(e.getMessage())).
499 @Path("engine/controllers/{controllerName}/decoders")
500 @Produces(MediaType.APPLICATION_JSON)
501 public Response decoders(@PathParam("controllerName") String controllerName) {
503 DroolsController drools = getDroolsController(controllerName);
504 List<ProtocolCoderToolset> decoders = EventProtocolCoder.manager.getDecoders
505 (drools.getGroupId(), drools.getArtifactId());
506 return Response.status(Response.Status.OK).
509 } catch (IllegalArgumentException | IllegalStateException e) {
510 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
511 controllerName, this.toString());
512 return Response.status(Response.Status.BAD_REQUEST).
513 entity(new Error(e.getMessage())).
519 @Path("engine/controllers/{controllerName}/decoders/filters")
520 @Produces(MediaType.APPLICATION_JSON)
521 public Response decoderFilters(@PathParam("controllerName") String controllerName) {
523 DroolsController drools = getDroolsController(controllerName);
524 List<CoderFilters> filters = EventProtocolCoder.manager.getDecoderFilters
525 (drools.getGroupId(), drools.getArtifactId());
526 return Response.status(Response.Status.OK).
529 } catch (IllegalArgumentException | IllegalStateException e) {
530 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
531 controllerName, this.toString());
532 return Response.status(Response.Status.BAD_REQUEST).
533 entity(new Error(e.getMessage())).
539 @Path("engine/controllers/{controllerName}/decoders/{topicName}")
540 @Produces(MediaType.APPLICATION_JSON)
541 public Response decoder(@PathParam("controllerName") String controllerName,
542 @PathParam("topicName") String topicName) {
544 DroolsController drools = getDroolsController(controllerName);
545 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
546 (drools.getGroupId(), drools.getArtifactId(), topicName);
547 return Response.status(Response.Status.OK).
550 } catch (IllegalArgumentException | IllegalStateException e) {
551 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
552 controllerName, this.toString());
553 return Response.status(Response.Status.BAD_REQUEST).
554 entity(new Error(e.getMessage())).
560 @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters")
561 @Produces(MediaType.APPLICATION_JSON)
562 public Response decoderFilter(@PathParam("controllerName") String controllerName,
563 @PathParam("topicName") String topicName) {
565 DroolsController drools = getDroolsController(controllerName);
566 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
567 (drools.getGroupId(), drools.getArtifactId(), topicName);
569 return Response.status(Response.Status.BAD_REQUEST).
570 entity(new Error(topicName + " does not exist")).
573 return Response.status(Response.Status.OK).
574 entity(decoder.getCoders()).
576 } catch (IllegalArgumentException | IllegalStateException e) {
577 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
578 controllerName, this.toString());
579 return Response.status(Response.Status.BAD_REQUEST).
580 entity(new Error(e.getMessage())).
586 @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}")
587 @Produces(MediaType.APPLICATION_JSON)
588 public Response decoderFilter(@PathParam("controllerName") String controllerName,
589 @PathParam("topicName") String topicName,
590 @PathParam("factClassName") String factClass) {
592 DroolsController drools = getDroolsController(controllerName);
593 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
594 (drools.getGroupId(), drools.getArtifactId(), topicName);
595 CoderFilters filters = decoder.getCoder(factClass);
597 return Response.status(Response.Status.BAD_REQUEST).
598 entity(new Error(topicName + ":" + factClass + " does not exist")).
601 return Response.status(Response.Status.OK).
604 } catch (IllegalArgumentException | IllegalStateException e) {
605 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
606 controllerName, this.toString());
607 return Response.status(Response.Status.BAD_REQUEST).
608 entity(new Error(e.getMessage())).
614 @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}")
615 @Consumes(MediaType.APPLICATION_JSON)
616 @Produces(MediaType.APPLICATION_JSON)
617 public Response decoderFilter(@PathParam("controllerName") String controllerName,
618 @PathParam("topicName") String topicName,
619 @PathParam("factClassName") String factClass,
620 JsonProtocolFilter configFilters) {
622 if (configFilters == null) {
623 return Response.status(Response.Status.BAD_REQUEST).
624 entity(new Error("Configuration Filters not provided")).
629 DroolsController drools = getDroolsController(controllerName);
630 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
631 (drools.getGroupId(), drools.getArtifactId(), topicName);
632 CoderFilters filters = decoder.getCoder(factClass);
634 return Response.status(Response.Status.BAD_REQUEST).
635 entity(new Error(topicName + ":" + factClass + " does not exist")).
637 filters.setFilter(configFilters);
638 return Response.status(Response.Status.OK).
641 } catch (IllegalArgumentException | IllegalStateException e) {
642 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
643 controllerName, this.toString());
644 return Response.status(Response.Status.BAD_REQUEST).
645 entity(new Error(e.getMessage())).
651 @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}/rules")
652 @Produces(MediaType.APPLICATION_JSON)
653 public Response decoderFilterRules(@PathParam("controllerName") String controllerName,
654 @PathParam("topicName") String topicName,
655 @PathParam("factClassName") String factClass) {
657 DroolsController drools = getDroolsController(controllerName);
658 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
659 (drools.getGroupId(), drools.getArtifactId(), topicName);
661 CoderFilters filters = decoder.getCoder(factClass);
663 return Response.status(Response.Status.BAD_REQUEST).
664 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " does not exist")).
667 JsonProtocolFilter filter = filters.getFilter();
669 return Response.status(Response.Status.BAD_REQUEST).
670 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " no filters")).
673 return Response.status(Response.Status.OK).
674 entity(filter.getRules()).
676 } catch (IllegalArgumentException | IllegalStateException e) {
677 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
678 controllerName, this.toString());
679 return Response.status(Response.Status.BAD_REQUEST).
680 entity(new Error(e.getMessage())).
686 @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}/rules/{ruleName}")
687 @Produces(MediaType.APPLICATION_JSON)
688 public Response decoderFilterRules(@PathParam("controllerName") String controllerName,
689 @PathParam("topicName") String topicName,
690 @PathParam("factClassName") String factClass,
691 @PathParam("ruleName") String ruleName) {
693 DroolsController drools = getDroolsController(controllerName);
694 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
695 (drools.getGroupId(), drools.getArtifactId(), topicName);
697 CoderFilters filters = decoder.getCoder(factClass);
699 return Response.status(Response.Status.BAD_REQUEST).
700 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " does not exist")).
703 JsonProtocolFilter filter = filters.getFilter();
705 return Response.status(Response.Status.BAD_REQUEST).
706 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " no filters")).
709 return Response.status(Response.Status.OK).
710 entity(filter.getRules(ruleName)).
712 } catch (IllegalArgumentException | IllegalStateException e) {
713 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
714 controllerName, this.toString());
715 return Response.status(Response.Status.BAD_REQUEST).
716 entity(new Error(e.getMessage())).
722 @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}/rules/{ruleName}")
723 @Consumes(MediaType.APPLICATION_JSON)
724 @Produces(MediaType.APPLICATION_JSON)
725 public Response deleteDecoderFilterRule(@PathParam("controllerName") String controllerName,
726 @PathParam("topicName") String topicName,
727 @PathParam("factClassName") String factClass,
728 @PathParam("ruleName") String ruleName,
732 DroolsController drools = getDroolsController(controllerName);
733 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
734 (drools.getGroupId(), drools.getArtifactId(), topicName);
736 CoderFilters filters = decoder.getCoder(factClass);
738 return Response.status(Response.Status.BAD_REQUEST).
739 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " does not exist")).
742 JsonProtocolFilter filter = filters.getFilter();
744 return Response.status(Response.Status.BAD_REQUEST).
745 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " no filters")).
749 filter.deleteRules(ruleName);
750 return Response.status(Response.Status.OK).
751 entity(filter.getRules()).
755 if (rule.getName() == null || !rule.getName().equals(ruleName))
756 return Response.status(Response.Status.BAD_REQUEST).
757 entity(new Error(controllerName + ":" + topicName + ":" + factClass + ":" + ruleName +
758 " rule name request inconsistencies (" + rule.getName() + ")")).
761 filter.deleteRule(ruleName, rule.getRegex());
762 return Response.status(Response.Status.OK).
763 entity(filter.getRules()).
765 } catch (IllegalArgumentException | IllegalStateException e) {
766 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
767 controllerName, this.toString());
768 return Response.status(Response.Status.BAD_REQUEST).
769 entity(new Error(e.getMessage())).
775 @Path("engine/controllers/{controllerName}/decoders/{topicName}/filters/{factClassName}/rules")
776 @Produces(MediaType.APPLICATION_JSON)
777 public Response decoderFilterRule(@PathParam("controllerName") String controllerName,
778 @PathParam("topicName") String topicName,
779 @PathParam("factClassName") String factClass,
780 JsonProtocolFilter.FilterRule rule) {
783 DroolsController drools = getDroolsController(controllerName);
784 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
785 (drools.getGroupId(), drools.getArtifactId(), topicName);
787 CoderFilters filters = decoder.getCoder(factClass);
789 return Response.status(Response.Status.BAD_REQUEST).
790 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " does not exist")).
793 JsonProtocolFilter filter = filters.getFilter();
795 return Response.status(Response.Status.BAD_REQUEST).
796 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " no filters")).
799 if (rule.getName() == null)
800 return Response.status(Response.Status.BAD_REQUEST).
801 entity(new Error(controllerName + ":" + topicName + ":" + factClass +
802 " rule name request inconsistencies (" + rule.getName() + ")")).
805 filter.addRule(rule.getName(), rule.getRegex());
806 return Response.status(Response.Status.OK).
807 entity(filter.getRules()).
809 } catch (IllegalArgumentException | IllegalStateException e) {
810 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
811 controllerName, this.toString());
812 return Response.status(Response.Status.BAD_REQUEST).
813 entity(new Error(e.getMessage())).
819 @Path("engine/controllers/{controllerName}/encoders")
820 @Produces(MediaType.APPLICATION_JSON)
821 public Response encoderFilters(@PathParam("controllerName") String controllerName) {
822 List<CoderFilters> encoders;
824 PolicyController controller = PolicyController.factory.get(controllerName);
825 if (controller == null)
826 return Response.status(Response.Status.BAD_REQUEST).
827 entity(new Error(controllerName + " does not exist")).
829 DroolsController drools = controller.getDrools();
831 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).
832 entity(new Error(controllerName + " has not drools component")).
834 encoders = EventProtocolCoder.manager.getEncoderFilters
835 (drools.getGroupId(), drools.getArtifactId());
836 } catch (IllegalArgumentException e) {
837 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
838 controllerName, this.toString());
839 return Response.status(Response.Status.BAD_REQUEST).
840 entity(new Error(controllerName + " not found: " + e.getMessage())).
842 } catch (IllegalStateException e) {
843 logger.warn(MessageCodes.EXCEPTION_ERROR, e,
844 controllerName, this.toString());
845 return Response.status(Response.Status.NOT_ACCEPTABLE).
846 entity(new Error(controllerName + " is not accepting the request")).build();
849 return Response.status(Response.Status.OK).
855 @Path("engine/controllers/{controllerName}/decoders/{topic}")
856 @Produces(MediaType.APPLICATION_JSON)
857 public Response decode(@PathParam("controllerName") String controllerName,
858 @PathParam("topic") String topic,
861 PolicyController policyController = PolicyController.factory.get(controllerName);
863 CodingResult result = new CodingResult();
864 result.decoding = false;
865 result.encoding = false;
866 result.jsonEncoding = null;
870 event = EventProtocolCoder.manager.decode
871 (policyController.getDrools().getGroupId(),
872 policyController.getDrools().getArtifactId(),
875 result.decoding = true;
876 } catch (Exception e) {
877 return Response.status(Response.Status.BAD_REQUEST).
878 entity(new Error(e.getMessage())).
883 result.jsonEncoding = EventProtocolCoder.manager.encode(topic, event);
884 result.encoding = true;
885 } catch (Exception e) {
886 return Response.status(Response.Status.OK).
891 return Response.status(Response.Status.OK).
897 @Path("engine/topics")
898 @Produces(MediaType.APPLICATION_JSON)
899 public TopicEndpoint topics() {
900 return TopicEndpoint.manager;
903 @SuppressWarnings("unchecked")
905 @Path("engine/topics/sources")
906 @Produces(MediaType.APPLICATION_JSON)
907 public List<TopicSource> sources() {
908 return (List<TopicSource>) TopicEndpoint.manager.getTopicSources();
911 @SuppressWarnings("unchecked")
913 @Path("engine/topics/sinks")
914 @Produces(MediaType.APPLICATION_JSON)
915 public List<TopicSink> sinks() {
916 return (List<TopicSink>) TopicEndpoint.manager.getTopicSinks();
920 @Path("engine/topics/sources/ueb")
921 @Produces(MediaType.APPLICATION_JSON)
922 public List<UebTopicSource> uebSources() {
923 return TopicEndpoint.manager.getUebTopicSources();
927 @Path("engine/topics/sinks/ueb")
928 @Produces(MediaType.APPLICATION_JSON)
929 public List<UebTopicSink> uebSinks() {
930 return (List<UebTopicSink>) TopicEndpoint.manager.getUebTopicSinks();
934 @Path("engine/topics/sources/dmaap")
935 @Produces(MediaType.APPLICATION_JSON)
936 public List<DmaapTopicSource> dmaapSources() {
937 return TopicEndpoint.manager.getDmaapTopicSources();
941 @Path("engine/topics/sinks/dmaap")
942 @Produces(MediaType.APPLICATION_JSON)
943 public List<DmaapTopicSink> dmaapSinks() {
944 return (List<DmaapTopicSink>) TopicEndpoint.manager.getDmaapTopicSinks();
947 @SuppressWarnings("unchecked")
949 @Path("engine/topics/{topic}/sources")
950 @Produces(MediaType.APPLICATION_JSON)
951 public List<TopicSource> sourceTopic(@PathParam("topic") String topic) {
952 List<String> topics = new ArrayList<String>();
955 return (List<TopicSource>) TopicEndpoint.manager.getTopicSources(topics);
958 @SuppressWarnings("unchecked")
960 @Path("engine/topics/{topic}/sinks")
961 @Produces(MediaType.APPLICATION_JSON)
962 public List<TopicSink> sinkTopic(@PathParam("topic") String topic) {
963 List<String> topics = new ArrayList<String>();
966 return (List<TopicSink>) TopicEndpoint.manager.getTopicSinks(topics);
971 @Path("engine/topics/{topic}/ueb/source")
972 @Produces(MediaType.APPLICATION_JSON)
973 public UebTopicSource uebSourceTopic(@PathParam("topic") String topic) {
974 return TopicEndpoint.manager.getUebTopicSource(topic);
978 @Path("engine/topics/{topic}/ueb/sink")
979 @Produces(MediaType.APPLICATION_JSON)
980 public UebTopicSink uebSinkTopic(@PathParam("topic") String topic) {
981 return TopicEndpoint.manager.getUebTopicSink(topic);
985 @Path("engine/topics/{topic}/dmaap/source")
986 @Produces(MediaType.APPLICATION_JSON)
987 public DmaapTopicSource dmaapSourceTopic(@PathParam("topic") String topic) {
988 return TopicEndpoint.manager.getDmaapTopicSource(topic);
992 @Path("engine/topics/{topic}/dmaap/sink")
993 @Produces(MediaType.APPLICATION_JSON)
994 public DmaapTopicSink dmaapSinkTopic(@PathParam("topic") String topic) {
995 return TopicEndpoint.manager.getDmaapTopicSink(topic);
999 @Path("engine/topics/{topic}/ueb/source/events")
1000 @Produces(MediaType.APPLICATION_JSON)
1001 public Response uebSourceEvent(@PathParam("topic") String topicName) {
1003 UebTopicSource uebReader = TopicEndpoint.manager.getUebTopicSource(topicName);
1004 String[] events = uebReader.getRecentEvents();
1005 return Response.status(Status.OK).
1011 @Path("engine/topics/{topic}/ueb/sink/events")
1012 @Produces(MediaType.APPLICATION_JSON)
1013 public Response uebSinkEvent(@PathParam("topic") String topicName) {
1015 UebTopicSink uebSink = TopicEndpoint.manager.getUebTopicSink(topicName);
1016 String[] events = uebSink.getRecentEvents();
1017 return Response.status(Status.OK).
1023 @Path("engine/topics/{topic}/dmaap/source/events")
1024 @Produces(MediaType.APPLICATION_JSON)
1025 public Response dmaapSourcevent(@PathParam("topic") String topicName) {
1027 DmaapTopicSource uebReader = TopicEndpoint.manager.getDmaapTopicSource(topicName);
1028 String[] events = uebReader.getRecentEvents();
1029 return Response.status(Status.OK).
1035 @Path("engine/topics/{topic}/dmaap/sink/events")
1036 @Produces(MediaType.APPLICATION_JSON)
1037 public Response dmaapSinkEvent(@PathParam("topic") String topicName) {
1039 DmaapTopicSink uebSink = TopicEndpoint.manager.getDmaapTopicSink(topicName);
1040 String[] events = uebSink.getRecentEvents();
1041 return Response.status(Status.OK).
1047 @Path("engine/topics/{topic}/ueb/sources/events")
1048 @Consumes(MediaType.TEXT_PLAIN)
1049 @Produces(MediaType.APPLICATION_JSON)
1050 public Response uebOffer(@PathParam("topic") String topicName,
1053 UebTopicSource uebReader = TopicEndpoint.manager.getUebTopicSource(topicName);
1054 boolean success = uebReader.offer(json);
1056 return Response.status(Status.OK).
1057 entity("Successfully injected event over " + topicName).
1060 return Response.status(Status.NOT_ACCEPTABLE).
1061 entity("Failure to inject event over " + topicName).
1063 } catch (Exception e) {
1064 return Response.status(Response.Status.BAD_REQUEST).
1065 entity(new Error(e.getMessage())).
1071 @Path("engine/topics/{topic}/dmaap/sources/events")
1072 @Consumes(MediaType.TEXT_PLAIN)
1073 @Produces(MediaType.APPLICATION_JSON)
1074 public Response dmaapOffer(@PathParam("topic") String topicName,
1077 DmaapTopicSource dmaapReader = TopicEndpoint.manager.getDmaapTopicSource(topicName);
1078 boolean success = dmaapReader.offer(json);
1080 return Response.status(Status.OK).
1081 entity("Successfully injected event over " + topicName).
1084 return Response.status(Status.NOT_ACCEPTABLE).
1085 entity("Failure to inject event over " + topicName).
1087 } catch (Exception e) {
1088 return Response.status(Response.Status.BAD_REQUEST).
1089 entity(new Error(e.getMessage())).
1095 @Path("engine/topics/lock")
1096 @Produces(MediaType.APPLICATION_JSON)
1097 @Consumes(MediaType.APPLICATION_JSON)
1098 public Response lockTopics() {
1099 boolean success = TopicEndpoint.manager.lock();
1101 return Response.status(Status.OK).
1102 entity("Endpoints are locked").
1105 return Response.status(Status.SERVICE_UNAVAILABLE).
1106 entity("Endpoints cannot be locked").
1111 @Path("engine/topics/lock")
1112 @Produces(MediaType.APPLICATION_JSON)
1113 @Consumes(MediaType.APPLICATION_JSON)
1114 public Response unlockTopics() {
1115 boolean success = TopicEndpoint.manager.unlock();
1117 return Response.status(Status.OK).
1118 entity("Endpoints are unlocked").
1121 return Response.status(Status.SERVICE_UNAVAILABLE).
1122 entity("Endpoints cannot be unlocked").
1127 @Path("engine/topics/{topic}/ueb/sources/lock")
1128 @Produces(MediaType.APPLICATION_JSON)
1129 @Consumes(MediaType.APPLICATION_JSON)
1130 public Response lockTopic(@PathParam("topic") String topicName) {
1131 UebTopicSource reader = TopicEndpoint.manager.getUebTopicSource(topicName);
1132 boolean success = reader.lock();
1134 return Response.status(Status.OK).
1135 entity("Endpoints are unlocked").
1138 return Response.status(Status.SERVICE_UNAVAILABLE).
1139 entity("Endpoints cannot be unlocked").
1144 @Path("engine/topics/{topic}/ueb/sources/unlock")
1145 @Produces(MediaType.APPLICATION_JSON)
1146 @Consumes(MediaType.APPLICATION_JSON)
1147 public Response unlockTopic(@PathParam("topic") String topicName) {
1148 UebTopicSource reader = TopicEndpoint.manager.getUebTopicSource(topicName);
1149 boolean success = reader.unlock();
1151 return Response.status(Status.OK).
1152 entity("Endpoints are unlocked").
1155 return Response.status(Status.SERVICE_UNAVAILABLE).
1156 entity("Endpoints cannot be unlocked").
1161 @Path("engine/controllers/{controllerName}/lock")
1162 @Produces(MediaType.APPLICATION_JSON)
1163 public Response lockController(@PathParam("controllerName") String controllerName) {
1164 PolicyController policyController = PolicyController.factory.get(controllerName);
1165 boolean success = policyController.lock();
1167 return Response.status(Status.OK).
1168 entity("Controller " + controllerName + " is now locked").
1171 return Response.status(Status.SERVICE_UNAVAILABLE).
1172 entity("Controller " + controllerName + " cannot be locked").
1177 @Path("engine/controllers/{controllerName}/lock")
1178 @Produces(MediaType.APPLICATION_JSON)
1179 public Response unlockController(@PathParam("controllerName") String controllerName) {
1180 PolicyController policyController = PolicyController.factory.get(controllerName);
1181 boolean success = policyController.unlock();
1183 return Response.status(Status.OK).
1184 entity("Controller " + controllerName + " is now unlocked").
1187 return Response.status(Status.SERVICE_UNAVAILABLE).
1188 entity("Controller " + controllerName + " cannot be unlocked").
1193 @Path("engine/util/coders/filters/rules/{ruleName}")
1194 @Produces(MediaType.APPLICATION_JSON)
1195 public Response rules(@DefaultValue("false") @QueryParam("negate") boolean negate,
1196 @PathParam("ruleName") String name,
1198 String literalRegex = Pattern.quote(regex);
1200 literalRegex = "^(?!" + literalRegex + "$).*";
1202 return Response.status(Status.OK).
1203 entity(new JsonProtocolFilter.FilterRule(name,literalRegex)).
1208 @Path("engine/util/uuid")
1209 public Response uuid() {
1210 return Response.status(Status.OK).
1211 entity(UUID.randomUUID().toString()).
1216 protected DroolsController getDroolsController(String controllerName) throws IllegalArgumentException {
1217 PolicyController controller = PolicyController.factory.get(controllerName);
1218 if (controller == null)
1219 throw new IllegalArgumentException(controllerName + " does not exist");
1221 DroolsController drools = controller.getDrools();
1223 throw new IllegalArgumentException(controllerName + " has no drools configuration");
1229 * Helper classes for aggregation of results
1233 public static class Endpoints {
1234 public List<TopicSource> sources;
1235 public List<TopicSink> sinks;
1237 public Endpoints(List<TopicSource> sources,
1238 List<TopicSink> sinks) {
1239 this.sources = sources;
1244 public static class Endpoint {
1245 public TopicSource source;
1246 public TopicSink sink;
1248 public Endpoint(TopicSource source,
1250 this.source = source;
1255 public static class CodingResult {
1256 public String jsonEncoding;
1257 public Boolean encoding;
1258 public Boolean decoding;
1261 public static class Error {
1262 public String error;
1267 public Error(String error) {