view src/main/java/christie/topology/manager/TopologyManager.java @ 45:12c9bf81d429

fix TopologyManager.java
author akahori
date Thu, 02 Aug 2018 11:45:46 +0900
parents cf5a75bc3e55
children 8dfd93810041
line wrap: on
line source

package christie.topology.manager;

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 christie.topology.HostMessage;
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 {

    private TopologyManagerConfig conf;
    private Logger logger = Logger.getLogger(TopologyManager.class);

    public TopologyManager(TopologyManagerConfig conf) {
        this.conf = conf;
    }

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

        getLocalDGM().put("absCookieTable", new HashMap<String, String>());
        getLocalDGM().put("config", conf );

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


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

        try {
            FileReader reader = new FileReader(new File(conf.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<>());
                }

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

                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: " + conf.confFilePath);
            e.printStackTrace();
        } catch (ParseException e) {
            logger.error("File format error: " + conf.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("topology", new HashMap<String, LinkedList<HostMessage>>());
        getLocalDGM().put("createdList", new LinkedList<String>());
        cgm.setup(new CreateHash());

        cgm.setup(new TopologyFinish());

    }


}