Program: 116459
Project: 116459
Buggy source:
/** 
 * Updates ClusterContext for given cluster
 * @param parentMonitor parent of the monitor
 * @param context
 * @return ClusterMonitor - Updated ClusterContext
 * @throws org.apache.stratos.autoscaler.exception.PolicyValidationException
 * @throws org.apache.stratos.autoscaler.exception.PartitionValidationException
 */
public static ClusterMonitor getClusterMonitor(ParentComponentMonitor parentMonitor,ClusterContext context,String appId) throws PolicyValidationException, PartitionValidationException, TopologyInConsistentException {
  String clusterId=context.getId();
  String serviceName=context.getServiceName();
  Cluster cluster;
  ClusterMonitor clusterMonitor;
  TopologyManager.acquireReadLockForCluster(serviceName,clusterId);
  try {
    Topology topology=TopologyManager.getTopology();
    if (topology.serviceExists(serviceName)) {
      Service service=topology.getService(serviceName);
      if (service.clusterExists(clusterId)) {
        cluster=service.getCluster(clusterId);
        if (log.isDebugEnabled()) {
          log.debug("String_Node_Str" + clusterId);
        }
      }
 else {
        String msg="String_Node_Str" + clusterId + "String_Node_Str"+ "String_Node_Str"+ serviceName;
        throw new TopologyInConsistentException(msg);
      }
    }
 else {
      String msg="String_Node_Str" + serviceName + "String_Node_Str";
      throw new TopologyInConsistentException(msg);
    }
    String autoscalePolicyName=cluster.getAutoscalePolicyName();
    String deploymentPolicyName=cluster.getDeploymentPolicyName();
    if (log.isDebugEnabled()) {
      log.debug("String_Node_Str" + deploymentPolicyName);
      log.debug("String_Node_Str" + autoscalePolicyName);
    }
    AutoscalePolicy policy=PolicyManager.getInstance().getAutoscalePolicy(autoscalePolicyName);
    DeploymentPolicy deploymentPolicy=PolicyManager.getInstance().getDeploymentPolicy(deploymentPolicyName);
    if (deploymentPolicy == null) {
      String msg="String_Node_Str" + deploymentPolicyName;
      log.error(msg);
      throw new PolicyValidationException(msg);
    }
    Partition[] allPartitions=deploymentPolicy.getAllPartitions();
    if (allPartitions == null) {
      String msg="String_Node_Str" + deploymentPolicyName;
      log.error(msg);
      throw new PolicyValidationException(msg);
    }
    CloudControllerClient.getInstance().validateDeploymentPolicy(cluster.getServiceName(),deploymentPolicy);
    clusterMonitor=new ClusterMonitor(cluster.getClusterId(),cluster.getServiceName(),deploymentPolicy,policy);
    clusterMonitor.setAppId(cluster.getAppId());
    for (    PartitionGroup partitionGroup : deploymentPolicy.getPartitionGroups()) {
      NetworkPartitionContext networkPartitionContext=new NetworkPartitionContext(partitionGroup.getId(),partitionGroup.getPartitionAlgo(),partitionGroup.getPartitions());
      for (      Partition partition : partitionGroup.getPartitions()) {
        PartitionContext partitionContext=new PartitionContext(partition);
        partitionContext.setServiceName(cluster.getServiceName());
        partitionContext.setProperties(cluster.getProperties());
        partitionContext.setNetworkPartitionId(partitionGroup.getId());
        for (        Member member : cluster.getMembers()) {
          String memberId=member.getMemberId();
          if (member.getPartitionId().equalsIgnoreCase(partition.getId())) {
            MemberContext memberContext=new MemberContext();
            memberContext.setClusterId(member.getClusterId());
            memberContext.setMemberId(memberId);
            memberContext.setPartition(partition);
            memberContext.setProperties(convertMemberPropsToMemberContextProps(member.getProperties()));
            if (MemberStatus.Activated.equals(member.getStatus())) {
              partitionContext.addActiveMember(memberContext);
            }
 else             if (MemberStatus.Created.equals(member.getStatus()) || MemberStatus.Starting.equals(member.getStatus())) {
              partitionContext.addPendingMember(memberContext);
            }
 else             if (MemberStatus.Suspended.equals(member.getStatus())) {
            }
            partitionContext.addMemberStatsContext(new MemberStatsContext(memberId));
            if (log.isInfoEnabled()) {
              log.info(String.format("String_Node_Str",memberId));
            }
          }
        }
        if (cluster.hasMembers()) {
          StatusChecker.getInstance().onMemberStatusChange(clusterId);
        }
        networkPartitionContext.addPartitionContext(partitionContext);
        if (log.isInfoEnabled()) {
          log.info(String.format("String_Node_Str",partitionContext.getPartitionId()));
        }
      }
      clusterMonitor.addNetworkPartitionCtxt(networkPartitionContext);
      clusterMonitor.setParent(parentMonitor);
      if (!parentMonitor.isHasDependent() && !context.hasChild()) {
        clusterMonitor.setHasDependent(true);
      }
      if (log.isInfoEnabled()) {
        log.info(String.format("String_Node_Str",networkPartitionContext.getId()));
      }
    }
    if (cluster.getStatus() != clusterMonitor.getStatus()) {
      clusterMonitor.setStatus(cluster.getStatus());
    }
  }
  finally {
    TopologyManager.releaseReadLockForCluster(serviceName,clusterId);
  }
  if (cluster.getProperties() != null) {
    clusterMonitor.setHasPrimary(Boolean.parseBoolean(cluster.getProperties().getProperty(Constants.IS_PRIMARY)));
  }
  log.info("String_Node_Str" + clusterMonitor.toString());
  return clusterMonitor;
}

