<commit_before>#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
#
# This file is built up from an autogenerated template resource_server.py and
# contains code/hooks at different point during processing a request, specific
# to type of resource. For eg. allocation of mac/ip-addr for a port during its
# creation.

import json
import re

import cfgm_common
from vnc_quota import *
from gen.resource_xsd import *
from gen.resource_common import *
from gen.resource_server import *
from pprint import pformat


class FloatingIpServer(FloatingIpServerGen):
    generate_default_instance = False

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        proj_dict = obj_dict['project_refs'][0]
        if 'uuid' in proj_dict:
            proj_uuid = proj_dict['uuid']
        else:
            proj_uuid = db_conn.fq_name_to_uuid('project', proj_dict['to'])
        (ok, proj_dict) = QuotaHelper.get_project_dict(proj_uuid, db_conn)
        if not ok:
            return (False, (500, 'Internal error : ' + pformat(proj_dict)))

        obj_type = 'floating-ip'
        QuotaHelper.ensure_quota_project_present(obj_type, proj_uuid, proj_dict, db_conn)

        if 'floating_ip_back_refs' in proj_dict:
            quota_count = len(proj_dict['floating_ip_back_refs'])
            (ok, quota_limit) = QuotaHelper.check_quota_limit(proj_dict, obj_type, quota_count)
            if not ok:
                return (False, (403, pformat(obj_dict['fq_name']) + ' : ' + quota_limit))

        vn_fq_name = obj_dict['fq_name'][:-2]
        req_ip = obj_dict.get("floating_ip_address", None)
        try:
            fip_addr = cls.addr_mgmt.ip_alloc_req(
                vn_fq_name, asked_ip_addr=req_ip)
        except Exception as e:
            return (False, (500, str(e)))
        obj_dict['floating_ip_address'] = fip_addr
        print 'AddrMgmt: alloc %s FIP for vn=%s, tenant=%s, askip=%s' \
            % (obj_dict['floating_ip_address'], vn_fq_name, tenant_name,
               req_ip)
        return True, ""
    # end http_post_collection

    @classmethod
    def http_post_collection_fail(cls, tenant_name, obj_dict, db_conn):
        vn_fq_name = obj_dict['fq_name'][:-2]
        fip_addr = obj_dict['floating_ip_address']
        print 'AddrMgmt: free FIP %s for vn=%s tenant=%s, on post fail' % (fip_addr, vn_fq_name, tenant_name)
        cls.addr_mgmt.ip_free_req(fip_addr, vn_fq_name)
        return True, ""
    # end http_post_collection_fail

    @classmethod
    def http_delete(cls, id, obj_dict, db_conn):
        vn_fq_name = obj_dict['fq_name'][:-2]
        fip_addr = obj_dict['floating_ip_address']
        print 'AddrMgmt: free FIP %s for vn=%s' % (fip_addr, vn_fq_name)
        cls.addr_mgmt.ip_free_req(fip_addr, vn_fq_name)
        return True, ""
    # end http_delete

    @classmethod
    def http_delete_fail(cls, id, obj_dict, db_conn):
        vn_fq_name = obj_dict['fq_name'][:-2]
        req_ip = obj_dict.get("floating_ip_address", None)
        if req_ip == None:
            return True, ""
        try:
            cls.addr_mgmt.ip_alloc_req(vn_fq_name, asked_ip_addr=req_ip)
        except Exception as e:
            return (False, (500, str(e)))
        print 'AddrMgmt: alloc %s FIP for vn=%s, tenant=%s to recover DELETE failure' \
            % (obj_dict['floating_ip_address'], vn_fq_name, tenant_name)
        return True, ""
    # end http_delete_fail

    @classmethod
    def dbe_create_notification(cls, obj_ids, obj_dict):
        fip_addr = obj_dict['floating_ip_address']
        vn_fq_name = obj_dict['fq_name'][:-2]
        cls.addr_mgmt.ip_alloc_notify(fip_addr, vn_fq_name)
    # end dbe_create_notification

    @classmethod
    def dbe_delete_notification(cls, obj_ids, obj_dict):
        fip_addr = obj_dict['floating_ip_address']
        vn_fq_name = obj_dict['fq_name'][:-2]
        cls.addr_mgmt.ip_free_notify(fip_addr, vn_fq_name)
    # end dbe_delete_notification

