Ejemplo n.º 1
0
    def __init__(self,
                 host=None,
                 port=None,
                 use_ssl=True,
                 auth=None,
                 umf_api_version=UMF_API_VERSION):
        """Creates a new client to some NWA.

        :param host: The host where service resides
        :param port: The port where service resides
        :param use_ssl: True to use SSL, False to use HTTP
        :param: auth: function to generate Authorization header.
        """
        self.host = host
        self.port = port
        self.use_ssl = use_ssl
        self.auth = auth
        self.umf_api_version = umf_api_version

        LOG.info(
            _LI('NWA init: host=%(host)s port=%(port)s use_ssl=%(use_ssl)s '
                'auth=%(auth)s'), {
                    'host': self.host,
                    'port': self.port,
                    'use_ssl': self.use_ssl,
                    'auth': self.auth
                })
        LOG.info(_LI('NWA init: umf api version: %s'), self.umf_api_version)
Ejemplo n.º 2
0
    def _terminate_l2_network(self, context, nwa_data, **kwargs):
        tenant_id = kwargs.get('tenant_id')
        nwa_tenant_id = kwargs.get('nwa_tenant_id')
        nwa_info = kwargs.get('nwa_info')
        network_id = nwa_info['network']['id']

        # port check on segment.
        if check_vlan(network_id, nwa_data):
            raise nwa_exc.AgentProxyException(value=nwa_data)

        # delete vlan
        # raise AgentProxyException if fail
        nwa_data = self._delete_vlan(context, nwa_data=nwa_data, **kwargs)
        # delete vlan end.

        # tenant network check.
        for k in nwa_data:
            if re.match('NW_.*', k):
                raise nwa_exc.AgentProxyException(value=nwa_data)

        LOG.info(_LI("delete_tenant_nw"))
        # raise AgentProxyException if fail
        nwa_data = self._delete_tenant_nw(context, nwa_data=nwa_data, **kwargs)

        # delete tenant
        LOG.info(_LI("delete_tenant"))
        nwa_data = self.proxy_tenant.delete_tenant(context,
                                                   nwa_data=nwa_data,
                                                   **kwargs)
        # delete tenant end.

        # delete nwa_tenant binding.
        LOG.info(_LI("delete_nwa_tenant_binding"))
        return self.nwa_tenant_rpc.delete_nwa_tenant_binding(
            context, tenant_id, nwa_tenant_id)
Ejemplo n.º 3
0
    def create_tenant_rpc_server(self, tid):
        """create_ blocking rpc server

        @param tid: openstack tenant id
        """
        ret = {}

        if tid in self.rpc_servers:
            LOG.warning(
                _LW("already in message queue and server. queue=%s"),
                self.rpc_servers[tid]['topic']
            )
            return {'result': 'FAILED'}

        topic = "%s-%s" % (self.topic, tid)

        target = Target(
            topic=topic, server=cfg.CONF.host, fanout=False)

        assert n_rpc.TRANSPORT is not None
        serializer = n_rpc.RequestContextSerializer(None)

        server = get_rpc_server(
            n_rpc.TRANSPORT, target,
            self.agent_top.endpoints,
            'blocking', serializer
        )

        LOG.debug("RPCServer create: topic=%s", topic)
        if self.greenpool.free() < 1:
            self.greenpool_size += nwa_const.NWA_GREENPOOL_ADD_SIZE
            self.greenpool.resize(self.greenpool_size)
            LOG.info(_LI('RPCServer greenpool resize %s'), self.greenpool_size)

        def server_start():
            while True:
                try:
                    LOG.debug('RPCServer thread %d start %s',
                              (self.greenpool.running(), server))
                    server.start()
                    LOG.debug('RPCServer thread end %s', server)
                    break
                except Exception as e:
                    LOG.exception(_LE('RPCServer thread start failed: %s'), e)

        self.rpc_servers[tid] = {
            'thread': self.greenpool.spawn(server_start),
            'server': server,
            'topic': topic
        }
        eventlet.sleep(0)
        LOG.info(_LI('RPCServer started: %(topic)s server=%(server)s'),
                 {'topic': topic, 'server': server})

        ret['result'] = 'SUCCESS'
        ret['tenant_id'] = tid
        ret['topic'] = topic

        return ret
Ejemplo n.º 4
0
 def _log_rest_request(self, method, url, body):
     name = workflow.NwaWorkflow.name(url)
     body_str = ''
     if isinstance(body, dict):
         body_str = jsonutils.dumps(body, sort_keys=True)
     if name:
         LOG.info(_LI('NWA workflow: %(name)s %(body)s'),
                  {'name': name, 'body': body_str})
     else:
         LOG.info(_LI('NWA %(method)s %(url)s %(body)s'),
                  {'method': method,
                   'url': self._url(url),
                   'body': body_str})
