# Path: apic_ml2/neutron/extensions/cisco_apic.py
# ALIAS = 'cisco-apic'
# ALLOW_ROUTE_LEAK = 'apic:allow_route_leak'
# NET_ATTRIBUTES = {
#     ALLOW_ROUTE_LEAK: {
#         'allow_post': True, 'allow_put': False,
#         'is_visible': True, 'default': False,
#         'convert_to': conv.convert_to_boolean,
#     },
# }
# EXTENDED_ATTRIBUTES_2_0 = {
#     attributes.NETWORKS: dict(NET_ATTRIBUTES.items()),
# }
# class Cisco_apic(extensions.ExtensionDescriptor):
#     def get_name(cls):
#     def get_alias(cls):
#     def get_description(cls):
#     def get_updated(cls):
#     def get_extended_resources(self, version):
# 
# Path: apic_ml2/neutron/extensions/cisco_apic_l3.py
# ALIAS = 'cisco-apic-l3'
# USE_ROUTING_CONTEXT = 'apic:use_routing_context'
# EXT_GW_ATTRIBUTES = {
#     USE_ROUTING_CONTEXT: {
#         'allow_post': True, 'allow_put': False,
#         'is_visible': True, 'default': None,
#         'validate': {'type:uuid_or_none': None},
#     }
# }
# EXTENDED_ATTRIBUTES_2_0 = {
#     l3.ROUTERS: dict(EXT_GW_ATTRIBUTES.items())
# }
# class Cisco_apic_l3(extensions.ExtensionDescriptor):
#     def get_name(cls):
#     def get_alias(cls):
#     def get_description(cls):
#     def get_updated(cls):
#     def get_extended_resources(self, version):


from neutron_lib.db import model_base
from apic_ml2.neutron.extensions import cisco_apic
from apic_ml2.neutron.extensions import cisco_apic_l3
import sqlalchemy as sa
class RouterExtensionDb(model_base.BASEV2):

    __tablename__ = 'apic_ml2_router_extensions'

    router_id = sa.Column(
        sa.String(36), sa.ForeignKey('routers.id', ondelete="CASCADE"),
        primary_key=True)
    use_routing_context = sa.Column(
        sa.String(36), sa.ForeignKey('routers.id', ondelete="RESTRICT"))


class ExtensionDbMixin(object):

    def _set_if_not_none(self, res_dict, res_attr, db_attr):
        if db_attr is not None:
            res_dict[res_attr] = db_attr

    def get_network_extn_db(self, session, network_id):
        db_obj = (session.query(NetworkExtensionDb).filter_by(
                  network_id=network_id).first())
        result = {}
        if db_obj:
            self._set_if_not_none(result, cisco_apic.ALLOW_ROUTE_LEAK,
                                  db_obj['allow_route_leak'])
        return result

    def set_network_extn_db(self, session, network_id, res_dict):
        db_obj = (session.query(NetworkExtensionDb).filter_by(
                  network_id=network_id).first())
        db_obj = db_obj or NetworkExtensionDb(network_id=network_id)

# Path: apic_ml2/neutron/extensions/cisco_apic.py
# ALIAS = 'cisco-apic'
# ALLOW_ROUTE_LEAK = 'apic:allow_route_leak'
# NET_ATTRIBUTES = {
#     ALLOW_ROUTE_LEAK: {
#         'allow_post': True, 'allow_put': False,
#         'is_visible': True, 'default': False,
#         'convert_to': conv.convert_to_boolean,
#     },
# }
# EXTENDED_ATTRIBUTES_2_0 = {
#     attributes.NETWORKS: dict(NET_ATTRIBUTES.items()),
# }
# class Cisco_apic(extensions.ExtensionDescriptor):
#     def get_name(cls):
#     def get_alias(cls):
#     def get_description(cls):
#     def get_updated(cls):
#     def get_extended_resources(self, version):
# 
# Path: apic_ml2/neutron/extensions/cisco_apic_l3.py
# ALIAS = 'cisco-apic-l3'
# USE_ROUTING_CONTEXT = 'apic:use_routing_context'
# EXT_GW_ATTRIBUTES = {
#     USE_ROUTING_CONTEXT: {
#         'allow_post': True, 'allow_put': False,
#         'is_visible': True, 'default': None,
#         'validate': {'type:uuid_or_none': None},
#     }
# }
# EXTENDED_ATTRIBUTES_2_0 = {
#     l3.ROUTERS: dict(EXT_GW_ATTRIBUTES.items())
# }
# class Cisco_apic_l3(extensions.ExtensionDescriptor):
#     def get_name(cls):
#     def get_alias(cls):
#     def get_description(cls):
#     def get_updated(cls):
#     def get_extended_resources(self, version):


# Path: apic_ml2/neutron/plugins/ml2/drivers/cisco/apic/extension_db.py
from neutron_lib.db import model_base
from apic_ml2.neutron.extensions import cisco_apic
from apic_ml2.neutron.extensions import cisco_apic_l3
import sqlalchemy as sa

class RouterExtensionDb(model_base.BASEV2):

    __tablename__ = 'apic_ml2_router_extensions'

    router_id = sa.Column(
        sa.String(36), sa.ForeignKey('routers.id', ondelete="CASCADE"),
        primary_key=True)
    use_routing_context = sa.Column(
        sa.String(36), sa.ForeignKey('routers.id', ondelete="RESTRICT"))


class ExtensionDbMixin(object):

    def _set_if_not_none(self, res_dict, res_attr, db_attr):
        if db_attr is not None:
            res_dict[res_attr] = db_attr

    def get_network_extn_db(self, session, network_id):
        db_obj = (session.query(NetworkExtensionDb).filter_by(
                  network_id=network_id).first())
        result = {}
        if db_obj:
            self._set_if_not_none(result, cisco_apic.ALLOW_ROUTE_LEAK,
                                  db_obj['allow_route_leak'])
        return result

    def set_network_extn_db(self, session, network_id, res_dict):
        db_obj = (session.query(NetworkExtensionDb).filter_by(
                  network_id=network_id).first())
        db_obj = db_obj or NetworkExtensionDb(network_id=network_id)

        if cisco_apic.ALLOW_ROUTE_LEAK in res_dict: