# Path: service/components.py
# class Component():
#     __metaclass__ = abc.ABCMeta
#     """
#     A component class that receives JSON requests and returns stringyfied jsons.
# 
#     To implement a new component, extend this class.
#     """
# 
#     @abc.abstractmethod
#     def process(self, json):
#         pass
# 
#     def receive_post(self):
#         if request.method == "POST":
#             req = request.get_json()
#             output = self.process(req)
#             return str(output)
# 
#     def receive_get(self):
#         if request.method == "GET":
#             output = self.process("")
#             return str(output)
# 
# Path: service/server.py
# class Server():
#     def __init__(self, name, port):
#     def add_component_post(self, route, component):
#         def binder():
#             def binded():
#     def add_component_get(self, route, component):
#         def binder():
#             def binded():
#     def run(self):
# 
# Path: service/client.py
# class LocalClient(Client):
#     def __init__(self, port):
#         self._url = "http://127.0.0.1"
#         self._port = port
# 
# Path: inference/mulval_translator.py
# def generate_attack_graph(self, env):
#     logger.info("Generating attack graph.")
#     files_before = os.listdir()
# 
#     self._make_topology()
#     self._add_vulnerabilities()
#     self.mulval_file.close()
# 
#     def missing_env(name):
#         # checks that all environment variables needed to run MulVAL are defined
#         if name not in env:
#             logger.error("{} not present in environment.".format(name))
#             return True
#         return False
# 
#     if missing_env("MULVALROOT"): return None
#     if missing_env("XSB_DIR"): return None
# 
#     env["PATH"] = "{}:{}".format(env["PATH"], os.path.join(env["MULVALROOT"], "bin"))
#     env["PATH"] = "{}:{}".format(env["PATH"], os.path.join(env["MULVALROOT"], "utils"))
#     env["PATH"] = "{}:{}".format(env["PATH"], os.path.join(env["XSB_DIR"], "bin"))
# 
#     # Handle some types of installation for different distributions
#     if "JAVA_HOME" in env:
#         env["PATH"] = "{}:{}".format(env["PATH"], os.path.join(env["JAVA_HOME"], "bin"))
# 
#     subprocess.Popen(['graph_gen.sh mulval_input.P -p'], shell=True, env=env).wait()
#     # self._save_output()
#     attackGraphJSON = self._graphXMLtoJSON()
# 
#     self._cleanup(files_before)
# 
#     return attackGraphJSON


import sys
import os
from service.components import Component
from service.server import Server, config
from service.client import LocalClient
from inference.mulval_translator import generate_attack_graph
from __future__ import absolute_import

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))


class AttackGraphExporter(Component):
    def __init__(self, client, env):
        self.client = client
        self.env    = env

    def process(self, _):
        """
           Must have all services started, otherwise it outputs
           the contents of AttackGraph.txt

           Returns the JSON formatted string of the mulval attack graph.
        """
        return generate_attack_graph(self.client, self.env)

class InferenceClient(LocalClient):
    def get_attack_graph(self):
        return self.get("/attack_graph")

def inference_service(env=os.environ.copy()):
    """
    The inference service provides an overlay over the Mulval service.
    """

    client = LocalClient(config["graph"])

# Path: service/components.py
# class Component():
#     __metaclass__ = abc.ABCMeta
#     """
#     A component class that receives JSON requests and returns stringyfied jsons.
# 
#     To implement a new component, extend this class.
#     """
# 
#     @abc.abstractmethod
#     def process(self, json):
#         pass
# 
#     def receive_post(self):
#         if request.method == "POST":
#             req = request.get_json()
#             output = self.process(req)
#             return str(output)
# 
#     def receive_get(self):
#         if request.method == "GET":
#             output = self.process("")
#             return str(output)
# 
# Path: service/server.py
# class Server():
#     def __init__(self, name, port):
#     def add_component_post(self, route, component):
#         def binder():
#             def binded():
#     def add_component_get(self, route, component):
#         def binder():
#             def binded():
#     def run(self):
# 
# Path: service/client.py
# class LocalClient(Client):
#     def __init__(self, port):
#         self._url = "http://127.0.0.1"
#         self._port = port
# 
# Path: inference/mulval_translator.py
# def generate_attack_graph(self, env):
#     logger.info("Generating attack graph.")
#     files_before = os.listdir()
# 
#     self._make_topology()
#     self._add_vulnerabilities()
#     self.mulval_file.close()
# 
#     def missing_env(name):
#         # checks that all environment variables needed to run MulVAL are defined
#         if name not in env:
#             logger.error("{} not present in environment.".format(name))
#             return True
#         return False
# 
#     if missing_env("MULVALROOT"): return None
#     if missing_env("XSB_DIR"): return None
# 
#     env["PATH"] = "{}:{}".format(env["PATH"], os.path.join(env["MULVALROOT"], "bin"))
#     env["PATH"] = "{}:{}".format(env["PATH"], os.path.join(env["MULVALROOT"], "utils"))
#     env["PATH"] = "{}:{}".format(env["PATH"], os.path.join(env["XSB_DIR"], "bin"))
# 
#     # Handle some types of installation for different distributions
#     if "JAVA_HOME" in env:
#         env["PATH"] = "{}:{}".format(env["PATH"], os.path.join(env["JAVA_HOME"], "bin"))
# 
#     subprocess.Popen(['graph_gen.sh mulval_input.P -p'], shell=True, env=env).wait()
#     # self._save_output()
#     attackGraphJSON = self._graphXMLtoJSON()
# 
#     self._cleanup(files_before)
# 
#     return attackGraphJSON


# Path: inference/inference_service.py
import sys
import os
from service.components import Component
from service.server import Server, config
from service.client import LocalClient
from inference.mulval_translator import generate_attack_graph

from __future__ import absolute_import

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))


class AttackGraphExporter(Component):
    def __init__(self, client, env):
        self.client = client
        self.env    = env

    def process(self, _):
        """
           Must have all services started, otherwise it outputs
           the contents of AttackGraph.txt

           Returns the JSON formatted string of the mulval attack graph.
        """
        return generate_attack_graph(self.client, self.env)

class InferenceClient(LocalClient):
    def get_attack_graph(self):
        return self.get("/attack_graph")

def inference_service(env=os.environ.copy()):
    """
    The inference service provides an overlay over the Mulval service.
    """

    client = LocalClient(config["graph"])

    server = Server("inference", config["inference"])