<commit_before>#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
import logging
import requests
from requests.exceptions import ConnectionError

import ConfigParser
import pprint
from cfgm_common import jsonutils as json
import sys
import time
import platform
import functools
import __main__ as main
import ssl
import re
import os

import gen.resource_common
import gen.vnc_api_client_gen
from gen.vnc_api_client_gen import all_resource_type_tuples
from gen.resource_xsd import *
from gen.resource_client import *
from gen.generatedssuper import GeneratedsSuper

from cfgm_common import rest, utils
from cfgm_common.exceptions import *
from cfgm_common import ssl_adapter

from pprint import pformat


def check_homepage(func):
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        if not self._srv_root_url:
            homepage = self._request(rest.OP_GET, self._base_url,
                                    retry_on_error=False)
            self._parse_homepage(homepage)
        return func(self, *args, **kwargs)
    return wrapper

def compare_refs(old_refs, new_refs):
    # compare refs in an object
    old_ref_dict = dict((':'.join(ref['to']), ref['attr']) for ref in old_refs or [])
    new_ref_dict = dict((':'.join(ref['to']), ref['attr']) for ref in new_refs or [])
    return old_ref_dict == new_ref_dict
# end compare_refs

def get_object_class(res_type):
    cls_name = '%s' %(utils.CamelCase(res_type))
    return utils.str_to_class(cls_name, __name__)
# end get_object_class

def _read_cfg(cfg_parser, section, option, default):
        try:
            val = cfg_parser.get(section, option)
        except (AttributeError,
                ConfigParser.NoOptionError,
                ConfigParser.NoSectionError):
            val = default

        return val
#end _read_cfg

def _cfg_curl_logging(api_curl_log_file=None):
    formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s',datefmt='%Y/%m/%d %H:%M:%S')
    curl_logger = logging.getLogger('log_curl')
    curl_logger.setLevel(logging.DEBUG)
    api_curl_log_file="/var/log/contrail/%s" %(api_curl_log_file)
    if os.path.exists(api_curl_log_file):
        curl_log_handler = logging.FileHandler(api_curl_log_file,mode='a')
    else:
        curl_log_handler = logging.FileHandler(api_curl_log_file,mode='w')
    curl_log_handler.setFormatter(formatter)
    curl_logger.addHandler(curl_log_handler)
    return curl_logger
# End _cfg_curl_logging

class ActionUriDict(dict):
    """Action uri dictionary with operator([]) overloading to parse home page
       and populate the action_uri, if not populated already.
    """
    def __init__(self, vnc_api,  *args, **kwargs):
        dict.__init__(self, args, **kwargs)
        self.vnc_api = vnc_api

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            homepage = self.vnc_api._request(rest.OP_GET, self.vnc_api._base_url,
                                    retry_on_error=False)
            self.vnc_api._parse_homepage(homepage)
            return dict.__getitem__(self, key)


