<commit_before># Copyright (C) 2015 <NAME>, <NAME> and <NAME>.
# Copyright (C) 2015 Research and Education Advanced Network New Zealand Ltd.
#
# 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 logging
import yaml

from dp import DP
from port import Port
from vlan import VLAN
from watcher_conf import WatcherConf

def get_logger(logname):
    return logging.getLogger(logname + '.config')

def read_config(config_file, logname):
    logger = get_logger(logname)
    try:
        with open(config_file, 'r') as stream:
            conf = yaml.safe_load(stream)
    except yaml.YAMLError as ex:
        mark = ex.problem_mark
        errormsg = "Error in file: {0} at ({1}, {2})".format(
            config_file,
            mark.line + 1,
            mark.column + 1)
        logger.error(errormsg)
        return None
    return conf


def dp_parser(config_file, logname):
    logger = get_logger(logname)
    conf = read_config(config_file, logname)
    if conf is None:
        return None

    version = conf.pop('version', 1)

    if version == 1:
        return _dp_parser_v1(conf, config_file, logname)
    elif version == 2:
        return _dp_parser_v2(conf, config_file, logname)
    else:
        logger.error("unsupported config version number: {0}".format(version))
        return None

def port_parser(p_identifier, port_conf, vlans):
    port = Port(p_identifier, port_conf)

    if port.mirror is not None:
        # ignore other config
        return port
    if port.native_vlan is not None:
        v_identifier = port.native_vlan
        vlan = vlans.setdefault(v_identifier,  VLAN(v_identifier))
        vlan.untagged.append(port)
    for v_identifier in port.tagged_vlans:
        vlan = vlans.setdefault(v_identifier,  VLAN(v_identifier))
        vlan.tagged.append(port)

    return port


def _dp_parser_v1(conf, config_file, logname):
    logger = get_logger(logname)

    # TODO: warn when the configuration contains meaningless elements
    # they are probably typos
    if 'dp_id' not in conf:
        logger.error('dp_id not configured in file {0}'.format(config_file))

    dp_id = conf['dp_id']
    dp = DP(dp_id, conf)

    interfaces_conf = conf.pop('interfaces', {})
    vlans_conf = conf.pop('vlans', {})
    acls_conf = conf.pop('acls', {})

    logger.info(str(dp))
    vlans = {}
    port = {}
    for vid, vlan_conf in vlans_conf.iteritems():
        vlans[vid] = VLAN(vid, vlan_conf)
    for port_num, port_conf in interfaces_conf.iteritems():
        dp.add_port(port_parser(port_num, port_conf, vlans))
    for acl_num, acl_conf in acls_conf.iteritems():
        dp.add_acl(acl_num, acl_conf)
    for vlan in vlans.itervalues():
        dp.add_vlan(vlan)
    try:
        dp.sanity_check()
    except AssertionError as err:
        self.logger.exception("Error in config file: {0}".format(err))
        return None

    return [dp]

def _dp_parser_v2(conf, config_file, logname):
    logger = get_logger(logname)

    if 'dps' not in conf:
        logger.error("dps not configured in file: {0}".format(config_file))<commit_msg>Fix 'self' reference in dp_parser_v1 exception logger<commit_after># Copyright (C) 2015 <NAME>, <NAME> and <NAME>.
# Copyright (C) 2015 Research and Education Advanced Network New Zealand Ltd.
#
# 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 logging
import yaml

from dp import DP
from port import Port
from vlan import VLAN
from watcher_conf import WatcherConf

def get_logger(logname):
    return logging.getLogger(logname + '.config')

def read_config(config_file, logname):
    logger = get_logger(logname)
    try:
        with open(config_file, 'r') as stream:
            conf = yaml.safe_load(stream)
    except yaml.YAMLError as ex:
        mark = ex.problem_mark
        errormsg = "Error in file: {0} at ({1}, {2})".format(
            config_file,
            mark.line + 1,
            mark.column + 1)
        logger.error(errormsg)
        return None
    return conf


def dp_parser(config_file, logname):
    logger = get_logger(logname)
    conf = read_config(config_file, logname)
    if conf is None:
        return None

    version = conf.pop('version', 1)

    if version == 1:
        return _dp_parser_v1(conf, config_file, logname)
    elif version == 2:
        return _dp_parser_v2(conf, config_file, logname)
    else:
        logger.error("unsupported config version number: {0}".format(version))
        return None

def port_parser(p_identifier, port_conf, vlans):
    port = Port(p_identifier, port_conf)

    if port.mirror is not None:
        # ignore other config
        return port
    if port.native_vlan is not None:
        v_identifier = port.native_vlan
        vlan = vlans.setdefault(v_identifier,  VLAN(v_identifier))
        vlan.untagged.append(port)
    for v_identifier in port.tagged_vlans:
        vlan = vlans.setdefault(v_identifier,  VLAN(v_identifier))
        vlan.tagged.append(port)

    return port


def _dp_parser_v1(conf, config_file, logname):
    logger = get_logger(logname)

    # TODO: warn when the configuration contains meaningless elements
    # they are probably typos
    if 'dp_id' not in conf:
        logger.error('dp_id not configured in file {0}'.format(config_file))

    dp_id = conf['dp_id']
    dp = DP(dp_id, conf)

    interfaces_conf = conf.pop('interfaces', {})
    vlans_conf = conf.pop('vlans', {})
    acls_conf = conf.pop('acls', {})

    logger.info(str(dp))
    vlans = {}
    port = {}
    for vid, vlan_conf in vlans_conf.iteritems():
        vlans[vid] = VLAN(vid, vlan_conf)
    for port_num, port_conf in interfaces_conf.iteritems():
        dp.add_port(port_parser(port_num, port_conf, vlans))
    for acl_num, acl_conf in acls_conf.iteritems():
        dp.add_acl(acl_num, acl_conf)
    for vlan in vlans.itervalues():
        dp.add_vlan(vlan)
    try:
        dp.sanity_check()
    except AssertionError as err:
        logger.exception("Error in config file: {0}".format(err))
        return None

    return [dp]

def _dp_parser_v2(conf, config_file, logname):
    logger = get_logger(logname)

    if 'dps' not in conf:
        logger.error("dps not configured in file: {0}".format(config_file))
{'fraction_non_alphanumeric': 0.09654118627587165, 'fraction_numerical': 0.007917766356438394, 'mean_word_length': 3.1002277904328017, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '7243018', 'n_tokens_mistral': 2326, 'n_tokens_neox': 2267, 'n_words': 687}