Ejemplo n.º 5
0
    def _delete_nat(self, context, fip):
        if not fip['router_id'] or not fip['fixed_ip_address']:
            LOG.debug('already deleted %s', fip)
            return
        tenant_id = nwa_l3_db.get_tenant_id_by_router(
            context.session, fip['router_id']
        )
        nwa_tenant_id = nwa_com_utils.get_nwa_tenant_id(tenant_id)

        fl_data = {
            'floating_ip_address': fip['floating_ip_address'],
            'fixed_ip_address': fip['fixed_ip_address'],
            'id': fip['id'],
            'device_id': fip['router_id'],
            'floating_network_id': fip['floating_network_id'],
            'tenant_id': fip['tenant_id']
        }
        LOG.info(_LI('delete_nat fl_data=%s'), fl_data)

        proxy = self._get_nwa_proxy(self, tenant_id)
        proxy.delete_nat(
            context, tenant_id=tenant_id,
            nwa_tenant_id=nwa_tenant_id,
            floating=fl_data
        )
Ejemplo n.º 6
0
 def get_tenant_semaphore(cls, tenant_id):
     if not isinstance(tenant_id, six.string_types) or tenant_id == '':
         raise TypeError('%s is not a string' % tenant_id)
     with Semaphore.lock:
         if tenant_id not in Semaphore.tenants:
             LOG.info(_LI('create semaphore for %s'), tenant_id)
             Semaphore.tenants[tenant_id] = Semaphore()
         return Semaphore.tenants[tenant_id]
Ejemplo n.º 7
0
 def _log_workflow_success(self, data):
     name = ''
     if self._post_data:
         post_url = self._post_data[0]
         name = (workflow.NwaWorkflow.name(post_url) or post_url)
     LOG.info(_LI("NWA workflow: %(name)s %(workflow)s"),
              {'name': name,
               'workflow': jsonutils.dumps(data, indent=4, sort_keys=True)})
Ejemplo n.º 8
0
 def get_tenant_semaphore(cls, tenant_id):
     if not isinstance(tenant_id, six.string_types) or tenant_id == '':
         raise TypeError('%s is not a string' % tenant_id)
     with Semaphore.lock:
         if tenant_id not in Semaphore.tenants:
             LOG.info(_LI('create semaphore for %s'), tenant_id)
             Semaphore.tenants[tenant_id] = Semaphore()
         return Semaphore.tenants[tenant_id]
    def update_tenant_rpc_servers(self, rpc_context, **kwargs):
        session = db_api.get_session()

        with session.begin(subtransactions=True):
            q_tids = [q.tenant_id
                      for q in necnwa_api.get_nwa_tenant_queues(session)]
        plugin = manager.NeutronManager.get_plugin()
        tenant_ids = [server['tenant_id'] for server in kwargs['servers']]
        ret = []
        for tenant_id in set(q_tids) - set(tenant_ids):
            LOG.info(_LI("RPCServer only db tid=%s, send create_server"),
                     tenant_id)
            plugin.nwa_rpc.create_server(rpc_context, tenant_id)
            ret.append({'tenant_id': tenant_id})
        for tenant_id in set(tenant_ids) - set(q_tids):
            LOG.info(_LI("RPCServer only agent tid=%s, send delete_server"),
                     tenant_id)
            plugin.nwa_rpc.delete_server(rpc_context, tenant_id)
        return {'servers': ret}
Ejemplo n.º 10
0
 def _log_rest_response(self, status_code, data):
     status = ''
     progress = ''
     if isinstance(data, dict) and data.get('status'):
         status = data.get('status')
         progress = data.get('progress')
     LOG.info(_LI("NWA HTTP %(code)s %(status)s %(progress)s"),
              {'code': status_code,
               'status': status, 'progress': progress})
     if status == 'FAILED':
         self._log_workflow_error(data)
     elif status == 'SUCCEED':
         self._log_workflow_success(data)
