/**
  * Validate post request Json object for FlowFilterEntriesResource.
  *
  * @param requestBody the request Json object
  * @return true, if successful
  */
 private boolean validatePost(final JsonObject requestBody) {
   LOG.trace("Start FlowFilterEntriesResourceValidator#validatePost()");
   boolean isValid = false;
   setInvalidParameter(VtnServiceJsonConsts.FLOWFILTERENTRY);
   if (requestBody.has(VtnServiceJsonConsts.FLOWFILTERENTRY)
       && requestBody.get(VtnServiceJsonConsts.FLOWFILTERENTRY).isJsonObject()) {
     final JsonObject ffEntry = requestBody.getAsJsonObject(VtnServiceJsonConsts.FLOWFILTERENTRY);
     // validation for mandatory key: seqnum
     setInvalidParameter(VtnServiceJsonConsts.SEQNUM);
     if (ffEntry.has(VtnServiceJsonConsts.SEQNUM)
         && ffEntry.getAsJsonPrimitive(VtnServiceJsonConsts.SEQNUM).getAsString() != null
         && !ffEntry
             .getAsJsonPrimitive(VtnServiceJsonConsts.SEQNUM)
             .getAsString()
             .trim()
             .isEmpty()) {
       isValid =
           validator.isValidRange(
               ffEntry.getAsJsonPrimitive(VtnServiceJsonConsts.SEQNUM).getAsString().trim(),
               VtnServiceJsonConsts.VAL_1,
               VtnServiceJsonConsts.VAL_65535);
     } else {
       isValid = false;
     }
     if (isValid) {
       isValid = validator.isValidFlowFilterEntry(requestBody);
     }
   }
   LOG.trace("Complete FlowFilterEntriesResourceValidator#validatePost()");
   return isValid;
 }
 /**
  * Validate uri parameters for HostAddress API.
  *
  * @return true, if successful
  */
 @Override
 public final boolean validateUri() {
   LOG.trace("Start HostAddressResourceValidator#validateUri()");
   boolean isValid = false;
   setInvalidParameter(VtnServiceJsonConsts.URI + VtnServiceJsonConsts.VTNNAME);
   if (resource instanceof HostAddressResource
       && ((HostAddressResource) resource).getVtnName() != null
       && !((HostAddressResource) resource).getVtnName().trim().isEmpty()) {
     isValid =
         validator.isValidMaxLengthAlphaNum(
             ((HostAddressResource) resource).getVtnName().trim(), VtnServiceJsonConsts.LEN_31);
     if (isValid) {
       setInvalidParameter(VtnServiceJsonConsts.URI + VtnServiceJsonConsts.VBRNAME);
       if (((HostAddressResource) resource).getVbrName() != null
           && !((HostAddressResource) resource).getVbrName().trim().isEmpty()) {
         isValid =
             validator.isValidMaxLengthAlphaNum(
                 ((HostAddressResource) resource).getVbrName().trim(),
                 VtnServiceJsonConsts.LEN_31);
       } else {
         isValid = false;
       }
     }
     setListOpFlag(false);
   }
   LOG.trace("Complete HostAddressResourceValidator#validateUri()");
   return isValid;
 }
 /**
  * Validate put request Json object for HostAddressResource
  *
  * @param requestBody the request Json object
  * @return true, if successful
  */
 private boolean validatePut(final JsonObject requestBody) {
   LOG.trace("Start HostAddressResourceValidator#validatePut()");
   boolean isValid = false;
   setInvalidParameter(VtnServiceJsonConsts.IPADDRESS);
   if (requestBody.has(VtnServiceJsonConsts.IPADDRESS)
       && requestBody.get(VtnServiceJsonConsts.IPADDRESS).isJsonObject()) {
     final JsonObject ipaddr = requestBody.getAsJsonObject(VtnServiceJsonConsts.IPADDRESS);
     // validation for mandatory keys: ipaddr
     setInvalidParameter(VtnServiceJsonConsts.IPADDR);
     if (ipaddr.has(VtnServiceJsonConsts.IPADDR)
         && ipaddr.getAsJsonPrimitive(VtnServiceJsonConsts.IPADDR).getAsString() != null) {
       isValid =
           validator.isValidIpV4(
               ipaddr.getAsJsonPrimitive(VtnServiceJsonConsts.IPADDR).getAsString().trim());
     }
     if (isValid) {
       // validation for mandatory keys: netmask
       setInvalidParameter(VtnServiceJsonConsts.PREFIX);
       if (ipaddr.has(VtnServiceJsonConsts.PREFIX)
           && ipaddr.getAsJsonPrimitive(VtnServiceJsonConsts.PREFIX).getAsString() != null) {
         isValid =
             validator.isValidRange(
                 ipaddr.getAsJsonPrimitive(VtnServiceJsonConsts.PREFIX).getAsString().trim(),
                 VtnServiceJsonConsts.VAL_1,
                 VtnServiceJsonConsts.VAL_30);
       } else {
         isValid = false;
       }
     }
   }
   LOG.trace("Complete HostAddressResourceValidator#validatePut()");
   return isValid;
 }
 /**
  * Validate uri parameters for Vtn Mapping API.
  *
  * @return true, if successful
  */
 @Override
 public final boolean validateUri() {
   LOG.trace("Start VtnMappingResourceValidator#validateUri()");
   boolean isValid = false;
   setInvalidParameter(VtnServiceJsonConsts.URI + VtnServiceJsonConsts.VTNNAME);
   if (resource instanceof VtnMappingsResource
       && ((VtnMappingsResource) resource).getVtnName() != null
       && !((VtnMappingsResource) resource).getVtnName().isEmpty()) {
     isValid =
         validator.isValidMaxLengthAlphaNum(
             ((VtnMappingsResource) resource).getVtnName(), VtnServiceJsonConsts.LEN_31);
     setListOpFlag(true);
   } else if (resource instanceof VtnMappingResource
       && ((VtnMappingResource) resource).getVtnName() != null
       && !((VtnMappingResource) resource).getVtnName().isEmpty()) {
     isValid =
         validator.isValidMaxLengthAlphaNum(
             ((VtnMappingResource) resource).getVtnName(), VtnServiceJsonConsts.LEN_31);
     if (isValid) {
       setInvalidParameter(VtnServiceJsonConsts.URI + VtnServiceJsonConsts.MAPPINGID);
       if (((VtnMappingResource) resource).getMappingId() != null
           && !((VtnMappingResource) resource).getMappingId().isEmpty()) {
         final String[] mappingId =
             ((VtnMappingResource) resource).getMappingId().split(VtnServiceJsonConsts.HYPHEN);
         isValid = validator.isValidMappingId(mappingId);
       } else {
         isValid = false;
       }
     }
     setListOpFlag(false);
   }
   LOG.trace("Completed VtnMappingResourceValidator#validateUri()");
   return isValid;
 }
  /**
   * Validate uri parameters for FlowFilterEntries API.
   *
   * @return true, if successful
   */
  @Override
  public final boolean validateUri() {
    LOG.trace("Start FlowFilterEntriesResourceValidator#validateUri()");
    boolean isValid = false;
    // For FlowFilterEntriesResource instance
    setInvalidParameter(VtnServiceJsonConsts.URI + VtnServiceJsonConsts.VTNNAME);
    if (resource instanceof FlowFilterEntriesResource
        && ((FlowFilterEntriesResource) resource).getVtnName() != null
        && !((FlowFilterEntriesResource) resource).getVtnName().trim().isEmpty()) {
      isValid =
          validator.isValidMaxLengthAlphaNum(
              ((FlowFilterEntriesResource) resource).getVtnName().trim(),
              VtnServiceJsonConsts.LEN_31);
      if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.URI + VtnServiceJsonConsts.FFTYPE);
        if (((FlowFilterEntriesResource) resource).getFfType() != null
            && !((FlowFilterEntriesResource) resource).getFfType().trim().isEmpty()) {
          isValid =
              VtnServiceJsonConsts.IN.equalsIgnoreCase(
                      ((FlowFilterEntriesResource) resource).getFfType().trim())
                  || VtnServiceJsonConsts.OUT.equalsIgnoreCase(
                      ((FlowFilterEntriesResource) resource).getFfType().trim());
        } else {
          isValid = false;
        }
      }
      setListOpFlag(true);
    }

    LOG.trace("Complete FlowFilterEntriesResourceValidator#validateUri()");
    return isValid;
  }
 /** Validate request Json object for get, put and post method of HostAddress API. */
 @Override
 public final void validate(final String method, final JsonObject requestBody)
     throws VtnServiceException {
   LOG.trace("Start HostAddressResourceValidator#validate()");
   boolean isValid = false;
   try {
     isValid = validateUri();
     LOG.info("Validating request for " + method + " of HostAddressResourceValidator");
     if (isValid && requestBody != null && VtnServiceConsts.GET.equals(method)) {
       isValid = validator.isValidGet(requestBody, isListOpFlag());
       setInvalidParameter(validator.getInvalidParameter());
       updateOpParameterForList(requestBody);
     } else if (isValid && requestBody != null && VtnServiceConsts.PUT.equals(method)) {
       isValid = validatePut(requestBody);
     } else if (isValid) {
       setInvalidParameter(VtnServiceConsts.INCORRECT_METHOD_INVOCATION);
       isValid = false;
     }
   } catch (final NumberFormatException e) {
     if (method.equals(VtnServiceConsts.GET)) {
       setInvalidParameter(validator.getInvalidParameter());
     }
     LOG.error("Inside catch:NumberFormatException");
     isValid = false;
   } catch (final ClassCastException e) {
     if (method.equals(VtnServiceConsts.GET)) {
       setInvalidParameter(validator.getInvalidParameter());
     }
     LOG.error("Inside catch:ClassCastException");
     isValid = false;
   }
   // Throws exception if validation fails
   if (!isValid) {
     LOG.error("Validation failed");
     throw new VtnServiceException(
         Thread.currentThread().getStackTrace()[1].getMethodName(),
         UncJavaAPIErrorCode.VALIDATION_ERROR.getErrorCode(),
         UncJavaAPIErrorCode.VALIDATION_ERROR.getErrorMessage());
   }
   LOG.info("Validation successful");
   LOG.trace("Complete HostAddressResourceValidator#validate()");
 }
  /**
   * @param requestBody , for request
   * @param opFlag , for getting option1 type
   * @return true , if parameter are in correct format values
   */
  public final boolean isValidGetForMappingIdIndex(
      final JsonObject requestBody, final boolean opFlag) {
    LOG.trace("Start CommonValidator#isValidGetForMappingIdIndex");
    boolean isValid = true;

    // validation for key: targetdb
    setInvalidParameter(VtnServiceJsonConsts.TARGETDB);
    isValid = validator.isValidRequestDBState(requestBody);

    /*
     * Remove unwanted parameters from request body for Show APIs
     */
    if (!opFlag) {
      if (requestBody.has(VtnServiceJsonConsts.OP)) {
        requestBody.remove(VtnServiceJsonConsts.OP);
      } else {
        LOG.debug("No need to remove");
      }
      if (requestBody.has(VtnServiceJsonConsts.INDEX)) {
        requestBody.remove(VtnServiceJsonConsts.INDEX);
      } else {
        LOG.debug("No need to remove");
      }
      if (requestBody.has(VtnServiceJsonConsts.MAX)) {
        requestBody.remove(VtnServiceJsonConsts.MAX);
      } else {
        LOG.debug("No need to remove");
      }
    } else {
      // validation for key: op
      if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.OP);
        isValid = validator.isValidOperationForDetail(requestBody);
      }
      // validation for key: index where index is combination of
      // controller id and domain id
      if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.INDEX);
        if (requestBody.has(VtnServiceJsonConsts.INDEX)
            && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.INDEX).getAsString() != null) {
          final String[] mappingId =
              requestBody
                  .getAsJsonPrimitive(VtnServiceJsonConsts.INDEX)
                  .getAsString()
                  .split(VtnServiceJsonConsts.HYPHEN);
          isValid = validator.isValidMappingId(mappingId);
        }
      }
      // validation for key: max_repitition
      if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.MAX);
        isValid = validator.isValidMaxRepetition(requestBody);
      }

      // validation for key: vnode_type
      if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.VNODETYPE);
        if (requestBody.has(VtnServiceJsonConsts.VNODETYPE)
            && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.VNODETYPE).getAsString()
                != null) {
          final String vnodeType =
              requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.VNODETYPE).getAsString();
          isValid = validator.isValidVnodeType(vnodeType);
        }
      }

      // validation for key: vnode_name
      if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.VNODENAME);
        if (requestBody.has(VtnServiceJsonConsts.VNODENAME)
            && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.VNODENAME).getAsString()
                != null) {
          final String vnodeName =
              requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.VNODENAME).getAsString();
          isValid = validator.isValidMaxLengthAlphaNum(vnodeName, VtnServiceJsonConsts.LEN_31);
        }
      }

      // validation for key: if_name
      if (isValid) {
        setInvalidParameter(VtnServiceJsonConsts.VIFNAME);
        if (requestBody.has(VtnServiceJsonConsts.VIFNAME)
            && requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.VIFNAME).getAsString() != null) {
          final String ifName =
              requestBody.getAsJsonPrimitive(VtnServiceJsonConsts.VIFNAME).getAsString();
          isValid = validator.isValidMaxLengthAlphaNum(ifName, VtnServiceJsonConsts.LEN_31);
        }
      }
    }
    LOG.trace("Complete CommonValidator#isValidGetForMappingIdIndex");
    return isValid;
  }