<commit_before># All Rights Reserved.
#
#    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.

import sqlalchemy as sa
from sqlalchemy import orm

from neutron import context as nctx
from neutron.db import api as db_api
from neutron.db import model_base
from neutron.db import models_v2
from neutron import manager
from oslo_db import exception as db_exc
from oslo_log import log as logging

LOG = logging.getLogger(__name__)


class HAIPAddressToPortAssocation(model_base.BASEV2):

    """Port Owner for HA IP Address.

    This table is used to store the mapping between the HA IP Address
    and the Port ID of the Neutron Port which currently owns this
    IP Address.
    """

    __tablename__ = 'apic_ml2_ha_ipaddress_to_port_owner'

    ha_ip_address = sa.Column(sa.String(64), nullable=False,
                              primary_key=True)
    port_id = sa.Column(sa.String(64), sa.ForeignKey('ports.id',
                                                     ondelete='CASCADE'),
                        nullable=False, primary_key=True)


class PortForHAIPAddress(object):

    def _get_ha_ipaddress(self, port_id, ipaddress, session=None):
        session = session or db_api.get_session()
        return session.query(HAIPAddressToPortAssocation).filter_by(
            port_id=port_id, ha_ip_address=ipaddress).first()

    def get_port_for_ha_ipaddress(self, ipaddress, network_id):
        """Returns the Neutron Port ID for the HA IP Addresss."""
        session = db_api.get_session()
        port_ha_ip = session.query(HAIPAddressToPortAssocation).join(
            models_v2.Port).filter(
                HAIPAddressToPortAssocation.ha_ip_address == ipaddress).filter(
                    models_v2.Port.network_id == network_id).first()
        return port_ha_ip

    def get_ha_ipaddresses_for_port(self, port_id):
        """Returns the HA IP Addressses associated with a Port."""
        session = db_api.get_session()
        objs = session.query(HAIPAddressToPortAssocation).filter_by(
            port_id=port_id).all()
        return sorted([x['ha_ip_address'] for x in objs])

    def set_port_id_for_ha_ipaddress(self, port_id, ipaddress, session=None):
        """Stores a Neutron Port Id as owner of HA IP Addr (idempotent API)."""
        session = session or db_api.get_session()
        session.expunge_all()
        try:
            with session.begin(subtransactions=True):
                obj = self._get_ha_ipaddress(port_id, ipaddress, session)
                if obj:
                    return obj
                else:
                    obj = HAIPAddressToPortAssocation(port_id=port_id,
                                                      ha_ip_address=ipaddress)
                    session.add(obj)
                    return obj
        except db_exc.DBDuplicateEntry:
            LOG.debug('Duplicate IP ownership entry for tuple %s',
                      (port_id, ipaddress))

    def delete_port_id_for_ha_ipaddress(self, port_id, ipaddress,
                                        session=None):
        session = session or db_api.get_session()
        with session.begin(subtransactions=True):
            try:
                return session.query(
                    HAIPAddressToPortAssocation).filter_by(
                        port_id=port_id,
                        ha_ip_address=ipaddress).delete()
            except orm.exc.NoResultFound:
                return

    def get_ha_port_associations(self):
        session = db_api.get_session()
        return session.query(HAIPAddressToPortAssocation).all()


class HAIPOwnerDbMixin(object):

    def __init__(self):
        self.ha_ip_handler = PortForHAIPAddress()

    def _get_plugin(self):
        return manager.NeutronManager.get_plugin()

    def update_ip_owner(self, ip_owner_info):
        ports_to_update = set()
        port_id = ip_owner_info.get('port')
        ipv4 = ip_owner_info.get('ip_address_v4')
        ipv6 = ip_owner_info.get('ip_address_v6')
        network_id = ip_owner_info.get('network_id')
        if not port_id or (not ipv4 and not ipv6):
            return ports_to_update
        LOG.debug("Got IP owner update: %s", ip_owner_info)
        core_plugin = self._get_plugin()
        port = core_plugin.get_port(nctx.get_admin_context(), port_id)
        if not port:
            LOG.debug("Ignoring update for non-existent port: %s", port_id)
            return ports_to_update
        ports_to_update.add(port_id)
        for ipa in [ipv4, ipv6]:
            if not ipa:
                continue
            try:
                old_owner = self.ha_ip_handler.get_port_for_ha_ipaddress(
                    ipa, network_id or port['network_id'])
                session = db_api.get_session()
                with session.begin(subtransactions=True):
                    self.ha_ip_handler.set_port_id_for_ha_ipaddress(port_id,
                                                                    ipa,
                                                                    session)
                    if old_owner and old_owner['port_id'] != port_id:
                        self.ha_ip_handler.delete_port_id_for_ha_ipaddress(
                            old_owner['port_id'], ipa, session)
                        ports_to_update.add(old_owner['port_id'])
            except db_exc.DBReferenceError as dbe:
                LOG.debug("Ignoring FK error for port %s: %s", port_id, dbe)
        return ports_to_update<commit_msg>Transaction fixes for HA IPs<commit_after># All Rights Reserved.
#
#    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.

import sqlalchemy as sa
from sqlalchemy import orm

from neutron import context as nctx
from neutron.db import api as db_api
from neutron.db import model_base
from neutron.db import models_v2
from neutron import manager
from oslo_db import exception as db_exc
from oslo_log import log as logging

