<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 Nicira Networks, Inc
# 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 netaddr import IPNetwork, IPAddress
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.network.quantum import melange_connection


LOG = logging.getLogger("nova.network.quantum.melange_ipam_lib")

FLAGS = flags.FLAGS


def get_ipam_lib(net_man):
    return QuantumMelangeIPAMLib()


class QuantumMelangeIPAMLib(object):
    """Implements Quantum IP Address Management (IPAM) interface
       using the Melange service, which is access using the Melange
       web services API.
    """

    def __init__(self):
        """Initialize class used to connect to Melange server"""
        self.m_conn = melange_connection.MelangeConnection()

    def create_subnet(self, context, label, project_id,
                      quantum_net_id, priority, cidr=None,
                      gateway=None, gateway_v6=None, cidr_v6=None,
                      dns1=None, dns2=None):
        """Contact Melange and create a subnet for any non-NULL
           IPv4 or IPv6 subnets.

           Also create a entry in the Nova networks DB, but only
           to store values not represented in Melange or to
           temporarily provide compatibility with Nova code that
           accesses IPAM data directly via the DB (e.g., nova-api)
        """
        tenant_id = project_id or FLAGS.quantum_default_tenant_id
        if cidr:
            self.m_conn.create_block(quantum_net_id, cidr,
                                     project_id=tenant_id,
                                     gateway=gateway,
                                     dns1=dns1, dns2=dns2)
        if cidr_v6:
            self.m_conn.create_block(quantum_net_id, cidr_v6,
                                     project_id=tenant_id,
                                     gateway=gateway_v6,
                                     dns1=dns1, dns2=dns2)

        net = {"uuid": quantum_net_id,
               "project_id": project_id,
               "priority": priority,
               "label": label}
        if FLAGS.quantum_use_dhcp:
            if cidr:
                n = IPNetwork(cidr)
                net['dhcp_start'] = IPAddress(n.first + 2)
        else:
            net['dhcp_start'] = None
        admin_context = context.elevated()
        network = db.network_create_safe(admin_context, net)

    def allocate_fixed_ip(self, context, project_id, quantum_net_id,
                          network_tenant_id, vif_ref):
        """Pass call to allocate fixed IP on to Melange"""
        ip = self.m_conn.allocate_ip(quantum_net_id, network_tenant_id,
                                     vif_ref['uuid'], project_id,
                                     vif_ref['address'])
        return ip[0]['address']

    def get_network_id_by_cidr(self, context, cidr, project_id):
        """Find the Quantum UUID associated with a IPv4 CIDR
           address for the specified tenant.
        """
        tenant_id = project_id or FLAGS.quantum_default_tenant_id
        all_blocks = self.m_conn.get_blocks(tenant_id)
        for b in all_blocks['ip_blocks']:
            LOG.debug("block: %s" % b)
            if b['cidr'] == cidr:
                return b['network_id']
        raise exception.NotFound(_("No network found for cidr %s" % cidr))<commit_msg>Store the correct tenant_id/project_id<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2011 Nicira Networks, Inc
# 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 netaddr import IPNetwork, IPAddress
from nova import db
from nova import exception
from nova import flags
from nova import log as logging
from nova.network.quantum import melange_connection


LOG = logging.getLogger("nova.network.quantum.melange_ipam_lib")

FLAGS = flags.FLAGS


def get_ipam_lib(net_man):
    return QuantumMelangeIPAMLib()


class QuantumMelangeIPAMLib(object):
    """Implements Quantum IP Address Management (IPAM) interface
       using the Melange service, which is access using the Melange
       web services API.
    """

    def __init__(self):
        """Initialize class used to connect to Melange server"""
        self.m_conn = melange_connection.MelangeConnection()

    def create_subnet(self, context, label, project_id,
                      quantum_net_id, priority, cidr=None,
                      gateway=None, gateway_v6=None, cidr_v6=None,
                      dns1=None, dns2=None):
        """Contact Melange and create a subnet for any non-NULL
           IPv4 or IPv6 subnets.

           Also create a entry in the Nova networks DB, but only
           to store values not represented in Melange or to
           temporarily provide compatibility with Nova code that
           accesses IPAM data directly via the DB (e.g., nova-api)
        """
        tenant_id = project_id or FLAGS.quantum_default_tenant_id
        if cidr:
            self.m_conn.create_block(quantum_net_id, cidr,
                                     project_id=tenant_id,
                                     gateway=gateway,
                                     dns1=dns1, dns2=dns2)
        if cidr_v6:
            self.m_conn.create_block(quantum_net_id, cidr_v6,
                                     project_id=tenant_id,
                                     gateway=gateway_v6,
                                     dns1=dns1, dns2=dns2)

        net = {"uuid": quantum_net_id,
               "project_id": tenant_id,
               "priority": priority,
               "label": label}
        if FLAGS.quantum_use_dhcp:
            if cidr:
                n = IPNetwork(cidr)
                net['dhcp_start'] = IPAddress(n.first + 2)
        else:
            net['dhcp_start'] = None
        admin_context = context.elevated()
        network = db.network_create_safe(admin_context, net)

    def allocate_fixed_ip(self, context, project_id, quantum_net_id,
                          network_tenant_id, vif_ref):
        """Pass call to allocate fixed IP on to Melange"""
        ip = self.m_conn.allocate_ip(quantum_net_id, network_tenant_id,
                                     vif_ref['uuid'], project_id,
                                     vif_ref['address'])
        return ip[0]['address']

    def get_network_id_by_cidr(self, context, cidr, project_id):
        """Find the Quantum UUID associated with a IPv4 CIDR
           address for the specified tenant.
        """
        tenant_id = project_id or FLAGS.quantum_default_tenant_id
        all_blocks = self.m_conn.get_blocks(tenant_id)
        for b in all_blocks['ip_blocks']:
            LOG.debug("block: %s" % b)
            if b['cidr'] == cidr:
                return b['network_id']
        raise exception.NotFound(_("No network found for cidr %s" % cidr))
{'fraction_non_alphanumeric': 0.0745987963891675, 'fraction_numerical': 0.007773319959879639, 'mean_word_length': 2.083494395052184, 'pattern_counts': {'":': 8, '<': 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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '238213', 'n_tokens_mistral': 2322, 'n_tokens_neox': 2251, 'n_words': 676}