/**
   * Invalidate any policy with action. It will send the cluster message to clean this policy in all
   * the nodes.
   *
   * @param policyId
   * @param action
   */
  public void invalidateCache(String policyId, String action) {

    int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();

    if (log.isDebugEnabled()) {
      log.debug(
          "Trigger invalidateCache to tenant :  "
              + tenantId
              + " and policy "
              + policyId
              + " for  action "
              + ": "
              + action);
    }

    IdentityCacheKey cacheKey = new IdentityCacheKey(tenantId, policyId);
    PolicyStatus policyStatus = (PolicyStatus) getValueFromCache(cacheKey);

    if (policyStatus == null) {
      policyStatus = new PolicyStatus(policyId, 0, action);
    } else {
      policyStatus.setStatusCount(policyStatus.getStatusCount() + 1);
      policyStatus.setPolicyAction(action);
    }
    updateToCache(cacheKey, policyStatus);

    synchronized (localPolicyCacheMap) {
      if (localPolicyCacheMap.get(cacheKey.getTenantId()) != null) {
        if (localPolicyCacheMap.get(cacheKey.getTenantId()).get(cacheKey.getKey()) != null) {
          PolicyStatus status =
              localPolicyCacheMap.get(cacheKey.getTenantId()).get(cacheKey.getKey());
          status.setPolicyAction(getPriorityAction(status.getPolicyAction(), action));
        }
      } else {
        Map<String, PolicyStatus> map = new HashMap<String, PolicyStatus>();

        map.put(policyId, policyStatus);
        localPolicyCacheMap.put(cacheKey.getTenantId(), map);
      }
    }
  }
  /**
   * This method triggered by cache entry listener.
   *
   * @param identityCacheKey
   * @param policyStatus
   */
  public static void updateLocalPolicyCacheMap(
      IdentityCacheKey identityCacheKey, PolicyStatus policyStatus) {
    if (identityCacheKey.getKey() != null) {
      if (!identityCacheKey.getKey().equals("")) {
        if (log.isDebugEnabled()) {
          log.debug(
              "Updating local cache map for the tenant : "
                  + identityCacheKey.getTenantId()
                  + " and Policy : "
                  + identityCacheKey.getKey());
        }
        synchronized (localPolicyCacheMap) {
          if (localPolicyCacheMap.get(identityCacheKey.getTenantId()) != null) {
            if (localPolicyCacheMap
                    .get(identityCacheKey.getTenantId())
                    .get(identityCacheKey.getKey())
                != null) {
              PolicyStatus status =
                  localPolicyCacheMap
                      .get(identityCacheKey.getTenantId())
                      .get(identityCacheKey.getKey());
              status.setPolicyAction(
                  getPriorityAction(status.getPolicyAction(), policyStatus.getPolicyAction()));
              if (log.isDebugEnabled()) {
                log.debug(
                    "Updated existing policy in local cache map :  Policy : "
                        + identityCacheKey.getKey()
                        + " and new action : "
                        + getPriorityAction(
                            status.getPolicyAction(), policyStatus.getPolicyAction()));
              }
            } else {
              localPolicyCacheMap
                  .get(identityCacheKey.getTenantId())
                  .put(policyStatus.getPolicyId(), policyStatus);
              if (log.isDebugEnabled()) {
                log.debug(
                    "Adding policy in to the local cache policy map : policy :  "
                        + identityCacheKey.getKey());
              }
            }
          } else {
            Map<String, PolicyStatus> map = new HashMap<String, PolicyStatus>();

            map.put(policyStatus.getPolicyId(), policyStatus);
            localPolicyCacheMap.put(identityCacheKey.getTenantId(), map);
            if (log.isDebugEnabled()) {
              log.debug(
                  "Adding policy in to the local cache policy map : policy :  "
                      + identityCacheKey.getKey()
                      + " add new entry for this tenant : "
                      + identityCacheKey.getTenantId());
            }
          }
        }
      } else {
        cacheInvalidationState.put(identityCacheKey.getTenantId(), 1);
        if (log.isDebugEnabled()) {
          log.debug(
              "Trigger event to clear all cache in tenant :  " + identityCacheKey.getTenantId());
        }
      }
    }
  }