Ejemplo n.º 11
0
    def _terminate_l2_network(self, context, nwa_data, **kwargs):
        tenant_id = kwargs.get('tenant_id')
        nwa_tenant_id = kwargs.get('nwa_tenant_id')
        nwa_info = kwargs.get('nwa_info')
        network_id = nwa_info['network']['id']

        # port check on segment.
        if check_vlan(network_id, nwa_data):
            raise nwa_exc.AgentProxyException(value=nwa_data)

        # delete vlan
        # raise AgentProxyException if fail
        nwa_data = self._delete_vlan(context, nwa_data=nwa_data, **kwargs)
        # delete vlan end.

        # tenant network check.
        for k in nwa_data:
            if re.match('NW_.*', k):
                raise nwa_exc.AgentProxyException(value=nwa_data)

        LOG.info(_LI("delete_tenant_nw"))
        # raise AgentProxyException if fail
        nwa_data = self._delete_tenant_nw(context, nwa_data=nwa_data, **kwargs)

        # delete tenant
        LOG.info(_LI("delete_tenant"))
        nwa_data = self.proxy_tenant.delete_tenant(
            context,
            nwa_data=nwa_data,
            **kwargs
        )
        # delete tenant end.

        # delete nwa_tenant binding.
        LOG.info(_LI("delete_nwa_tenant_binding"))
        return self.nwa_tenant_rpc.delete_nwa_tenant_binding(
            context, tenant_id, nwa_tenant_id
        )
Ejemplo n.º 12
0
    def __init__(self, host=None, port=None, use_ssl=True, auth=None,
                 umf_api_version=UMF_API_VERSION):
        """Creates a new client to some NWA.

        :param host: The host where service resides
        :param port: The port where service resides
        :param use_ssl: True to use SSL, False to use HTTP
        :param: auth: function to generate Authorization header.
        """
        self.host = host
        self.port = port
        self.use_ssl = use_ssl
        self.auth = auth
        self.umf_api_version = umf_api_version

        LOG.info(
            _LI('NWA init: host=%(host)s port=%(port)s use_ssl=%(use_ssl)s '
                'auth=%(auth)s'),
            {'host': self.host, 'port': self.port, 'use_ssl': self.use_ssl,
             'auth': self.auth}
        )
        LOG.info(_LI('NWA init: umf api version: %s'),
                 self.umf_api_version)
Ejemplo n.º 13
0
    def update_floatingip(self, context, fpid, floatingip):
        port_id_specified = 'port_id' in floatingip['floatingip']
        if not port_id_specified:
            LOG.error(_LE("port_id key is not found in %s"), floatingip)
            raise exc.PortNotFound(port_id=None)

        port_id = floatingip['floatingip'].get('port_id')
        try:
            if port_id_specified and not port_id:
                floating = context.session.query(l3_db.FloatingIP).filter_by(
                    id=fpid).one()
                self._delete_nat(context, floating)
        except sa.orm.exc.NoResultFound:
            raise exc.PortNotFound(port_id=port_id)

        ret = super(NECNWAL3Plugin, self).update_floatingip(
            context, fpid, floatingip)

        try:
            if port_id_specified and port_id:
                floating = context.session.query(l3_db.FloatingIP).filter_by(
                    id=fpid).one()
                tenant_id = nwa_l3_db.get_tenant_id_by_router(
                    context.session,
                    floating['router_id']
                )
                nwa_tenant_id = nwa_com_utils.get_nwa_tenant_id(tenant_id)

                fl_data = {
                    'floating_ip_address': floating['floating_ip_address'],
                    'fixed_ip_address': floating['fixed_ip_address'],
                    'id': fpid, 'device_id': floating['router_id'],
                    'floating_network_id': floating['floating_network_id'],
                    'tenant_id': floating['tenant_id'],
                    'floating_port_id': floating['floating_port_id']
                }
                LOG.info(_LI('setting_nat fl_data is %s'), fl_data)
                proxy = self._get_nwa_proxy(self, tenant_id)
                proxy.setting_nat(
                    context, tenant_id=tenant_id,
                    nwa_tenant_id=nwa_tenant_id,
                    floating=fl_data
                )

        except sa.orm.exc.NoResultFound:
            raise exc.PortNotFound(port_id=port_id)

        return ret
Ejemplo n.º 14
0
 def get_nwa_proxy(self, tid, context=None):
     if tid not in self.nwa_proxies:
         self.nwa_proxies[tid] = nwa_proxy_api.NECNWAProxyApi(
             nwa_const.NWA_AGENT_TOPIC, tid
         )
         if context:
             self._create_nwa_agent_tenant_queue(context, tid)
             nwa_topics = self.get_nwa_topics(context, tid)
             if len(nwa_topics) == 1:
                 LOG.info(_LI('NWA tenant queue: new topic is %s'),
                          str(nwa_topics[0]))
             else:
                 LOG.warning(_LW('NWA tenant queue is not created. tid=%s'),
                             tid)
     LOG.debug('proxy tid=%s', tid)
     return self.nwa_proxies[tid]
