blob: b24435a6098e96550928102a95e06dcc9b8da42c [file] [log] [blame]
Vince Lehmanb8b18062015-07-14 13:07:22 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
dulalsaurab0dcdb322018-08-15 20:39:07 +00003# Copyright (C) 2015-2019, 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
dulalsaurab0dcdb322018-08-15 20:39:07 +000063import sys
64from mininet.log import error
carlosmscabralf40ecd12013-02-01 18:15:58 -020065
ashuef3490b2015-02-17 11:01:04 -060066class confNDNHost():
carlosmscabral6d3dd602013-03-23 11:12:34 -030067
ashuef3490b2015-02-17 11:01:04 -060068 def __init__(self, name, app='', params='', cpu=None, cores=None, cache=None):
carlosmscabralf40ecd12013-02-01 18:15:58 -020069 self.name = name
70 self.app = app
Ashlesh Gawande3a4afb12015-07-09 09:23:30 -050071 self.params = params
carlosmscabral29432252013-02-04 11:54:16 -020072 self.cpu = cpu
carlosmscabrale121a7b2013-02-18 18:14:53 -030073 self.cores = cores
ashuef3490b2015-02-17 11:01:04 -060074 self.cache = cache
75
carlosmscabralf40ecd12013-02-01 18:15:58 -020076 def __repr__(self):
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -050077 return " Name: {} App: {} Params: {} CPU: {} Cores: {} Cores: {} Cache: {}" \
78 .format(self.name, self.app, self.params, self.cpu, self.cores, self.cache)
carlosmscabralf40ecd12013-02-01 18:15:58 -020079
Vince Lehmanfbd47c92015-10-14 16:00:06 -050080class confNdnSwitch:
81 def __init__(self, name):
82 self.name = name
83
ashuef3490b2015-02-17 11:01:04 -060084class confNDNLink():
carlosmscabral6d3dd602013-03-23 11:12:34 -030085
carlosmscabralf40ecd12013-02-01 18:15:58 -020086 def __init__(self,h1,h2,linkDict=None):
87 self.h1 = h1
88 self.h2 = h2
89 self.linkDict = linkDict
carlosmscabral6d3dd602013-03-23 11:12:34 -030090
carlosmscabralf40ecd12013-02-01 18:15:58 -020091 def __repr__(self):
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -050092 return "h1: {} h2: {} params: {}".format(self.h1, self.h2, self.linkDict)
carlosmscabral6d3dd602013-03-23 11:12:34 -030093
carlosmscabral29432252013-02-04 11:54:16 -020094def parse_hosts(conf_arq):
95 'Parse hosts section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020096 config = ConfigParser.RawConfigParser()
97 config.read(conf_arq)
carlosmscabral6d3dd602013-03-23 11:12:34 -030098
carlosmscabralf40ecd12013-02-01 18:15:58 -020099 hosts = []
carlosmscabral6d3dd602013-03-23 11:12:34 -0300100
ashuef3490b2015-02-17 11:01:04 -0600101 items = config.items('nodes')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300102
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -0500103 # makes a first-pass read to hosts section to find empty host sections
dulalsaurab0dcdb322018-08-15 20:39:07 +0000104 coordinates = []
ashuef3490b2015-02-17 11:01:04 -0600105 for item in items:
106 name = item[0]
107 rest = item[1].split()
dulalsaurab0dcdb322018-08-15 20:39:07 +0000108 # check for the duplicate coordinates
109 if "radius" in item[1]:
110 if item[1] in coordinates:
111 error("FATAL: Duplicate Coordinate, \"{}\" used by multiple nodes\n" \
112 .format(item[1]))
113 sys.exit(1)
114 else:
115 coordinates.append(item[1])
ashuef3490b2015-02-17 11:01:04 -0600116 if len(rest) == 0:
117 config.set('nodes', name, '_')
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -0500118 # updates 'items' list
ashuef3490b2015-02-17 11:01:04 -0600119 items = config.items('nodes')
120
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -0500121 # 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.'
Alexander Lane517d34c2018-06-01 12:35:11 -0500174 arq = open(conf_arq, 'r')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300175
carlosmscabralf40ecd12013-02-01 18:15:58 -0200176 links = []
Alexander Lane517d34c2018-06-01 12:35:11 -0500177 linkSectionFlag = False
carlosmscabral6d3dd602013-03-23 11:12:34 -0300178
Alexander Lane517d34c2018-06-01 12:35:11 -0500179 for line in arq:
180 if linkSectionFlag:
181 args = line.split()
carlosmscabral6d3dd602013-03-23 11:12:34 -0300182
Alexander Lane517d34c2018-06-01 12:35:11 -0500183 # checks for non-empty line
184 if len(args) == 0:
185 continue
carlosmscabral6d3dd602013-03-23 11:12:34 -0300186
Alexander Lane517d34c2018-06-01 12:35:11 -0500187 h1, h2 = args.pop(0).split(':')
ashuef3490b2015-02-17 11:01:04 -0600188
Alexander Lane517d34c2018-06-01 12:35:11 -0500189 link_dict = {}
Caio Eliasc6b56032014-09-19 14:12:48 -0300190
Alexander Lane517d34c2018-06-01 12:35:11 -0500191 for arg in args:
192 arg_name, arg_value = arg.split('=')
193 key = arg_name
194 value = arg_value
195 if key in ['bw','jitter','max_queue_size']:
196 value = int(value)
197 if key in ['loss']:
198 value = float(value)
199 link_dict[key] = value
carlosmscabral6d3dd602013-03-23 11:12:34 -0300200
Alexander Lane517d34c2018-06-01 12:35:11 -0500201 links.append(confNDNLink(h1,h2,link_dict))
carlosmscabral6d3dd602013-03-23 11:12:34 -0300202
Alexander Lane517d34c2018-06-01 12:35:11 -0500203 elif line == "[links]\n":
204 linkSectionFlag = True
carlosmscabral6d3dd602013-03-23 11:12:34 -0300205
Ashlesh Gawande27b5e1b2018-08-06 17:47:15 -0500206 return links