Added routes to OMF nodes
[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         xmpp_root = self._attributes.get_attribute_value("xmppRoot")
38         self._api = OmfAPI(slice, host, port, password, xmpp_root)
39  
40         super(TestbedController, self).do_setup()
41
42     @property
43     def api(self):
44         return self._api
45
46     def set(self, guid, name, value, time = TIME_NOW):
47         super(TestbedController, self).set(guid, name, value, time)
48         element = self._elements[guid]
49         if element:
50             try:
51                 setattr(element, name, value)
52             except:
53                 # We ignore these errors while recovering.
54                 # Some attributes are immutable, and setting
55                 # them is necessary (to recover the state), but
56                 # some are not (they throw an exception).
57                 if not self.recovering:
58                     raise
59
60     def get(self, guid, name, time = TIME_NOW):
61         value = super(TestbedController, self).get(guid, name, time)
62         element = self._elements.get(guid)
63         try:
64             return getattr(element, name)
65         except (KeyError, AttributeError):
66             return value
67
68     def shutdown(self):
69         if self.api: 
70             self.api.disconnect()
71