Merge "VNF_Catalogue Codebase"
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / dpi / config.py
1 #!/bin/env python
2
3 ##
4 ## Copyright (c) 2010-2017 Intel Corporation
5 ##
6 ## Licensed under the Apache License, Version 2.0 (the "License");
7 ## you may not use this file except in compliance with the License.
8 ## You may obtain a copy of the License at
9 ##
10 ##     http://www.apache.org/licenses/LICENSE-2.0
11 ##
12 ## Unless required by applicable law or agreed to in writing, software
13 ## distributed under the License is distributed on an "AS IS" BASIS,
14 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ## See the License for the specific language governing permissions and
16 ## limitations under the License.
17 ##
18
19 import getopt
20 import sys
21 from systemconfig import *
22
23 class Config:
24     _debug = False;
25     _test_systems = [];
26     _output_file_name = None;
27     _input_file_name = None
28     _input_file_name2 = None
29     _max_port_rate = 0.85
30     _sut = None
31     _accuracy = 2;
32     _threshold = 0.95
33     _once = None
34     _skipTime = 10
35     _testLength = 120
36     _dpiCoreList = range(1, 5)
37     _checkConditions = False;
38     _interCheckDuration = float(1)
39
40     def getInputFileName(self):
41         return self._input_file_name
42
43     def getInputFileName2(self):
44         return self._input_file_name2
45
46     def toString(self):
47         ret = ""
48         ret += "Test systems: \n"
49         for ts in self._test_systems:
50             ret += ts.toString();
51
52         if (self._sut is not None):
53             ret += "SUT: \n"
54             ret += self._sut.toString();
55
56         ret += "Output file name: " + str(self._output_file_name) + "\n"
57         ret += "Max port rate: " + str(self._max_port_rate) + "\n"
58         ret += "Accuracy: " + str(self._accuracy) + " digits after point"
59         return ret
60
61     def getErrorTestOne(self):
62         if (len(self._test_systems) == 0):
63             return "Missing test systems";
64         if (self._output_file_name is None):
65             return "No output file or input file defined";
66         return None
67
68     def getErrorTestTwo(self):
69         if (self._input_file_name is None):
70             return "Input file is missing"
71         if (self._input_file_name == self._output_file_name):
72             return "Input file and output file are the same"
73         return self.getErrorTestOne();
74
75     def getErrorMakeTable(self):
76         if (self._input_file_name is None):
77             return "Missing input file"
78         if (self._input_file_name2 is None):
79             return "Missing file with performance resuilts"
80         if (self._output_file_name is None):
81             return "No output file or input file defined";
82         if (self._input_file_name2 == self._input_file_name):
83             return "Input file used multiple times"
84         if (self._input_file_name == self._output_file_name):
85             return "output file is the same as the input file"
86         if (self._input_file_name2 == self._output_file_name):
87             return "output file is the same as the input file 2"
88
89         return None
90
91     def usageAndExit(self, argv0):
92         print "Usage: " + str(argv0)
93         print "-t    Add a test system, syntax: " + SystemConfig.expectedSyntax()
94         print "-s    Add SUT, syntax: " + SystemConfig.expectedSyntax()
95         print "-o    Ouput file name"
96         print "-a    Accuracy, number of digits after point"
97         print "-i    Input file"
98         print "-j    File with performance results"
99         print "-m    Maximum per port rate, by default 0.85 (85%)"
100         print "-d    Enable debugging"
101         print "-w    Fraction of connections to reach, by default is 0.95 (95%)"
102         print "-h    Show help"
103         print "-q    Run a single test iteration, syntax of argument "
104         print "-b    Skip time, by default 10 sec"
105         print "-l    Test length, by default 120 sec"
106         print "-n    Maximum number of DPI cores to test"
107         print "-k    Period between checking conditions, 1 second by default"
108         print "-c    Check conditions during 10 second period after convergence"
109         print "      is msr,conn,ss (i.e. -q 4000,100000,38.91)"
110         exit(-1);
111
112     def parse(self, programName, args):
113         try:
114             opts, args = getopt.getopt(args, "t:s:o:a:i:q:m:dhw:j:b:l:n:k:c")
115         except getopt.GetoptError as err:
116             print str(err)
117             return;
118         for option, arg in opts:
119             if(option == "-t"):
120                 for ts in arg.split(","):
121                     syntaxErr = SystemConfig.checkSyntax(ts)
122                     if (syntaxErr != ""):
123                         print syntaxErr
124                         exit(-1);
125                     self._test_systems.append(SystemConfig(ts));
126             elif(option == "-s"):
127                 syntaxErr = SystemConfig.checkSyntax(ts)
128                 if (syntaxErr != ""):
129                     print syntaxErr
130                     exit(-1);
131                 self._sut = SystemConfig(arg);
132             elif(option == "-w"):
133                 self._threshold = float(arg)
134             elif(option == "-o"):
135                 self._output_file_name = arg;
136             elif(option == '-a'):
137                 self._accuracy = int(arg);
138             elif(option == "-i"):
139                 self._input_file_name = arg;
140             elif(option == "-j"):
141                 self._input_file_name2 = arg;
142             elif(option == "-q"):
143                 self._once = arg.split(",")
144             elif(option == "-c"):
145                 self._checkConditions = True;
146             elif(option == "-m"):
147                 self._max_port_rate = float(arg);
148             elif(option == "-k"):
149                 self._interCheckDuration = float(arg);
150             elif(option == "-d"):
151                 self._debug = True
152             elif(option == '-h'):
153                 self.usageAndExit(programName)
154             elif(option == '-b'):
155                 self._skipTime = int(arg)
156             elif(option == '-l'):
157                 self._testLength = int(arg)
158             elif(option == '-n'):
159                 self._dpiCoreList = self.strToList(arg)
160             else:
161                 self.usageAndExit(programName);
162
163     def strToList(self, arg):
164         elements = [];
165         tokens = arg.split(",");
166
167         for a in tokens:
168             if (a.count('-') == 0):
169                 elements.append(int(a))
170             elif (a.count('-') == 1):
171                 beg = int(a.split('-')[0]);
172                 end = int(a.split('-')[1]);
173                 if (beg > end):
174                     raise Exception("Invalid list input format")
175                 elements += range(beg, end + 1);
176             else:
177                 raise Exception("Invalid list input format")
178         return elements;