aboutsummaryrefslogtreecommitdiffstats
path: root/src/mod/trapd_trap_config.py
blob: bfcbb416798598a5b3b9c6f8ec9fc1035f1456d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# ============LICENSE_START=======================================================
# org.onap.dcae
# ================================================================================
# Copyright (c) 2017 AT&T Intellectual Property. 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.
# ============LICENSE_END=========================================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
#
"""
trapd_trap_conf reads config file of traps and stores/returns them
in a data dictionary that is used to compare arriving SNMP OID's
to the list contained in this file for a keep(/publish) or ignore
decision.
"""

__docformat__ = 'restructuredtext'

import os
import sys
import string
import time
import traceback
from trapd_exit import cleanup_and_exit


prog_name = os.path.basename(__file__)


# # # # # # # # # # #
# fx: read_trap_config
# # # # # # # # # # #

def read_trap_config(_yc_trap_conf, _dcae_logger):
    """
    Load trap config file specified in yaml conf.  This config (1) specifies
    which traps should be published(inclusion) and which traps should be discarded
    (not present in config) and (2) maps SNMP Notify OID to DMAAP/MR topics
    :Parameters:
      none
    :Exceptions:
      file open
        this function will throw an exception if unable to open
        _yc_trap_conf
    :Keywords:
      NotifyOID trap config topic
    :Variables:
    """

    _trap_conf_dict = {}

    if os.path.isfile(_yc_trap_conf):
        _dcae_logger.debug('Reading trap config file %s ' % _yc_trap_conf)
    else:
        _dcae_logger.error('ERROR:  trap config file %s does NOT exist - exiting'
                           % (_yc_trap_conf))
        cleanup_and_exit(1, "undefined")

    # reset dictionaries in case we've been here before
    _num_trap_conf_entries = 0

    field_separator = " "

    _dcae_logger.debug('processing trap config settings from %s'
                       % (_yc_trap_conf))
    for line in open(_yc_trap_conf):
        # format:
        #
        # oid_including_regex <topic>
        #
        if line[0] != '#':
            columns = line.rstrip().split(field_separator)
            # process trap config entries
            if len(columns) == 2:
                _trap_conf_oid = columns[0]
                _trap_conf_dict[_trap_conf_oid] = columns[1]
                _dcae_logger.debug('%d oid: %s topic: %s' %
                                   (_num_trap_conf_entries, _trap_conf_oid, _trap_conf_dict[_trap_conf_oid]))
                _num_trap_conf_entries += 1
            else:
                _dcae_logger.debug('ERROR: Invalid trap config entry - '
                                   'skipping: %s' % (line.rstrip()))

    _dcae_logger.debug('%d trap config entries found in %s' % (_num_trap_conf_entries,
                                                               _yc_trap_conf))

    return _trap_conf_dict