Source

imalse / experiments / api.py

Full commit
"""This file contains the experiment factory."""

from core.ns3.experiment.imalse_netns_experiment import ImalseNetnsExperiment
from core.ns3.experiment.imalse_pure_sim_experiment import ImalsePureSimExperiment
from util.util import load_experiment

class Basic(object):
    
    """Basic mode, this is maybe deprecated."""
    
    def initparser(self, parser): 
        """No extra parser details."""
        pass
    
    def install_cmds(self, srv_addr): 
        """CMD's are not installed in this mode."""
        pass

MODE_MAP = {
        'netns3' : ImalseNetnsExperiment,
        'sim' : ImalsePureSimExperiment,
        'basic' : Basic,
        }

CLASS_MAP = {
        "complex_net_exp" : "ComplexNetExperiment",
        "manual_topo_bt_exp" : "ManualTopoBTExperiment",
        "manual_topo_exp" : "ManualTopoExperiment",
        "static_route_exp" : "StaticRouteExperiment",
        "topo_exp" : "TopoExperiment"
        }

def experiment_factory(experiment, mode):
    """
    
    Dynamically create an experiment class based on the chosen experiment 
    and simulation mode.  
    
    """    
    
    module = load_experiment(experiment)
    
    try:
        class_name = CLASS_MAP[experiment]
    except KeyError:
        print "Experiment does not exist"
        return None
    
    ExperimentClass = module.__dict__[class_name]    
    ModeClass = MODE_MAP[mode]
    
    class Experiment(ExperimentClass, ModeClass):
        
        def __init__(self):
            ModeClass.__init__(self)            
            ExperimentClass.__init__(self)            
        
        def setup(self):
            ModeClass.setup(self)
            ExperimentClass.setup(self)                             
    
    return Experiment()