<commit_before># Copyright (c) 2015 OpenStack Foundation.
# 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.

import copy
import os.path
import socket
import ssl
import time

import eventlet
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import excutils

from networking_l2gw._i18n import _LE, _LW
from networking_l2gw.services.l2gateway.common import constants as n_const

LOG = logging.getLogger(__name__)
OVSDB_UNREACHABLE_MSG = _LW('Unable to reach OVSDB server %s')
OVSDB_CONNECTED_MSG = 'Connected to OVSDB server %s'


class BaseConnection(object):
    """Connects to OVSDB server.

       Connects to an ovsdb server with/without SSL
       on a given host and TCP port.
    """
    def __init__(self, conf, gw_config, mgr=None):
        self.responses = []
        self.connected = False
        self.mgr = mgr
        self.enable_manager = cfg.CONF.ovsdb.enable_manager
        if self.enable_manager:
            self.manager_table_listening_port = (
                cfg.CONF.ovsdb.manager_table_listening_port)
            self.ip_ovsdb_mapping = self._get_ovsdb_ip_mapping()
            self.s = None
            self.check_c_sock = None
            self.check_sock_rcv = False
            self.ovsdb_dicts = {}
            self.ovsdb_fd_states = {}
            self.ovsdb_conn_list = []
            eventlet.greenthread.spawn(self._rcv_socket)
        else:
            self.gw_config = gw_config
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            if gw_config.use_ssl:
                ssl_sock = ssl.wrap_socket(
                    self.socket,
                    server_side=False,
                    keyfile=gw_config.private_key,
                    certfile=gw_config.certificate,
                    cert_reqs=ssl.CERT_REQUIRED,
                    ssl_version=ssl.PROTOCOL_TLSv1,
                    ca_certs=gw_config.ca_cert)
                self.socket = ssl_sock
            retryCount = 0
            while True:
                try:
                    self.socket.connect((str(gw_config.ovsdb_ip),
                                         int(gw_config.ovsdb_port)))
                    break
                except (socket.error, socket.timeout):
                    LOG.warning(OVSDB_UNREACHABLE_MSG, gw_config.ovsdb_ip)
                    if retryCount == conf.max_connection_retries:
                        # Retried for max_connection_retries times.
                        # Give up and return so that it can be tried in
                        # the next periodic interval.
                        with excutils.save_and_reraise_exception(reraise=True):
                            LOG.exception(_LE("Socket error in connecting to "
                                              "the OVSDB server"))
                    else:
                        time.sleep(1)
                        retryCount += 1

            # Successfully connected to the socket
            LOG.debug(OVSDB_CONNECTED_MSG, gw_config.ovsdb_ip)
            self.connected = True

    def _get_ovsdb_ip_mapping(self):
        ovsdb_ip_mapping = {}
        ovsdb_hosts = cfg.CONF.ovsdb.ovsdb_hosts
        if ovsdb_hosts != '':
            ovsdb_hosts = ovsdb_hosts.split(',')
            for host in ovsdb_hosts:
                host_splits = str(host).split(':')
                ovsdb_identifier = str(host_splits[0]).strip()
                ovsdb_ip = str(host_splits[1]).strip()
                ovsdb_ip_mapping[ovsdb_ip] = ovsdb_identifier
            return ovsdb_ip_mapping

    def _is_ssl_configured(self, addr, client_sock):
        priv_key_path = cfg.CONF.ovsdb.l2_gw_agent_priv_key_base_path
        cert_path = cfg.CONF.ovsdb.l2_gw_agent_cert_base_path
        ca_cert_path = cfg.CONF.ovsdb.l2_gw_agent_ca_cert_base_path
        use_ssl = priv_key_path and cert_path and ca_cert_path
        if use_ssl:
            LOG.debug("ssl is enabled with priv_key_path %s, cert_path %s, "
                      "ca_cert_path %s", priv_key_path,
                      cert_path, ca_cert_path)
            if addr in self.ip_ovsdb_mapping.keys():
                ovsdb_id = self.ip_ovsdb_mapping.get(addr)
                priv_key_file = priv_key_path + "/" + ovsdb_id + ".key"
                cert_file = cert_path + "/" + ovsdb_id + ".cert"
                ca_cert_file = ca_cert_path + "/" + ovsdb_id + ".ca_cert"
                is_priv_key = os.path.isfile(priv_key_file)
                is_cert_file = os.path.isfile(cert_file)
                is_ca_cert_file = os.path.isfile(ca_cert_file)
                if not is_priv_key:
                    LOG.error(_LE("Could not find private key in"
                                  " %(path)s dir, expecting in the "
                                  "file name %(file)s "),
                              {'path': priv_key_path,
                               'file': ovsdb_id + ".key"})
                    return client_sock
                if not is_cert_file:
                    LOG.error(_LE("Could not find cert in %(path)s dir, "
                                  "expecting in the file name %(file)s"),
                              {'path': cert_path,
                               'file': ovsdb_id + ".cert"})
                    return client_sock
                if not is_ca_cert_file:
                    LOG.error(_LE("Could not find cacert in %(path)s "
                                  "dir, expecting in the file name "
                                  "%(file)s"),
                              {'path': ca_cert_path,
                               'file': ovsdb_id + ".ca_cert"})
                    return client_sock
                if (is_priv_key and is_cert_file and is_ca_cert_file):
                    ssl_conn_stream = ssl.wrap_socket(
                        client_sock,
                        server_side=True,
                        keyfile=priv_key_file,
                        certfile=cert_file,
                        ssl_version=ssl.PROTOCOL_SSLv23,
                        ca_certs=ca_cert_file)
                    client_sock = ssl_conn_stream
                    return client_sock
            else:
                LOG.error(_LE("you have enabled SSL for ovsdb %s, "
                              "expecting the ovsdb identifier and ovdb IP "
                              "entry in ovsdb_hosts in l2gateway_agent.ini"),
                          addr)
                return client_sock
        else:
            return client_sock

    def _rcv_socket(self):
        # Create a socket object.
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = ''                        # Get local machine name
        port = self.manager_table_listening_port
        # configured port for your service.
        self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.s.bind((host, port))        # Bind to the port
        self.s.listen(5)                 # Now wait for client connection.
        while True:
            # Establish connection with client.
            c_sock, ip_addr = self.s.accept()
            addr = ip_addr[0]<commit_msg>This cleans up a bit the code that fixed bug 1602523.<commit_after># Copyright (c) 2015 OpenStack Foundation.
