Vince Lehman | b8b1806 | 2015-07-14 13:07:22 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2015 The University of Memphis, |
| 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 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 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 89 | class confNDNLink(): |
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 __init__(self,h1,h2,linkDict=None): |
| 92 | self.h1 = h1 |
| 93 | self.h2 = h2 |
| 94 | self.linkDict = linkDict |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 95 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 96 | def __repr__(self): |
| 97 | return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 98 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 99 | def parse_hosts(conf_arq): |
| 100 | 'Parse hosts section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 101 | config = ConfigParser.RawConfigParser() |
| 102 | config.read(conf_arq) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 103 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 104 | hosts = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 105 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 106 | items = config.items('nodes') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 107 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 108 | #makes a first-pass read to hosts section to find empty host sections |
| 109 | for item in items: |
| 110 | name = item[0] |
| 111 | rest = item[1].split() |
| 112 | if len(rest) == 0: |
| 113 | config.set('nodes', name, '_') |
| 114 | #updates 'items' list |
| 115 | items = config.items('nodes') |
| 116 | |
| 117 | #makes a second-pass read to hosts section to properly add hosts |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 118 | for item in items: |
| 119 | |
| 120 | name = item[0] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 121 | |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 122 | rest = shlex.split(item[1]) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 123 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 124 | uris = rest |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 125 | params = {} |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 126 | cpu = None |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 127 | cores = None |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 128 | cache = None |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 129 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 130 | for uri in uris: |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 131 | if re.match("cpu",uri): |
| 132 | cpu = float(uri.split('=')[1]) |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 133 | elif re.match("cores",uri): |
| 134 | cores = uri.split('=')[1] |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 135 | elif re.match("cache",uri): |
| 136 | cache = uri.split('=')[1] |
| 137 | elif re.match("mem",uri): |
| 138 | mem = uri.split('=')[1] |
Ashlesh Gawande | 557cb84 | 2015-07-01 15:39:44 -0500 | [diff] [blame] | 139 | elif re.match("app",uri): |
| 140 | app = uri.split('=')[1] |
| 141 | elif re.match("_", uri): |
| 142 | app = "" |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 143 | else: |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 144 | params[uri.split('=')[0]] = uri.split('=')[1] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 145 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 146 | hosts.append(confNDNHost(name, app, params, cpu, cores, cache)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 147 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 148 | return hosts |
| 149 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 150 | def parse_links(conf_arq): |
| 151 | 'Parse links section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 152 | arq = open(conf_arq,'r') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 153 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 154 | links = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 155 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 156 | while True: |
| 157 | line = arq.readline() |
| 158 | if line == '[links]\n': |
| 159 | break |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 160 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 161 | while True: |
| 162 | line = arq.readline() |
| 163 | if line == '': |
| 164 | break |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 165 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 166 | args = line.split() |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 167 | |
| 168 | #checks for non-empty line |
| 169 | if len(args) == 0: |
| 170 | continue |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 171 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 172 | h1, h2 = args.pop(0).split(':') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 173 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 174 | link_dict = {} |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 175 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 176 | for arg in args: |
| 177 | arg_name, arg_value = arg.split('=') |
| 178 | key = arg_name |
| 179 | value = arg_value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 180 | if key in ['bw','jitter','max_queue_size']: |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 181 | value = int(value) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 182 | if key in ['loss']: |
| 183 | value = float(value) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 184 | link_dict[key] = value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 185 | |
ashu | ef3490b | 2015-02-17 11:01:04 -0600 | [diff] [blame] | 186 | links.append(confNDNLink(h1,h2,link_dict)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 187 | |
| 188 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 189 | return links |