@POST
 @Path("/groups")
 @Consumes(APPLICATION_JSON)
 @Produces(APPLICATION_JSON)
 @ApiOperation(
     value = "Create a new group trigger",
     response = Trigger.class,
     notes = "Returns created GroupTrigger")
 @ApiResponses(
     value = {
       @ApiResponse(code = 200, message = "Success, Group Trigger Created"),
       @ApiResponse(code = 500, message = "Internal server error"),
       @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters")
     })
 public Response createGroupTrigger(
     @ApiParam(value = "Trigger definition to be created", name = "groupTrigger", required = true)
         final Trigger groupTrigger) {
   try {
     if (null != groupTrigger) {
       if (isEmpty(groupTrigger.getId())) {
         groupTrigger.setId(Trigger.generateId());
       } else if (definitions.getTrigger(tenantId, groupTrigger.getId()) != null) {
         return ResponseUtil.badRequest("Trigger with ID [" + groupTrigger.getId() + "] exists.");
       }
       definitions.addGroupTrigger(tenantId, groupTrigger);
       log.debug("Group Trigger: " + groupTrigger.toString());
       return ResponseUtil.ok(groupTrigger);
     } else {
       return ResponseUtil.badRequest("Trigger is null");
     }
   } catch (Exception e) {
     log.debug(e.getMessage(), e);
     return ResponseUtil.internalError(e.getMessage());
   }
 }
  @PUT
  @Path("/{triggerId}/conditions/{triggerMode}")
  @Consumes(APPLICATION_JSON)
  @Produces(APPLICATION_JSON)
  @ApiOperation(
      value =
          "Set the conditions for the trigger. This replaces any existing conditions. Returns "
              + "the new conditions.")
  @ApiResponses(
      value = {
        @ApiResponse(code = 200, message = "Success, Condition Set created"),
        @ApiResponse(code = 404, message = "No trigger found"),
        @ApiResponse(code = 500, message = "Internal server error"),
        @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters")
      })
  public Response setConditions(
      @ApiParam(value = "The relevant Trigger.", required = true) @PathParam("triggerId")
          final String triggerId,
      @ApiParam(value = "FIRING or AUTORESOLVE (not case sensitive).", required = true)
          @PathParam("triggerMode")
          final String triggerMode,
      @ApiParam(
              value =
                  "Json representation of a condition list. For examples of Condition types, See "
                      + "https://github.com/hawkular/hawkular-alerts/blob/master/hawkular-alerts-rest-tests/"
                      + "src/test/groovy/org/hawkular/alerts/rest/ConditionsITest.groovy") //
          String jsonConditions) {
    try {
      Mode mode = Mode.valueOf(triggerMode.toUpperCase());
      Collection<Condition> conditions = new ArrayList<>();
      if (!isEmpty(jsonConditions)) {

        ObjectMapper om = new ObjectMapper();
        JsonNode rootNode = om.readTree(jsonConditions);
        for (JsonNode conditionNode : rootNode) {
          Condition condition = JacksonDeserializer.deserializeCondition(conditionNode);
          if (condition == null) {
            return ResponseUtil.badRequest("Bad json conditions: " + jsonConditions);
          }
          condition.setTriggerId(triggerId);
          condition.setTriggerMode(mode);
          conditions.add(condition);
        }
      }

      conditions = definitions.setConditions(tenantId, triggerId, mode, conditions);
      if (log.isDebugEnabled()) {
        log.debug("Conditions: " + conditions);
      }
      return ResponseUtil.ok(conditions);

    } catch (IllegalArgumentException e) {
      return ResponseUtil.badRequest("Bad argument: " + e.getMessage());
    } catch (NotFoundException e) {
      return ResponseUtil.notFound(e.getMessage());
    } catch (Exception e) {
      log.debug(e.getMessage(), e);
      return ResponseUtil.internalError(e.getMessage());
    }
  }
  @POST
  @Path("/{triggerId}/conditions")
  @Consumes(APPLICATION_JSON)
  @Produces(APPLICATION_JSON)
  @ApiOperation(
      value =
          "Deprecated : Use PUT /alerts/triggers/{triggerId}/conditions to set the entire "
              + "condition set in one service.")
  @ApiResponses(
      value = {
        @ApiResponse(code = 200, message = "Success, Condition created"),
        @ApiResponse(code = 404, message = "No trigger found"),
        @ApiResponse(code = 500, message = "Internal server error"),
        @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters")
      })
  @Deprecated
  public Response createCondition(
      @ApiParam(value = "Trigger definition id to be retrieved", required = true)
          @PathParam("triggerId")
          final String triggerId,
      @ApiParam(
              value =
                  "Json representation of a condition. For examples of Condition types, See "
                      + "https://github.com/hawkular/hawkular-alerts/blob/master/hawkular-alerts-rest-tests/"
                      + "src/test/groovy/org/hawkular/alerts/rest/ConditionsITest.groovy") //
          String jsonCondition) {
    try {
      if (isEmpty(jsonCondition) || !jsonCondition.contains("type")) {
        return ResponseUtil.badRequest("json condition empty or without type");
      }

      ObjectMapper om = new ObjectMapper();
      JsonNode rootNode = om.readTree(jsonCondition);
      Condition condition = JacksonDeserializer.deserializeCondition(rootNode);

      if (condition == null) {
        return ResponseUtil.badRequest("Bad json condition");
      }
      condition.setTriggerId(triggerId);

      Collection<Condition> conditions;
      conditions =
          definitions.addCondition(
              tenantId, condition.getTriggerId(), condition.getTriggerMode(), condition);
      if (log.isDebugEnabled()) {
        log.debug("Conditions: " + conditions);
      }
      return ResponseUtil.ok(conditions);

    } catch (NotFoundException e) {
      return ResponseUtil.notFound(e.getMessage());
    } catch (Exception e) {
      log.debug(e.getMessage(), e);
      return ResponseUtil.internalError(e.getMessage());
    }
  }
 @POST
 @Path("/")
 @Consumes(APPLICATION_JSON)
 @Produces(APPLICATION_JSON)
 @ApiOperation(
     value = "Create a new Event.",
     notes = "Returns created Event.",
     response = Event.class)
 @ApiResponses(
     value = {
       @ApiResponse(code = 200, message = "Success, Event Created."),
       @ApiResponse(code = 500, message = "Internal server error.", response = ApiError.class),
       @ApiResponse(
           code = 400,
           message = "Bad Request/Invalid Parameters.",
           response = ApiError.class)
     })
 public Response createEvent(
     @ApiParam(
             value = "Event to be created. Category and Text fields required,",
             name = "event",
             required = true)
         final Event event) {
   try {
     if (null != event) {
       if (isEmpty(event.getId())) {
         return ResponseUtil.badRequest("Event with id null.");
       }
       if (isEmpty(event.getCategory())) {
         return ResponseUtil.badRequest("Event with category null.");
       }
       event.setTenantId(tenantId);
       if (null != alertsService.getEvent(tenantId, event.getId(), true)) {
         return ResponseUtil.badRequest("Event with ID [" + event.getId() + "] exists.");
       }
       /*
          New events are sent directly to the engine for inference process.
          Input events and new ones generated by the alerts engine are persisted at the end of the process.
       */
       alertsService.addEvents(Collections.singletonList(event));
       if (log.isDebugEnabled()) {
         log.debug("Event: " + event.toString());
       }
       return ResponseUtil.ok(event);
     } else {
       return ResponseUtil.badRequest("Event is null");
     }
   } catch (Exception e) {
     log.debug(e.getMessage(), e);
     if (e.getCause() != null && e.getCause() instanceof IllegalArgumentException) {
       return ResponseUtil.badRequest("Bad arguments: " + e.getMessage());
     }
     return ResponseUtil.internalError(e);
   }
 }
  @PUT
  @Path("/{triggerId}/conditions/{conditionId}")
  @Produces(APPLICATION_JSON)
  @ApiOperation(
      value =
          "Deprecated : Use PUT /alerts/triggers/{triggerId}/conditions to set the entire "
              + "condition set in one service.")
  @ApiResponses(
      value = {
        @ApiResponse(code = 200, message = "Success, Condition updated"),
        @ApiResponse(code = 404, message = "No Condition found"),
        @ApiResponse(code = 500, message = "Internal server error"),
        @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters")
      })
  @Deprecated
  public Response updateCondition(
      @ApiParam(value = "Trigger definition id to be retrieved", required = true)
          @PathParam("triggerId")
          final String triggerId,
      @PathParam("conditionId") final String conditionId,
      @ApiParam(value = "Json representation of a condition") String jsonCondition) {
    try {
      Trigger trigger = definitions.getTrigger(tenantId, triggerId);
      if (trigger == null) {
        return ResponseUtil.notFound("No trigger found for triggerId: " + triggerId);
      }
      if (isEmpty(jsonCondition) || !jsonCondition.contains("type")) {
        return ResponseUtil.badRequest("json condition empty or without type");
      }

      ObjectMapper om = new ObjectMapper();
      JsonNode rootNode = om.readTree(jsonCondition);
      Condition condition = JacksonDeserializer.deserializeCondition(rootNode);
      if (condition == null) {
        return ResponseUtil.badRequest("Bad json condition");
      }
      condition.setTriggerId(triggerId);
      boolean exists = false;
      if (conditionId.equals(condition.getConditionId())) {
        exists = (definitions.getCondition(tenantId, condition.getConditionId()) != null);
      }
      if (!exists) {
        return ResponseUtil.notFound("Condition not found for conditionId: " + conditionId);
      } else {
        Collection<Condition> conditions = definitions.updateCondition(tenantId, condition);
        if (log.isDebugEnabled()) {
          log.debug("Conditions: " + conditions);
        }
        return ResponseUtil.ok(conditions);
      }
    } catch (Exception e) {
      log.debug(e.getMessage(), e);
      return ResponseUtil.internalError(e.getMessage());
    }
  }
  @POST
  @Path("/groups/members")
  @Consumes(APPLICATION_JSON)
  @Produces(APPLICATION_JSON)
  @ApiOperation(
      value = "Create a new member trigger for a parent trigger.",
      response = Trigger.class,
      notes = "Returns Member Trigger created if operation finished correctly")
  @ApiResponses(
      value = {
        @ApiResponse(code = 200, message = "Success, Member Trigger Created"),
        @ApiResponse(code = 500, message = "Internal server error"),
        @ApiResponse(code = 404, message = "Group trigger not found."),
        @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters")
      })
  public Response createGroupMember(
      @ApiParam(
              value = "Group member trigger to be created",
              name = "groupMember",
              required = true) //
          final GroupMemberInfo groupMember) {
    try {
      if (null == groupMember) {
        return ResponseUtil.badRequest("MemberTrigger is null");
      }
      String groupId = groupMember.getGroupId();
      String memberName = groupMember.getMemberName();
      if (isEmpty(groupId)) {
        return ResponseUtil.badRequest("MemberTrigger groupId is null");
      }
      if (isEmpty(memberName)) {
        return ResponseUtil.badRequest("MemberTrigger memberName is null");
      }
      Trigger child =
          definitions.addMemberTrigger(
              tenantId,
              groupId,
              groupMember.getMemberId(),
              memberName,
              groupMember.getMemberContext(),
              groupMember.getDataIdMap());
      if (log.isDebugEnabled()) {
        log.debug("Child Trigger: " + child.toString());
      }
      return ResponseUtil.ok(child);

    } catch (NotFoundException e) {
      log.debug(e.getMessage(), e);
      return ResponseUtil.notFound(e.getMessage());
    } catch (Exception e) {
      log.debug(e.getMessage(), e);
      return ResponseUtil.internalError(e.getMessage());
    }
  }
 @PUT
 @Path("/tags")
 @Consumes(APPLICATION_JSON)
 @ApiOperation(value = "Add tags to existing Events.")
 @ApiResponses(
     value = {
       @ApiResponse(code = 200, message = "Success, Events tagged successfully."),
       @ApiResponse(code = 500, message = "Internal server error.", response = ApiError.class),
       @ApiResponse(
           code = 400,
           message = "Bad Request/Invalid Parameters.",
           response = ApiError.class)
     })
 public Response addTags(
     @ApiParam(required = true, value = "Comma separated list of eventIds to tag.")
         @QueryParam("eventIds")
         final String eventIds,
     @ApiParam(
             required = true,
             value = "Comma separated list of tags to add, " + "each tag of format 'name|value'.")
         @QueryParam("tags")
         final String tags) {
   try {
     if (!isEmpty(eventIds) || isEmpty(tags)) {
       // criteria just used for convenient type translation
       EventsCriteria c = buildCriteria(null, null, eventIds, null, null, tags, false);
       alertsService.addEventTags(tenantId, c.getEventIds(), c.getTags());
       if (log.isDebugEnabled()) {
         log.debugf("Tagged alertIds:%s, %s", c.getEventIds(), c.getTags());
       }
       return ResponseUtil.ok();
     } else {
       return ResponseUtil.badRequest("EventIds and Tags required for adding tags");
     }
   } catch (Exception e) {
     log.debug(e.getMessage(), e);
     if (e.getCause() != null && e.getCause() instanceof IllegalArgumentException) {
       return ResponseUtil.badRequest("Bad arguments: " + e.getMessage());
     }
     return ResponseUtil.internalError(e);
   }
 }
 @DELETE
 @Path("/tags")
 @Consumes(APPLICATION_JSON)
 @ApiOperation(value = "Remove tags from existing Events.")
 @ApiResponses(
     value = {
       @ApiResponse(code = 200, message = "Success, Events untagged successfully."),
       @ApiResponse(code = 500, message = "Internal server error.", response = ApiError.class),
       @ApiResponse(
           code = 400,
           message = "Bad Request/Invalid Parameters.",
           response = ApiError.class)
     })
 public Response deleteTags(
     @ApiParam(required = true, value = "Comma separated list of eventIds to untag.")
         @QueryParam("eventIds")
         final String eventIds,
     @ApiParam(required = true, value = "Comma separated list of tag names to remove.")
         @QueryParam("tagNames")
         final String tagNames) {
   try {
     if (!isEmpty(eventIds) || isEmpty(tagNames)) {
       Collection<String> ids = Arrays.asList(eventIds.split(","));
       Collection<String> tags = Arrays.asList(tagNames.split(","));
       alertsService.removeEventTags(tenantId, ids, tags);
       if (log.isDebugEnabled()) {
         log.debugf("Untagged eventIds:%s, %s", ids, tags);
       }
       return ResponseUtil.ok();
     } else {
       return ResponseUtil.badRequest("EventIds and Tags required for removing tags");
     }
   } catch (Exception e) {
     log.debug(e.getMessage(), e);
     if (e.getCause() != null && e.getCause() instanceof IllegalArgumentException) {
       return ResponseUtil.badRequest("Bad arguments: " + e.getMessage());
     }
     return ResponseUtil.internalError(e);
   }
 }
 @DELETE
 @Path("/{triggerId}/conditions/{conditionId}")
 @Produces(APPLICATION_JSON)
 @ApiOperation(
     value =
         "Deprecated : Use PUT /alerts/triggers/{triggerId}/conditions to set the entire "
             + "condition set in one service.")
 @ApiResponses(
     value = {
       @ApiResponse(code = 200, message = "Success, Condition deleted"),
       @ApiResponse(code = 404, message = "No Condition found"),
       @ApiResponse(code = 500, message = "Internal server error"),
       @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters")
     })
 @Deprecated
 public Response deleteCondition(
     @ApiParam(value = "Trigger definition id to be retrieved", required = true)
         @PathParam("triggerId")
         final String triggerId,
     @PathParam("conditionId") final String conditionId) {
   try {
     Trigger trigger = definitions.getTrigger(tenantId, triggerId);
     if (trigger == null) {
       return ResponseUtil.notFound("No trigger found for triggerId: " + triggerId);
     }
     Condition condition = definitions.getCondition(tenantId, conditionId);
     if (condition == null) {
       return ResponseUtil.notFound("No condition found for conditionId: " + conditionId);
     }
     if (!condition.getTriggerId().equals(triggerId)) {
       return ResponseUtil.badRequest(
           "ConditionId: " + conditionId + " does not belong to triggerId: " + triggerId);
     }
     Collection<Condition> conditions = definitions.removeCondition(tenantId, conditionId);
     if (log.isDebugEnabled()) {
       log.debug("Conditions: " + conditions);
     }
     return ResponseUtil.ok(conditions);
   } catch (Exception e) {
     log.debug(e.getMessage(), e);
     return ResponseUtil.internalError(e.getMessage());
   }
 }
  @POST
  @Path("/groups/members/{memberId}/unorphan")
  @Consumes(APPLICATION_JSON)
  @ApiOperation(value = "Make a non-orphan member trigger into an orphan.")
  @ApiResponses(
      value = {
        @ApiResponse(code = 200, message = "Success, Trigger updated"),
        @ApiResponse(code = 500, message = "Internal server error"),
        @ApiResponse(code = 404, message = "Trigger doesn't exist/Invalid Parameters")
      })
  public Response unorphanMemberTrigger(
      @ApiParam(value = "Member Trigger id to be made an orphan.", required = true) //
          @PathParam("memberId") //
          final String memberId,
      @ApiParam(
              required = true,
              name = "memberTrigger",
              value = "Only context and dataIdMap are used when changing back to a non-orphan.") //
          final UnorphanMemberInfo unorphanMemberInfo) {
    try {
      if (null == unorphanMemberInfo) {
        return ResponseUtil.badRequest("MemberTrigger is null");
      }
      Trigger child =
          definitions.unorphanMemberTrigger(
              tenantId,
              memberId,
              unorphanMemberInfo.getMemberContext(),
              unorphanMemberInfo.getDataIdMap());
      if (log.isDebugEnabled()) {
        log.debug("Member Trigger: " + child);
      }
      return ResponseUtil.ok();

    } catch (NotFoundException e) {
      return ResponseUtil.notFound("Member Trigger " + memberId + " doesn't exist for update");

    } catch (Exception e) {
      log.debug(e.getMessage(), e);
      return ResponseUtil.internalError(e.getMessage());
    }
  }
 @POST
 @Path("/groups/{groupId}/dampenings")
 @Consumes(APPLICATION_JSON)
 @Produces(APPLICATION_JSON)
 @ApiOperation(
     value = "Create a new group dampening",
     notes = "Returns Dampening created if operation finishes correctly")
 @ApiResponses(
     value = {
       @ApiResponse(code = 200, message = "Success, Dampening created"),
       @ApiResponse(code = 500, message = "Internal server error"),
       @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters")
     })
 public Response createGroupDampening(
     @ApiParam(value = "Group Trigger definition id attached to dampening", required = true) //
         @PathParam("groupId") //
         final String groupId,
     @ApiParam(value = "Dampening definition to be created", required = true) //
         final Dampening dampening) {
   try {
     dampening.setTriggerId(groupId);
     boolean exists = (definitions.getDampening(tenantId, dampening.getDampeningId()) != null);
     if (!exists) {
       // make sure we have the best chance of clean data..
       Dampening d = getCleanDampening(dampening);
       definitions.addGroupDampening(tenantId, d);
       if (log.isDebugEnabled()) {
         log.debug("Dampening: " + d);
       }
       return ResponseUtil.ok(d);
     } else {
       return ResponseUtil.badRequest(
           "Existing dampening for dampeningId: " + dampening.getDampeningId());
     }
   } catch (Exception e) {
     log.debug(e.getMessage(), e);
     return ResponseUtil.internalError(e.getMessage());
   }
 }
 @PUT
 @Path("/delete")
 @Produces(APPLICATION_JSON)
 @ApiOperation(
     value = "Delete events with optional filtering.",
     notes = "Return number of events deleted.",
     response = Integer.class)
 @ApiResponses(
     value = {
       @ApiResponse(code = 200, message = "Success."),
       @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters."),
       @ApiResponse(code = 500, message = "Internal server error.", response = ApiError.class)
     })
 public Response deleteEvents(
     @ApiParam(
             required = false,
             value = "Filter out events created before this time, millisecond since epoch.")
         @QueryParam("startTime")
         final Long startTime,
     @ApiParam(
             required = false,
             value = "Filter out events created after this time, millisecond since epoch.")
         @QueryParam("endTime")
         final Long endTime,
     @ApiParam(
             required = false,
             value =
                 "Filter out events for unspecified eventIds, "
                     + "comma separated list of event IDs.")
         @QueryParam("eventIds")
         final String eventIds,
     @ApiParam(
             required = false,
             value =
                 "Filter out events for unspecified triggers, "
                     + "comma separated list of trigger IDs.")
         @QueryParam("triggerIds")
         final String triggerIds,
     @ApiParam(
             required = false,
             value =
                 "Filter out events for unspecified categories, "
                     + "comma separated list of category values.")
         @QueryParam("categories")
         final String categories,
     @ApiParam(
             required = false,
             value =
                 "Filter out events for unspecified tags, comma separated list of tags, "
                     + "each tag of format 'name|value'. Specify '*' for value to match all values.")
         @QueryParam("tags")
         final String tags) {
   try {
     EventsCriteria criteria =
         buildCriteria(startTime, endTime, eventIds, triggerIds, categories, tags, null);
     int numDeleted = alertsService.deleteEvents(tenantId, criteria);
     if (log.isDebugEnabled()) {
       log.debug("Events deleted: " + numDeleted);
     }
     return ResponseUtil.ok(numDeleted);
   } catch (Exception e) {
     log.debug(e.getMessage(), e);
     if (e.getCause() != null && e.getCause() instanceof IllegalArgumentException) {
       return ResponseUtil.badRequest("Bad arguments: " + e.getMessage());
     }
     return ResponseUtil.internalError(e);
   }
 }
 @GET
 @Path("/")
 @Produces(APPLICATION_JSON)
 @ApiOperation(
     value = "Get events with optional filtering.",
     response = Event.class,
     responseContainer = "List")
 @ApiResponses(
     value = {
       @ApiResponse(code = 200, message = "Successfully fetched list of events."),
       @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters."),
       @ApiResponse(code = 500, message = "Internal server error.", response = ApiError.class)
     })
 public Response findEvents(
     @ApiParam(
             required = false,
             value = "Filter out events created before this time, millisecond since epoch.")
         @QueryParam("startTime")
         final Long startTime,
     @ApiParam(
             required = false,
             value = "Filter out events created after this time, millisecond since epoch.")
         @QueryParam("endTime")
         final Long endTime,
     @ApiParam(
             required = false,
             value =
                 "Filter out events for unspecified eventIds, "
                     + "comma separated list of event IDs.")
         @QueryParam("eventIds")
         final String eventIds,
     @ApiParam(
             required = false,
             value =
                 "Filter out events for unspecified triggers, "
                     + "comma separated list of trigger IDs.")
         @QueryParam("triggerIds")
         final String triggerIds,
     @ApiParam(
             required = false,
             value =
                 "Filter out events for unspecified categories, "
                     + "comma separated list of category values.")
         @QueryParam("categories")
         final String categories,
     @ApiParam(
             required = false,
             value =
                 "Filter out events for unspecified tags, comma separated list of tags, "
                     + "each tag of format 'name|value'. Specify '*' for value to match all values.")
         @QueryParam("tags")
         final String tags,
     @ApiParam(required = false, value = "Return only thin events, do not include: evalSets.")
         @QueryParam("thin")
         final Boolean thin,
     @Context final UriInfo uri) {
   Pager pager = RequestUtil.extractPaging(uri);
   try {
     EventsCriteria criteria =
         buildCriteria(startTime, endTime, eventIds, triggerIds, categories, tags, thin);
     Page<Event> eventPage = alertsService.getEvents(tenantId, criteria, pager);
     if (log.isDebugEnabled()) {
       log.debug("Events: " + eventPage);
     }
     if (isEmpty(eventPage)) {
       return ResponseUtil.ok(eventPage);
     }
     return ResponseUtil.paginatedOk(eventPage, uri);
   } catch (Exception e) {
     log.debug(e.getMessage(), e);
     if (e.getCause() != null && e.getCause() instanceof IllegalArgumentException) {
       return ResponseUtil.badRequest("Bad arguments: " + e.getMessage());
     }
     return ResponseUtil.internalError(e);
   }
 }
  @PUT
  @Path("/groups/{groupId}/conditions/{triggerMode}")
  @Consumes(APPLICATION_JSON)
  @Produces(APPLICATION_JSON)
  @ApiOperation(
      value =
          "Set the conditions for the group trigger. This replaces any existing conditions on "
              + "the group and member conditions.  Returns the new group conditions.")
  @ApiResponses(
      value = {
        @ApiResponse(code = 200, message = "Success, Group Condition Set created"),
        @ApiResponse(code = 404, message = "No trigger found"),
        @ApiResponse(code = 500, message = "Internal server error"),
        @ApiResponse(code = 400, message = "Bad Request/Invalid Parameters")
      })
  public Response setGroupConditions(
      @ApiParam(value = "The relevant Group Trigger.", required = true) @PathParam("groupId")
          final String groupId,
      @ApiParam(value = "FIRING or AUTORESOLVE (not case sensitive).", required = true)
          @PathParam("triggerMode")
          final String triggerMode,
      @ApiParam(
              value =
                  "Json representation of GroupConditionsInfo. For examples of Condition types, See "
                      + "https://github.com/hawkular/hawkular-alerts/blob/master/hawkular-alerts-rest-tests/"
                      + "src/test/groovy/org/hawkular/alerts/rest/ConditionsITest.groovy") //
          String jsonGroupConditionsInfo) {
    try {
      if (isEmpty(jsonGroupConditionsInfo)) {
        return ResponseUtil.badRequest("GroupConditionsInfo can not be null");
      }

      Mode mode = Mode.valueOf(triggerMode.toUpperCase());
      Collection<Condition> conditions = new ArrayList<>();

      ObjectMapper om = new ObjectMapper();
      JsonNode rootNode = om.readTree(jsonGroupConditionsInfo);
      JsonNode conditionsNode = rootNode.get("conditions");
      for (JsonNode conditionNode : conditionsNode) {
        Condition condition = JacksonDeserializer.deserializeCondition(conditionNode);
        if (condition == null) {
          return ResponseUtil.badRequest("Bad json conditions: " + conditionsNode.toString());
        }
        condition.setTriggerId(groupId);
        condition.setTriggerMode(mode);
        conditions.add(condition);
      }

      JsonNode dataIdMemberMapNode = rootNode.get("dataIdMemberMap");
      Map<String, Map<String, String>> dataIdMemberMap = null;
      if (null != dataIdMemberMapNode) {
        dataIdMemberMap = om.treeToValue(dataIdMemberMapNode, Map.class);
      }

      conditions =
          definitions.setGroupConditions(tenantId, groupId, mode, conditions, dataIdMemberMap);

      if (log.isDebugEnabled()) {
        log.debug("Conditions: " + conditions);
      }
      return ResponseUtil.ok(conditions);

    } catch (IllegalArgumentException e) {
      return ResponseUtil.badRequest("Bad trigger mode: " + triggerMode);
    } catch (NotFoundException e) {
      return ResponseUtil.notFound(e.getMessage());
    } catch (Exception e) {
      log.debug(e.getMessage(), e);
      return ResponseUtil.internalError(e.getMessage());
    }
  }