# 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.

import copy
import os.path
import socket
import ssl
import time

import eventlet
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import excutils

from networking_l2gw._i18n import _LE, _LW
from networking_l2gw.services.l2gateway.common import constants as n_const

LOG = logging.getLogger(__name__)
OVSDB_UNREACHABLE_MSG = _LW('Unable to reach OVSDB server %s')
OVSDB_CONNECTED_MSG = 'Connected to OVSDB server %s'


class BaseConnection(object):
    """Connects to OVSDB server.

       Connects to an ovsdb server with/without SSL
       on a given host and TCP port.
    """
    def __init__(self, conf, gw_config, mgr=None):
        self.responses = []
        self.connected = False
        self.mgr = mgr
        self.enable_manager = cfg.CONF.ovsdb.enable_manager
        if self.enable_manager:
            self.manager_table_listening_port = (
                cfg.CONF.ovsdb.manager_table_listening_port)
            self.ip_ovsdb_mapping = self._get_ovsdb_ip_mapping()
            self.s = None
            self.check_c_sock = None
            self.check_sock_rcv = False
            self.ovsdb_dicts = {}
            self.ovsdb_fd_states = {}
            self.ovsdb_conn_list = []
            eventlet.greenthread.spawn(self._rcv_socket)
        else:
            self.gw_config = gw_config
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            if gw_config.use_ssl:
                ssl_sock = ssl.wrap_socket(
                    self.socket,
                    server_side=False,
                    keyfile=gw_config.private_key,
                    certfile=gw_config.certificate,
                    cert_reqs=ssl.CERT_REQUIRED,
                    ssl_version=ssl.PROTOCOL_TLSv1,
                    ca_certs=gw_config.ca_cert)
                self.socket = ssl_sock
            retryCount = 0
            while True:
                try:
                    self.socket.connect((str(gw_config.ovsdb_ip),
                                         int(gw_config.ovsdb_port)))
                    break
                except (socket.error, socket.timeout):
                    LOG.warning(OVSDB_UNREACHABLE_MSG, gw_config.ovsdb_ip)
                    if retryCount == conf.max_connection_retries:
                        # Retried for max_connection_retries times.
                        # Give up and return so that it can be tried in
                        # the next periodic interval.
                        with excutils.save_and_reraise_exception(reraise=True):
                            LOG.exception(_LE("Socket error in connecting to "
                                              "the OVSDB server"))
                    else:
                        time.sleep(1)
                        retryCount += 1

            # Successfully connected to the socket
            LOG.debug(OVSDB_CONNECTED_MSG, gw_config.ovsdb_ip)
            self.connected = True

    def _get_ovsdb_ip_mapping(self):
        ovsdb_ip_mapping = {}
        ovsdb_hosts = cfg.CONF.ovsdb.ovsdb_hosts
        if ovsdb_hosts != '':
            ovsdb_hosts = ovsdb_hosts.split(',')
            for host in ovsdb_hosts:
                host_splits = str(host).split(':')
                ovsdb_identifier = str(host_splits[0]).strip()
                ovsdb_ip = str(host_splits[1]).strip()
                ovsdb_ip_mapping[ovsdb_ip] = ovsdb_identifier
            return ovsdb_ip_mapping

    def _is_ssl_configured(self, addr, client_sock):
        priv_key_path = cfg.CONF.ovsdb.l2_gw_agent_priv_key_base_path
        cert_path = cfg.CONF.ovsdb.l2_gw_agent_cert_base_path
        ca_cert_path = cfg.CONF.ovsdb.l2_gw_agent_ca_cert_base_path
        use_ssl = priv_key_path and cert_path and ca_cert_path
        if use_ssl:
            LOG.debug("ssl is enabled with priv_key_path %s, cert_path %s, "
                      "ca_cert_path %s", priv_key_path,
                      cert_path, ca_cert_path)
            if addr in self.ip_ovsdb_mapping.keys():
                ovsdb_id = self.ip_ovsdb_mapping.get(addr)
                priv_key_file = priv_key_path + "/" + ovsdb_id + ".key"
                cert_file = cert_path + "/" + ovsdb_id + ".cert"
                ca_cert_file = ca_cert_path + "/" + ovsdb_id + ".ca_cert"
                is_priv_key = os.path.isfile(priv_key_file)
                is_cert_file = os.path.isfile(cert_file)
                is_ca_cert_file = os.path.isfile(ca_cert_file)
                if is_priv_key and is_cert_file and is_ca_cert_file:
                    ssl_conn_stream = ssl.wrap_socket(
                        client_sock,
                        server_side=True,
                        keyfile=priv_key_file,
                        certfile=cert_file,
                        ssl_version=ssl.PROTOCOL_SSLv23,
                        ca_certs=ca_cert_file)
                    client_sock = ssl_conn_stream
                else:
                    if not is_priv_key:
                        LOG.error(_LE("Could not find private key in"
                                      " %(path)s dir, expecting in the "
                                      "file name %(file)s "),
                                  {'path': priv_key_path,
                                   'file': ovsdb_id + ".key"})
                    if not is_cert_file:
                        LOG.error(_LE("Could not find cert in %(path)s dir, "
                                      "expecting in the file name %(file)s"),
                                  {'path': cert_path,
                                   'file': ovsdb_id + ".cert"})
                    if not is_ca_cert_file:
                        LOG.error(_LE("Could not find cacert in %(path)s "
                                      "dir, expecting in the file name "
                                      "%(file)s"),
                                  {'path': ca_cert_path,
                                   'file': ovsdb_id + ".ca_cert"})
            else:
                LOG.error(_LE("you have enabled SSL for ovsdb %s, "
                              "expecting the ovsdb identifier and ovdb IP "
                              "entry in ovsdb_hosts in l2gateway_agent.ini"),
                          addr)
        return client_sock

    def _rcv_socket(self):
        # Create a socket object.
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = ''                        # Get local machine name
        port = self.manager_table_listening_port
        # configured port for your service.
        self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.s.bind((host, port))        # Bind to the port
        self.s.listen(5)                 # Now wait for client connection.
        while True:
            # Establish connection with client.
            c_sock, ip_addr = self.s.accept()
            addr = ip_addr[0]
{'fraction_non_alphanumeric': 0.072914641259965, 'fraction_numerical': 0.00408321991055804, 'mean_word_length': 1.5369944097336403, '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': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4555243', 'n_tokens_mistral': 4521, 'n_tokens_neox': 4338, 'n_words': 1013}