blob: 725c1b08b9387dd51f20e634900217641be0bcef [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Ashlesh Gawandeda475f02017-03-01 17:20:58 -06003# Copyright (C) 2015-2017, The University of Memphis,
Ashlesh Gawande0cccdb82016-08-15 12:58:06 -05004# Arizona Board of Regents,
5# Regents of the University of California.
Vince Lehmanb8b18062015-07-14 13:07:22 -05006#
7# This file is part of Mini-NDN.
8# See AUTHORS.md for a complete list of Mini-NDN authors and contributors.
9#
10# Mini-NDN is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# Mini-NDN is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with Mini-NDN, e.g., in COPYING.md file.
22# If not, see <http://www.gnu.org/licenses/>.
23#
24# This file incorporates work covered by the following copyright and
25# permission notice:
26#
27# Mininet 2.2.1 License
28#
29# Copyright (c) 2013-2015 Open Networking Laboratory
30# Copyright (c) 2009-2012 Bob Lantz and The Board of Trustees of
31# The Leland Stanford Junior University
32#
33# Original authors: Bob Lantz and Brandon Heller
34#
35# We are making Mininet available for public use and benefit with the
36# expectation that others will use, modify and enhance the Software and
37# contribute those enhancements back to the community. However, since we
38# would like to make the Software available for broadest use, with as few
39# restrictions as possible permission is hereby granted, free of charge, to
40# any person obtaining a copy of this Software to deal in the Software
41# under the copyrights without restriction, including without limitation
42# the rights to use, copy, modify, merge, publish, distribute, sublicense,
43# and/or sell copies of the Software, and to permit persons to whom the
44# Software is furnished to do so, subject to the following conditions:
45#
46# The above copyright notice and this permission notice shall be included
47# in all copies or substantial portions of the Software.
48#
49# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
50# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
51# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
52# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
53# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
54# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
55# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56#
57# The name and trademarks of copyright holder(s) may NOT be used in
58# advertising or publicity pertaining to the Software or any derivatives
59# without specific, written prior permission.
60
carlosmscabral29432252013-02-04 11:54:16 -020061import ConfigParser, re
Ashlesh Gawande557cb842015-07-01 15:39:44 -050062import shlex
carlosmscabralf40ecd12013-02-01 18:15:58 -020063
ashuef3490b2015-02-17 11:01:04 -060064class confNDNHost():
carlosmscabral6d3dd602013-03-23 11:12:34 -030065
ashuef3490b2015-02-17 11:01:04 -060066 def __init__(self, name, app='', params='', cpu=None, cores=None, cache=None):
carlosmscabralf40ecd12013-02-01 18:15:58 -020067 self.name = name
68 self.app = app
ashuef3490b2015-02-17 11:01:04 -060069 self.uri_tuples = params
Ashlesh Gawande3a4afb12015-07-09 09:23:30 -050070 self.params = params
carlosmscabral29432252013-02-04 11:54:16 -020071 self.cpu = cpu
carlosmscabrale121a7b2013-02-18 18:14:53 -030072 self.cores = cores
ashuef3490b2015-02-17 11:01:04 -060073 self.cache = cache
74
75 # For now assume leftovers are NLSR configuration parameters
76 self.nlsrParameters = params
carlosmscabral6d3dd602013-03-23 11:12:34 -030077
carlosmscabralf40ecd12013-02-01 18:15:58 -020078 def __repr__(self):
ashuef3490b2015-02-17 11:01:04 -060079 return 'Name: ' + self.name + \
80 ' App: ' + self.app + \
81 ' URIS: ' + str(self.uri_tuples) + \
82 ' CPU: ' + str(self.cpu) + \
83 ' Cores: ' + str(self.cores) + \
84 ' Cache: ' + str(self.cache) + \
85 ' Radius: ' + str(self.radius) + \
86 ' Angle: ' + str(self.angle) + \
87 ' NLSR Parameters: ' + self.nlsrParameters
carlosmscabralf40ecd12013-02-01 18:15:58 -020088
Vince Lehmanfbd47c92015-10-14 16:00:06 -050089class confNdnSwitch:
90 def __init__(self, name):
91 self.name = name
92
ashuef3490b2015-02-17 11:01:04 -060093class confNDNLink():
carlosmscabral6d3dd602013-03-23 11:12:34 -030094
carlosmscabralf40ecd12013-02-01 18:15:58 -020095 def __init__(self,h1,h2,linkDict=None):
96 self.h1 = h1
97 self.h2 = h2
98 self.linkDict = linkDict
carlosmscabral6d3dd602013-03-23 11:12:34 -030099
carlosmscabralf40ecd12013-02-01 18:15:58 -0200100 def __repr__(self):
101 return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict)
carlosmscabral6d3dd602013-03-23 11:12:34 -0300102
carlosmscabral29432252013-02-04 11:54:16 -0200103def parse_hosts(conf_arq):
104 'Parse hosts section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -0200105 config = ConfigParser.RawConfigParser()
106 config.read(conf_arq)
carlosmscabral6d3dd602013-03-23 11:12:34 -0300107
carlosmscabralf40ecd12013-02-01 18:15:58 -0200108 hosts = []
carlosmscabral6d3dd602013-03-23 11:12:34 -0300109
ashuef3490b2015-02-17 11:01:04 -0600110 items = config.items('nodes')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300111
ashuef3490b2015-02-17 11:01:04 -0600112 #makes a first-pass read to hosts section to find empty host sections
113 for item in items:
114 name = item[0]
115 rest = item[1].split()
116 if len(rest) == 0:
117 config.set('nodes', name, '_')
118 #updates 'items' list
119 items = config.items('nodes')
120
121 #makes a second-pass read to hosts section to properly add hosts
carlosmscabralf40ecd12013-02-01 18:15:58 -0200122 for item in items:
123
124 name = item[0]
carlosmscabral6d3dd602013-03-23 11:12:34 -0300125
Ashlesh Gawande557cb842015-07-01 15:39:44 -0500126 rest = shlex.split(item[1])
carlosmscabral6d3dd602013-03-23 11:12:34 -0300127
carlosmscabralf40ecd12013-02-01 18:15:58 -0200128 uris = rest
ashuef3490b2015-02-17 11:01:04 -0600129 params = {}
carlosmscabral29432252013-02-04 11:54:16 -0200130 cpu = None
carlosmscabrale121a7b2013-02-18 18:14:53 -0300131 cores = None
ashuef3490b2015-02-17 11:01:04 -0600132 cache = None
carlosmscabral6d3dd602013-03-23 11:12:34 -0300133
carlosmscabralf40ecd12013-02-01 18:15:58 -0200134 for uri in uris:
carlosmscabral29432252013-02-04 11:54:16 -0200135 if re.match("cpu",uri):
136 cpu = float(uri.split('=')[1])
carlosmscabrale121a7b2013-02-18 18:14:53 -0300137 elif re.match("cores",uri):
138 cores = uri.split('=')[1]
ashuef3490b2015-02-17 11:01:04 -0600139 elif re.match("cache",uri):
140 cache = uri.split('=')[1]
141 elif re.match("mem",uri):
142 mem = uri.split('=')[1]
Ashlesh Gawande557cb842015-07-01 15:39:44 -0500143 elif re.match("app",uri):
144 app = uri.split('=')[1]
145 elif re.match("_", uri):
146 app = ""
carlosmscabral29432252013-02-04 11:54:16 -0200147 else:
ashuef3490b2015-02-17 11:01:04 -0600148 params[uri.split('=')[0]] = uri.split('=')[1]
carlosmscabral6d3dd602013-03-23 11:12:34 -0300149
ashuef3490b2015-02-17 11:01:04 -0600150 hosts.append(confNDNHost(name, app, params, cpu, cores, cache))
carlosmscabral6d3dd602013-03-23 11:12:34 -0300151
carlosmscabralf40ecd12013-02-01 18:15:58 -0200152 return hosts
153
Vince Lehmanfbd47c92015-10-14 16:00:06 -0500154def parse_switches(conf_arq):
155 'Parse switches section from the conf file.'
156 config = ConfigParser.RawConfigParser()
157 config.read(conf_arq)
158
159 switches = []
160
161 try:
162 items = config.items('switches')
163 except ConfigParser.NoSectionError:
164 return switches
165
166 for item in items:
167 name = item[0]
168 switches.append(confNdnSwitch(name))
169
170 return switches
171
carlosmscabral29432252013-02-04 11:54:16 -0200172def parse_links(conf_arq):
173 'Parse links section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -0200174 arq = open(conf_arq,'r')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300175
carlosmscabralf40ecd12013-02-01 18:15:58 -0200176 links = []
carlosmscabral6d3dd602013-03-23 11:12:34 -0300177
carlosmscabralf40ecd12013-02-01 18:15:58 -0200178 while True:
179 line = arq.readline()
180 if line == '[links]\n':
181 break
carlosmscabral6d3dd602013-03-23 11:12:34 -0300182
carlosmscabralf40ecd12013-02-01 18:15:58 -0200183 while True:
184 line = arq.readline()
185 if line == '':
186 break
carlosmscabral6d3dd602013-03-23 11:12:34 -0300187
carlosmscabralf40ecd12013-02-01 18:15:58 -0200188 args = line.split()
ashuef3490b2015-02-17 11:01:04 -0600189
190 #checks for non-empty line
191 if len(args) == 0:
192 continue
Caio Eliasc6b56032014-09-19 14:12:48 -0300193
carlosmscabralf40ecd12013-02-01 18:15:58 -0200194 h1, h2 = args.pop(0).split(':')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300195
carlosmscabralf40ecd12013-02-01 18:15:58 -0200196 link_dict = {}
carlosmscabral6d3dd602013-03-23 11:12:34 -0300197
carlosmscabralf40ecd12013-02-01 18:15:58 -0200198 for arg in args:
199 arg_name, arg_value = arg.split('=')
200 key = arg_name
201 value = arg_value
carlosmscabral6d3dd602013-03-23 11:12:34 -0300202 if key in ['bw','jitter','max_queue_size']:
carlosmscabralf40ecd12013-02-01 18:15:58 -0200203 value = int(value)
carlosmscabral6d3dd602013-03-23 11:12:34 -0300204 if key in ['loss']:
205 value = float(value)
carlosmscabralf40ecd12013-02-01 18:15:58 -0200206 link_dict[key] = value
carlosmscabral6d3dd602013-03-23 11:12:34 -0300207
ashuef3490b2015-02-17 11:01:04 -0600208 links.append(confNDNLink(h1,h2,link_dict))
carlosmscabral6d3dd602013-03-23 11:12:34 -0300209
210
carlosmscabralf40ecd12013-02-01 18:15:58 -0200211 return links