Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
dulalsaurab | 0dcdb32 | 2018-08-15 20:39:07 +0000 | [diff] [blame] | 3 | # Copyright (C) 2015-2019, The University of Memphis, |
Ashlesh Gawande | 0cccdb8 | 2016-08-15 12:58:06 -0500 | [diff] [blame] | 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 6 | # |
| 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 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 61 | import ConfigParser, re |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 62 | import shlex |
dulalsaurab | 0dcdb32 | 2018-08-15 20:39:07 +0000 | [diff] [blame] | 63 | import sys |
| 64 | from mininet.log import error |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 65 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 66 | class confNDNHost(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 67 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 68 | def __init__(self, name, app='', params='', cpu=None, cores=None, cache=None): |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 69 | self.name = name |
| 70 | self.app = app |
Ashlesh Gawande | 3a4afb1 | 2015-07-09 09:23:30 -0500 | [diff] [blame] | 71 | self.params = params |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 72 | self.cpu = cpu |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 73 | self.cores = cores |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 74 | self.cache = cache |
| 75 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 76 | def __repr__(self): |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 77 | return " Name: {} App: {} Params: {} CPU: {} Cores: {} Cores: {} Cache: {}" \ |
| 78 | .format(self.name, self.app, self.params, self.cpu, self.cores, self.cache) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 79 | |
Vince Lehman | fbd47c9 | 2015-10-14 16:00:06 -0500 | [diff] [blame] | 80 | class confNdnSwitch: |
| 81 | def __init__(self, name): |
| 82 | self.name = name |
| 83 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 84 | class confNDNLink(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 85 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 86 | def __init__(self,h1,h2,linkDict=None): |
| 87 | self.h1 = h1 |
| 88 | self.h2 = h2 |
| 89 | self.linkDict = linkDict |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 90 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 91 | def __repr__(self): |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 92 | return "h1: {} h2: {} params: {}".format(self.h1, self.h2, self.linkDict) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 93 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 94 | def parse_hosts(conf_arq): |
| 95 | 'Parse hosts section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 96 | config = ConfigParser.RawConfigParser() |
| 97 | config.read(conf_arq) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 98 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 99 | hosts = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 100 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 101 | items = config.items('nodes') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 102 | |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 103 | # makes a first-pass read to hosts section to find empty host sections |
dulalsaurab | 0dcdb32 | 2018-08-15 20:39:07 +0000 | [diff] [blame] | 104 | coordinates = [] |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 105 | for item in items: |
| 106 | name = item[0] |
| 107 | rest = item[1].split() |
dulalsaurab | 0dcdb32 | 2018-08-15 20:39:07 +0000 | [diff] [blame] | 108 | # 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]) |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 116 | if len(rest) == 0: |
| 117 | config.set('nodes', name, '_') |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 118 | # updates 'items' list |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 119 | items = config.items('nodes') |
| 120 | |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 121 | # makes a second-pass read to hosts section to properly add hosts |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 122 | for item in items: |
| 123 | |
| 124 | name = item[0] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 125 | |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 126 | rest = shlex.split(item[1]) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 127 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 128 | uris = rest |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 129 | params = {} |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 130 | cpu = None |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 131 | cores = None |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 132 | cache = None |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 133 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 134 | for uri in uris: |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 135 | if re.match("cpu",uri): |
| 136 | cpu = float(uri.split('=')[1]) |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 137 | elif re.match("cores",uri): |
| 138 | cores = uri.split('=')[1] |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 139 | elif re.match("cache",uri): |
| 140 | cache = uri.split('=')[1] |
| 141 | elif re.match("mem",uri): |
| 142 | mem = uri.split('=')[1] |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 143 | elif re.match("app",uri): |
| 144 | app = uri.split('=')[1] |
| 145 | elif re.match("_", uri): |
| 146 | app = "" |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 147 | else: |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 148 | params[uri.split('=')[0]] = uri.split('=')[1] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 149 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 150 | hosts.append(confNDNHost(name, app, params, cpu, cores, cache)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 151 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 152 | return hosts |
| 153 | |
Vince Lehman | fbd47c9 | 2015-10-14 16:00:06 -0500 | [diff] [blame] | 154 | def 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 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 172 | def parse_links(conf_arq): |
| 173 | 'Parse links section from the conf file.' |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 174 | arq = open(conf_arq, 'r') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 175 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 176 | links = [] |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 177 | linkSectionFlag = False |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 178 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 179 | for line in arq: |
| 180 | if linkSectionFlag: |
| 181 | args = line.split() |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 182 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 183 | # checks for non-empty line |
| 184 | if len(args) == 0: |
| 185 | continue |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 186 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 187 | h1, h2 = args.pop(0).split(':') |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 188 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 189 | link_dict = {} |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 190 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 191 | 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 |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 200 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 201 | links.append(confNDNLink(h1,h2,link_dict)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 202 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 203 | elif line == "[links]\n": |
| 204 | linkSectionFlag = True |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 205 | |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 206 | return links |