Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
Alexander Lane | 9944cf5 | 2018-05-17 12:16:50 -0500 | [diff] [blame] | 3 | # Copyright (C) 2015-2018, 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 |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 63 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 64 | class confNDNHost(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 65 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 66 | def __init__(self, name, app='', params='', cpu=None, cores=None, cache=None): |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 67 | self.name = name |
| 68 | self.app = app |
Ashlesh Gawande | 3a4afb1 | 2015-07-09 09:23:30 -0500 | [diff] [blame] | 69 | self.params = params |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 70 | self.cpu = cpu |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 71 | self.cores = cores |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 72 | self.cache = cache |
| 73 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 74 | def __repr__(self): |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 75 | return " Name: {} App: {} Params: {} CPU: {} Cores: {} Cores: {} Cache: {}" \ |
| 76 | .format(self.name, self.app, self.params, self.cpu, self.cores, self.cache) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 77 | |
Vince Lehman | fbd47c9 | 2015-10-14 16:00:06 -0500 | [diff] [blame] | 78 | class confNdnSwitch: |
| 79 | def __init__(self, name): |
| 80 | self.name = name |
| 81 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 82 | class confNDNLink(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 83 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 84 | def __init__(self,h1,h2,linkDict=None): |
| 85 | self.h1 = h1 |
| 86 | self.h2 = h2 |
| 87 | self.linkDict = linkDict |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 88 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 89 | def __repr__(self): |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 90 | return "h1: {} h2: {} params: {}".format(self.h1, self.h2, self.linkDict) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 91 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 92 | def parse_hosts(conf_arq): |
| 93 | 'Parse hosts section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 94 | config = ConfigParser.RawConfigParser() |
| 95 | config.read(conf_arq) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 96 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 97 | hosts = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 98 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 99 | items = config.items('nodes') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 100 | |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 101 | # makes a first-pass read to hosts section to find empty host sections |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 102 | for item in items: |
| 103 | name = item[0] |
| 104 | rest = item[1].split() |
| 105 | if len(rest) == 0: |
| 106 | config.set('nodes', name, '_') |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 107 | # updates 'items' list |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 108 | items = config.items('nodes') |
| 109 | |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 110 | # makes a second-pass read to hosts section to properly add hosts |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 111 | for item in items: |
| 112 | |
| 113 | name = item[0] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 114 | |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 115 | rest = shlex.split(item[1]) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 116 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 117 | uris = rest |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 118 | params = {} |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 119 | cpu = None |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 120 | cores = None |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 121 | cache = None |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 122 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 123 | for uri in uris: |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 124 | if re.match("cpu",uri): |
| 125 | cpu = float(uri.split('=')[1]) |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 126 | elif re.match("cores",uri): |
| 127 | cores = uri.split('=')[1] |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 128 | elif re.match("cache",uri): |
| 129 | cache = uri.split('=')[1] |
| 130 | elif re.match("mem",uri): |
| 131 | mem = uri.split('=')[1] |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 132 | elif re.match("app",uri): |
| 133 | app = uri.split('=')[1] |
| 134 | elif re.match("_", uri): |
| 135 | app = "" |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 136 | else: |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 137 | params[uri.split('=')[0]] = uri.split('=')[1] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 138 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 139 | hosts.append(confNDNHost(name, app, params, cpu, cores, cache)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 140 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 141 | return hosts |
| 142 | |
Vince Lehman | fbd47c9 | 2015-10-14 16:00:06 -0500 | [diff] [blame] | 143 | def parse_switches(conf_arq): |
| 144 | 'Parse switches section from the conf file.' |
| 145 | config = ConfigParser.RawConfigParser() |
| 146 | config.read(conf_arq) |
| 147 | |
| 148 | switches = [] |
| 149 | |
| 150 | try: |
| 151 | items = config.items('switches') |
| 152 | except ConfigParser.NoSectionError: |
| 153 | return switches |
| 154 | |
| 155 | for item in items: |
| 156 | name = item[0] |
| 157 | switches.append(confNdnSwitch(name)) |
| 158 | |
| 159 | return switches |
| 160 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 161 | def parse_links(conf_arq): |
| 162 | 'Parse links section from the conf file.' |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 163 | arq = open(conf_arq, 'r') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 164 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 165 | links = [] |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 166 | linkSectionFlag = False |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 167 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 168 | for line in arq: |
| 169 | if linkSectionFlag: |
| 170 | args = line.split() |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 171 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 172 | # checks for non-empty line |
| 173 | if len(args) == 0: |
| 174 | continue |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 175 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 176 | h1, h2 = args.pop(0).split(':') |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 177 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 178 | link_dict = {} |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 179 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 180 | for arg in args: |
| 181 | arg_name, arg_value = arg.split('=') |
| 182 | key = arg_name |
| 183 | value = arg_value |
| 184 | if key in ['bw','jitter','max_queue_size']: |
| 185 | value = int(value) |
| 186 | if key in ['loss']: |
| 187 | value = float(value) |
| 188 | link_dict[key] = value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 189 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 190 | links.append(confNDNLink(h1,h2,link_dict)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 191 | |
Alexander Lane | 517d34c | 2018-06-01 12:35:11 -0500 | [diff] [blame] | 192 | elif line == "[links]\n": |
| 193 | linkSectionFlag = True |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 194 | |
Ashlesh Gawande | 27b5e1b | 2018-08-06 17:47:15 -0500 | [diff] [blame] | 195 | return links |