view src/main/java/christie/topology/manager/TopologyManager.java @ 64:f884c1bd0d36

fix IncomingHosts and RecordTopology. and refactor.
author akahori
date Thu, 30 Aug 2018 10:55:37 +0900
parents cfd79a71f9cd
children f1f34c3e33d4
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 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 {

    @Peek
    TopologyManagerConfig topologyManagerConfig;

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

    public TopologyManager() {
    }

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

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

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

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

        try {
            FileReader reader = new FileReader(new File(topologyManagerConfig.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);
                    resultParse.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, target);

                    resultParse.get(source).add(nodeInfo);

                    //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: " + topologyManagerConfig.confFilePath);
            e.printStackTrace();
        } catch (ParseException e) {
            logger.error("File format error: " + topologyManagerConfig.confFilePath);
            e.printStackTrace();
        }

        // for recode topology information
        // cookie List
        getLocalDGM().put("running", false);
        getLocalDGM().put("resultParse", resultParse);
        getLocalDGM().put("nodeNames", nodeNames);
        getLocalDGM().put("hostInfoMap", new HashMap<String, HostMessage>());
        getLocalDGM().put("topology", new HashMap<String, HashMap<String, HostMessage>>());
        getLocalDGM().put("nodeNum", nodeNum);
        getLocalDGM().put("createdList", new LinkedList<String>());

        cgm.setup(new IncomingHosts());
        cgm.setup(new ConfigWaiter());
        cgm.setup(new CreateHash());
        cgm.setup(new TopologyFinish());

    }


}