# end class FloatingIpServer


class InstanceIpServer(InstanceIpServerGen):
    generate_default_instance = False

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        if ((vn_fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (vn_fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric and link-local address allocations
            return True,  ""

        req_ip = obj_dict.get("instance_ip_address", None)
        try:
            ip_addr = cls.addr_mgmt.ip_alloc_req(
                vn_fq_name, asked_ip_addr=req_ip)
        except Exception as e:
            return (False, (500, str(e)))
        obj_dict['instance_ip_address'] = ip_addr
        print 'AddrMgmt: alloc %s for vn=%s, tenant=%s, askip=%s' \
            % (obj_dict['instance_ip_address'],
               vn_fq_name, tenant_name, req_ip)
        return True, ""
    # end http_post_collection

    @classmethod
    def http_post_collection_fail(cls, tenant_name, obj_dict, db_conn):
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        if ((vn_fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (vn_fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric and link-local address allocations
            return True,  ""

        ip_addr = obj_dict['instance_ip_address']
        print 'AddrMgmt: free IP %s, vn=%s tenant=%s on post fail' % (ip_addr, vn_fq_name, tenant_name)
        cls.addr_mgmt.ip_free_req(ip_addr, vn_fq_name)
        return True, ""
    # end http_post_collection_fail

    @classmethod
    def http_delete(cls, id, obj_dict, db_conn):
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        if ((vn_fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (vn_fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric and link-local address allocations
            return True,  ""

        ip_addr = obj_dict['instance_ip_address']
        print 'AddrMgmt: free IP %s, vn=%s' % (ip_addr, vn_fq_name)
        cls.addr_mgmt.ip_free_req(ip_addr, vn_fq_name)
        return True, ""
    # end http_delete

    @classmethod
    def http_delete_fail(cls, id, obj_dict, db_conn):
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        if ((vn_fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (vn_fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric and link-local address allocations
            return True,  ""

        req_ip = obj_dict.get("instance_ip_address", None)
        if req_ip == None:
            return True, ""
        try:
            cls.addr_mgmt.ip_alloc_req(vn_fq_name, asked_ip_addr=req_ip)
        except Exception as e:
            return (False, (500, str(e)))
        print 'AddrMgmt: alloc %s for vn=%s, tenant=%s to recover DELETE failure' \
            % (obj_dict['instance_ip_address'], vn_fq_name, tenant_name)
        return True, ""
    # end http_delete_fail

    @classmethod
    def dbe_create_notification(cls, obj_ids, obj_dict):
        ip_addr = obj_dict['instance_ip_address']
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        cls.addr_mgmt.ip_alloc_notify(ip_addr, vn_fq_name)
    # end dbe_create_notification

    @classmethod
    def dbe_delete_notification(cls, obj_ids, obj_dict):
        ip_addr = obj_dict['instance_ip_address']
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        cls.addr_mgmt.ip_free_notify(ip_addr, vn_fq_name)
    # end dbe_delete_notification

# end class InstanceIpServer


class LogicalRouterServer(LogicalRouterServerGen):
    generate_default_instance = False

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        try:
            fq_name = obj_dict['fq_name']
            proj_uuid = db_conn.fq_name_to_uuid('project', fq_name[0:2])
        except NoIdError:
            return (False, (500, 'No Project ID error : ' + proj_uuid))

        (ok, proj_dict) = QuotaHelper.get_project_dict(proj_uuid, db_conn)
        if not ok:
            return (False, (500, 'Internal error : ' + pformat(proj_dict)))

        obj_type = 'logical-router'
        QuotaHelper.ensure_quota_project_present(obj_type, proj_uuid, proj_dict, db_conn)
        if 'logical_routers' in proj_dict:
            quota_count = len(proj_dict['logical_routers'])
            (ok, quota_limit) = QuotaHelper.check_quota_limit(proj_dict, obj_type, quota_count)
            if not ok:
                return (False, (403, pformat(obj_dict['fq_name']) + ' : ' + quota_limit))
        return True, ""
    # end http_post_collection

# end class LogicalRouterServer


class VirtualMachineInterfaceServer(VirtualMachineInterfaceServerGen):
    generate_default_instance = False

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        vn_dict = obj_dict['virtual_network_refs'][0]
        vn_uuid = vn_dict['uuid']
        (ok, vn_dict) = QuotaHelper.get_objtype_dict(vn_uuid, 'virtual-network', db_conn)
        if not ok:
            return (False, (500, 'Internal error : ' + pformat(vn_dict)))

        if vn_dict['parent_type'] == 'project':
            proj_uuid = vn_dict['parent_uuid']
            (ok, proj_dict) = QuotaHelper.get_project_dict(proj_uuid, db_conn)
            if not ok:
                return (False, (500, 'Internal error : ' + pformat(proj_dict)))

            obj_type = 'virtual-machine-interface'
            QuotaHelper.ensure_quota_project_present(obj_type, proj_uuid, proj_dict, db_conn)
            if 'virtual_machine_interfaces' in proj_dict:
                quota_count = len(proj_dict['virtual_machine_interfaces'])
                (ok, quota_limit) = QuotaHelper.check_quota_limit(proj_dict, obj_type, quota_count)
                if not ok:
                    return (False, (403, pformat(obj_dict['fq_name']) + ' : ' + quota_limit))

        inmac = None
        if 'virtual_machine_interface_mac_addresses' in obj_dict:
            mc = obj_dict['virtual_machine_interface_mac_addresses']
            if 'mac_address' in mc:
                if len(mc['mac_address'])==1:
                    inmac = mc['mac_address']
        if inmac!=None:
            mac_addrs_obj = MacAddressesType(inmac)
        else:
            mac_addr = cls.addr_mgmt.mac_alloc(obj_dict)
            mac_addrs_obj = MacAddressesType([mac_addr])
        mac_addrs_json = json.dumps(
            mac_addrs_obj,
            default=lambda o: dict((k, v)
                                   for k, v in o.__dict__.iteritems()))
        mac_addrs_dict = json.loads(mac_addrs_json)
        obj_dict['virtual_machine_interface_mac_addresses'] = mac_addrs_dict
        return True, ""
    # end http_post_collection

# end class VirtualMachineInterfaceServer


class VirtualNetworkServer(VirtualNetworkServerGen):

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        try:
            fq_name = obj_dict['fq_name']
            proj_uuid = db_conn.fq_name_to_uuid('project', fq_name[0:2])
        except NoIdError:
            return (False, (500, 'No Project ID error : ' + proj_uuid))

        (ok, proj_dict) = QuotaHelper.get_project_dict(proj_uuid, db_conn)
        if not ok:
            return (False, (500, 'Internal error : ' + pformat(proj_dict)))

        obj_type = 'virtual-network'
        QuotaHelper.ensure_quota_project_present(obj_type, proj_uuid, proj_dict, db_conn)
        if 'virtual_networks' in proj_dict:
            quota_count = len(proj_dict['virtual_networks'])
            (ok, quota_limit) = QuotaHelper.check_quota_limit(proj_dict, obj_type, quota_count)
            if not ok:
                return (False, (403, pformat(obj_dict['fq_name']) + ' : ' + quota_limit))

        try:
            cls.addr_mgmt.net_create_req(obj_dict)
        except Exception as e:
            return (False, (500, str(e)))

        return True, ""
    # end http_post_collection

    @classmethod
    def http_post_collection_fail(cls, tenant_name, obj_dict, db_conn):
        cls.addr_mgmt.net_delete_req(obj_dict)
        return True, ""
    # end post_collection_fail

    @classmethod
    def http_put(cls, id, fq_name, obj_dict, db_conn):
        if ((fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric subnet updates
            return True,  ""

        ipam_refs = obj_dict.get('network_ipam_refs', None)
        if not ipam_refs:
            # NOP for addr-mgmt module
            return True,  ""

        vn_id = {'uuid': id}
        (read_ok, read_result) = db_conn.dbe_read('virtual-network', vn_id)
        if not read_ok:
            return (False, (500, read_result))
        (ok, result) = cls.addr_mgmt.net_check_subnet_quota(read_result,
                                                            obj_dict, db_conn)
        if not ok:
            return (ok, (403, result))
        (ok, result) = cls.addr_mgmt.net_check_subnet_overlap(read_result,
                                                              obj_dict)
        if not ok:
            return (ok, (409, result))
        (ok, result) = cls.addr_mgmt.net_check_subnet_delete(read_result,
                                                             obj_dict)
        if not ok:
            return (ok, (409, result))

        try:
            cls.addr_mgmt.net_update_req(fq_name, read_result, obj_dict, id)
        except Exception as e:
            return (False, (500, str(e)))

        return True, ""
    # end http_put

    @classmethod<commit_msg>Don't ignore lack of network_ipam_refs in request for addr-mgmt purposes if there already as a ref to ipam<commit_after>#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
#
# This file is built up from an autogenerated template resource_server.py and
# contains code/hooks at different point during processing a request, specific
# to type of resource. For eg. allocation of mac/ip-addr for a port during its
# creation.

import json
import re

import cfgm_common
from vnc_quota import *
from gen.resource_xsd import *
from gen.resource_common import *
from gen.resource_server import *
from pprint import pformat


class FloatingIpServer(FloatingIpServerGen):
    generate_default_instance = False

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        proj_dict = obj_dict['project_refs'][0]
        if 'uuid' in proj_dict:
            proj_uuid = proj_dict['uuid']
        else:
            proj_uuid = db_conn.fq_name_to_uuid('project', proj_dict['to'])
        (ok, proj_dict) = QuotaHelper.get_project_dict(proj_uuid, db_conn)
        if not ok:
            return (False, (500, 'Internal error : ' + pformat(proj_dict)))

        obj_type = 'floating-ip'
        QuotaHelper.ensure_quota_project_present(obj_type, proj_uuid, proj_dict, db_conn)

        if 'floating_ip_back_refs' in proj_dict:
            quota_count = len(proj_dict['floating_ip_back_refs'])
            (ok, quota_limit) = QuotaHelper.check_quota_limit(proj_dict, obj_type, quota_count)
            if not ok:
                return (False, (403, pformat(obj_dict['fq_name']) + ' : ' + quota_limit))

        vn_fq_name = obj_dict['fq_name'][:-2]
        req_ip = obj_dict.get("floating_ip_address", None)
        try:
            fip_addr = cls.addr_mgmt.ip_alloc_req(
                vn_fq_name, asked_ip_addr=req_ip)
        except Exception as e:
            return (False, (500, str(e)))
        obj_dict['floating_ip_address'] = fip_addr
        print 'AddrMgmt: alloc %s FIP for vn=%s, tenant=%s, askip=%s' \
            % (obj_dict['floating_ip_address'], vn_fq_name, tenant_name,
               req_ip)
        return True, ""
    # end http_post_collection

    @classmethod
    def http_post_collection_fail(cls, tenant_name, obj_dict, db_conn):
        vn_fq_name = obj_dict['fq_name'][:-2]
        fip_addr = obj_dict['floating_ip_address']
        print 'AddrMgmt: free FIP %s for vn=%s tenant=%s, on post fail' % (fip_addr, vn_fq_name, tenant_name)
        cls.addr_mgmt.ip_free_req(fip_addr, vn_fq_name)
        return True, ""
    # end http_post_collection_fail

    @classmethod
    def http_delete(cls, id, obj_dict, db_conn):
        vn_fq_name = obj_dict['fq_name'][:-2]
        fip_addr = obj_dict['floating_ip_address']
        print 'AddrMgmt: free FIP %s for vn=%s' % (fip_addr, vn_fq_name)
        cls.addr_mgmt.ip_free_req(fip_addr, vn_fq_name)
        return True, ""
    # end http_delete

    @classmethod
    def http_delete_fail(cls, id, obj_dict, db_conn):
        vn_fq_name = obj_dict['fq_name'][:-2]
        req_ip = obj_dict.get("floating_ip_address", None)
        if req_ip == None:
            return True, ""
        try:
            cls.addr_mgmt.ip_alloc_req(vn_fq_name, asked_ip_addr=req_ip)
        except Exception as e:
            return (False, (500, str(e)))
        print 'AddrMgmt: alloc %s FIP for vn=%s, tenant=%s to recover DELETE failure' \
            % (obj_dict['floating_ip_address'], vn_fq_name, tenant_name)
        return True, ""
    # end http_delete_fail

    @classmethod
    def dbe_create_notification(cls, obj_ids, obj_dict):
        fip_addr = obj_dict['floating_ip_address']
        vn_fq_name = obj_dict['fq_name'][:-2]
        cls.addr_mgmt.ip_alloc_notify(fip_addr, vn_fq_name)
    # end dbe_create_notification

    @classmethod
    def dbe_delete_notification(cls, obj_ids, obj_dict):
        fip_addr = obj_dict['floating_ip_address']
        vn_fq_name = obj_dict['fq_name'][:-2]
        cls.addr_mgmt.ip_free_notify(fip_addr, vn_fq_name)
    # end dbe_delete_notification

# end class FloatingIpServer


class InstanceIpServer(InstanceIpServerGen):
    generate_default_instance = False

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        if ((vn_fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (vn_fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric and link-local address allocations
            return True,  ""

        req_ip = obj_dict.get("instance_ip_address", None)
        try:
            ip_addr = cls.addr_mgmt.ip_alloc_req(
                vn_fq_name, asked_ip_addr=req_ip)
        except Exception as e:
            return (False, (500, str(e)))
        obj_dict['instance_ip_address'] = ip_addr
        print 'AddrMgmt: alloc %s for vn=%s, tenant=%s, askip=%s' \
            % (obj_dict['instance_ip_address'],
               vn_fq_name, tenant_name, req_ip)
        return True, ""
    # end http_post_collection

    @classmethod
    def http_post_collection_fail(cls, tenant_name, obj_dict, db_conn):
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        if ((vn_fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (vn_fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric and link-local address allocations
            return True,  ""

        ip_addr = obj_dict['instance_ip_address']
        print 'AddrMgmt: free IP %s, vn=%s tenant=%s on post fail' % (ip_addr, vn_fq_name, tenant_name)
        cls.addr_mgmt.ip_free_req(ip_addr, vn_fq_name)
        return True, ""
    # end http_post_collection_fail

    @classmethod
    def http_delete(cls, id, obj_dict, db_conn):
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        if ((vn_fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (vn_fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric and link-local address allocations
            return True,  ""

        ip_addr = obj_dict['instance_ip_address']
        print 'AddrMgmt: free IP %s, vn=%s' % (ip_addr, vn_fq_name)
        cls.addr_mgmt.ip_free_req(ip_addr, vn_fq_name)
        return True, ""
    # end http_delete

    @classmethod
    def http_delete_fail(cls, id, obj_dict, db_conn):
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        if ((vn_fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (vn_fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric and link-local address allocations
            return True,  ""

        req_ip = obj_dict.get("instance_ip_address", None)
        if req_ip == None:
            return True, ""
        try:
            cls.addr_mgmt.ip_alloc_req(vn_fq_name, asked_ip_addr=req_ip)
        except Exception as e:
            return (False, (500, str(e)))
        print 'AddrMgmt: alloc %s for vn=%s, tenant=%s to recover DELETE failure' \
            % (obj_dict['instance_ip_address'], vn_fq_name, tenant_name)
        return True, ""
    # end http_delete_fail

    @classmethod
    def dbe_create_notification(cls, obj_ids, obj_dict):
        ip_addr = obj_dict['instance_ip_address']
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        cls.addr_mgmt.ip_alloc_notify(ip_addr, vn_fq_name)
    # end dbe_create_notification

    @classmethod
    def dbe_delete_notification(cls, obj_ids, obj_dict):
        ip_addr = obj_dict['instance_ip_address']
        vn_fq_name = obj_dict['virtual_network_refs'][0]['to']
        cls.addr_mgmt.ip_free_notify(ip_addr, vn_fq_name)
    # end dbe_delete_notification

# end class InstanceIpServer


class LogicalRouterServer(LogicalRouterServerGen):
    generate_default_instance = False

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        try:
            fq_name = obj_dict['fq_name']
            proj_uuid = db_conn.fq_name_to_uuid('project', fq_name[0:2])
        except NoIdError:
            return (False, (500, 'No Project ID error : ' + proj_uuid))

        (ok, proj_dict) = QuotaHelper.get_project_dict(proj_uuid, db_conn)
        if not ok:
            return (False, (500, 'Internal error : ' + pformat(proj_dict)))

        obj_type = 'logical-router'
        QuotaHelper.ensure_quota_project_present(obj_type, proj_uuid, proj_dict, db_conn)
        if 'logical_routers' in proj_dict:
            quota_count = len(proj_dict['logical_routers'])
            (ok, quota_limit) = QuotaHelper.check_quota_limit(proj_dict, obj_type, quota_count)
            if not ok:
                return (False, (403, pformat(obj_dict['fq_name']) + ' : ' + quota_limit))
        return True, ""
    # end http_post_collection

# end class LogicalRouterServer


class VirtualMachineInterfaceServer(VirtualMachineInterfaceServerGen):
    generate_default_instance = False

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        vn_dict = obj_dict['virtual_network_refs'][0]
        vn_uuid = vn_dict['uuid']
        (ok, vn_dict) = QuotaHelper.get_objtype_dict(vn_uuid, 'virtual-network', db_conn)
        if not ok:
            return (False, (500, 'Internal error : ' + pformat(vn_dict)))

        if vn_dict['parent_type'] == 'project':
            proj_uuid = vn_dict['parent_uuid']
            (ok, proj_dict) = QuotaHelper.get_project_dict(proj_uuid, db_conn)
            if not ok:
                return (False, (500, 'Internal error : ' + pformat(proj_dict)))

            obj_type = 'virtual-machine-interface'
            QuotaHelper.ensure_quota_project_present(obj_type, proj_uuid, proj_dict, db_conn)
            if 'virtual_machine_interfaces' in proj_dict:
                quota_count = len(proj_dict['virtual_machine_interfaces'])
                (ok, quota_limit) = QuotaHelper.check_quota_limit(proj_dict, obj_type, quota_count)
                if not ok:
                    return (False, (403, pformat(obj_dict['fq_name']) + ' : ' + quota_limit))

        inmac = None
        if 'virtual_machine_interface_mac_addresses' in obj_dict:
            mc = obj_dict['virtual_machine_interface_mac_addresses']
            if 'mac_address' in mc:
                if len(mc['mac_address'])==1:
                    inmac = mc['mac_address']
        if inmac!=None:
            mac_addrs_obj = MacAddressesType(inmac)
        else:
            mac_addr = cls.addr_mgmt.mac_alloc(obj_dict)
            mac_addrs_obj = MacAddressesType([mac_addr])
        mac_addrs_json = json.dumps(
            mac_addrs_obj,
            default=lambda o: dict((k, v)
                                   for k, v in o.__dict__.iteritems()))
        mac_addrs_dict = json.loads(mac_addrs_json)
        obj_dict['virtual_machine_interface_mac_addresses'] = mac_addrs_dict
        return True, ""
    # end http_post_collection

# end class VirtualMachineInterfaceServer


class VirtualNetworkServer(VirtualNetworkServerGen):

    @classmethod
    def http_post_collection(cls, tenant_name, obj_dict, db_conn):
        try:
            fq_name = obj_dict['fq_name']
            proj_uuid = db_conn.fq_name_to_uuid('project', fq_name[0:2])
        except NoIdError:
            return (False, (500, 'No Project ID error : ' + proj_uuid))

        (ok, proj_dict) = QuotaHelper.get_project_dict(proj_uuid, db_conn)
        if not ok:
            return (False, (500, 'Internal error : ' + pformat(proj_dict)))

        obj_type = 'virtual-network'
        QuotaHelper.ensure_quota_project_present(obj_type, proj_uuid, proj_dict, db_conn)
        if 'virtual_networks' in proj_dict:
            quota_count = len(proj_dict['virtual_networks'])
            (ok, quota_limit) = QuotaHelper.check_quota_limit(proj_dict, obj_type, quota_count)
            if not ok:
                return (False, (403, pformat(obj_dict['fq_name']) + ' : ' + quota_limit))

        try:
            cls.addr_mgmt.net_create_req(obj_dict)
        except Exception as e:
            return (False, (500, str(e)))

        return True, ""
    # end http_post_collection

    @classmethod
    def http_post_collection_fail(cls, tenant_name, obj_dict, db_conn):
        cls.addr_mgmt.net_delete_req(obj_dict)
        return True, ""
    # end post_collection_fail

    @classmethod
    def http_put(cls, id, fq_name, obj_dict, db_conn):
        if ((fq_name == cfgm_common.IP_FABRIC_VN_FQ_NAME) or
                (fq_name == cfgm_common.LINK_LOCAL_VN_FQ_NAME)):
            # Ignore ip-fabric subnet updates
            return True,  ""

        vn_id = {'uuid': id}
        (read_ok, read_result) = db_conn.dbe_read('virtual-network', vn_id)
        if not read_ok:
            return (False, (500, read_result))

        old_ipam_refs = read_result.get('network_ipam_refs')
        new_ipam_refs = obj_dict.get('network_ipam_refs')
        if not old_ipam_refs and not new_ipam_refs:
            # NOP for addr-mgmt module
            return True,  ""

        (ok, result) = cls.addr_mgmt.net_check_subnet_quota(read_result,
                                                            obj_dict, db_conn)
        if not ok:
            return (ok, (403, result))
        (ok, result) = cls.addr_mgmt.net_check_subnet_overlap(read_result,
                                                              obj_dict)
        if not ok:
            return (ok, (409, result))
        (ok, result) = cls.addr_mgmt.net_check_subnet_delete(read_result,
                                                             obj_dict)
        if not ok:
            return (ok, (409, result))

        try:
            cls.addr_mgmt.net_update_req(fq_name, read_result, obj_dict, id)
        except Exception as e:
            return (False, (500, str(e)))

        return True, ""
    # end http_put

    @classmethod
{'fraction_non_alphanumeric': 0.09914870494475639, 'fraction_numerical': 0.006230755297953269, 'mean_word_length': 2.6190351337178814, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7376666', 'n_tokens_mistral': 9662, 'n_tokens_neox': 9265, 'n_words': 1816}