<commit_before>#!/usr/bin/env python
#
# Copyright 2015 <EMAIL>
#
# 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.
#
##############################################################################
#
# Bootstrapping a floating ip network every needed configuration in
# OpenContrail.
#
# Basically it creates:
#  * virtual network with proerties and subnet
#  * network policy
#  * network ipam
#  * floating ip pool
#  * route target
# 
import re
import os
import sys
import logging
try:
    # for kilo ff
    from oslo_config import cfg
    from oslo_config import types
except ImportError:
    from oslo.config import cfg
    from oslo.config import types
from keystoneclient.v2_0 import client
from vnc_api import vnc_api
from vnc_api.gen.resource_client import FloatingIpPool
from vnc_api.gen.resource_client import VirtualNetwork
from vnc_api.gen.resource_client import NetworkIpam
from vnc_api.gen.resource_client import NetworkPolicy

from vnc_api.gen.resource_xsd import VnSubnetsType
from vnc_api.gen.resource_xsd import IpamSubnetType
from vnc_api.gen.resource_xsd import SubnetType

from vnc_api.gen.resource_xsd import PolicyEntriesType
from vnc_api.gen.resource_xsd import PolicyRuleType
from vnc_api.gen.resource_xsd import AddressType
from vnc_api.gen.resource_xsd import PortType
from vnc_api.gen.resource_xsd import ActionListType
from vnc_api.gen.resource_xsd import VirtualNetworkPolicyType
from vnc_api.gen.resource_xsd import RouteTargetList
from vnc_api.gen.resource_xsd import VirtualNetworkType
from vnc_api.gen.resource_xsd import SequenceType

from cfgm_common.exceptions import NoIdError

class RouteTarget(object):
    """ Validates Route Target Param """
    def __init__(self):
        self.rt_pat = re.compile('[0-9]+:[0-9]+')

    def __call__(self, value):
        if not self.rt_pat.match(value):
            raise ValueError('%s is not a valid route target (1234:4321)' % value)
        return value

    def __repr__(self):
        return "RouteTarget"

    def __eq__(self, other):
        self.__class__ == other.__class__

_default_net_name = 'float24'
conf = {
        'default_project': ['default-domain', 'admin'],
        'default_net_name': _default_net_name,
        'default_net': ['default-domain', 'admin', _default_net_name],
        'default_pool': ['default-domain', u'admin', _default_net_name, 'default'],
        'default_network_policy': ['default-domain', 'admin', 'default'],
        'default_allow_transit': True,
        'default_api_address': '127.0.0.1',
        }

def set_default_net_name(value):
    conf['default_net_name'] = value
    conf['default_net'][2] = value
    conf['default_pool'][2] = value

_common_opts = [
    cfg.Opt('ip_prefix',
            type=types.IPAddress(),
            help='IP address prefix of the floating ip network. Example: 10.0.0.0'),
    cfg.IntOpt('ip_prefix_len',
               help='IP address prefix length of the floating ip network. Example: 24 for ip_prefix/24'),
    cfg.Opt('gateway',
            type=types.IPAddress(),
            help='Default gatway of the floating ip network. Example: 10.0.0.1'),
    cfg.Opt('route_target',
            type=RouteTarget(),
            help='Default route target of the floating ip network. Example: 65000:30'),
    cfg.StrOpt('net_name',
               default=conf.get('default_net_name'),
               help='Name of the floating ip network. Default: %s' % conf.get('default_net_name')),
    cfg.StrOpt('api_address',
        default=conf.get('default_api_address'),
        help='Name of the floating ip network. Default: %s' % conf.get('default_api_address')),
        ]

log = logging.getLogger(__name__)

def get_keystone_client():
    """ Get a configured and connected keystoneclient """
    username = os.environ.get('OS_USERNAME')
    password = <PASSWORD>('<PASSWORD>')
    tenant = os.environ.get('OS_TENANT_NAME')
    url = os.environ.get('OS_AUTH_URL')
    assert username is not None
    assert password is not None
    assert tenant is not None
    assert url is not None
    cl = client.Client(username=username, password=password,
                       tenant_name=tenant, auth_url=url)
    return cl

def get_keystone_token(con):
    """ Get the keystone token """
    return con.get_token(con.session)

def get_project(con):
    """ Returns vnc_api.gen.resource_client.Project from api or None """
    try:
        return con.project_read(fq_name=conf.get('default_project', 'UNEXPECTED_VALUE'))
    except:
        log.debug('Unable to find project default-domain, admin:', exc_info=True)
        return None