class VncApi(object):
    _DEFAULT_WEB_SERVER = "127.0.0.1"

    hostname = platform.node()
    _DEFAULT_HEADERS = {
        'Content-type': 'application/json; charset="UTF-8"',
        'X-Contrail-Useragent': '%s:%s'
             %(hostname, getattr(main, '__file__', '')),
    }

    _AUTHN_SUPPORTED_TYPES = ["keystone"]
    _DEFAULT_AUTHN_TYPE = "keystone"
    _DEFAULT_AUTHN_HEADERS = _DEFAULT_HEADERS
    _DEFAULT_AUTHN_PROTOCOL = "http"
    _DEFAULT_AUTHN_SERVER = _DEFAULT_WEB_SERVER
    _DEFAULT_AUTHN_PORT = 35357
    _DEFAULT_AUTHN_URL = "/v2.0/tokens"
    _DEFAULT_AUTHN_USER = ""
    _DEFAULT_AUTHN_PASSWORD = ""
    _DEFAULT_AUTHN_TENANT = 'default-tenant'
    _DEFAULT_DOMAIN_ID = "default"

    # Keystone and and vnc-api SSL support
    # contrail-api will remain to be on http
    # with LB (haproxy/F5/nginx..etc) configured for
    # ssl termination on port 8082(default contrail-api port)
    _DEFAULT_API_SERVER_CONNECT="http"
    _DEFAULT_API_SERVER_SSL_CONNECT="https"
    _DEFAULT_KS_CERT_BUNDLE="/tmp/keystonecertbundle.pem"
    _DEFAULT_API_CERT_BUNDLE="/tmp/apiservercertbundle.pem"

    # Connection to api-server through Quantum
    _DEFAULT_WEB_PORT = 8082
    _DEFAULT_BASE_URL = "/"

    # The number of items beyond which instead of GET /<collection>
    # a POST /list-bulk-collection is issued
    POST_FOR_LIST_THRESHOLD = 25

    # Number of pools and number of pool per conn to api-server
    _DEFAULT_MAX_POOLS = 100
    _DEFAULT_MAX_CONNS_PER_POOL = 100

    def __init__(self, username=None, password=<PASSWORD>, tenant_name=None,
                 api_server_host='127.0.0.1', api_server_port='8082',
                 api_server_url=None, conf_file=None, user_info=None,
                 auth_token=None, auth_host=None, auth_port=None,
                 auth_protocol = None, auth_url=None, auth_type=None,
                 wait_for_connect=False, api_server_use_ssl=False,
                 domain_name=None):
        # TODO allow for username/password to be present in creds file

        self._obj_serializer = self._obj_serializer_diff
        for object_type, resource_type in all_resource_type_tuples:
            for oper_str in ('_create', '_read', '_update', '_delete',
                         's_list', '_get_default_id'):
                method = getattr(self, '_object%s' %(oper_str))
                bound_method = functools.partial(method, resource_type)
                functools.update_wrapper(bound_method, method)
                if oper_str == '_get_default_id':
                    setattr(self, 'get_default_%s_id' % (object_type),
                            bound_method)
                else:
                    setattr(self, '%s%s' %(object_type, oper_str),
                            bound_method)

        cfg_parser = ConfigParser.ConfigParser()
        try:
            cfg_parser.read(conf_file or
                            "/etc/contrail/vnc_api_lib.ini")
        except Exception as e:
            logger = logging.getLogger(__name__)
            logger.warn("Exception: %s", str(e))

        self._api_connect_protocol = VncApi._DEFAULT_API_SERVER_CONNECT
        # API server SSL Support<commit_msg>web_server and web_port value from vnc_api_lib.ini not honored<commit_after>#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#
import logging
import requests
from requests.exceptions import ConnectionError

import ConfigParser
import pprint
from cfgm_common import jsonutils as json
import sys
import time
import platform
import functools
import __main__ as main
import ssl
import re
import os

import gen.resource_common
import gen.vnc_api_client_gen
from gen.vnc_api_client_gen import all_resource_type_tuples
from gen.resource_xsd import *
from gen.resource_client import *
from gen.generatedssuper import GeneratedsSuper

from cfgm_common import rest, utils
from cfgm_common.exceptions import *
from cfgm_common import ssl_adapter

from pprint import pformat


def check_homepage(func):
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        if not self._srv_root_url:
            homepage = self._request(rest.OP_GET, self._base_url,
                                    retry_on_error=False)
            self._parse_homepage(homepage)
        return func(self, *args, **kwargs)
    return wrapper

def compare_refs(old_refs, new_refs):
    # compare refs in an object
    old_ref_dict = dict((':'.join(ref['to']), ref['attr']) for ref in old_refs or [])
    new_ref_dict = dict((':'.join(ref['to']), ref['attr']) for ref in new_refs or [])
    return old_ref_dict == new_ref_dict
# end compare_refs

def get_object_class(res_type):
    cls_name = '%s' %(utils.CamelCase(res_type))
    return utils.str_to_class(cls_name, __name__)
# end get_object_class

def _read_cfg(cfg_parser, section, option, default):
        try:
            val = cfg_parser.get(section, option)
        except (AttributeError,
                ConfigParser.NoOptionError,
                ConfigParser.NoSectionError):
            val = default

        return val
#end _read_cfg

def _cfg_curl_logging(api_curl_log_file=None):
    formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s',datefmt='%Y/%m/%d %H:%M:%S')
    curl_logger = logging.getLogger('log_curl')
    curl_logger.setLevel(logging.DEBUG)
    api_curl_log_file="/var/log/contrail/%s" %(api_curl_log_file)
    if os.path.exists(api_curl_log_file):
        curl_log_handler = logging.FileHandler(api_curl_log_file,mode='a')
    else:
        curl_log_handler = logging.FileHandler(api_curl_log_file,mode='w')
    curl_log_handler.setFormatter(formatter)
    curl_logger.addHandler(curl_log_handler)
    return curl_logger
# End _cfg_curl_logging