Ejemplo n.º 15
0
 def call_workflow(self, tenant_id, post, name, body):
     url = workflow.NwaWorkflow.path(name)
     try:
         wkf = nwa_sem.Semaphore.get_tenant_semaphore(tenant_id)
         if wkf.sem.locked():
             LOG.info(_LI('NWA sem %s(count)s: %(name)s %(url)s %(body)s'),
                      {'count': wkf.sem.balance,
                       'name': post.__name__,
                       'url': url,
                       'body': body})
         with wkf.sem:
             n = copy.copy(self)
             n.workflow_polling_log_post_data(url, body)
             http_status, rj = n.workflow_kick_and_wait(post, url, body)
             return http_status, rj
     except Exception as e:
         LOG.exception(_LE('%s'), e)
         return -1, None
Ejemplo n.º 16
0
    def update_tenant_rpc_servers(self, rpc_context, **kwargs):
        ret = {'servers': []}

        servers = kwargs.get('servers')
        plugin = manager.NeutronManager.get_plugin()
        session = db_api.get_session()

        with session.begin(subtransactions=True):
            queues = necnwa_api.get_nwa_tenant_queues(session)
            for queue in queues:
                tenant_ids = [server['tenant_id'] for server in servers]
                if queue.tenant_id in tenant_ids:
                    LOG.info(_LI("RPC Server active(tid=%s)"), queue.tenant_id)
                    continue
                else:
                    # create rpc server for tenant
                    LOG.debug("create_server: tid=%s", queue.tenant_id)
                    plugin.nwa_rpc.create_server(rpc_context, queue.tenant_id)
                    ret['servers'].append({'tenant_id': queue.tenant_id})

        return ret
Ejemplo n.º 17
0
 def __init__(self, host=None, port=None, use_ssl=True, auth=None,
              access_key_id=None, secret_access_key=None, **kwargs):
     load_workflow_list = kwargs.pop('load_workflow_list', True)
     if auth is None:
         auth = self._define_auth_function(access_key_id, secret_access_key)
     if not host or not port:
         if not cfgNWA.server_url:
             raise cfg.Error("'server_url' or (host, port) "
                             "must be specified.")
         host, port, use_ssl = self._parse_server_url(cfgNWA.server_url)
     super(NwaRestClient, self).__init__(host, port, use_ssl, auth,
                                         **kwargs)
     self._post_data = None
     self.workflow_first_wait = cfg.CONF.NWA.scenario_polling_first_timer
     self.workflow_wait_sleep = cfg.CONF.NWA.scenario_polling_timer
     self.workflow_retry_count = cfg.CONF.NWA.scenario_polling_count
     LOG.info(_LI('NWA init: workflow wait: %(first_wait)ss + '
                  '%(wait_sleep)ss x %(retry_count)s times.'),
              {'first_wait': self.workflow_first_wait,
               'wait_sleep': self.workflow_wait_sleep,
               'retry_count': self.workflow_retry_count})
     if load_workflow_list and not NwaRestClient.workflow_list_is_loaded:
         self.update_workflow_list()
         NwaRestClient.workflow_list_is_loaded = True
    def update_tenant_rpc_servers(self, rpc_context, **kwargs):
        ret = {'servers': []}

        servers = kwargs.get('servers')
        plugin = manager.NeutronManager.get_plugin()
        session = db_api.get_session()

        with session.begin(subtransactions=True):
            queues = necnwa_api.get_nwa_tenant_queues(session)
            for queue in queues:
                tenant_ids = [server['tenant_id'] for server in servers]
                if queue.tenant_id in tenant_ids:
                    LOG.info(_LI("RPC Server active(tid=%s)"),
                             queue.tenant_id)
                    continue
                else:
                    # create rpc server for tenant
                    LOG.debug("create_server: tid=%s", queue.tenant_id)
                    plugin.nwa_rpc.create_server(
                        rpc_context, queue.tenant_id
                    )
                    ret['servers'].append({'tenant_id': queue.tenant_id})

        return ret
Ejemplo n.º 19
0
 def delete_tenant_semaphore(cls, tenant_id):
     with Semaphore.lock:
         if tenant_id in Semaphore.tenants:
             LOG.info(_LI('delete semaphore for %s'), tenant_id)
             del Semaphore.tenants[tenant_id]
Ejemplo n.º 20
0
 def delete_tenant_semaphore(cls, tenant_id):
     with Semaphore.lock:
         if tenant_id in Semaphore.tenants:
             LOG.info(_LI('delete semaphore for %s'), tenant_id)
             del Semaphore.tenants[tenant_id]