Changeset 5f6c3af
- Timestamp:
- May 17, 2010 3:10:41 AM (15 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master, version-3.01, version-3.02
- Children:
- eab6ae1
- Parents:
- c7a982b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/compose.py
rc7a982b r5f6c3af 5 5 import os 6 6 import random 7 import copy 7 8 8 9 from optparse import OptionParser, OptionValueError 9 10 from federation.remote_service import service_caller 11 from federation.service_error import service_error 10 12 from federation import topdl 11 13 … … 22 24 return "%s:%s:%s:%s" % (self.name, self.required, 23 25 ",".join(self.provides), ",".join(self.accepts)) 24 25 26 def add_to_map(exp, mark, provides, accepts):27 """28 Create a constraint from a string of the form29 name:required:provides:accepts30 where name is the name of the node in the current topology, if the second31 field is "required" this is a required constraint, a list of attributes32 that this connection point provides and a list that it accepts. The33 provides and accepts lists are comma-separated. Note that the string34 passed in as exp should have whitespace removed. The constraint is added to35 the mark dict under the name key and entries in the provides and accepts36 are made to teh constraing for each attribute that it provides or accepts.37 """38 nn, r, p, a = exp.split(":")39 p = p.split(",")40 a = a.split(",")41 c = constraint(name=nn, required=(r == 'required'), provides=p, accepts=a)42 mark[nn] = c43 44 for attr, dict in ((p, provides), (a, accepts)):45 for a in attr:46 if a not in dict: dict[a] = [c]47 else: dict[a].append(c)48 26 49 27 def make_new_name(names, prefix="name"): … … 169 147 req = { 'description' : { 'ns2description': desc }, } 170 148 171 try: 172 r = service_caller('Ns2Topdl')(uri, req, cert) 173 except: 174 return None 149 r = service_caller('Ns2Topdl')(uri, req, cert) 175 150 176 151 if r.has_key('Ns2TopdlResponseBody'): … … 231 206 top.incorporate_elements() 232 207 233 def import_ tcl_constraints(marks, provides, accepts,contents):208 def import_ns2_constraints(contents): 234 209 """ 235 210 Contents is a list containing the lines of an annotated ns2 file. This … … 237 212 constraints in the namespace of the tcl experiment, as well as inserting 238 213 them in the accepts and provides indices. 239 """ 214 215 Constraints are given in lines of the form 216 name:required:provides:accepts 217 where name is the name of the node in the current topology, if the second 218 field is "required" this is a required constraint, a list of attributes 219 that this connection point provides and a list that it accepts. The 220 provides and accepts lists are comma-separated. The constraint is added to 221 the marks dict under the name key and that dict is returned. 222 """ 223 224 marks = { } 240 225 for l in contents: 241 226 m = const_re.search(l) 242 227 if m: 243 add_to_map(re.sub('\s', '', m.group(1)), marks, provides, 244 accepts) 245 228 exp = re.sub('\s', '', m.group(1)) 229 nn, r, p, a = exp.split(":") 230 p = p.split(",") 231 a = a.split(",") 232 c = constraint(name=nn, required=(r == 'required'), 233 provides=p, accepts=a) 234 marks[nn] = c 235 return marks 236 237 def import_ns2_component(fn): 238 """ 239 Pull a composition component in from an ns2 description. The Constraints 240 are parsed from the comments using import_ns2_constraints and the topology 241 is created using a call to a fedd exporting the Ns2Topdl function. A 242 topdl.Topology object rempresenting the component's topology and a dict 243 mapping constraints from the components names to the conttraints is 244 returned. If either the file read or the conversion fails, appropriate 245 Exceptions are thrown. 246 """ 247 f = open(fn, "r") 248 contents = [ l for l in f ] 249 250 marks = import_ns2_constraints(contents) 251 top = remote_ns2topdl(opts.url, "".join(contents), cert) 252 if not top: 253 raise RuntimeError("Cannot create topology from: %s" % fn) 254 255 return (top, marks) 256 257 def index_constraints(constraints, provides, accepts): 258 """ 259 Add constraints to the provides and accepts indices based on what the 260 attributes of the contstraints. 261 """ 262 for c in constraints: 263 for attr, dict in ((c.provides, provides), (c.accepts, accepts)): 264 for a in attr: 265 if a not in dict: dict[a] = [c] 266 else: dict[a].append(c) 246 267 247 268 def multi_callback(option, opt_str, value, parser): … … 299 320 accepts = { } 300 321 for fn, cnt in files: 301 marks = { }302 322 try: 303 f = open(fn, "r") 304 contents = [ l for l in f ] 323 top, marks = import_ns2_component(fn) 324 except service_error, e: 325 print >>sys.stderr, "Remote error on %s: %s" % (fn, e) 326 continue 305 327 except EnvironmentError, e: 306 328 print >>sys.stderr, "Error on %s: %s" % (fn, e) 307 329 continue 308 330 309 top = remote_ns2topdl(opts.url, "".join(contents), cert)310 if not top:311 sys.exit("Cannot create topology from: %s" % fn)312 313 331 for i in range(0, cnt): 314 332 t = top.clone() 315 import_tcl_constraints(marks, provides, accepts, contents) 316 add_interfaces(t, marks) 317 localize_names(t, names, marks) 333 m = copy.deepcopy(marks) 334 index_constraints(m.values(), provides, accepts) 335 add_interfaces(t, m) 336 localize_names(t, names, m) 318 337 t.incorporate_elements() 319 constraints.extend(m arks.values())338 constraints.extend(m.values()) 320 339 321 340 # Let the user know if they messed up on specifying constraints.
Note: See TracChangeset
for help on using the changeset viewer.