class ActionUriDict(dict):
    """Action uri dictionary with operator([]) overloading to parse home page
       and populate the action_uri, if not populated already.
    """
    def __init__(self, vnc_api,  *args, **kwargs):
        dict.__init__(self, args, **kwargs)
        self.vnc_api = vnc_api

    def __getitem__(self, key):
        try:
            return dict.__getitem__(self, key)
        except KeyError:
            homepage = self.vnc_api._request(rest.OP_GET, self.vnc_api._base_url,
                                    retry_on_error=False)
            self.vnc_api._parse_homepage(homepage)
            return dict.__getitem__(self, key)


class VncApi(object):
    _DEFAULT_WEB_SERVER = "127.0.0.1"

    hostname = platform.node()
    _DEFAULT_HEADERS = {
        'Content-type': 'application/json; charset="UTF-8"',
        'X-Contrail-Useragent': '%s:%s'
             %(hostname, getattr(main, '__file__', '')),
    }

    _AUTHN_SUPPORTED_TYPES = ["keystone"]
    _DEFAULT_AUTHN_TYPE = "keystone"
    _DEFAULT_AUTHN_HEADERS = _DEFAULT_HEADERS
    _DEFAULT_AUTHN_PROTOCOL = "http"
    _DEFAULT_AUTHN_SERVER = _DEFAULT_WEB_SERVER
    _DEFAULT_AUTHN_PORT = 35357
    _DEFAULT_AUTHN_URL = "/v2.0/tokens"
    _DEFAULT_AUTHN_USER = ""
    _DEFAULT_AUTHN_PASSWORD = ""
    _DEFAULT_AUTHN_TENANT = 'default-tenant'
    _DEFAULT_DOMAIN_ID = "default"

    # Keystone and and vnc-api SSL support
    # contrail-api will remain to be on http
    # with LB (haproxy/F5/nginx..etc) configured for
    # ssl termination on port 8082(default contrail-api port)
    _DEFAULT_API_SERVER_CONNECT="http"
    _DEFAULT_API_SERVER_SSL_CONNECT="https"
    _DEFAULT_KS_CERT_BUNDLE="/tmp/keystonecertbundle.pem"
    _DEFAULT_API_CERT_BUNDLE="/tmp/apiservercertbundle.pem"

    # Connection to api-server through Quantum
    _DEFAULT_WEB_PORT = 8082
    _DEFAULT_BASE_URL = "/"

    # The number of items beyond which instead of GET /<collection>
    # a POST /list-bulk-collection is issued
    POST_FOR_LIST_THRESHOLD = 25

    # Number of pools and number of pool per conn to api-server
    _DEFAULT_MAX_POOLS = 100
    _DEFAULT_MAX_CONNS_PER_POOL = 100

    def __init__(self, username=None, password=<PASSWORD>, tenant_name=None,
                 api_server_host=None, api_server_port=None,
                 api_server_url=None, conf_file=None, user_info=None,
                 auth_token=<PASSWORD>, auth_host=None, auth_port=None,
                 auth_protocol = None, auth_url=None, auth_type=None,
                 wait_for_connect=False, api_server_use_ssl=False,
                 domain_name=None):
        # TODO allow for username/password to be present in creds file

        self._obj_serializer = self._obj_serializer_diff
        for object_type, resource_type in all_resource_type_tuples:
            for oper_str in ('_create', '_read', '_update', '_delete',
                         's_list', '_get_default_id'):
                method = getattr(self, '_object%s' %(oper_str))
                bound_method = functools.partial(method, resource_type)
                functools.update_wrapper(bound_method, method)
                if oper_str == '_get_default_id':
                    setattr(self, 'get_default_%s_id' % (object_type),
                            bound_method)
                else:
                    setattr(self, '%s%s' %(object_type, oper_str),
                            bound_method)

        cfg_parser = ConfigParser.ConfigParser()
        try:
            cfg_parser.read(conf_file or
                            "/etc/contrail/vnc_api_lib.ini")
        except Exception as e:
            logger = logging.getLogger(__name__)
            logger.warn("Exception: %s", str(e))

        self._api_connect_protocol = VncApi._DEFAULT_API_SERVER_CONNECT
        # API server SSL Support
{'fraction_non_alphanumeric': 0.09221311475409837, 'fraction_numerical': 0.006462799495586381, 'mean_word_length': 2.9382371198013657, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3270343', 'n_tokens_mistral': 4143, 'n_tokens_neox': 3991, 'n_words': 854}