initial version
Change-Id: I699e0ab082657880998d8618fe29eb7f56c6c661
This commit is contained in:
0
watcher/common/messaging/__init__.py
Normal file
0
watcher/common/messaging/__init__.py
Normal file
0
watcher/common/messaging/events/__init__.py
Normal file
0
watcher/common/messaging/events/__init__.py
Normal file
48
watcher/common/messaging/events/event.py
Normal file
48
watcher/common/messaging/events/event.py
Normal file
@@ -0,0 +1,48 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
class Event(object):
|
||||
"""Generic event to use with EventDispatcher"""
|
||||
|
||||
def __init__(self, event_type=None, data=None, request_id=None):
|
||||
"""Default constructor
|
||||
|
||||
:param event_type: the type of the event
|
||||
:param data: a dictionary which contains data
|
||||
:param request_id: a string which represent the uuid of the request
|
||||
"""
|
||||
self._type = event_type
|
||||
self._data = data
|
||||
self._request_id = request_id
|
||||
|
||||
def get_type(self):
|
||||
return self._type
|
||||
|
||||
def set_type(self, type):
|
||||
self._type = type
|
||||
|
||||
def get_data(self):
|
||||
return self._data
|
||||
|
||||
def set_data(self, data):
|
||||
self._data = data
|
||||
|
||||
def set_request_id(self, id):
|
||||
self._request_id = id
|
||||
|
||||
def get_request_id(self):
|
||||
return self._request_id
|
||||
78
watcher/common/messaging/events/event_dispatcher.py
Normal file
78
watcher/common/messaging/events/event_dispatcher.py
Normal file
@@ -0,0 +1,78 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from watcher.decision_engine.framework.messaging.events import Events
|
||||
from watcher.openstack.common import log
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
class EventDispatcher(object):
|
||||
"""Generic event dispatcher which listen and dispatch events"""
|
||||
|
||||
def __init__(self):
|
||||
self._events = dict()
|
||||
|
||||
def __del__(self):
|
||||
self._events = None
|
||||
|
||||
def has_listener(self, event_type, listener):
|
||||
"""Return true if listener is register to event_type """
|
||||
# Check for event type and for the listener
|
||||
if event_type in self._events.keys():
|
||||
return listener in self._events[event_type]
|
||||
else:
|
||||
return False
|
||||
|
||||
def dispatch_event(self, event):
|
||||
LOG.debug("dispatch evt : %s" % str(event.get_type()))
|
||||
"""
|
||||
Dispatch an instance of Event class
|
||||
"""
|
||||
if Events.ALL in self._events.keys():
|
||||
listeners = self._events[Events.ALL]
|
||||
for listener in listeners:
|
||||
listener(event)
|
||||
|
||||
# Dispatch the event to all the associated listeners
|
||||
if event.get_type() in self._events.keys():
|
||||
listeners = self._events[event.get_type()]
|
||||
for listener in listeners:
|
||||
listener(event)
|
||||
|
||||
def add_event_listener(self, event_type, listener):
|
||||
"""Add an event listener for an event type"""
|
||||
# Add listener to the event type
|
||||
if not self.has_listener(event_type, listener):
|
||||
listeners = self._events.get(event_type, [])
|
||||
listeners.append(listener)
|
||||
self._events[event_type] = listeners
|
||||
|
||||
def remove_event_listener(self, event_type, listener):
|
||||
"""Remove event listener. """
|
||||
# Remove the listener from the event type
|
||||
if self.has_listener(event_type, listener):
|
||||
listeners = self._events[event_type]
|
||||
|
||||
if len(listeners) == 1:
|
||||
# Only this listener remains so remove the key
|
||||
del self._events[event_type]
|
||||
|
||||
else:
|
||||
# Update listeners chain
|
||||
listeners.remove(listener)
|
||||
self._events[event_type] = listeners
|
||||
109
watcher/common/messaging/messaging_core.py
Normal file
109
watcher/common/messaging/messaging_core.py
Normal file
@@ -0,0 +1,109 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from oslo_config import cfg
|
||||
|
||||
from watcher.common.messaging.events.event_dispatcher import \
|
||||
EventDispatcher
|
||||
from watcher.common.messaging.messaging_handler import \
|
||||
MessagingHandler
|
||||
from watcher.common.rpc import RequestContextSerializer
|
||||
|
||||
from watcher.objects.base import WatcherObjectSerializer
|
||||
from watcher.openstack.common import log
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
|
||||
WATCHER_MESSAGING_OPTS = [
|
||||
cfg.StrOpt('notifier_driver',
|
||||
default='messaging', help='The name of the driver used by'
|
||||
' oslo messaging'),
|
||||
cfg.StrOpt('executor',
|
||||
default='eventlet', help='The name of a message executor, for'
|
||||
'example: eventlet, blocking'),
|
||||
cfg.StrOpt('protocol',
|
||||
default='rabbit', help='The protocol used by the message'
|
||||
' broker, for example rabbit'),
|
||||
cfg.StrOpt('user',
|
||||
default='guest', help='The username used by the message '
|
||||
'broker'),
|
||||
cfg.StrOpt('password',
|
||||
default='guest', help='The password of user used by the '
|
||||
'message broker'),
|
||||
cfg.StrOpt('host',
|
||||
default='localhost', help='The host where the message broker'
|
||||
'is installed'),
|
||||
cfg.StrOpt('port',
|
||||
default='5672', help='The port used bythe message broker'),
|
||||
cfg.StrOpt('virtual_host',
|
||||
default='', help='The virtual host used by the message '
|
||||
'broker')
|
||||
]
|
||||
|
||||
CONF = cfg.CONF
|
||||
opt_group = cfg.OptGroup(name='watcher_messaging',
|
||||
title='Options for the messaging core')
|
||||
CONF.register_group(opt_group)
|
||||
CONF.register_opts(WATCHER_MESSAGING_OPTS, opt_group)
|
||||
|
||||
|
||||
class MessagingCore(EventDispatcher):
|
||||
API_VERSION = '1.0'
|
||||
|
||||
def __init__(self, publisher_id, topic_control, topic_status):
|
||||
EventDispatcher.__init__(self)
|
||||
self.serializer = RequestContextSerializer(WatcherObjectSerializer())
|
||||
self.publisher_id = publisher_id
|
||||
self.topic_control = self.build_topic(topic_control)
|
||||
self.topic_status = self.build_topic(topic_status)
|
||||
|
||||
def build_topic(self, topic_name):
|
||||
return MessagingHandler(self.publisher_id, topic_name, self,
|
||||
self.API_VERSION, self.serializer)
|
||||
|
||||
def connect(self):
|
||||
LOG.debug("connecting to rabbitMQ broker")
|
||||
self.topic_control.start()
|
||||
self.topic_status.start()
|
||||
|
||||
def disconnect(self):
|
||||
LOG.debug("Disconnect to rabbitMQ broker")
|
||||
self.topic_control.stop()
|
||||
self.topic_status.stop()
|
||||
|
||||
def publish_control(self, event, payload):
|
||||
return self.topic_control.publish_event(event, payload)
|
||||
|
||||
def publish_status(self, event, payload, request_id=None):
|
||||
return self.topic_status.publish_event(event, payload, request_id)
|
||||
|
||||
def get_version(self):
|
||||
return self.API_VERSION
|
||||
|
||||
def check_api_version(self, context):
|
||||
api_manager_version = self.client.call(
|
||||
context.to_dict(), 'check_api_version',
|
||||
api_version=self.API_VERSION)
|
||||
return api_manager_version
|
||||
|
||||
def response(self, evt, ctx, message):
|
||||
payload = {
|
||||
'request_id': ctx['request_id'],
|
||||
'msg': message
|
||||
}
|
||||
self.publish_status(evt, payload)
|
||||
107
watcher/common/messaging/messaging_handler.py
Normal file
107
watcher/common/messaging/messaging_handler.py
Normal file
@@ -0,0 +1,107 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import eventlet
|
||||
from oslo_config import cfg
|
||||
import oslo_messaging as om
|
||||
from threading import Thread
|
||||
from watcher.common.messaging.utils.transport_url_builder import \
|
||||
TransportUrlBuilder
|
||||
from watcher.common.rpc import JsonPayloadSerializer
|
||||
from watcher.common.rpc import RequestContextSerializer
|
||||
from watcher.openstack.common import log
|
||||
|
||||
eventlet.monkey_patch()
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class MessagingHandler(Thread):
|
||||
def __init__(self, publisher_id, topic_watcher, endpoint, version,
|
||||
serializer=None):
|
||||
Thread.__init__(self)
|
||||
self.__server = None
|
||||
self.__notifier = None
|
||||
self.__endpoints = []
|
||||
self.__topics = []
|
||||
self._publisher_id = publisher_id
|
||||
self._topic_watcher = topic_watcher
|
||||
self.__endpoints.append(endpoint)
|
||||
self.__version = version
|
||||
self.__serializer = serializer
|
||||
|
||||
def add_endpoint(self, endpoint):
|
||||
self.__endpoints.append(endpoint)
|
||||
|
||||
def remove_endpoint(self, endpoint):
|
||||
if endpoint in self.__endpoints:
|
||||
self.__endpoints.remove(endpoint)
|
||||
|
||||
def build_notifier(self):
|
||||
serializer = RequestContextSerializer(JsonPayloadSerializer())
|
||||
return om.Notifier(
|
||||
self.transport,
|
||||
driver=CONF.watcher_messaging.notifier_driver,
|
||||
publisher_id=self._publisher_id,
|
||||
topic=self._topic_watcher,
|
||||
serializer=serializer)
|
||||
|
||||
def build_server(self, targets):
|
||||
|
||||
return om.get_rpc_server(self.transport, targets,
|
||||
self.__endpoints,
|
||||
executor=CONF.
|
||||
watcher_messaging.executor,
|
||||
serializer=self.__serializer)
|
||||
|
||||
def __build_transport_url(self):
|
||||
return TransportUrlBuilder().url
|
||||
|
||||
def __config(self):
|
||||
try:
|
||||
self.transport = om.get_transport(
|
||||
cfg.CONF,
|
||||
url=self.__build_transport_url())
|
||||
self.__notifier = self.build_notifier()
|
||||
if 0 < len(self.__endpoints):
|
||||
targets = om.Target(
|
||||
topic=self._topic_watcher,
|
||||
server=CONF.watcher_messaging.host,
|
||||
version=self.__version)
|
||||
self.__server = self.build_server(targets)
|
||||
else:
|
||||
LOG.warn("you have no defined endpoint, \
|
||||
so you can only publish events")
|
||||
except Exception as e:
|
||||
LOG.error("configure : %s" % str(e.message))
|
||||
|
||||
def run(self):
|
||||
LOG.debug("configure MessagingHandler for %s" % self._topic_watcher)
|
||||
self.__config()
|
||||
if len(self.__endpoints) > 0:
|
||||
LOG.debug("Starting up server")
|
||||
self.__server.start()
|
||||
|
||||
def stop(self):
|
||||
LOG.debug('Stop up server')
|
||||
self.__server.wait()
|
||||
self.__server.stop()
|
||||
|
||||
def publish_event(self, event_type, payload, request_id=None):
|
||||
self.__notifier.info({'version_api': self.__version,
|
||||
'request_id': request_id},
|
||||
{'event_id': event_type}, payload)
|
||||
50
watcher/common/messaging/notification_handler.py
Normal file
50
watcher/common/messaging/notification_handler.py
Normal file
@@ -0,0 +1,50 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import eventlet
|
||||
from oslo import messaging
|
||||
|
||||
from watcher.common.messaging.utils.observable import \
|
||||
Observable
|
||||
from watcher.openstack.common import log
|
||||
|
||||
|
||||
eventlet.monkey_patch()
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
class NotificationHandler(Observable):
|
||||
def __init__(self, publisher_id):
|
||||
Observable.__init__(self)
|
||||
self.publisher_id = publisher_id
|
||||
|
||||
def info(self, ctx, publisher_id, event_type, payload, metadata):
|
||||
if publisher_id == self.publisher_id:
|
||||
self.set_changed()
|
||||
self.notify(ctx, publisher_id, event_type, metadata, payload)
|
||||
return messaging.NotificationResult.HANDLED
|
||||
|
||||
def warn(self, ctx, publisher_id, event_type, payload, metadata):
|
||||
if publisher_id == self.publisher_id:
|
||||
self.set_changed()
|
||||
self.notify(ctx, publisher_id, event_type, metadata, payload)
|
||||
return messaging.NotificationResult.HANDLED
|
||||
|
||||
def error(self, ctx, publisher_id, event_type, payload, metadata):
|
||||
if publisher_id == self.publisher_id:
|
||||
self.set_changed()
|
||||
self.notify(ctx, publisher_id, event_type, metadata, payload)
|
||||
return messaging.NotificationResult.HANDLED
|
||||
0
watcher/common/messaging/utils/__init__.py
Normal file
0
watcher/common/messaging/utils/__init__.py
Normal file
62
watcher/common/messaging/utils/observable.py
Normal file
62
watcher/common/messaging/utils/observable.py
Normal file
@@ -0,0 +1,62 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
|
||||
from watcher.common.messaging.utils.synchronization import \
|
||||
Synchronization
|
||||
from watcher.openstack.common import log
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
class Observable(Synchronization):
|
||||
def __init__(self):
|
||||
self.__observers = []
|
||||
self.changed = 0
|
||||
Synchronization.__init__(self)
|
||||
|
||||
def set_changed(self):
|
||||
self.changed = 1
|
||||
|
||||
def clear_changed(self):
|
||||
self.changed = 0
|
||||
|
||||
def has_changed(self):
|
||||
return self.changed
|
||||
|
||||
def register_observer(self, observer):
|
||||
if observer not in self.__observers:
|
||||
self.__observers.append(observer)
|
||||
|
||||
def unregister_observer(self, observer):
|
||||
try:
|
||||
self.__observers.remove(observer)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def notify(self, ctx=None, publisherid=None, event_type=None,
|
||||
metadata=None, payload=None, modifier=None):
|
||||
self.mutex.acquire()
|
||||
try:
|
||||
if not self.changed:
|
||||
return
|
||||
for observer in self.__observers:
|
||||
if modifier != observer:
|
||||
observer.update(self, ctx, metadata, publisherid,
|
||||
event_type, payload)
|
||||
self.clear_changed()
|
||||
finally:
|
||||
self.mutex.release()
|
||||
22
watcher/common/messaging/utils/synchronization.py
Normal file
22
watcher/common/messaging/utils/synchronization.py
Normal file
@@ -0,0 +1,22 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import threading
|
||||
|
||||
|
||||
class Synchronization(object):
|
||||
def __init__(self):
|
||||
self.mutex = threading.RLock()
|
||||
35
watcher/common/messaging/utils/transport_url_builder.py
Normal file
35
watcher/common/messaging/utils/transport_url_builder.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
# Copyright (c) 2015 b<>com
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
# implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from oslo_config import cfg
|
||||
from watcher.openstack.common import log
|
||||
|
||||
LOG = log.getLogger(__name__)
|
||||
CONF = cfg.CONF
|
||||
|
||||
|
||||
class TransportUrlBuilder(object):
|
||||
|
||||
@property
|
||||
def url(self):
|
||||
return "%s://%s:%s@%s:%s/%s" % (
|
||||
CONF.watcher_messaging.protocol,
|
||||
CONF.watcher_messaging.user,
|
||||
CONF.watcher_messaging.password,
|
||||
CONF.watcher_messaging.host,
|
||||
CONF.watcher_messaging.port,
|
||||
CONF.watcher_messaging.virtual_host
|
||||
)
|
||||
Reference in New Issue
Block a user