@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());
    }
  }
  @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());
    }
  }
  @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());
    }
  }