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).
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");
413 DroolsController drools = controller.getDrools();
415 throw new IllegalArgumentException(controllerName + " has no drools configuration");
421 @Path("engine/controllers/{controllerName}/decoders")
422 @Produces(MediaType.APPLICATION_JSON)
423 public Response decoders(@PathParam("controllerName") String controllerName) {
425 DroolsController drools = getDroolsController(controllerName);
426 List<ProtocolCoderToolset> decoders = EventProtocolCoder.manager.getDecoders
427 (drools.getGroupId(), drools.getArtifactId());
428 return Response.status(Response.Status.OK).
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())).
441 @Path("engine/controllers/{controllerName}/decoders/filters")
442 @Produces(MediaType.APPLICATION_JSON)
443 public Response decoderFilters(@PathParam("controllerName") String controllerName) {
445 DroolsController drools = getDroolsController(controllerName);
446 List<CoderFilters> filters = EventProtocolCoder.manager.getDecoderFilters
447 (drools.getGroupId(), drools.getArtifactId());
448 return Response.status(Response.Status.OK).
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())).
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) {
466 DroolsController drools = getDroolsController(controllerName);
467 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
468 (drools.getGroupId(), drools.getArtifactId(), topicName);
469 return Response.status(Response.Status.OK).
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())).
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) {
487 DroolsController drools = getDroolsController(controllerName);
488 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
489 (drools.getGroupId(), drools.getArtifactId(), topicName);
491 return Response.status(Response.Status.BAD_REQUEST).
492 entity(new Error(topicName + " does not exist")).
495 return Response.status(Response.Status.OK).
496 entity(decoder.getCoders()).
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())).
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) {
514 DroolsController drools = getDroolsController(controllerName);
515 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
516 (drools.getGroupId(), drools.getArtifactId(), topicName);
517 CoderFilters filters = decoder.getCoder(factClass);
519 return Response.status(Response.Status.BAD_REQUEST).
520 entity(new Error(topicName + ":" + factClass + " does not exist")).
523 return Response.status(Response.Status.OK).
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())).
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) {
544 if (configFilters == null) {
545 return Response.status(Response.Status.BAD_REQUEST).
546 entity(new Error("Configuration Filters not provided")).
551 DroolsController drools = getDroolsController(controllerName);
552 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
553 (drools.getGroupId(), drools.getArtifactId(), topicName);
554 CoderFilters filters = decoder.getCoder(factClass);
556 return Response.status(Response.Status.BAD_REQUEST).
557 entity(new Error(topicName + ":" + factClass + " does not exist")).
559 filters.setFilter(configFilters);
560 return Response.status(Response.Status.OK).
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())).
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) {
579 DroolsController drools = getDroolsController(controllerName);
580 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
581 (drools.getGroupId(), drools.getArtifactId(), topicName);
583 CoderFilters filters = decoder.getCoder(factClass);
585 return Response.status(Response.Status.BAD_REQUEST).
586 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " does not exist")).
589 JsonProtocolFilter filter = filters.getFilter();
591 return Response.status(Response.Status.BAD_REQUEST).
592 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " no filters")).
595 return Response.status(Response.Status.OK).
596 entity(filter.getRules()).
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())).
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) {
615 DroolsController drools = getDroolsController(controllerName);
616 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
617 (drools.getGroupId(), drools.getArtifactId(), topicName);
619 CoderFilters filters = decoder.getCoder(factClass);
621 return Response.status(Response.Status.BAD_REQUEST).
622 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " does not exist")).
625 JsonProtocolFilter filter = filters.getFilter();
627 return Response.status(Response.Status.BAD_REQUEST).
628 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " no filters")).
631 return Response.status(Response.Status.OK).
632 entity(filter.getRules(ruleName)).
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())).
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,
654 DroolsController drools = getDroolsController(controllerName);
655 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
656 (drools.getGroupId(), drools.getArtifactId(), topicName);
658 CoderFilters filters = decoder.getCoder(factClass);
660 return Response.status(Response.Status.BAD_REQUEST).
661 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " does not exist")).
664 JsonProtocolFilter filter = filters.getFilter();
666 return Response.status(Response.Status.BAD_REQUEST).
667 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " no filters")).
671 filter.deleteRules(ruleName);
672 return Response.status(Response.Status.OK).
673 entity(filter.getRules()).
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() + ")")).
683 filter.deleteRule(ruleName, rule.getRegex());
684 return Response.status(Response.Status.OK).
685 entity(filter.getRules()).
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())).
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) {
705 DroolsController drools = getDroolsController(controllerName);
706 ProtocolCoderToolset decoder = EventProtocolCoder.manager.getDecoders
707 (drools.getGroupId(), drools.getArtifactId(), topicName);
709 CoderFilters filters = decoder.getCoder(factClass);
711 return Response.status(Response.Status.BAD_REQUEST).
712 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " does not exist")).
715 JsonProtocolFilter filter = filters.getFilter();
717 return Response.status(Response.Status.BAD_REQUEST).
718 entity(new Error(controllerName + ":" + topicName + ":" + factClass + " no filters")).
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() + ")")).
727 filter.addRule(rule.getName(), rule.getRegex());
728 return Response.status(Response.Status.OK).
729 entity(filter.getRules()).
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())).
741 @Path("engine/controllers/{controllerName}/encoders")
742 @Produces(MediaType.APPLICATION_JSON)
743 public Response encoderFilters(@PathParam("controllerName") String controllerName) {
744 List<CoderFilters> encoders;
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")).
751 DroolsController drools = controller.getDrools();
753 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).
754 entity(new Error(controllerName + " has not drools component")).
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())).
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();
771 return Response.status(Response.Status.OK).
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,
783 PolicyController policyController = PolicyController.factory.get(controllerName);
785 CodingResult result = new CodingResult();
786 result.decoding = false;
787 result.encoding = false;
788 result.jsonEncoding = null;
792 event = EventProtocolCoder.manager.decode
793 (policyController.getDrools().getGroupId(),
794 policyController.getDrools().getArtifactId(),
797 result.decoding = true;
798 } catch (Exception e) {
799 return Response.status(Response.Status.BAD_REQUEST).
800 entity(new Error(e.getMessage())).
805 result.jsonEncoding = EventProtocolCoder.manager.encode(topic, event);
806 result.encoding = true;
807 } catch (Exception e) {
808 return Response.status(Response.Status.OK).
813 return Response.status(Response.Status.OK).
819 @Path("engine/topics")
820 @Produces(MediaType.APPLICATION_JSON)
821 public TopicEndpoint topics() {
822 return TopicEndpoint.manager;
825 @SuppressWarnings("unchecked")
827 @Path("engine/topics/sources")
828 @Produces(MediaType.APPLICATION_JSON)
829 public List<TopicSource> sources() {
830 return (List<TopicSource>) TopicEndpoint.manager.getTopicSources();
833 @SuppressWarnings("unchecked")
835 @Path("engine/topics/sinks")
836 @Produces(MediaType.APPLICATION_JSON)
837 public List<TopicSink> sinks() {
838 return (List<TopicSink>) TopicEndpoint.manager.getTopicSinks();
842 @Path("engine/topics/sources/ueb")
843 @Produces(MediaType.APPLICATION_JSON)
844 public List<UebTopicSource> uebSources() {
845 return TopicEndpoint.manager.getUebTopicSources();
849 @Path("engine/topics/sinks/ueb")
850 @Produces(MediaType.APPLICATION_JSON)
851 public List<UebTopicSink> uebSinks() {
852 return (List<UebTopicSink>) TopicEndpoint.manager.getUebTopicSinks();
856 @Path("engine/topics/sources/dmaap")
857 @Produces(MediaType.APPLICATION_JSON)
858 public List<DmaapTopicSource> dmaapSources() {
859 return TopicEndpoint.manager.getDmaapTopicSources();
863 @Path("engine/topics/sinks/dmaap")
864 @Produces(MediaType.APPLICATION_JSON)
865 public List<DmaapTopicSink> dmaapSinks() {
866 return (List<DmaapTopicSink>) TopicEndpoint.manager.getDmaapTopicSinks();
869 @SuppressWarnings("unchecked")
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>();
877 return (List<TopicSource>) TopicEndpoint.manager.getTopicSources(topics);
880 @SuppressWarnings("unchecked")
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>();
888 return (List<TopicSink>) TopicEndpoint.manager.getTopicSinks(topics);
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);
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);
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);
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);
921 @Path("engine/topics/{topic}/ueb/source/events")
922 @Produces(MediaType.APPLICATION_JSON)
923 public Response uebSourceEvent(@PathParam("topic") String topicName) {
925 UebTopicSource uebReader = TopicEndpoint.manager.getUebTopicSource(topicName);
926 String[] events = uebReader.getRecentEvents();
927 return Response.status(Status.OK).
933 @Path("engine/topics/{topic}/ueb/sink/events")
934 @Produces(MediaType.APPLICATION_JSON)
935 public Response uebSinkEvent(@PathParam("topic") String topicName) {
937 UebTopicSink uebSink = TopicEndpoint.manager.getUebTopicSink(topicName);
938 String[] events = uebSink.getRecentEvents();
939 return Response.status(Status.OK).
945 @Path("engine/topics/{topic}/dmaap/source/events")
946 @Produces(MediaType.APPLICATION_JSON)
947 public Response dmaapSourcevent(@PathParam("topic") String topicName) {
949 DmaapTopicSource uebReader = TopicEndpoint.manager.getDmaapTopicSource(topicName);
950 String[] events = uebReader.getRecentEvents();
951 return Response.status(Status.OK).
957 @Path("engine/topics/{topic}/dmaap/sink/events")
958 @Produces(MediaType.APPLICATION_JSON)
959 public Response dmaapSinkEvent(@PathParam("topic") String topicName) {
961 DmaapTopicSink uebSink = TopicEndpoint.manager.getDmaapTopicSink(topicName);
962 String[] events = uebSink.getRecentEvents();
963 return Response.status(Status.OK).
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,
975 UebTopicSource uebReader = TopicEndpoint.manager.getUebTopicSource(topicName);
976 boolean success = uebReader.offer(json);
978 return Response.status(Status.OK).
979 entity("Successfully injected event over " + topicName).
982 return Response.status(Status.NOT_ACCEPTABLE).
983 entity("Failure to inject event over " + topicName).
985 } catch (Exception e) {
986 return Response.status(Response.Status.BAD_REQUEST).
987 entity(new Error(e.getMessage())).
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,
999 DmaapTopicSource dmaapReader = TopicEndpoint.manager.getDmaapTopicSource(topicName);
1000 boolean success = dmaapReader.offer(json);
1002 return Response.status(Status.OK).
1003 entity("Successfully injected event over " + topicName).
1006 return Response.status(Status.NOT_ACCEPTABLE).
1007 entity("Failure to inject event over " + topicName).
1009 } catch (Exception e) {
1010 return Response.status(Response.Status.BAD_REQUEST).
1011 entity(new Error(e.getMessage())).
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();
1023 return Response.status(Status.OK).
1024 entity("Endpoints are locked").
1027 return Response.status(Status.SERVICE_UNAVAILABLE).
1028 entity("Endpoints cannot be locked").
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();
1039 return Response.status(Status.OK).
1040 entity("Endpoints are unlocked").
1043 return Response.status(Status.SERVICE_UNAVAILABLE).
1044 entity("Endpoints cannot be unlocked").
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();
1056 return Response.status(Status.OK).
1057 entity("Endpoints are unlocked").
1060 return Response.status(Status.SERVICE_UNAVAILABLE).
1061 entity("Endpoints cannot be unlocked").
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();
1073 return Response.status(Status.OK).
1074 entity("Endpoints are unlocked").
1077 return Response.status(Status.SERVICE_UNAVAILABLE).
1078 entity("Endpoints cannot be unlocked").
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();
1089 return Response.status(Status.OK).
1090 entity("Controller " + controllerName + " is now locked").
1093 return Response.status(Status.SERVICE_UNAVAILABLE).
1094 entity("Controller " + controllerName + " cannot be locked").
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();
1105 return Response.status(Status.OK).
1106 entity("Controller " + controllerName + " is now unlocked").
1109 return Response.status(Status.SERVICE_UNAVAILABLE).
1110 entity("Controller " + controllerName + " cannot be unlocked").
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,
1120 String literalRegex = Pattern.quote(regex);
1122 literalRegex = "^(?!" + literalRegex + "$).*";
1124 return Response.status(Status.OK).
1125 entity(new JsonProtocolFilter.FilterRule(name,literalRegex)).
1130 @Path("engine/util/uuid")
1131 public Response uuid() {
1132 return Response.status(Status.OK).
1133 entity(UUID.randomUUID().toString()).
1138 * Helper classes for aggregation of results
1142 public static class Endpoints {
1143 public List<TopicSource> sources;
1144 public List<TopicSink> sinks;
1146 public Endpoints(List<TopicSource> sources,
1147 List<TopicSink> sinks) {
1148 this.sources = sources;
1153 public static class Endpoint {
1154 public TopicSource source;
1155 public TopicSink sink;
1157 public Endpoint(TopicSource source,
1159 this.source = source;
1164 public static class CodingResult {
1165 public String jsonEncoding;
1166 public Boolean encoding;
1167 public Boolean decoding;
1170 public static class Error {
1171 public String error;
1176 public Error(String error) {