def get_net(con):
    """ Returns vnc_api.gen.resource_client.VirtualNetwork from api or None """
    try:
        return con.virtual_network_read(fq_name=conf.get('default_net', 'UNEXPECTED_VALUE'))
    except NoIdError:
        log.debug('Unable to find net.')
        return None

def get_default_pool(con):
    """ Returns vnc_api.gen.resource_client.FloatingIpPool from api or None """
    try:
        return con.floating_ip_pool_read(fq_name=conf.get('default_pool', 'UNEXPECTED_VALUE'))
    except NoIdError:
        log.debug('Unable to find pool.')
        return None

def get_default_ipam(con):
    """ Returns vnc_api.gen.resource_client.NetworkIpam from api or None """
    try:
        return con.network_ipam_read(fq_name=conf.get('default_net', 'UNEXPECTED_VALUE'))
    except NoIdError:
        log.debug('Unable to find net ipam')
        return None

def get_default_network_policy(con):
    """ Returns vnc_api.gen.resource_client.NetworkPolicy from api or None """
    try:
        return con.network_policy_read(fq_name=conf.get('default_network_policy', 'UNEXPECTED_VALUE'))
    except NoIdError:
        log.debug('Unable to find default_network_policy')
        return None

def gen_virtual_network(project, route_target_list, virtual_network_properties):
    vnet = VirtualNetwork(name=conf.get('default_net_name'),
                          parent_obj=project,
                          route_target_list=route_target_list,
                          virtual_network_properties=virtual_network_properties)
    return vnet

def gen_route_target_list(route_target):
    """ Returns a configured vnc_api.gen.resource_xsd.RouteTargetList """
    rt = RouteTargetList(route_target=route_target)
    return rt

def gen_virtual_network_properties(allow_transit):
    """ Returns a configured vnc_api.gen.resource_xsd.VirtualNetworkType """
    props = VirtualNetworkType(allow_transit=allow_transit)
    return props<commit_msg>os-606 set router_external to True<commit_after>#!/usr/bin/env python
#
# Copyright 2015 <EMAIL>
#
# 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.
#
##############################################################################
#
# Bootstrapping a floating ip network every needed configuration in
# OpenContrail.
#
# Basically it creates:
#  * virtual network with proerties and subnet
#  * network policy
#  * network ipam
#  * floating ip pool
#  * route target
# 
import re
import os
import sys
import logging
try:
    # for kilo ff
    from oslo_config import cfg
    from oslo_config import types
except ImportError:
    from oslo.config import cfg
    from oslo.config import types
from keystoneclient.v2_0 import client
from vnc_api import vnc_api
from vnc_api.gen.resource_client import FloatingIpPool
from vnc_api.gen.resource_client import VirtualNetwork
from vnc_api.gen.resource_client import NetworkIpam
from vnc_api.gen.resource_client import NetworkPolicy

from vnc_api.gen.resource_xsd import VnSubnetsType
from vnc_api.gen.resource_xsd import IpamSubnetType
from vnc_api.gen.resource_xsd import SubnetType

from vnc_api.gen.resource_xsd import PolicyEntriesType
from vnc_api.gen.resource_xsd import PolicyRuleType
from vnc_api.gen.resource_xsd import AddressType
from vnc_api.gen.resource_xsd import PortType
from vnc_api.gen.resource_xsd import ActionListType
from vnc_api.gen.resource_xsd import VirtualNetworkPolicyType
from vnc_api.gen.resource_xsd import RouteTargetList
from vnc_api.gen.resource_xsd import VirtualNetworkType
from vnc_api.gen.resource_xsd import SequenceType

from cfgm_common.exceptions import NoIdError

class RouteTarget(object):
    """ Validates Route Target Param """
    def __init__(self):
        self.rt_pat = re.compile('[0-9]+:[0-9]+')

    def __call__(self, value):
        if not self.rt_pat.match(value):
            raise ValueError('%s is not a valid route target (1234:4321)' % value)
        return value

    def __repr__(self):
        return "RouteTarget"

    def __eq__(self, other):
        self.__class__ == other.__class__

_default_net_name = 'float24'
conf = {
        'default_project': ['default-domain', 'admin'],
        'default_net_name': _default_net_name,
        'default_net': ['default-domain', 'admin', _default_net_name],
        'default_pool': ['default-domain', u'admin', _default_net_name, 'default'],
        'default_network_policy': ['default-domain', 'admin', 'default'],
        'default_allow_transit': True,
        'default_api_address': '127.0.0.1',
        }

