source: fedd/federation/experiment_info.py @ 6e33086

compt_changesinfo-ops
Last change on this file since 6e33086 was 6e33086, checked in by Ted Faber <faber@…>, 13 years ago

InfoSegment? to emulab access controllers

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#!/usr/local/bin/python
2
3import copy
4from datetime import datetime, timedelta
5from numbers import Number
6
7class allocation_info:
8    """
9    An allocation identified by allocationID.  It includes the local name
10    of the testbed on which it was allocated and the uri on which to
11    contact it.  In addition, the proofs of access are included.
12    """
13    def __init__(self, allocID, tb, uri, proof=None):
14        self.allocID = allocID
15        self.tb = tb
16        self.uri = uri
17        self.proof = proof
18        self.attrs = { }
19
20    def set_attribute(self, a, v):
21        a = a.lower()
22        if a not in self.attrs: self.attrs[a] = v
23
24    def get_attribute(self, a, default=None):
25        a = a.lower()
26        return self.attrs.get(a, default)
27
28class experiment_info:
29    """
30    Information about an experiment in the eperiment controller.  It includes
31    the fedid and localnames, the identity the experiment acts as, current
32    status, topology allocation logs and the allocations themselves.
33    """
34    def __init__(self, fedid, localname, identity=None):
35        self.fedid = fedid
36        self.localname = localname
37        self.identity = identity
38        self.top = None
39        self.status = 'empty'
40        self.log = []
41        self.alloc = { }
42        self.last_update = datetime.now()
43
44    def add_allocation(self, a):
45        self.alloc[a.tb] = a
46
47    def get_allocation(self, tb):
48        return self.alloc.get(tb,None)
49
50    def get_all_allocations(self):
51        return self.alloc.values()
52
53    def updated(self):
54        self.last_update = datetime.now()
55
56    def older_than(self, secs=None, dt=None):
57        """
58        If the last update of this info was more than secs seconds ago, or
59        before dt (a datetime), return True.  If both secs and dt or neither is
60        given return False.  If the last update time is completelt unknown
61        (which should never happen) return True.
62        """
63        if self.last_update is None:
64            return True
65        elif dt is None and isinstance(secs, Number): 
66            return self.last_update + timedelta(seconds=secs) < datetime.now()
67        elif secs is None and isinstance(dt, datetime):
68            return self.last_update < dt
69        else:
70            return False
71
72
73    def get_info(self):
74        """
75        Return the information as a dict suitable for a soap return value.
76        This is an infoResponseType in fedd_types.xsd.
77        """
78        # The state may be massaged by the service function that called
79        # get_info (e.g., encoded for XMLRPC transport) so copy structured
80        # fields.
81        rv = {
82                'experimentID': [
83                    { 'fedid': copy.deepcopy(self.fedid) },
84                    {'localname': self.localname },
85                ],
86                'experimentStatus': self.status,
87            }
88        if self.log:
89            rv['allocationLog'] = "".join(self.log)
90        if self.identity is not None:
91            rv['experimentAccess'] = {'X509': copy.deepcopy(self.identity)}
92        if self.top is not None:
93            rv['experimentdescription'] = \
94                { 'topdldescription': self.top.to_dict() }
95        return rv
96
Note: See TracBrowser for help on using the repository browser.