Change OMF attributes during runtime
[nepi.git] / src / nepi / testbeds / omf / execute.py
1 # -*- coding: utf-8 -*-
2
3 from constants import TESTBED_ID, TESTBED_VERSION
4 from nepi.core import testbed_impl
5 from nepi.util.constants import TIME_NOW
6
7 from nepi.testbeds.omf.omf_api import OmfAPI
8
9 import logging
10 import os
11 import time
12
13 class TestbedController(testbed_impl.TestbedController):
14     def __init__(self):
15         super(TestbedController, self).__init__(TESTBED_ID, TESTBED_VERSION)
16         self._home = None
17         self._api = None
18         self._logger = logging.getLogger('nepi.testbeds.omf')
19  
20     def do_setup(self):
21         debug = self._attributes.get_attribute_value("enableDebug")
22         if debug:
23             self._logger.setLevel(logging.DEBUG)
24
25         # create home
26         self._home = self._attributes.get_attribute_value("homeDirectory")
27         home = os.path.normpath(self._home)
28         if not os.path.exists(home):
29             os.makedirs(home, 0755)
30
31         # initialize OMF xmpp client
32         slice = self._attributes.get_attribute_value("xmppSlice")
33         host = self._attributes.get_attribute_value("xmppHost")
34         port = self._attributes.get_attribute_value("xmppPort")
35         password = self._attributes.get_attribute_value("xmppPassword")
36
37         self._api = OmfAPI(slice, host, port, password, debug)
38  
39         super(TestbedController, self).do_setup()
40
41     @property
42     def api(self):
43         return self._api
44
45     def set(self, guid, name, value, time = TIME_NOW):
46         super(TestbedController, self).set(guid, name, value, time)
47         element = self._elements[guid]
48         if element:
49             try:
50                 setattr(element, name, value)
51             except:
52                 # We ignore these errors while recovering.
53                 # Some attributes are immutable, and setting
54                 # them is necessary (to recover the state), but
55                 # some are not (they throw an exception).
56                 if not self.recovering:
57                     raise
58
59     def get(self, guid, name, time = TIME_NOW):
60         value = super(TestbedController, self).get(guid, name, time)
61         element = self._elements.get(guid)
62         try:
63             return getattr(element, name)
64         except (KeyError, AttributeError):
65             return value
66
67     def shutdown(self):
68         if self.api: 
69             self.api.disconnect()
70