Added use case "Forwaring Rate At Maximum Offered Load"
[samplevnf.git] / VNFs / DPPD-PROX / helper-scripts / dpi / statsconsfile.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 os
20 import struct
21
22 class StatsConsFile:
23     def __init__(self, file_name, tsc = None):
24         self._file = open(file_name, "rb");
25         try:
26             data = self._file.read(4*8);
27             dataUnpacked = struct.unpack("<qqqq", data);
28
29             self._hz = dataUnpacked[0]
30             if (tsc is None):
31                 self._tsc = dataUnpacked[1]
32             else:
33                 self._tsc = tsc;
34
35             self._entryCount = dataUnpacked[2]
36             fieldCount = dataUnpacked[3]
37
38             data = self._file.read(fieldCount);
39             fmt = "b" * fieldCount;
40
41             dataUnpacked = struct.unpack("<" + fmt, data);
42             self._entryFmt = "<";
43             self._entrySize = 0;
44
45             for e in dataUnpacked:
46                 if (e == 4):
47                     self._entryFmt += "i"
48                 elif (e == 8):
49                     self._entryFmt += "q"
50                 else:
51                     raise Exception("Unknown field format: " + str(e))
52                 self._entrySize += e
53         except:
54             print "except"
55             self._file.close();
56
57     def setBeg(self, tsc):
58         self._tsc = tsc
59
60     def getBeg(self):
61         return self._tsc;
62
63     def getHz(self):
64         return self._hz
65
66     def readNext(self):
67         ret = []
68         for i in range(self._entryCount):
69             entry = self._readNextEntry()
70             if (entry == None):
71                 return None;
72             ret.append(entry);
73         return ret;
74
75     def _readNextEntry(self):
76         try:
77             entry = self._file.read(self._entrySize);
78             entryUnpacked = struct.unpack(self._entryFmt, entry);
79             return list(entryUnpacked)
80         except:
81             return None;
82
83     def close(self):
84         self._file.close();