com.clustercontrol.nodemap.util.SearchConnectionExecutor.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.nodemap.util.SearchConnectionExecutor.java

Source

/*
    
Copyright (C) 2013 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.nodemap.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

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

import com.clustercontrol.commons.util.MonitoredThreadPoolExecutor;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.maintenance.util.HinemosPropertyUtil;
import com.clustercontrol.nodemap.bean.Association;
import com.clustercontrol.poller.impl.Snmp4jPollerImpl;
import com.clustercontrol.poller.util.DataTable;
import com.clustercontrol.repository.model.NodeInfo;
import com.clustercontrol.repository.model.NodeNetworkInterfaceInfo;
import com.clustercontrol.repository.session.RepositoryControllerBean;
import com.clustercontrol.util.HinemosTime;

public class SearchConnectionExecutor {
    // 
    private static Log m_log = LogFactory.getLog(SearchConnectionExecutor.class);

    private long now;
    private long start;
    //
    private final ExecutorService _executor;

    //
    private final boolean isL3;
    private final Set<String> oidSet;
    private final List<String> facilityIdList;
    private final RepositoryControllerBean bean = new RepositoryControllerBean();
    private ConcurrentHashMap<String, List<String>> macFacilityMap;

    /**
     * ???ExecutorService<BR>
     *
     * @param scopeId ?ID
     * @param isL3 L3????true L2????false?
     * @throws HinemosUnknown
     */
    public SearchConnectionExecutor(String scopeId, boolean isL3) throws HinemosUnknown {
        start = HinemosTime.currentTimeMillis();
        this.isL3 = isL3;

        // ??SNMP?
        int threadSize = HinemosPropertyUtil
                .getHinemosPropertyNum("nodemap.search.connection.thread", Long.valueOf(4)).intValue();
        m_log.info("static() : Thread Size = " + threadSize);
        m_log.info("SearchConnectionExecutor() : scopeId=" + scopeId + ",L3=" + isL3);

        // ??????OID?
        String oid = isL3 ? SearchConnectionProperties.DEFAULT_OID_ARP : SearchConnectionProperties.DEFAULT_OID_FDB;
        oidSet = new HashSet<String>();
        oidSet.add(oid);

        facilityIdList = bean.getFacilityIdList(scopeId, RepositoryControllerBean.ONE_LEVEL);

        _executor = new MonitoredThreadPoolExecutor(threadSize, threadSize, 0L, TimeUnit.MICROSECONDS,
                new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
                    private volatile int _count = 0;

                    @Override
                    public Thread newThread(Runnable r) {
                        return new Thread(r, "SearchConnectionExecutor-" + _count++);
                    }
                }, new ThreadPoolExecutor.AbortPolicy());

        now = HinemosTime.currentTimeMillis();
        m_log.debug("Constructer : " + (now - start) + "ms");
    }

    public List<Association> execute() throws Exception {
        List<Association> result = new ArrayList<Association>();
        List<Future<List<Association>>> futures = new ArrayList<Future<List<Association>>>();
        // ????MAC???
        // ??????ID????????ID????
        macFacilityMap = new ConcurrentHashMap<String, List<String>>();
        for (String id : facilityIdList) {
            if (!bean.isNode(id)) {
                continue;
            }

            NodeInfo node = bean.getNode(id);
            for (NodeNetworkInterfaceInfo info : node.getNodeNetworkInterfaceInfo()) {
                // NIC?MAC??????
                String nicMacAddress = info.getNicMacAddress().toUpperCase();
                if (!macFacilityMap.containsKey(nicMacAddress)) {
                    // ????????MAC??
                    macFacilityMap.put(nicMacAddress, new ArrayList<String>());
                }
                macFacilityMap.get(nicMacAddress).add(id);
            }
        }
        now = HinemosTime.currentTimeMillis();
        m_log.debug("get mac map : " + (now - start) + "ms");

        for (String facilityId : facilityIdList) {
            if (!bean.isNode(facilityId)) {
                // ??????
                continue;
            }

            NodeInfo node = bean.getNode(facilityId);

            if (!node.getPlatformFamily().equals("NW_EQUIPMENT")) {
                // ????????
                continue;
            }

            if (isL3 && !node.getHardwareType().equals("L3")) {
                // L3????????L3????????
                continue;
            }

            futures.add(_executor.submit(new SearchConnection(node)));
        }

        // ?????????????
        _executor.shutdown();

        // ??????
        for (Future<List<Association>> future : futures) {
            result.addAll(future.get());
        }
        now = HinemosTime.currentTimeMillis();
        m_log.debug("end : " + (now - start) + "ms");
        return result;
    }

    public class SearchConnection implements Callable<List<Association>> {
        private NodeInfo node;

        public SearchConnection(NodeInfo node) {
            this.node = node;
        }

        @Override
        public List<Association> call() throws Exception {
            List<Association> result = new ArrayList<Association>();
            DataTable tmpTable = null;

            m_log.info("SearchConnection() : polling to " + node.getFacilityId());

            tmpTable = Snmp4jPollerImpl.getInstance().polling(node.getAvailableIpAddress(), node.getSnmpPort(),
                    node.getSnmpVersion(), node.getSnmpCommunity(), node.getSnmpRetryCount(), node.getSnmpTimeout(),
                    oidSet, node.getSnmpSecurityLevel(), node.getSnmpUser(), node.getSnmpAuthPassword(),
                    node.getSnmpPrivPassword(), node.getSnmpAuthProtocol(), node.getSnmpPrivProtocol());

            now = HinemosTime.currentTimeMillis();
            m_log.debug("polling done (" + node.getFacilityId() + ") : " + (now - start) + "ms");

            for (String key : tmpTable.keySet()) {
                // NW????MAC???????
                String macAddress = tmpTable.getValue(key).getValue().toString().toUpperCase();
                if (!macFacilityMap.containsKey(macAddress)) {
                    continue;
                }
                for (String target : macFacilityMap.get(macAddress)) {
                    Association asso = new Association(node.getFacilityId(), target);
                    m_log.debug("call() : " + asso.getSource() + " -> " + asso.getTarget());
                    result.add(asso);
                }
            }

            now = HinemosTime.currentTimeMillis();
            m_log.debug("create association (" + node.getFacilityId() + ") : " + (now - start) + "ms");

            return result;
        }
    }
}