EMANE  1.2.1
build/lib/emane/events/tdmaschedule.py
Go to the documentation of this file.
1 #
2 # Copyright (c) 2015,2017 - Adjacent Link LLC, Bridgewater, New Jersey
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in
13 # the documentation and/or other materials provided with the
14 # distribution.
15 # * Neither the name of Adjacent Link LLC nor the names of its
16 # contributors may be used to endorse or promote products derived
17 # from this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32 
33 from __future__ import absolute_import, division, print_function
34 from pkg_resources import resource_filename
35 import sys
36 import re
37 import collections
38 from lxml import etree
39 
40 def decodeSI(value):
41  if not isinstance(value,str):
42  return None
43 
44  tmp = value.lstrip('0')
45 
46  powerOf10 = 0
47 
48  if tmp[-1] == 'G':
49  powerOf10 = 9
50 
51  elif tmp[-1] == 'M':
52  powerOf10 = 6
53 
54  elif tmp[-1] == 'K':
55  powerOf10 = 3
56 
57  if powerOf10:
58  tmp = tmp[0:-1]
59 
60  # location of decimal point, if exists
61  indexPoint = tmp.find('.')
62 
63  if indexPoint != -1:
64  numberOfDigitsAfterPoint = len(tmp) - indexPoint - 1
65 
66  if numberOfDigitsAfterPoint > powerOf10:
67  # need to move the decimal point, enough digits are present
68  splitPoint = len(tmp) - (numberOfDigitsAfterPoint - powerOf10)
69  tmp = tmp[0:splitPoint] + '.' + tmp[splitPoint:]
70  else:
71  # need to add some trailing 0s
72  tmp += '0' * (powerOf10 - numberOfDigitsAfterPoint)
73 
74  # remove the decimal point
75  tmp = tmp.replace('.','',1)
76 
77  else:
78  # need to add trailing 0s
79  tmp += '0' * powerOf10
80 
81  return tmp
82 
83 def expandIndex(index):
84  result = set()
85  for item in index.split(','):
86  match = re.match("(\d+):(\d+)",item)
87 
88  if match:
89  list(map(lambda x: result.add(x),list(range(int(match.group(1)),int(match.group(2))+1))))
90  else:
91  result.add(int(item))
92 
93  return result
94 
95 class TDMASchedule(object):
96  def __init__(self,scheduleXML):
97  self._structure = None
98  self._multiFrameDefaults = {}
99  self._frameDefaults = {}
100  self._info = {}
101 
102  tree = etree.parse(scheduleXML)
103 
104  root = tree.getroot()
105 
106  schemaDoc = etree.parse(resource_filename('emane.events',
107  'schema/tdmaschedule.xsd'))
108 
109  schema = etree.XMLSchema(etree=schemaDoc,attribute_defaults=True)
110 
111  if not schema(root):
112  message = []
113  for entry in schema.error_log:
114  message.append("%d: %s" % (entry.line,entry.message))
115  print("\n".join(message), file=sys.stderr)
116  exit(1)
117 
118  # if structrure is present this is a full schedule
119  for structure in root.xpath('/emane-tdma-schedule/structure'):
120  self._structure = {'slotduration' : int(structure.get('slotduration')),
121  'slotoverhead' : int(structure.get('slotoverhead')),
122  'bandwidth' : int(decodeSI(structure.get('bandwidth'))),
123  'slots' : int(structure.get('slots')),
124  'frames' : int(structure.get('frames'))}
125 
126  def nodeDefaults(node):
127  frequency = decodeSI(node.get('frequency'));
128  power = node.get('power')
129  serviceClass = node.get('class')
130  datarate = decodeSI(node.get('datarate'))
131  defaults = {}
132 
133  if frequency != None:
134  defaults['frequency'] = int(frequency)
135 
136  if power != None:
137  defaults['power'] = float(power)
138 
139  if serviceClass != None:
140  defaults['service'] = int(serviceClass)
141 
142  if datarate != None:
143  defaults['datarate'] = int(datarate)
144 
145  return defaults
146 
147 
148  multiframe = root.xpath('/emane-tdma-schedule/multiframe')[0]
149 
150  defaults = nodeDefaults(multiframe)
151 
152  if defaults:
153  self._multiFrameDefaults = defaults
154 
155  for frame in multiframe.xpath('frame'):
156  frameIndexes = expandIndex(frame.get('index'))
157 
158  defaults = nodeDefaults(frame)
159 
160  for frameIndex in frameIndexes:
161  self._frameDefaults[frameIndex] = defaults
162 
163  for slot in frame.xpath('slot'):
164  slotIndexes = expandIndex(slot.get('index'))
165 
166  nodes = expandIndex(slot.get('nodes'))
167 
168  args = {'type' : 'tx'}
169 
170  for child in slot:
171  if child.tag == 'tx':
172  args['type'] = 'tx'
173 
174  frequency = decodeSI(child.get('frequency'))
175 
176  if frequency != None:
177  args['frequency'] = int(frequency)
178 
179  power = child.get('power')
180 
181  if power != None:
182  args['power'] = float(power)
183 
184  serviceClass = child.get('class')
185 
186  if serviceClass != None:
187  args['service'] = int(serviceClass)
188 
189 
190  datarate = decodeSI(child.get('datarate'))
191 
192  if datarate != None:
193  args['datarate'] = int(datarate)
194 
195  destination = child.get('destination')
196 
197  if destination != None:
198  args['destination'] = int(destination)
199 
200  elif child.tag == 'rx':
201  args['type'] = 'rx'
202 
203  frequency = decodeSI(child.get('frequency'))
204 
205  if frequency != None:
206  args['frequency'] = int(frequency)
207 
208  else:
209  args['type'] = 'idle'
210 
211 
212  for node in nodes:
213  for frameIndex in frameIndexes:
214  for slotIndex in slotIndexes:
215  if node not in self._info:
216  self._info[node] = {}
217 
218  if frameIndex not in self._info[node]:
219  self._info[node][frameIndex] = {}
220 
221  self._info[node][frameIndex][slotIndex] = args
222 
223 
224  def defaults(self):
225  return self._multiFrameDefaults
226 
227  def defaultsFrame(self):
228  return self._frameDefaults
229 
230  def info(self):
231  return self._info
232 
233  def structure(self):
234  return self._structure