Statistics
| Branch: | Revision:

wcn_emulator / nepa_test.py @ b1497c9f

History | View | Annotate | Download (5.06 KB)

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

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

    
8
from os import path
9
from time import time
10

    
11
from parameters_parser import parameters
12
from network_builder import *
13
from test_code import *
14
from mininet.log import setLogLevel
15

    
16

    
17

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

    
23
class configurationFile():
24

    
25
    mandatoryOptions = {"testModule":None
26
            , "testClass":None, "times":1}
27
    confParams = {}
28
    className = None
29
    def __init__(self, fileName, stanza):
30
        # check if filename esists
31
        try:
32
            fd =  open(fileName, "r")
33
        except IOError:
34
            error("Can not open the configuration file: " + fileName\
35
                + "\n")
36
            sys.exit(1)
37
        self.parser = ConfigParser.SafeConfigParser()
38
        self.parser.read(fileName)
39

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

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

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

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

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

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

    
83

    
84
def link_conf(conf):
85
    link_opts = {}
86
    if conf.getConfigurations("link_bw"):
87
        link_opts["bw"] = int(conf.getConfigurations("link_bw"))
88
    if conf.getConfigurations("link_mean_delay"):
89
        link_opts["delay"] = (conf.getConfigurations("link_mean_delay"))
90
    if conf.getConfigurations("link_delay_sd"):
91
        link_opts["jitter"] = (conf.getConfigurations("link_delay_sd"))
92
    if conf.getConfigurations("link_delay_distribution"):
93
        link_opts["delay_distribution"] = (conf.getConfigurations("link_delay_distribution"))
94
    if conf.getConfigurations("link_loss"):
95
        link_opts["loss"] = (conf.getConfigurations("link_loss"))
96
    return link_opts
97

    
98
def nepa_test():
99
    setLogLevel('info')
100
    need = [
101
            ("-f", ["configFile", True, "",
102
                "file with the available configurations", str]),
103
            ("-t", ["testName", True, "",
104
                "base name for test output", str])
105
           ]
106
    opt = [
107
            ("-d", ["drawGraph", False, False,
108
                "draw the graph before you run the test", int]),
109
            ("-g", ["graphFile", True, "",
110
                "file with the topology (overrides configuration)", str])
111
          ]
112

    
113
    P = conf(path.basename(__file__),need, opt)
114
    P.parseArgs()
115
    if P.checkCorrectness() == False:
116
        P.printUsage()
117
        sys.exit(1)
118

    
119
    configFile = P.getParam("configFile")
120
    testName = P.getParam("testName")
121
    C = configurationFile(configFile, testName)
122
    #import code
123
    #code.interact(local=locals())
124
    # parse the conf file
125
    networkGraph = P.getParam("graphFile")
126
    if networkGraph == "":
127
        networkGraph = C.getConfigurations("graphDefinition")
128
        if networkGraph == None:
129
            error("No graph topology specified in config file or command " + \
130
                "line!\n")
131
            sys.exit(1)
132
    drawGraph = P.getParam("drawGraph")
133

    
134
    link_opts = link_conf(C)
135

    
136
    net = GraphNet(networkGraph, draw = drawGraph, link_opts = link_opts)
137
    net.start()
138
    net.enableForwarding()
139
    enableShortestRoutes = C.getConfigurations("enableShortestRoutes")
140
    if enableShortestRoutes == None or enableShortestRoutes.lower() == "true":
141
        net.setShortestRoutes()
142
    #CLI(net)
143
    graphname = networkGraph.split('/')[-1].split('.')[0]
144
    testPath = testName + "_" + graphname + "_" + str(int(time()))
145
    for i in range(int(C.getConfigurations("times"))):
146
        info("+++++++ Round: "+str(i+1) + '\n')
147
        test = C.className(net, testPath, C.confParams)
148
        test.runTest()
149
    net.stop()
150
    test.changePermissions()
151
    info("*** Done with experiment: " + testName + "\n")
152

    
153
if __name__ == "__main__":
154
    nepa_test()