"Fossies" - the Fresh Open Source Software Archive

Member "neutron-14.0.3/neutron/services/logapi/rpc/server.py" (22 Oct 2019, 4542 Bytes) of package /linux/misc/openstack/neutron-14.0.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "server.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 14.0.2_vs_15.0.0.

    1 # Copyright (C) 2017 Fujitsu Limited
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 
   16 from neutron_lib.callbacks import resources as r_const
   17 from neutron_lib import rpc as n_rpc
   18 from oslo_log import helpers as log_helpers
   19 from oslo_log import log as logging
   20 import oslo_messaging
   21 
   22 from neutron.api.rpc.callbacks import events
   23 from neutron.api.rpc.handlers import resources_rpc
   24 from neutron.services.logapi.common import constants as log_const
   25 from neutron.services.logapi.common import db_api
   26 
   27 LOG = logging.getLogger(__name__)
   28 
   29 # RPC methods mapping
   30 RPC_RESOURCES_METHOD_MAP = {}
   31 
   32 
   33 # This function must be called when a log_driver is registered.
   34 def register_rpc_methods(resource_type, rpc_methods):
   35     """Register RPC methods.
   36 
   37     :param resource_type: string and must be a valid resource type.
   38     :param rpc_methods: list of RPC methods to be registered.
   39            This param would look like:
   40            [
   41                {'PORT': get_sg_log_info_for_port},
   42                {'LOG_RESOURCE': get_sg_log_info_for_log_resources}
   43            ]
   44     """
   45     if resource_type not in RPC_RESOURCES_METHOD_MAP:
   46         RPC_RESOURCES_METHOD_MAP[resource_type] = rpc_methods
   47 
   48 
   49 def get_rpc_method(resource_type, rpc_method_key):
   50     if resource_type not in RPC_RESOURCES_METHOD_MAP:
   51         raise NotImplementedError()
   52 
   53     for rpc_method in RPC_RESOURCES_METHOD_MAP[resource_type]:
   54         if rpc_method_key in rpc_method.keys():
   55             return list(rpc_method.values())[0]
   56 
   57     raise NotImplementedError()
   58 
   59 
   60 def get_sg_log_info_for_port(context, port_id):
   61     return db_api.get_sg_log_info_for_port(context, port_id)
   62 
   63 
   64 def get_sg_log_info_for_log_resources(context, log_resources):
   65     return db_api.get_sg_log_info_for_log_resources(context, log_resources)
   66 
   67 
   68 class LoggingApiSkeleton(object):
   69     """Skeleton proxy code for agent->server communication."""
   70 
   71     # History
   72     #   1.0 Initial version
   73     #   1.1 Introduce resource_type as a keyword in order to extend
   74     #   support for other resources
   75 
   76     target = oslo_messaging.Target(
   77         version='1.1', namespace=log_const.RPC_NAMESPACE_LOGGING)
   78 
   79     def __init__(self):
   80         self.conn = n_rpc.Connection()
   81         self.conn.create_consumer(log_const.LOGGING_PLUGIN, [self],
   82                                   fanout=False)
   83 
   84     @log_helpers.log_method_call
   85     def get_sg_log_info_for_port(self, context, port_id, **kwargs):
   86         resource_type = kwargs.get('resource_type', log_const.SECURITY_GROUP)
   87         LOG.debug("Logging agent requests log info "
   88                   "for port with resource type %s", resource_type)
   89         rpc_method = get_rpc_method(resource_type, r_const.PORT)
   90         return rpc_method(context, port_id)
   91 
   92     @log_helpers.log_method_call
   93     def get_sg_log_info_for_log_resources(self, context,
   94                                           log_resources, **kwargs):
   95         resource_type = kwargs.get('resource_type', log_const.SECURITY_GROUP)
   96         LOG.debug("Logging agent requests log info "
   97                   "for log resources with resource type %s", resource_type)
   98         rpc_method = get_rpc_method(resource_type, log_const.LOG_RESOURCE)
   99         return rpc_method(context, log_resources)
  100 
  101 
  102 class LoggingApiNotification(object):
  103 
  104     def __init__(self):
  105         self.notification_api = resources_rpc.ResourcesPushRpcApi()
  106 
  107     @log_helpers.log_method_call
  108     def create_log(self, context, log_obj):
  109         self.notification_api.push(context, [log_obj], events.CREATED)
  110 
  111     @log_helpers.log_method_call
  112     def update_log(self, context, log_obj):
  113         self.notification_api.push(context, [log_obj], events.UPDATED)
  114 
  115     @log_helpers.log_method_call
  116     def delete_log(self, context, log_obj):
  117         self.notification_api.push(context, [log_obj], events.DELETED)
  118 
  119     @log_helpers.log_method_call
  120     def resource_update(self, context, log_objs):
  121         """Tell to agent when resources related to log_objects updated"""
  122         self.notification_api.push(context, log_objs, events.UPDATED)