# Copyright (C) 2014 eNovance SAS <licensing@enovance.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 neutron_lib.db import model_base
import sqlalchemy as sa
from sqlalchemy import orm

from neutron.common import constants as n_const
from neutron.db.models import agent as agent_model
from neutron.db import models_v2


class L3HARouterAgentPortBinding(model_base.BASEV2):
    """Represent agent binding state of a HA router port.

    A HA Router has one HA port per agent on which it is spawned.
    This binding table stores which port is used for a HA router by a
    L3 agent.
    """

    __tablename__ = 'ha_router_agent_port_bindings'
    __table_args__ = (
        sa.UniqueConstraint(
            'router_id', 'l3_agent_id',
            name='uniq_ha_router_agent_port_bindings0port_id0l3_agent_id'),
        model_base.BASEV2.__table_args__
    )
    port_id = sa.Column(sa.String(36), sa.ForeignKey('ports.id',
                                                     ondelete='CASCADE'),
                        nullable=False, primary_key=True)
    port = orm.relationship(models_v2.Port)

    router_id = sa.Column(sa.String(36), sa.ForeignKey('routers.id',
                                                       ondelete='CASCADE'),
                          nullable=False)

    l3_agent_id = sa.Column(sa.String(36),
                            sa.ForeignKey("agents.id",
                                          ondelete='CASCADE'))
    agent = orm.relationship(agent_model.Agent)

    state = sa.Column(sa.Enum(n_const.HA_ROUTER_STATE_ACTIVE,
                              n_const.HA_ROUTER_STATE_STANDBY,
                              name='l3_ha_states'),
                      default=n_const.HA_ROUTER_STATE_STANDBY,
                      server_default=n_const.HA_ROUTER_STATE_STANDBY)


class L3HARouterNetwork(model_base.BASEV2, model_base.HasProjectPrimaryKey):
    """Host HA network for a tenant.

    One HA Network is used per tenant, all HA router ports are created
    on this network.
    """

    __tablename__ = 'ha_router_networks'

    network_id = sa.Column(sa.String(36),
                           sa.ForeignKey('networks.id', ondelete="CASCADE"),
                           nullable=False, primary_key=True)
    network = orm.relationship(models_v2.Network)


class L3HARouterVRIdAllocation(model_base.BASEV2):
    """VRID allocation per HA network.

    Keep a track of the VRID allocations per HA network.
    """

    __tablename__ = 'ha_router_vrid_allocations'

    network_id = sa.Column(sa.String(36),
                           sa.ForeignKey('networks.id', ondelete="CASCADE"),
                           nullable=False, primary_key=True)
    vr_id = sa.Column(sa.Integer(), nullable=False, primary_key=True)

[((1579, 1611), 'sqlalchemy.orm.relationship', 'orm.relationship', (['models_v2.Port'], {}), '(models_v2.Port)\n', (1595, 1611), False, 'from sqlalchemy import orm\n'), ((1974, 2009), 'sqlalchemy.orm.relationship', 'orm.relationship', (['agent_model.Agent'], {}), '(agent_model.Agent)\n', (1990, 2009), False, 'from sqlalchemy import orm\n'), ((2775, 2810), 'sqlalchemy.orm.relationship', 'orm.relationship', (['models_v2.Network'], {}), '(models_v2.Network)\n', (2791, 2810), False, 'from sqlalchemy import orm\n'), ((1187, 1302), 'sqlalchemy.UniqueConstraint', 'sa.UniqueConstraint', (['"""router_id"""', '"""l3_agent_id"""'], {'name': '"""uniq_ha_router_agent_port_bindings0port_id0l3_agent_id"""'}), "('router_id', 'l3_agent_id', name=\n    'uniq_ha_router_agent_port_bindings0port_id0l3_agent_id')\n", (1206, 1302), True, 'import sqlalchemy as sa\n'), ((1395, 1408), 'sqlalchemy.String', 'sa.String', (['(36)'], {}), '(36)\n', (1404, 1408), True, 'import sqlalchemy as sa\n'), ((1410, 1455), 'sqlalchemy.ForeignKey', 'sa.ForeignKey', (['"""ports.id"""'], {'ondelete': '"""CASCADE"""'}), "('ports.id', ondelete='CASCADE')\n", (1423, 1455), True, 'import sqlalchemy as sa\n'), ((1639, 1652), 'sqlalchemy.String', 'sa.String', (['(36)'], {}), '(36)\n', (1648, 1652), True, 'import sqlalchemy as sa\n'), ((1654, 1701), 'sqlalchemy.ForeignKey', 'sa.ForeignKey', (['"""routers.id"""'], {'ondelete': '"""CASCADE"""'}), "('routers.id', ondelete='CASCADE')\n", (1667, 1701), True, 'import sqlalchemy as sa\n'), ((1829, 1842), 'sqlalchemy.String', 'sa.String', (['(36)'], {}), '(36)\n', (1838, 1842), True, 'import sqlalchemy as sa\n'), ((1872, 1918), 'sqlalchemy.ForeignKey', 'sa.ForeignKey', (['"""agents.id"""'], {'ondelete': '"""CASCADE"""'}), "('agents.id', ondelete='CASCADE')\n", (1885, 1918), True, 'import sqlalchemy as sa\n'), ((2033, 2130), 'sqlalchemy.Enum', 'sa.Enum', (['n_const.HA_ROUTER_STATE_ACTIVE', 'n_const.HA_ROUTER_STATE_STANDBY'], {'name': '"""l3_ha_states"""'}), "(n_const.HA_ROUTER_STATE_ACTIVE, n_const.HA_ROUTER_STATE_STANDBY,\n    name='l3_ha_states')\n", (2040, 2130), True, 'import sqlalchemy as sa\n'), ((2608, 2621), 'sqlalchemy.String', 'sa.String', (['(36)'], {}), '(36)\n', (2617, 2621), True, 'import sqlalchemy as sa\n'), ((2650, 2698), 'sqlalchemy.ForeignKey', 'sa.ForeignKey', (['"""networks.id"""'], {'ondelete': '"""CASCADE"""'}), "('networks.id', ondelete='CASCADE')\n", (2663, 2698), True, 'import sqlalchemy as sa\n'), ((3047, 3060), 'sqlalchemy.String', 'sa.String', (['(36)'], {}), '(36)\n', (3056, 3060), True, 'import sqlalchemy as sa\n'), ((3089, 3137), 'sqlalchemy.ForeignKey', 'sa.ForeignKey', (['"""networks.id"""'], {'ondelete': '"""CASCADE"""'}), "('networks.id', ondelete='CASCADE')\n", (3102, 3137), True, 'import sqlalchemy as sa\n'), ((3222, 3234), 'sqlalchemy.Integer', 'sa.Integer', ([], {}), '()\n', (3232, 3234), True, 'import sqlalchemy as sa\n')]