com.clustercontrol.performance.util.code.CollectorItemCodeTable.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.performance.util.code.CollectorItemCodeTable.java

Source

/*
    
 Copyright (C) 2006 NTT DATA Corporation
    
 This program is free software; you can redistribute it and/or
 Modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation, version 2.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 */

package com.clustercontrol.performance.util.code;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.commons.util.JpaTransactionManager;
import com.clustercontrol.fault.FacilityNotFound;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.performance.bean.CollectorItemInfo;
import com.clustercontrol.performance.factory.OperateCollectCategoryMaster;
import com.clustercontrol.performance.factory.OperateCollectItemCodeMaster;
import com.clustercontrol.performance.monitor.entity.CollectorCategoryMstData;
import com.clustercontrol.performance.monitor.entity.CollectorItemCodeMstData;
import com.clustercontrol.performance.monitor.model.CollectorCategoryCollectMstEntity;
import com.clustercontrol.performance.monitor.model.CollectorItemCalcMethodMstEntity;
import com.clustercontrol.performance.monitor.util.QueryUtil;
import com.clustercontrol.performance.util.CollectorMasterCache;
import com.clustercontrol.performance.util.PollingDataManager;
import com.clustercontrol.repository.factory.FacilitySelector;
import com.clustercontrol.repository.factory.NodeProperty;
import com.clustercontrol.repository.model.NodeDeviceInfo;
import com.clustercontrol.repository.model.NodeInfo;
import com.clustercontrol.repository.session.RepositoryControllerBean;

/**
 * ????
 * 
 * @version 4.0
 * @since 1.0
 */
public class CollectorItemCodeTable {
    private static Log m_log = LogFactory.getLog(CollectorItemCodeTable.class);
    private static final Map<String, CollectorItemTreeItem> m_codeTable;

    /**
     * static field
     */
    static {
        m_codeTable = new ConcurrentHashMap<String, CollectorItemTreeItem>(); // ??

        JpaTransactionManager jtm = new JpaTransactionManager();
        if (!jtm.isNestedEm()) {
            m_log.warn("refresh() : transactioin has not been begined.");
            jtm.close();
        } else {

            // ??????
            HashMap<String, CollectorItemTreeItem> categoryTable = new HashMap<String, CollectorItemTreeItem>();

            try {
                // ???
                Collection<CollectorCategoryMstData> cate = new OperateCollectCategoryMaster().findAll();

                for (CollectorCategoryMstData category : cate) {
                    CollectorItemTreeItem categoryItem = new CollectorItemTreeItem(null, null, category, null,
                            null); // ???????null

                    // ????
                    categoryTable.put(category.getCategoryCode(), categoryItem);
                }
            } catch (Exception e) {
                m_log.warn("static() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            }

            try {
                // ????
                Collection<CollectorItemCodeMstData> ctItemCodeMst = new OperateCollectItemCodeMaster().findAll();

                for (CollectorItemCodeMstData codeData : ctItemCodeMst) {
                    String itemCode = codeData.getItemCode();

                    if (itemCode == null) {
                        continue;
                    }

                    if (codeData.getCategoryCode() != null && codeData.isDeviceSupport() != null
                            && codeData.isGraphRange() != null) {

                        // ?????
                        CollectorItemTreeItem categoryTreeItem = categoryTable.get(codeData.getCategoryCode());

                        // ???????null
                        CollectorItemTreeItem parentItem = null;
                        if (codeData.getParentItemCode() != null) {
                            parentItem = m_codeTable.get(codeData.getParentItemCode());
                        }

                        // ????????????
                        if (parentItem == null) {
                            parentItem = categoryTreeItem;
                        }

                        CollectorItemTreeItem ctItem = new CollectorItemTreeItem(parentItem, codeData, null, null,
                                null);

                        // ????
                        m_codeTable.put(itemCode, ctItem);
                    }
                }

            } catch (Exception e) {
                m_codeTable.clear();
                // ?
                m_log.warn("CollectorItemCodeTable static field error . ", e);
            }
        }
    }

    /**
     * ?ID??????
     * 
     */
    private static class PlatformIdAndSubPlatformId {
        private String m_platformId;
        private String m_subPlatformId;

        public PlatformIdAndSubPlatformId(String platformId, String subPlatformId) {
            m_platformId = platformId;
            m_subPlatformId = subPlatformId;
        }

        public String getPlatformId() {
            return m_platformId;
        }

        public String getSubPlatformId() {
            return m_subPlatformId;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((m_platformId == null) ? 0 : m_platformId.hashCode());
            result = prime * result + ((m_subPlatformId == null) ? 0 : m_subPlatformId.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            PlatformIdAndSubPlatformId other = (PlatformIdAndSubPlatformId) obj;
            if (m_platformId == null) {
                if (other.m_platformId != null)
                    return false;
            } else if (!m_platformId.equals(other.m_platformId))
                return false;
            if (m_subPlatformId == null) {
                if (other.m_subPlatformId != null)
                    return false;
            } else if (!m_subPlatformId.equals(other.m_subPlatformId))
                return false;
            return true;
        }
    }

    /**
     * ??facilityId?????????ID????
     * 
     * @param facilityId
     * @return
     * @throws HinemosUnknown
     */
    private static Set<CollectorItemCodeMstData> getEnableCodeSet(String facilityId) throws HinemosUnknown {
        m_log.debug("getEnableCodeSet() facilityId = " + facilityId);

        // ID?????????????????
        Set<CollectorItemCodeMstData> enableItemCodeSetInEveryNode = null;

        // ID????NULL?????????
        if (facilityId == null || "".equals(facilityId)) {
            m_log.debug("getEnableCodeSet() codeSet is 0");
            return enableItemCodeSetInEveryNode;
        }

        try {
            // ???SessionBean
            RepositoryControllerBean bean = new RepositoryControllerBean();

            ////
            // ???
            ////
            List<String> nodeList = null;
            if (bean.isNode(facilityId)) { // ???
                m_log.debug("getEnableCodeSet() facilityId is node");

                // ??
                nodeList = new ArrayList<String>();
                nodeList.add(facilityId);
            } else { // ???
                m_log.debug("getEnableCodeSet() facilityId is scope");

                // ??????ID??
                nodeList = bean.getNodeFacilityIdList(facilityId, null, RepositoryControllerBean.ALL);
            }

            ////
            // ???????ID?
            ////
            Set<PlatformIdAndSubPlatformId> platformSet = new HashSet<PlatformIdAndSubPlatformId>();
            for (String nodeId : nodeList) {
                m_log.debug("getEnableCodeSet() target node  = " + nodeId);
                NodeInfo node = bean.getNode(nodeId);

                platformSet
                        .add(new PlatformIdAndSubPlatformId(node.getPlatformFamily(), node.getSubPlatformFamily()));
            }

            ////
            // ?????????ID??
            ////
            for (PlatformIdAndSubPlatformId platform : platformSet) {

                if (m_log.isDebugEnabled()) {
                    m_log.debug("getEnableCodeSet() " + "platformId = " + platform.getPlatformId()
                            + ", subPlatformId = " + platform.getSubPlatformId());
                }

                // ???????
                Collection<CollectorCategoryCollectMstEntity> collects;
                // ????????
                List<CollectorItemCalcMethodMstEntity> calsMethods;
                try {
                    collects = QueryUtil.getCollectorCategoryCollectMstByPlatformIdSubPlatformId(
                            platform.getPlatformId(), platform.getSubPlatformId());
                    calsMethods = QueryUtil.getCollectorItemCalcMethodMstByPlatformIdSubPlatformId(
                            platform.getPlatformId(), platform.getSubPlatformId());
                    if (m_log.isDebugEnabled()) {
                        m_log.debug("getEnableCodeSet() " + "platformId = " + platform.getPlatformId()
                                + ", subPlatformId = " + platform.getSubPlatformId()
                                + ", CollectorCategoryCollectMstEntity size = " + collects.size()
                                + ", CollectorItemCalcMethodMstEntity size = " + calsMethods.size());
                    }
                    // VM???????????????????????
                    // ???????? ?????????
                    if (!platform.getSubPlatformId().isEmpty()) {
                        Collection<CollectorCategoryCollectMstEntity> physicalCollects = QueryUtil
                                .getCollectorCategoryCollectMstByPlatformIdSubPlatformId(platform.getPlatformId(),
                                        "");
                        Collection<CollectorItemCalcMethodMstEntity> physicalCalsMethods = QueryUtil
                                .getCollectorItemCalcMethodMstByPlatformIdSubPlatformId(platform.getPlatformId(),
                                        "");
                        collects.addAll(physicalCollects);
                        calsMethods.addAll(physicalCalsMethods);
                        if (m_log.isDebugEnabled()) {
                            m_log.debug("getEnableCodeSet() " + "platformId = " + platform.getPlatformId()
                                    + ", physical platform, " + ", CollectorCategoryCollectMstEntity size = "
                                    + physicalCollects.size() + ", CollectorItemCalcMethodMstEntity size = "
                                    + physicalCalsMethods.size());
                        }
                    }
                } catch (Exception e) {
                    m_log.warn("getEnableCodeSet() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
                    return null;
                }

                // categoryCode?collectMethod??
                HashMap<String, String> categoryMap = new HashMap<String, String>();
                for (CollectorCategoryCollectMstEntity collect : collects) {
                    m_log.debug("getEnableCodeSet() add map categoryCode = " + collect.getId().getCategoryCode()
                            + ", collectMethod = " + collect.getCollectMethod());
                    categoryMap.put(collect.getId().getCategoryCode(), collect.getCollectMethod());
                }

                // ????????????Set
                HashSet<CollectorItemCodeMstData> enableItemCodeSetByPlatform = new HashSet<CollectorItemCodeMstData>();

                // ????????????????????????
                for (CollectorItemCalcMethodMstEntity calcBean : calsMethods) {

                    CollectorItemCodeMstData codeBean;
                    try {
                        m_log.debug("getEnableCodeSet() search itemCode = " + calcBean.getId().getItemCode());
                        codeBean = CollectorMasterCache.getCategoryCodeMst(calcBean.getId().getItemCode());
                        if (codeBean == null) {
                            // ???????
                            m_log.warn("getEnableCodeSet() codeBean is null. id = " + calcBean.getId());
                            return null;
                        }
                    } catch (Exception e) {
                        m_log.warn("getEnableCodeSet() : " + e.getClass().getSimpleName() + ", " + e.getMessage(),
                                e);
                        return null;
                    }

                    // ?????????????
                    // ??????????Set??
                    if (categoryMap.get(codeBean.getCategoryCode()) != null && categoryMap
                            .get(codeBean.getCategoryCode()).equals(calcBean.getId().getCollectMethod())) {
                        // ??DTO?
                        CollectorItemCodeMstData codeData = new CollectorItemCodeMstData(codeBean.getItemCode(),
                                codeBean.getCategoryCode(), codeBean.getParentItemCode(), codeBean.getItemName(),
                                codeBean.getMeasure(), codeBean.isDeviceSupport(), codeBean.getDeviceType(),
                                codeBean.isGraphRange());
                        m_log.debug("getEnableCodeSet() add itemCode = " + calcBean.getId().getItemCode());
                        enableItemCodeSetByPlatform.add(codeData);
                    }

                }
                // ???????????????AND?
                // ????????????????????
                // ???????????????????
                // ???????AND?set.retainAll
                // ??????????????????????????
                if (enableItemCodeSetInEveryNode == null) {
                    m_log.debug("getEnableCodeSet() enableCodeSetInEveryNode is null");
                    enableItemCodeSetInEveryNode = enableItemCodeSetByPlatform;
                }
                enableItemCodeSetInEveryNode.retainAll(enableItemCodeSetByPlatform);
                m_log.debug("getEnableCodeSet() enableCodeSetInEveryNode size = "
                        + enableItemCodeSetInEveryNode.size());
            }

        } catch (FacilityNotFound e) {
            m_log.debug("getEnableCodeSet " + facilityId);
        } catch (HinemosUnknown e) {
            throw e;
        } catch (Exception e) {
            m_log.warn("getEnableCodeSet() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
            throw new HinemosUnknown(e.getMessage(), e);
        }

        // 1??????0?Set?
        if (enableItemCodeSetInEveryNode == null) {
            enableItemCodeSetInEveryNode = new HashSet<CollectorItemCodeMstData>();
        }
        return enableItemCodeSetInEveryNode;
    }

    /**
     * ??facilityId????????
     * 
     * @param facilityId
     * @return
     * @throws HinemosUnknown
     */
    public static List<CollectorItemInfo> getAvailableCollectorItemList(String facilityId) throws HinemosUnknown {
        m_log.debug("getAvailableCollectorItemList() facilityId = " + facilityId);

        // null check
        if (facilityId == null || "".equals(facilityId)) {
            return new ArrayList<CollectorItemInfo>();
        }

        // ???????
        Set<NodeDeviceInfo> deviceSet = getDeviceSetContainsAllNodes(facilityId);
        m_log.debug("getAvailableCollectorItemList() facilityId = " + facilityId + ", deviceSet size = "
                + deviceSet.size());

        // ?????
        Set<CollectorItemCodeMstData> itemCodeSet = getEnableCodeSet(facilityId);
        m_log.debug("getAvailableCollectorItemList() facilityId = " + facilityId + ", itemCodeSet size = "
                + itemCodeSet.size());

        // 
        List<CollectorItemInfo> list = new ArrayList<CollectorItemInfo>();

        for (CollectorItemCodeMstData itemCode : itemCodeSet) {
            m_log.debug("getAvailableCollectorItemList() facilityId = " + facilityId + ", itemCode = "
                    + itemCode.getItemCode() + ", deviceSupport = " + itemCode.isDeviceSupport().booleanValue());

            CollectorItemInfo itemInfo = null;

            if (itemCode.isDeviceSupport().booleanValue()) {
                //???
                for (NodeDeviceInfo deviceInfo : deviceSet) {
                    if (itemCode.getDeviceType() != null
                            && itemCode.getDeviceType().equals(deviceInfo.getDeviceType())) {
                        itemInfo = new CollectorItemInfo(null, itemCode.getItemCode(),
                                deviceInfo.getDeviceDisplayName());//collectorId is null

                        m_log.debug("getAvailableCollectorItemList() facilityId = " + facilityId + ", itemCode = "
                                + itemCode.getItemCode() + ", deviceDisplayName = "
                                + deviceInfo.getDeviceDisplayName());
                        list.add(itemInfo);
                    }
                }

                // ALL Device?
                m_log.debug("getAvailableCollectorItemList() facilityId = " + facilityId + ", itemCode = "
                        + itemCode.getItemCode() + ", deviceDisplayName = " + PollingDataManager.ALL_DEVICE_NAME);
                itemInfo = new CollectorItemInfo(null, itemCode.getItemCode(), PollingDataManager.ALL_DEVICE_NAME);//collectorId is null
                list.add(itemInfo);

            } else {
                //????
                itemInfo = new CollectorItemInfo(null, itemCode.getItemCode(), "");//collectorId is null

                m_log.debug("getAvailableCollectorItemList() facilityId = " + facilityId + ", itemCode = "
                        + itemCode.getItemCode());
                list.add(itemInfo);
            }
        }

        // Sort
        Collections.sort(list, new Comparator<CollectorItemInfo>() {
            @Override
            public int compare(CollectorItemInfo o1, CollectorItemInfo o2) {
                // TODO Auto-generated method stub
                return o1.getItemCode().compareTo(o2.getItemCode());
            }
        });

        m_log.debug("getAvailableCollectorItemList() facilityId = " + facilityId + ", list size = " + list.size());
        return list;
    }

    /**
     * ??facilityId????????????
     * 
     * @param facilityId
     * @return
     */
    private static Set<NodeDeviceInfo> getDeviceSetContainsAllNodes(String facilityId) {
        m_log.debug("getDeviceSetContainsAllNodes() facilityId = " + facilityId);

        // 
        Set<NodeDeviceInfo> deviceSet = new HashSet<NodeDeviceInfo>();

        // ID????NULL?
        if (facilityId == null || "".equals(facilityId)) {
            return deviceSet;
        }

        // ?ID
        // targetFacilityIdList?????
        List<String> targetFacilityIdList = FacilitySelector.getNodeFacilityIdList(facilityId, null,
                RepositoryControllerBean.ALL, false, true);
        List<NodeDeviceInfo> checkTargetList = null;
        for (String targetFacilityId : targetFacilityIdList) {

            // ()targetFacilityId???
            List<NodeDeviceInfo> checkList = new ArrayList<NodeDeviceInfo>();
            try {
                NodeInfo node = NodeProperty.getProperty(targetFacilityId);
                checkList.addAll(node.getNodeDeviceInfo());
                checkList.addAll(node.getNodeCpuInfo());
                checkList.addAll(node.getNodeMemoryInfo());
                checkList.addAll(node.getNodeNetworkInterfaceInfo());
                checkList.addAll(node.getNodeDiskInfo());
                checkList.addAll(node.getNodeFilesystemInfo());
            } catch (FacilityNotFound e) {
            }

            // 1?
            if (checkTargetList == null) {
                checkTargetList = checkList;
            }
            // 2???????
            else {
                List<NodeDeviceInfo> tmpList = new ArrayList<NodeDeviceInfo>();

                for (NodeDeviceInfo checkInfo : checkList) {
                    for (NodeDeviceInfo targetInfo : checkTargetList) {

                        // ???????????
                        if (checkInfo.getDeviceType().equals(targetInfo.getDeviceType())
                                && checkInfo.getDeviceDisplayName().equals(targetInfo.getDeviceDisplayName())) {
                            tmpList.add(checkInfo);
                            m_log.debug("getDeviceSetContainsAllNodes() facilityId = " + facilityId + ", "
                                    + "add device deviceType = " + checkInfo.getDeviceType()
                                    + ", deviceDisplayName = " + checkInfo.getDeviceDisplayName());
                            break;
                        }
                    }
                }
                checkTargetList = tmpList;
            }
        }

        if (checkTargetList != null) {
            deviceSet.addAll(checkTargetList);
        }

        return deviceSet;
    }

    /**
     * ????????????
     * ? ???[???]
     * 
     * @param itemCode ?
     * @param displayName ???
     * @return ?
     */
    public static String getFullItemName(String itemCode, String displayName) {
        m_log.debug("getFullItemName() itemCode = " + itemCode + ", displayName = " + displayName);

        try {
            CollectorItemCodeMstData bean = CollectorMasterCache.getCategoryCodeMst(itemCode);
            String itemName = bean.getItemName();
            if (bean.isDeviceSupport().booleanValue()) {
                itemName = itemName + "[" + displayName + "]";
            }

            m_log.debug("getFullItemName() itemCode = " + itemCode + ", displayName = " + displayName
                    + " : itemName = " + itemName);
            return itemName;
        } catch (Exception e) {
            m_log.warn("getFullItemName() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e);
        }

        m_log.debug("getFullItemName() itemCode = " + itemCode + ", displayName = " + displayName + " : itemName = "
                + itemCode + " is not found.");
        return itemCode + " is not found.";
    }

    /**
     * ?????CollectorItemTreeItem?HashMap???
     * @return
     */
    public static Map<String, CollectorItemTreeItem> getItemCodeMap() {
        return m_codeTable;
    }

}