Revert "Full Juju Charm support"
[osm/SO.git] / rwlaunchpad / plugins / rwvns / vala / rwsdn-python / rwsdn-plugin.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import logging
19
20 import gi
21 gi.require_version('RwTypes', '1.0')
22 gi.require_version('RwSdn', '1.0')
23 from gi.repository import (
24 GObject,
25 RwSdn, # Vala package
26 RwTypes)
27
28 import rw_status
29 import rwlogger
30
31 import rift.cal
32 import rift.sdn
33
34 logger = logging.getLogger('rwsdn')
35
36 rwstatus = rw_status.rwstatus_from_exc_map({
37 IndexError: RwTypes.RwStatus.NOTFOUND,
38 KeyError: RwTypes.RwStatus.NOTFOUND,
39
40 })
41
42
43 class TopologyPlugin(GObject.Object, RwSdn.Topology):
44 def __init__(self):
45 GObject.Object.__init__(self)
46 self._impl = None
47
48 @rwstatus
49 def do_init(self, rwlog_ctx):
50 providers = {
51 "sdnsim": rift.sdn.SdnSim,
52 "mock": rift.sdn.Mock,
53 }
54
55 logger.addHandler(
56 rwlogger.RwLogger(
57 subcategory="rwsdn",
58 log_hdl=rwlog_ctx,
59 )
60 )
61
62 self._impl = {}
63 for name, impl in providers.items():
64 try:
65 self._impl[name] = impl()
66
67 except Exception:
68 msg = "unable to load SDN implementation for {}"
69 logger.exception(msg.format(name))
70
71 @rwstatus
72 def do_get_network_list(self, account, network_top):
73 obj = self._impl[account.account_type]
74 return obj.get_network_list(account, network_top)
75
76 def main():
77 @rwstatus
78 def blah():
79 raise IndexError()
80
81 a = blah()
82 assert(a == RwTypes.RwStatus.NOTFOUND)
83
84 @rwstatus({IndexError: RwTypes.RwStatus.NOTCONNECTED})
85 def blah2():
86 """Some function"""
87 raise IndexError()
88
89 a = blah2()
90 assert(a == RwTypes.RwStatus.NOTCONNECTED)
91 assert(blah2.__doc__ == "Some function")
92
93 if __name__ == '__main__':
94 main()
95