LOG = logging.getLogger(__name__)


class HAIPAddressToPortAssocation(model_base.BASEV2):

    """Port Owner for HA IP Address.

    This table is used to store the mapping between the HA IP Address
    and the Port ID of the Neutron Port which currently owns this
    IP Address.
    """

    __tablename__ = 'apic_ml2_ha_ipaddress_to_port_owner'

    ha_ip_address = sa.Column(sa.String(64), nullable=False,
                              primary_key=True)
    port_id = sa.Column(sa.String(64), sa.ForeignKey('ports.id',
                                                     ondelete='CASCADE'),
                        nullable=False, primary_key=True)


class PortForHAIPAddress(object):

    def _get_ha_ipaddress(self, port_id, ipaddress, session=None):
        ip = None
        session = session or db_api.get_session()
        with session.begin(subtransactions=True):
            ip = session.query(HAIPAddressToPortAssocation).filter_by(
                port_id=port_id, ha_ip_address=ipaddress).first()
        return ip

    def get_port_for_ha_ipaddress(self, ipaddress, network_id,
                                  session=None):
        """Returns the Neutron Port ID for the HA IP Addresss."""
        port_ha_ip = None
        session = session or db_api.get_session()
        with session.begin(subtransactions=True):
            port_ha_ip = session.query(HAIPAddressToPortAssocation).join(
                models_v2.Port).filter(
                    HAIPAddressToPortAssocation.ha_ip_address ==
                    ipaddress).filter(
                        models_v2.Port.network_id == network_id).first()
        return port_ha_ip

    def get_ha_ipaddresses_for_port(self, port_id):
        """Returns the HA IP Addressses associated with a Port."""
        objs = []
        session = db_api.get_session()
        with session.begin(subtransactions=True):
            objs = session.query(HAIPAddressToPortAssocation).filter_by(
                port_id=port_id).all()
        return sorted([x['ha_ip_address'] for x in objs])

    def set_port_id_for_ha_ipaddress(self, port_id, ipaddress, session=None):
        """Stores a Neutron Port Id as owner of HA IP Addr (idempotent API)."""
        session = session or db_api.get_session()
        session.expunge_all()
        try:
            with session.begin(subtransactions=True):
                obj = self._get_ha_ipaddress(port_id, ipaddress, session)
                if obj:
                    return obj
                else:
                    obj = HAIPAddressToPortAssocation(port_id=port_id,
                                                      ha_ip_address=ipaddress)
                    session.add(obj)
                    return obj
        except db_exc.DBDuplicateEntry:
            LOG.debug('Duplicate IP ownership entry for tuple %s',
                      (port_id, ipaddress))

    def delete_port_id_for_ha_ipaddress(self, port_id, ipaddress,
                                        session=None):
        session = session or db_api.get_session()
        with session.begin(subtransactions=True):
            try:
                return session.query(
                    HAIPAddressToPortAssocation).filter_by(
                        port_id=port_id,
                        ha_ip_address=ipaddress).delete()
            except orm.exc.NoResultFound:
                return

    def get_ha_port_associations(self):
        associations = []
        session = db_api.get_session()
        with session.begin(subtransactions=True):
            associations = session.query(HAIPAddressToPortAssocation).all()
        return associations


class HAIPOwnerDbMixin(object):

    def __init__(self):
        self.ha_ip_handler = PortForHAIPAddress()

    def _get_plugin(self):
        return manager.NeutronManager.get_plugin()

    def update_ip_owner(self, ip_owner_info):
        ports_to_update = set()
        port_id = ip_owner_info.get('port')
        ipv4 = ip_owner_info.get('ip_address_v4')
        ipv6 = ip_owner_info.get('ip_address_v6')
        network_id = ip_owner_info.get('network_id')
        if not port_id or (not ipv4 and not ipv6):
            return ports_to_update
        LOG.debug("Got IP owner update: %s", ip_owner_info)
        core_plugin = self._get_plugin()
        # REVISIT: just use SQLAlchemy session and models_v2.Port?
        port = core_plugin.get_port(nctx.get_admin_context(), port_id)
        if not port:
            LOG.debug("Ignoring update for non-existent port: %s", port_id)
            return ports_to_update
        ports_to_update.add(port_id)
        for ipa in [ipv4, ipv6]:
            if not ipa:
                continue
            try:
                session = db_api.get_session()
                with session.begin(subtransactions=True):
                    old_owner = self.ha_ip_handler.get_port_for_ha_ipaddress(
                        ipa, network_id or port['network_id'], session=session)
                    self.ha_ip_handler.set_port_id_for_ha_ipaddress(port_id,
                                                                    ipa,
                                                                    session)
                    if old_owner and old_owner['port_id'] != port_id:
                        self.ha_ip_handler.delete_port_id_for_ha_ipaddress(
                            old_owner['port_id'], ipa, session=session)
                        ports_to_update.add(old_owner['port_id'])
            except db_exc.DBReferenceError as dbe:
                LOG.debug("Ignoring FK error for port %s: %s", port_id, dbe)
        return ports_to_update
{'fraction_non_alphanumeric': 0.07477305303392259, 'fraction_numerical': 0.0034241121197642935, 'mean_word_length': 2.0542315175097277, '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': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6098396', 'n_tokens_mistral': 3572, 'n_tokens_neox': 3450, 'n_words': 876}