Statistics
| Branch: | Revision:

wcn_emulator / nepa_test.py @ 2fd1c362

History | View | Annotate | Download (5.94 KB)

1
#!/usr/bin/env python
2
import sys
3
from os import path
4
# add the nepa_test directory to sys path
5
sys.path.append(path.dirname(sys.argv[0]))
6
import inherit_config_parser
7
import ConfigParser
8
import inspect
9
import StringIO
10

    
11
sys.path.append('test_code')
12
sys.path.insert(0, './')
13

    
14
from os import path
15
from time import time
16

    
17
from parameters_parser import parameters
18
from network_builder import *
19
from test_code import *
20
from mininet.log import setLogLevel
21

    
22

    
23
class conf(parameters):
24
    def checkCorrectness(self):
25
        self.checkNeededParams()
26
        return True
27

    
28

    
29
class ConfigurationFile():
30

    
31
    mandatoryOptions = {"testModule": None, "testClass": None, "times": 1}
32
    confParams = {}
33
    className = None
34

    
35
    def __init__(self, fileName, stanza, overrideOption=""):
36
        # check if filename esists
37
        if not path.isfile(fileName):
38
            error("Can not open the configuration file: " + fileName + "\n")
39
            sys.exit(1)
40
        self.parser = inherit_config_parser.InheritConfigParser()
41
        self.parser.optionxform = str
42
        self.parser.read(fileName)
43

    
44
        self.testName = stanza
45
        if stanza not in self.parser.sections():
46
            error("Can not find configuration " + stanza
47
                  + " in file " + fileName + "\n")
48
            sys.exit(1)
49
        for o in self.mandatoryOptions:
50
            self.mandatoryOptions[o] = \
51
                self.getConfigurations(o, raiseError=True)
52

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

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

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

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

    
76
        if overrideOption:
77
            options = overrideOption.replace(",", "\n")
78
            overrideConf = StringIO.StringIO("[DEFAULT]\n" + options + "\n")
79
            tmpParser = ConfigParser.ConfigParser()
80
            tmpParser.optionxform = str
81
            tmpParser.readfp(overrideConf)
82
            for name, value in tmpParser.defaults().items():
83
                print name, value
84
                self.confParams[name] = value
85

    
86
    def getConfigurations(self, name, raiseError=False):
87
        try:
88
            r = self.parser.get(self.testName, name)
89
        except ConfigParser.NoOptionError:
90
            if raiseError:
91
                error("No option \'" + name + "\' found!\n")
92
                sys.exit()
93
            else:
94
                return None
95
        return r
96

    
97

    
98
def link_conf(conf):
99
    link_opts = {}
100
    if conf.getConfigurations("link_bw"):
101
        link_opts["bw"] = int(conf.getConfigurations("link_bw"))
102
    if conf.getConfigurations("link_mean_delay"):
103
        link_opts["delay"] = (conf.getConfigurations("link_mean_delay"))
104
    if conf.getConfigurations("link_delay_sd"):
105
        link_opts["jitter"] = (conf.getConfigurations("link_delay_sd"))
106
    if conf.getConfigurations("link_delay_distribution"):
107
        link_opts["delay_distribution"] = \
108
            (conf.getConfigurations("link_delay_distribution"))
109
    if conf.getConfigurations("link_loss"):
110
        link_opts["loss"] = (conf.getConfigurations("link_loss"))
111
    return link_opts
112

    
113

    
114
def nepa_test():
115
    setLogLevel('info')
116
    need = [
117
        ("-f", ["configFile", True, "",
118
         "file with the available configurations", str]),
119
        ("-t", ["testName", True, "",
120
         "base name for test output", str])
121
        ]
122
    opt = [
123
        ("-d", ["drawGraph", False, False,
124
         "draw the graph before you run the test", int]),
125
        ("-g", ["graphFile", True, "",
126
         "file with the topology (overrides configuration)", str]),
127
        ("-o", ["overrideOption", True, "",
128
         "comma separated list of options to override in the ini file \
129
         (ex: a=10,b=100)", str]),
130
        ]
131

    
132
    P = conf(path.basename(__file__), need, opt)
133
    P.parseArgs()
134
    if not P.checkCorrectness():
135
        P.printUsage()
136
        sys.exit(1)
137

    
138
    configFile = P.getParam("configFile")
139
    testName = P.getParam("testName")
140
    C = ConfigurationFile(configFile, testName, P.getParam("overrideOption"))
141
    # parse the conf file
142
    networkGraph = P.getParam("graphFile")
143
    if networkGraph == "":
144
        networkGraph = C.getConfigurations("graphDefinition")
145
        if not networkGraph:
146
            error("No graph topology specified in conf file or command line!\n")
147
            sys.exit(1)
148
    drawGraph = P.getParam("drawGraph")
149

    
150
    link_opts = link_conf(C)
151

    
152
    net = GraphNet(networkGraph, draw=drawGraph, link_opts=link_opts)
153
    net.start()
154
    net.enableForwarding()
155
    enableShortestRoutes = C.getConfigurations("enableShortestRoutes")
156
    shortestPathComputation = C.getConfigurations("shortestPathComputation")
157
    if enableShortestRoutes or shortestPathComputation:
158
        if enableShortestRoutes.lower() == "true" or \
159
           shortestPathComputation.lower() == "true":
160
            net.setShortestPaths()
161
    else:
162
        net.setShortestPaths()
163

    
164
    # CLI(net)
165
    graphname = networkGraph.split('/')[-1].split('.')[0]
166
    testPath = testName + "_" + graphname + "_" + str(int(time()))
167
    for i in range(int(C.getConfigurations("times"))):
168
        info("+++++++ Round: "+str(i+1) + '\n')
169
        test = C.className(net, testPath, C.confParams)
170
        test.runTest()
171
    net.stop()
172
    test.changePermissions()
173
    info("*** Done with experiment: " + testName + "\n")
174

    
175
if __name__ == "__main__":
176
    nepa_test()