Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
Ashlesh Gawande | da475f0 | 2017-03-01 17:20:58 -0600 | [diff] [blame] | 3 | # Copyright (C) 2015-2017, 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 |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 69 | self.uri_tuples = params |
Ashlesh Gawande | 3a4afb1 | 2015-07-09 09:23:30 -0500 | [diff] [blame] | 70 | self.params = params |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 71 | self.cpu = cpu |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 72 | self.cores = cores |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 73 | self.cache = cache |
| 74 | |
| 75 | # For now assume leftovers are NLSR configuration parameters |
| 76 | self.nlsrParameters = params |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 77 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 78 | def __repr__(self): |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 79 | 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 |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 88 | |
Vince Lehman | fbd47c9 | 2015-10-14 16:00:06 -0500 | [diff] [blame] | 89 | class confNdnSwitch: |
| 90 | def __init__(self, name): |
| 91 | self.name = name |
| 92 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 93 | class confNDNLink(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 94 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 95 | def __init__(self,h1,h2,linkDict=None): |
| 96 | self.h1 = h1 |
| 97 | self.h2 = h2 |
| 98 | self.linkDict = linkDict |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 99 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 100 | def __repr__(self): |
| 101 | return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 102 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 103 | def parse_hosts(conf_arq): |
| 104 | 'Parse hosts section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 105 | config = ConfigParser.RawConfigParser() |
| 106 | config.read(conf_arq) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 107 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 108 | hosts = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 109 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 110 | items = config.items('nodes') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 111 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 112 | #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 |
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.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [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 = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 177 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 178 | while True: |
| 179 | line = arq.readline() |
| 180 | if line == '[links]\n': |
| 181 | break |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 182 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 183 | while True: |
| 184 | line = arq.readline() |
| 185 | if line == '': |
| 186 | break |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 187 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 188 | args = line.split() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 189 | |
| 190 | #checks for non-empty line |
| 191 | if len(args) == 0: |
| 192 | continue |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 193 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 194 | h1, h2 = args.pop(0).split(':') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 195 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 196 | link_dict = {} |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 197 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 198 | for arg in args: |
| 199 | arg_name, arg_value = arg.split('=') |
| 200 | key = arg_name |
| 201 | value = arg_value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 202 | if key in ['bw','jitter','max_queue_size']: |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 203 | value = int(value) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 204 | if key in ['loss']: |
| 205 | value = float(value) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 206 | link_dict[key] = value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 207 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 208 | links.append(confNDNLink(h1,h2,link_dict)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 209 | |
| 210 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 211 | return links |