def set_default_net_name(value):
    conf['default_net_name'] = value
    conf['default_net'][2] = value
    conf['default_pool'][2] = value

_common_opts = [
    cfg.Opt('ip_prefix',
            type=types.IPAddress(),
            help='IP address prefix of the floating ip network. Example: 10.0.0.0'),
    cfg.IntOpt('ip_prefix_len',
               help='IP address prefix length of the floating ip network. Example: 24 for ip_prefix/24'),
    cfg.Opt('gateway',
            type=types.IPAddress(),
            help='Default gatway of the floating ip network. Example: 10.0.0.1'),
    cfg.Opt('route_target',
            type=RouteTarget(),
            help='Default route target of the floating ip network. Example: 65000:30'),
    cfg.StrOpt('net_name',
               default=conf.get('default_net_name'),
               help='Name of the floating ip network. Default: %s' % conf.get('default_net_name')),
    cfg.StrOpt('api_address',
        default=conf.get('default_api_address'),
        help='Name of the floating ip network. Default: %s' % conf.get('default_api_address')),
        ]

log = logging.getLogger(__name__)

def get_keystone_client():
    """ Get a configured and connected keystoneclient """
    username = os.environ.get('OS_USERNAME')
    password = <PASSWORD>('OS_PASSWORD')
    tenant = os.environ.get('OS_TENANT_NAME')
    url = os.environ.get('OS_AUTH_URL')
    assert username is not None
    assert password is not None
    assert tenant is not None
    assert url is not None
    cl = client.Client(username=username, password=password,
                       tenant_name=tenant, auth_url=url)
    return cl

def get_keystone_token(con):
    """ Get the keystone token """
    return con.get_token(con.session)

def get_project(con):
    """ Returns vnc_api.gen.resource_client.Project from api or None """
    try:
        return con.project_read(fq_name=conf.get('default_project', 'UNEXPECTED_VALUE'))
    except:
        log.debug('Unable to find project default-domain, admin:', exc_info=True)
        return None

def get_net(con):
    """ Returns vnc_api.gen.resource_client.VirtualNetwork from api or None """
    try:
        return con.virtual_network_read(fq_name=conf.get('default_net', 'UNEXPECTED_VALUE'))
    except NoIdError:
        log.debug('Unable to find net.')
        return None

def get_default_pool(con):
    """ Returns vnc_api.gen.resource_client.FloatingIpPool from api or None """
    try:
        return con.floating_ip_pool_read(fq_name=conf.get('default_pool', 'UNEXPECTED_VALUE'))
    except NoIdError:
        log.debug('Unable to find pool.')
        return None

def get_default_ipam(con):
    """ Returns vnc_api.gen.resource_client.NetworkIpam from api or None """
    try:
        return con.network_ipam_read(fq_name=conf.get('default_net', 'UNEXPECTED_VALUE'))
    except NoIdError:
        log.debug('Unable to find net ipam')
        return None

def get_default_network_policy(con):
    """ Returns vnc_api.gen.resource_client.NetworkPolicy from api or None """
    try:
        return con.network_policy_read(fq_name=conf.get('default_network_policy', 'UNEXPECTED_VALUE'))
    except NoIdError:
        log.debug('Unable to find default_network_policy')
        return None

def gen_virtual_network(project, route_target_list, virtual_network_properties):
    vnet = VirtualNetwork(name=conf.get('default_net_name'),
                          parent_obj=project,
                          route_target_list=route_target_list,
                          router_external=True,
                          virtual_network_properties=virtual_network_properties)
    return vnet

def gen_route_target_list(route_target):
    """ Returns a configured vnc_api.gen.resource_xsd.RouteTargetList """
    rt = RouteTargetList(route_target=route_target)
    return rt

def gen_virtual_network_properties(allow_transit):
    """ Returns a configured vnc_api.gen.resource_xsd.VirtualNetworkType """
    props = VirtualNetworkType(allow_transit=allow_transit)
    return props
{'fraction_non_alphanumeric': 0.10600282485875706, 'fraction_numerical': 0.007697740112994351, 'mean_word_length': 3.6232451844596802, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5825590', 'n_tokens_mistral': 4291, 'n_tokens_neox': 4218, 'n_words': 1096}