<commit_before># Copyright 2015 Intel Corporation
#
# 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.

"""
Thread Listener of the Openstack notifications
"""

__author__ = 'gpetralia'

from threading import Thread
import pika
import json
from monitoring_service.epa_database.openstack.nova_handler import NovaHandler
from monitoring_service.epa_database.openstack.neutron_handler import FloatingIpHandler
from monitoring_service.epa_database.openstack.neutron_handler import PortHandler
from monitoring_service.epa_database.openstack.neutron_handler import RouterHandler
from monitoring_service.epa_database.openstack.neutron_handler import NetworkHandler
from monitoring_service.epa_database.openstack.cinder_handler import VolumeHandler
from monitoring_service.epa_database.openstack.cinder_handler import SnapshotHandler
from monitoring_service.epa_database.openstack.heat_handler import OrchestrationHandler

handlers_mapping = {
    'compute': NovaHandler,
    'floatingip': FloatingIpHandler,
    'port': PortHandler,
    'router': RouterHandler,
    'network': NetworkHandler,
    'volume': VolumeHandler,
    'snapshot': SnapshotHandler,
    'orchestration': OrchestrationHandler
}


class NotificationsConsumer(Thread):
    """
    Class that creates and runs the notifications consumer
    """
    def __init__(self, rb_username,
                 rb_password,
                 rb_host,
                 rb_port,
                 config,
                 graph_db,
                 notification_queue='notifications.info'):
        """
        NotificationConsumer constructor
        :param rb_username: Openstack RabbitMQ username
        :param rb_password: Openstack RabbitMQ password
        :param rb_host: Openstack RabbitMQ password
        :param rb_port: Openstack RabbitMQ port
        :param config: Epa Controller config file path
        :param graph_db: Instance of Neo4j Epa DB
        :param notification_queue: Openstack RabbitMQ notification queue
        """

        self.delay = 1
        super(NotificationsConsumer, self).__init__()
        self.conn_string = 'amqp://' + \
                           rb_username + ':' \
                           + rb_password + '@' \
                           + rb_host + ':' \
                           + rb_port + '/%2F'

        self.queue = notification_queue
        self.handlers = dict()
        self.before = 0
        self.after = 0

        # Register events' handlers
        for handler in handlers_mapping:
            self.handlers[handler] = handlers_mapping[handler](config, graph_db)

    def consume_notifications(self):
        """
        Start the listener of the notification queue

        """
        queue_name = self.queue
        parameters = pika.URLParameters(self.conn_string)
        connection = pika.BlockingConnection(parameters)

        channel = connection.channel()
        channel.queue_declare(queue=queue_name)
        print ' [*] Waiting for OpenStack messages.'
        channel.basic_consume(self.callback,
                              queue=queue_name,
                              no_ack=True)

        channel.start_consuming()

    def callback(self, ch, method, properties, body):
        """
        Callback called every time a message is received<commit_msg>Replace notification queue with exchanges<commit_after># Copyright 2015 Intel Corporation
#
# 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.

"""
Thread Listener of the Openstack notifications
"""

__author__ = 'gpetralia'

from threading import Thread
import pika
import json
from monitoring_service.epa_database.openstack.nova_handler import NovaHandler
from monitoring_service.epa_database.openstack.neutron_handler import FloatingIpHandler
from monitoring_service.epa_database.openstack.neutron_handler import PortHandler
from monitoring_service.epa_database.openstack.neutron_handler import RouterHandler
from monitoring_service.epa_database.openstack.neutron_handler import NetworkHandler
from monitoring_service.epa_database.openstack.cinder_handler import VolumeHandler
from monitoring_service.epa_database.openstack.cinder_handler import SnapshotHandler
from monitoring_service.epa_database.openstack.heat_handler import OrchestrationHandler

handlers_mapping = {
    'compute': NovaHandler,
    'floatingip': FloatingIpHandler,
    'port': PortHandler,
    'router': RouterHandler,
    'network': NetworkHandler,
    'volume': VolumeHandler,
    'snapshot': SnapshotHandler,
    'orchestration': OrchestrationHandler
}


class NotificationsConsumer(Thread):
    """
    Class that creates and runs the notifications consumer
    """
    def __init__(self, rb_username,
                 rb_password,
                 rb_host,
                 rb_port,
                 config,
                 graph_db,
                 notification_queue='notifications.info'):
        """
        NotificationConsumer constructor
        :param rb_username: Openstack RabbitMQ username
        :param rb_password: Openstack RabbitMQ password
        :param rb_host: Openstack RabbitMQ password
        :param rb_port: Openstack RabbitMQ port
        :param config: Epa Controller config file path
        :param graph_db: Instance of Neo4j Epa DB
        :param notification_queue: Openstack RabbitMQ notification queue
        """

        self.delay = 1
        super(NotificationsConsumer, self).__init__()
        self.conn_string = 'amqp://' + \
                           rb_username + ':' \
                           + rb_password + '@' \
                           + rb_host + ':' \
                           + rb_port + '/%2F'

        self.queue = notification_queue
        self.handlers = dict()
        self.before = 0
        self.after = 0

        # Register events' handlers
        for handler in handlers_mapping:
            self.handlers[handler] = handlers_mapping[handler](config, graph_db)

    def consume_notifications(self):
        """
        Start the listener of the notification queue

        """
        parameters = pika.URLParameters(self.conn_string)
        connection = pika.BlockingConnection(parameters)

        channel = connection.channel()
        channel.exchange_declare(exchange='nova', type='topic')
        result = channel.queue_declare(exclusive=True)
        queue_name = result.method.queue
        routing_key='notifications.info'
        exchanges = ['ceilometer', 'cinder', 'glance', 'heat', 'nova', 'neutron']
        self.bind_queue(channel, queue_name, exchanges, routing_key)
        channel.basic_consume(self.callback,
                              queue=queue_name,
                              no_ack=True)

        channel.start_consuming()
        print ' [*] Waiting for OpenStack messages.'

    def bind_queue(self, channel, queue_name, exchanges, routing_key):
        """
        Bind list of exchanges to the given queue for the given routing queue

        :param channel: AMQP channel
        :param queue_name: exclusive queue to collect Openstack notifications
        :param exchanges: list of exchanges
        :param routing_key: Routing key
        """
        for exchange in exchanges:
            channel.exchange_declare(exchange=exchange, type='topic')

        channel.queue_bind(exchange=exchange, queue=queue_name, routing_key=routing_key)

    def callback(self, ch, method, properties, body):
        """
        Callback called every time a message is received
{'fraction_non_alphanumeric': 0.07291295570597316, 'fraction_numerical': 0.0030875192969956064, 'mean_word_length': 2.803974706413731, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5097954', 'n_tokens_mistral': 2202, 'n_tokens_neox': 2100, 'n_words': 625}