Statistics
| Branch: | Revision:

wcn_emulator / wcn_simulator.py @ 461e26e8

History | View | Annotate | Download (4.13 KB)

1
#!/usr/bin/env python
2
import sys
3
import ConfigParser
4
import inspect
5

    
6
sys.path.append('test_code')
7

    
8
import os 
9
from time import time
10

    
11
from parameters_parser import parameters
12
from network_builder import *
13
from test_code import *
14

    
15

    
16

    
17
class conf(parameters):
18
    def checkCorrectness(self):
19
        self.checkNeededParams()
20
        return True
21

    
22
class configurationFile():
23

    
24
    mandatoryOptions = {"testModule":None
25
            , "testClass":None}
26
    confParams = {}
27
    className = None
28
    def __init__(self, fileName, stanza):
29
        """ receives the configuration fileName and the stanza to be 
30
        parsed """
31

    
32
        if not os.path.isfile(fileName):
33
            error("Can not open the configuration file: " + fileName\
34
                + "\n")
35
            sys.exit(1)
36
        self.parser = ConfigParser.SafeConfigParser()
37
        self.parser.read(fileName)
38
        self.testName = stanza 
39

    
40
        if stanza not in self.parser.sections():
41
            error("Can not find configuration " + stanza \
42
                    + " in file " + fileName + "\n")
43
            sys.exit(1)
44

    
45
        for o in self.mandatoryOptions:
46
            self.mandatoryOptions[o] = \
47
                self.getConfigurations(o, raiseError=True)
48

    
49
        # this builds a module with the correct path in python namespace
50
        moduleName = "test_code." + self.mandatoryOptions['testModule']
51

    
52
        if moduleName not in sys.modules:
53
            errorString = "ERROR: no " \
54
                + self.mandatoryOptions['testModule'] \
55
                + " module  has been loaded!\n"
56
            error(errorString)
57
            sys.exit(1)
58

    
59
        if self.mandatoryOptions['testClass'] not in \
60
                zip(*inspect.getmembers(sys.modules[moduleName]))[0]:
61
            errorString = "ERROR: no " \
62
                + self.mandatoryOptions['testClass'] \
63
                + " simulation class is present in "\
64
                + moduleName.replace(".","/")+".py" + "\n"
65
            error(errorString)
66
            sys.exit(1)
67

    
68
        self.className = getattr(sys.modules[moduleName],
69
            self.mandatoryOptions['testClass'])
70

    
71
        for name, value in self.parser.items(self.testName):
72
            self.confParams[name] = value
73

    
74
    def getConfigurations(self, name, raiseError=False):
75
        try:
76
            r = self.parser.get(self.testName, name)
77
        except ConfigParser.NoOptionError:
78
            if raiseError:
79
                error("No option \'" + name + "\' found!\n")
80
                sys.exit()
81
            else:
82
                return None
83
        return r
84

    
85
if __name__ == '__main__':
86
    setLogLevel('info')
87
    need = [
88
            ("-f", ["configFile", True, "",
89
                "file with the available configurations", str]),
90
            ("-t", ["testName", True, "",
91
                "name of the configuration to run", str])
92
           ]
93
    opt = [
94
            ("-d", ["drawGraph", False, False,
95
                "draw the graph before you run the test", int]),
96
            ("-g", ["graphFile", True, "",
97
                "file with the topology (overrides configuration)", str])
98
          ]
99

    
100
    P = conf(os.path.basename(__file__),need, opt)
101
    P.parseArgs()
102
    if P.checkCorrectness() == False:
103
        P.printUsage()
104
        sys.exit(1)
105

    
106
    configFile = P.getParam("configFile")
107
    testName = P.getParam("testName")
108
    C = configurationFile(configFile, testName)
109
    #import code
110
    #code.interact(local=locals())
111
    # parse the conf file
112
    networkGraph = P.getParam("graphFile")
113
    if networkGraph == "":
114
        networkGraph = C.getConfigurations("graphDefinition")
115
        if networkGraph == None:
116
            error("No graph topology specified in config file or command " + \
117
                "line!\n")
118
            sys.exit(1)
119
    drawGraph = P.getParam("drawGraph")
120

    
121
    net = GraphNet(networkGraph, draw = drawGraph)
122
    net.start()
123
    net.enableForwarding()
124
    net.setShortestRoutes()
125
    testPath = testName+"_"+str(int(time()))
126
    #CLI(net)
127
    for i in range(1):
128
        info("+++++++ Round: "+str(i+1) + '\n')
129
        test = C.className(net, testPath, C.confParams)
130
        test.runTest()
131
    net.stop()
132
    test.changePermissions()
133
    info("*** Done with experiment: " + testName + "\n")