<commit_before>#   Copyright (c) 2017 Orange.  # 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.

from oslo_log import log as logging
from oslo_versionedobjects import fields as obj_fields

from networking_bgpvpn.neutron.db import bgpvpn_db

from neutron.api.rpc.callbacks import resources
from neutron.common import utils
from neutron.db import api as db_api
from neutron.objects import base
from neutron.objects import common_types
from neutron.objects.ports import IPAllocation
from neutron.objects.ports import Port
from neutron.objects.router import RouterPort
from neutron.objects.subnet import Subnet

from neutron_lib.api.definitions import bgpvpn as bgpvpn_api
from neutron_lib.api.definitions import bgpvpn_routes_control as bgpvpn_rc_api
from neutron_lib import constants

LOG = logging.getLogger(__name__)


def _get_gateway_mac_by_subnet(obj_context, subnet):
    if not subnet.gateway_ip:
        LOG.error("no gateway IP defined for subnet %s", subnet)
        return None

    ip_allocation = IPAllocation.get_object(obj_context,
                                            network_id=subnet.network_id,
                                            subnet_id=subnet.id,
                                            ip_address=subnet.gateway_ip)
    if ip_allocation:
        port = Port.get_object(obj_context, id=ip_allocation.port_id)
        return str(port.mac_address)
    else:
        LOG.debug("no port allocated to gateway IP for subnet %s", subnet.id)
        return None


def _get_subnets_info(obj_context, net_id):
    subnets = Subnet.get_objects(obj_context, network_id=net_id)
    return [
        {'ip_version': subnet.ip_version,
         'cidr': subnet.cidr,
         'gateway_ip': subnet.gateway_ip,
         'gateway_mac': _get_gateway_mac_by_subnet(obj_context, subnet)
         }
        for subnet in subnets
    ]


class BGPVPNTypeField(obj_fields.AutoTypedField):
    AUTO_TYPE = obj_fields.Enum(valid_values=bgpvpn_api.BGPVPN_TYPES)


@base.NeutronObjectRegistry.register
class BGPVPN(base.NeutronDbObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    db_model = bgpvpn_db.BGPVPN
    fields = {
        'id': common_types.UUIDField(),
        'project_id': obj_fields.StringField(),
        'type': BGPVPNTypeField(),
        'name': obj_fields.StringField(),
        'route_targets': obj_fields.ListOfStringsField(nullable=True),
        'import_targets': obj_fields.ListOfStringsField(nullable=True),
        'export_targets': obj_fields.ListOfStringsField(nullable=True),
        'route_distinguishers': obj_fields.ListOfStringsField(nullable=True)
    }

    foreign_keys = {'BGPVPNNetAssociation': {'id': 'bgpvpn_id'},
                    'BGPVPNRouterAssociation': {'id': 'bgpvpn_id'},
                    'BGPVPNPortAssociation': {'id': 'bgpvpn_id'}}

    @classmethod
    def modify_fields_from_db(cls, db_obj):
        result = super(BGPVPN, cls).modify_fields_from_db(db_obj)
        for field in ['route_targets',
                      'import_targets',
                      'export_targets',
                      'route_distinguishers']:
            if field in result:
                result[field] = (result[field].split(',')
                                 if result[field] else [])
        return result

    @classmethod
    def modify_fields_to_db(cls, fields):
        result = super(BGPVPN, cls).modify_fields_to_db(fields)
        for field in ['route_targets',
                      'import_targets',
                      'export_targets',
                      'route_distinguishers']:
            if field in result:
                result[field] = ','.join(result.get(field, []))
        return result


@base.NeutronObjectRegistry.register<commit_msg>bgpvpn: add 'local_pref' to OVO BGPVPN object<commit_after>#   Copyright (c) 2017 Orange.  # 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.

from oslo_log import log as logging
from oslo_versionedobjects import fields as obj_fields

from networking_bgpvpn.neutron.db import bgpvpn_db

from neutron.api.rpc.callbacks import resources
from neutron.common import utils
from neutron.db import api as db_api
from neutron.objects import base
from neutron.objects import common_types
from neutron.objects.ports import IPAllocation
from neutron.objects.ports import Port
from neutron.objects.router import RouterPort
from neutron.objects.subnet import Subnet

from neutron_lib.api.definitions import bgpvpn as bgpvpn_api
from neutron_lib.api.definitions import bgpvpn_routes_control as bgpvpn_rc_api
from neutron_lib import constants

LOG = logging.getLogger(__name__)


def _get_gateway_mac_by_subnet(obj_context, subnet):
    if not subnet.gateway_ip:
        LOG.error("no gateway IP defined for subnet %s", subnet)
        return None

    ip_allocation = IPAllocation.get_object(obj_context,
                                            network_id=subnet.network_id,
                                            subnet_id=subnet.id,
                                            ip_address=subnet.gateway_ip)
    if ip_allocation:
        port = Port.get_object(obj_context, id=ip_allocation.port_id)
        return str(port.mac_address)
    else:
        LOG.debug("no port allocated to gateway IP for subnet %s", subnet.id)
        return None


def _get_subnets_info(obj_context, net_id):
    subnets = Subnet.get_objects(obj_context, network_id=net_id)
    return [
        {'ip_version': subnet.ip_version,
         'cidr': subnet.cidr,
         'gateway_ip': subnet.gateway_ip,
         'gateway_mac': _get_gateway_mac_by_subnet(obj_context, subnet)
         }
        for subnet in subnets
    ]


class BGPVPNTypeField(obj_fields.AutoTypedField):
    AUTO_TYPE = obj_fields.Enum(valid_values=bgpvpn_api.BGPVPN_TYPES)


@base.NeutronObjectRegistry.register
class BGPVPN(base.NeutronDbObject):
    # Version 1.0: Initial version
    VERSION = '1.0'

    db_model = bgpvpn_db.BGPVPN
    fields = {
        'id': common_types.UUIDField(),
        'project_id': obj_fields.StringField(),
        'type': BGPVPNTypeField(),
        'name': obj_fields.StringField(),
        'route_targets': obj_fields.ListOfStringsField(nullable=True),
        'import_targets': obj_fields.ListOfStringsField(nullable=True),
        'export_targets': obj_fields.ListOfStringsField(nullable=True),
        'route_distinguishers': obj_fields.ListOfStringsField(nullable=True),
        'local_pref': obj_fields.IntegerField(nullable=True),
    }

    foreign_keys = {'BGPVPNNetAssociation': {'id': 'bgpvpn_id'},
                    'BGPVPNRouterAssociation': {'id': 'bgpvpn_id'},
                    'BGPVPNPortAssociation': {'id': 'bgpvpn_id'}}

    @classmethod
    def modify_fields_from_db(cls, db_obj):
        result = super(BGPVPN, cls).modify_fields_from_db(db_obj)
        for field in ['route_targets',
                      'import_targets',
                      'export_targets',
                      'route_distinguishers']:
            if field in result:
                result[field] = (result[field].split(',')
                                 if result[field] else [])
        return result

    @classmethod
    def modify_fields_to_db(cls, fields):
        result = super(BGPVPN, cls).modify_fields_to_db(fields)
        for field in ['route_targets',
                      'import_targets',
                      'export_targets',
                      'route_distinguishers']:
            if field in result:
                result[field] = ','.join(result.get(field, []))
        return result


@base.NeutronObjectRegistry.register
{'fraction_non_alphanumeric': 0.08491981077650859, 'fraction_numerical': 0.0027691242644513675, 'mean_word_length': 2.745894554883319, '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': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5883204', 'n_tokens_mistral': 2649, 'n_tokens_neox': 2525, 'n_words': 625}