view src/main/java/christie/topology/manager/TopologyManager.java @ 44:6664efac18ee

fix CheckCommingHost ConfigWaiter IncommingHosts
author akahori
date Thu, 02 Aug 2018 11:44:47 +0900
parents cf5a75bc3e55
children 12c9bf81d429
line wrap: on
line source

package christie.topology.manager;

import christie.annotation.Peek;
import christie.codegear.CodeGear;
import christie.codegear.CodeGearManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;

import org.apache.log4j.Logger;

import com.alexmerz.graphviz.ParseException;
import com.alexmerz.graphviz.Parser;
import com.alexmerz.graphviz.objects.*;

public class TopologyManager extends CodeGear {

    @Peek
    TopologyManagerConfig config;

    Logger logger = Logger.getLogger(TopologyManager.class);


    @Override
    protected void run(CodeGearManager cgm) {
        cgm.setup(new CheckComingHost());

        // if (!conf.dynamic) は, conf.dynamic = trueの動作がわからないので,  省いた.

        LinkedList<String> nodeNames = new LinkedList<String>();
        HashMap<String, LinkedList<NodeInfo>> topology = new HashMap<String, LinkedList<NodeInfo>>();
        int nodeNum = 0;

        try {
            FileReader reader = new FileReader(new File(config.confFilePath));
            Parser parser = new Parser();
            parser.parse(reader);


            ArrayList<Graph> digraphs = parser.getGraphs();


            for (Graph digraph : digraphs) {
                ArrayList<Node> nodes = digraph.getNodes(false);
                nodeNum = nodes.size();

                for (Node node : nodes) {
                    String nodeName = node.getId().getId();
                    nodeNames.add(nodeName);
                    topology.put(nodeName, new LinkedList<NodeInfo>());
                }

                ArrayList<Edge> edges = digraph.getEdges();
                HashMap<String, NodeInfo> hash = new HashMap<String, NodeInfo>();

                String connection;
                String source;
                String target;

                NodeInfo nodeInfo;

                // まず1回グラフを読み込む
                for (Edge edge : edges) {
                    connection = edge.getAttribute("label");
                    source = edge.getSource().getNode().getId().getId();
                    target = edge.getTarget().getNode().getId().getId();
                    nodeInfo = new NodeInfo(source, connection);

                    //Question: この下の2行はどんな役目?
                    //LinkedList<NodeInfo> sources = topology.get(target);
                    //sources.add(nodeInfo); // addしてその後の使い道は...?

                    hash.put(source + "," + target, nodeInfo);
                }

                // hash.get(target + "," + source); をして, グラフを逆にたどって, reverseNameにlabelを入れてる.
                for (Edge edge : edges) {
                    connection = edge.getAttribute("label");
                    source = edge.getSource().getNode().getId().getId();
                    target = edge.getTarget().getNode().getId().getId();
                    nodeInfo = hash.get(target + "," + source);

                    if (nodeInfo != null) {
                        nodeInfo.reverseName = connection;
                    }

                }
            }

        } catch (FileNotFoundException e) {
            logger.error("File not found: " + config.confFilePath);
            e.printStackTrace();
        } catch (ParseException e) {
            logger.error("File format error: " + config.confFilePath);
            e.printStackTrace();
        }

        // for recode topology information
        // cookie List
        getLocalDGM().put("running", false);
        getLocalDGM().put("resultParse", topology);
        getLocalDGM().put("nodeNames", nodeNames);

        cgm.setup(new IncomingHosts());



        // Question: この処理何をやっているのかわからない. 一応, その下にそれっぽいコードを書いた.
        // ConfigWaiter cs3 = new ConfigWaiter(nodeNum);
        // cs3.done.setKey("local", "done");

        cgm.setup(new ConfigWaiter());
        getLocalDGM().put("nodeNum", nodeNum);
        getLocalDGM().put("done", false);

    }


}