Added all common modules in conductor directory
[optf/has.git] / conductor / conductor / messaging.py
1 #
2 # -------------------------------------------------------------------------
3 #   Copyright (c) 2015-2017 AT&T Intellectual Property
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 #
19
20 from oslo_config import cfg
21
22 from conductor.common import music
23 from conductor.common.music.messaging import component
24
25 DEFAULT_URL = "__default__"
26 TRANSPORTS = {}
27
28 CONF = cfg.CONF
29
30 # Pull in messaging server opts. We use them here.
31 MESSAGING_SERVER_OPTS = component.MESSAGING_SERVER_OPTS
32 CONF.register_opts(MESSAGING_SERVER_OPTS, group='messaging_server')
33
34
35 def setup():
36     """Messaging setup, if any"""
37     # oslo_messaging.set_transport_defaults('conductor')
38     pass
39
40
41 # TODO(jdandrea): Remove Music-specific aspects (keyspace -> namespace?)
42 # TODO(jdandrea): Make Music an oslo rpc backend (difficulty level: high?)
43 def get_transport(conf, url=None, optional=False, cache=True):
44     """Initialise the Music messaging layer."""
45     global TRANSPORTS
46     cache_key = url or DEFAULT_URL
47     transport = TRANSPORTS.get(cache_key)
48
49     if not transport or not cache:
50         try:
51             # "Somebody set up us the API." ;)
52             # Yes, we know an API is not a transport. Cognitive dissonance FTW!
53             # TODO(jdandrea): try/except to catch problems
54             keyspace = conf.messaging_server.keyspace
55             transport = music.api.API()
56             transport.keyspace_create(keyspace=keyspace)
57         except Exception:
58             if not optional or url:
59                 # NOTE(sileht): oslo_messaging is configured but unloadable
60                 # so reraise the exception
61                 raise
62             return None
63         else:
64             if cache:
65                 TRANSPORTS[cache_key] = transport
66     return transport
67
68
69 def cleanup():
70     """Cleanup the Music messaging layer."""
71     global TRANSPORTS
72     for url in TRANSPORTS:
73         del TRANSPORTS[url]