Fixed version (ground truth):
/** 
 * Updates ClusterContext for given cluster
 * @param parentMonitor parent of the monitor
 * @param context
 * @return ClusterMonitor - Updated ClusterContext
 * @throws org.apache.stratos.autoscaler.exception.PolicyValidationException
 * @throws org.apache.stratos.autoscaler.exception.PartitionValidationException
 */
public static ClusterMonitor getClusterMonitor(ParentComponentMonitor parentMonitor,ClusterContext context,String appId) throws PolicyValidationException, PartitionValidationException, TopologyInConsistentException {
  String clusterId=context.getId();
  String serviceName=context.getServiceName();
  Cluster cluster;
  ClusterMonitor clusterMonitor;
  TopologyManager.acquireReadLockForCluster(serviceName,clusterId);
  try {
    Topology topology=TopologyManager.getTopology();
    if (topology.serviceExists(serviceName)) {
      Service service=topology.getService(serviceName);
      if (service.clusterExists(clusterId)) {
        cluster=service.getCluster(clusterId);
        if (log.isDebugEnabled()) {
          log.debug("String_Node_Str" + clusterId);
        }
      }
 else {
        String msg="String_Node_Str" + clusterId + "String_Node_Str"+ "String_Node_Str"+ serviceName;
        throw new TopologyInConsistentException(msg);
      }
    }
 else {
      String msg="String_Node_Str" + serviceName + "String_Node_Str";
      throw new TopologyInConsistentException(msg);
    }
    String autoscalePolicyName=cluster.getAutoscalePolicyName();
    String deploymentPolicyName=cluster.getDeploymentPolicyName();
    if (log.isDebugEnabled()) {
      log.debug("String_Node_Str" + deploymentPolicyName);
      log.debug("String_Node_Str" + autoscalePolicyName);
    }
    AutoscalePolicy policy=PolicyManager.getInstance().getAutoscalePolicy(autoscalePolicyName);
    DeploymentPolicy deploymentPolicy=PolicyManager.getInstance().getDeploymentPolicy(deploymentPolicyName);
    if (deploymentPolicy == null) {
      String msg="String_Node_Str" + deploymentPolicyName;
      log.error(msg);
      throw new PolicyValidationException(msg);
    }
    Partition[] allPartitions=deploymentPolicy.getAllPartitions();
    if (allPartitions == null) {
      String msg="String_Node_Str" + deploymentPolicyName;
      log.error(msg);
      throw new PolicyValidationException(msg);
    }
    CloudControllerClient.getInstance().validateDeploymentPolicy(cluster.getServiceName(),deploymentPolicy);
    clusterMonitor=new ClusterMonitor(cluster.getClusterId(),cluster.getServiceName(),deploymentPolicy,policy);
    clusterMonitor.setAppId(cluster.getAppId());
    for (    PartitionGroup partitionGroup : deploymentPolicy.getPartitionGroups()) {
      NetworkPartitionContext networkPartitionContext=new NetworkPartitionContext(partitionGroup.getId(),partitionGroup.getPartitionAlgo(),partitionGroup.getPartitions());
      for (      Partition partition : partitionGroup.getPartitions()) {
        PartitionContext partitionContext=new PartitionContext(partition);
        partitionContext.setServiceName(cluster.getServiceName());
        partitionContext.setProperties(cluster.getProperties());
        partitionContext.setNetworkPartitionId(partitionGroup.getId());
        for (        Member member : cluster.getMembers()) {
          String memberId=member.getMemberId();
          if (member.getPartitionId().equalsIgnoreCase(partition.getId())) {
            MemberContext memberContext=new MemberContext();
            memberContext.setClusterId(member.getClusterId());
            memberContext.setMemberId(memberId);
            memberContext.setPartition(partition);
            memberContext.setProperties(convertMemberPropsToMemberContextProps(member.getProperties()));
            if (MemberStatus.Activated.equals(member.getStatus())) {
              partitionContext.addActiveMember(memberContext);
            }
 else             if (MemberStatus.Created.equals(member.getStatus()) || MemberStatus.Starting.equals(member.getStatus())) {
              partitionContext.addPendingMember(memberContext);
            }
 else             if (MemberStatus.Suspended.equals(member.getStatus())) {
            }
            partitionContext.addMemberStatsContext(new MemberStatsContext(memberId));
            if (log.isInfoEnabled()) {
              log.info(String.format("String_Node_Str",memberId));
            }
          }
        }
        networkPartitionContext.addPartitionContext(partitionContext);
        if (log.isInfoEnabled()) {
          log.info(String.format("String_Node_Str",partitionContext.getPartitionId()));
        }
      }
      clusterMonitor.addNetworkPartitionCtxt(networkPartitionContext);
      clusterMonitor.setParent(parentMonitor);
      if (!parentMonitor.isHasDependent() && !context.hasChild()) {
        clusterMonitor.setHasDependent(true);
      }
      AutoscalerContext.getInstance().addMonitor(clusterMonitor);
      if (log.isInfoEnabled()) {
        log.info(String.format("String_Node_Str",networkPartitionContext.getId()));
      }
    }
    if (cluster.getStatus() != clusterMonitor.getStatus()) {
      clusterMonitor.setStatus(cluster.getStatus());
    }
    if (!cluster.hasMembers()) {
      StatusChecker.getInstance().onMemberStatusChange(clusterId);
    }
  }
  finally {
    TopologyManager.releaseReadLockForCluster(serviceName,clusterId);
  }
  if (cluster.getProperties() != null) {
    clusterMonitor.setHasPrimary(Boolean.parseBoolean(cluster.getProperties().getProperty(Constants.IS_PRIMARY)));
  }
  log.info("String_Node_Str" + clusterMonitor.toString());
  return clusterMonitor;
}