Skip to content
Snippets Groups Projects
config.py 10.58 KiB
import json
import jsonschema

CONFIG_SCHEMA = {
    '$schema': 'https://json-schema.org/draft-07/schema#',

    'definitions': {
        'timeout': {
            'type': 'number',
            'maximum': 60,  # sanity
            'exclusiveMinimum': 0
        },
        'ssh-credentials': {
            'type': 'object',
            'properties': {
                'username': {'type': 'string'},
                'private-key': {'type': 'string'},
                'known-hosts': {'type': 'string'}
            },
            'required': ['private-key', 'known-hosts'],
            'additionalProperties': False
        },
        'nokia-ssh-credentials': {
            'type': 'object',
            'properties': {
                'username': {'type': 'string'},
                'password': {'type': 'string'},
                'hostkey_verify': {'type': 'boolean'},
                'key_filename': {'type': 'string'}
            },
            'one-of': [
                {
                    'required': ['username', 'password']
                },
                {
                    'required': ['username', 'key_filename']
                },
                {
                    'required': ['ssh_config']
                }
            ],
            'additionalProperties': False
        },
        'ims': {
            'type': 'object',
            'properties': {
                'api': {'type': 'string'},
                'username': {'type': 'string'},
                'password': {'type': 'string'},
                'verify-ssl': {'type': 'boolean'},
            },
            'required': ['api', 'username', 'password'],
            'additionalProperties': False
        },
        'otrs-export': {
            'type': 'object',
            'properties': {
                'username': {'type': 'string'},
                'private-key': {'type': 'string'},
                'known-hosts': {'type': 'string'},
                'destination': {'type': 'string'}
            },
            'required': [
                'username',
                'private-key',
                'known-hosts',
                'destination'
            ],
            'additionalProperties': False
        },
        'redis-credentials': {
            'type': 'object',
            'properties': {
                'hostname': {'type': 'string'},
                'port': {'type': 'integer'},
                'password': {'type': 'string'},
                'celery-db-index': {'type': 'integer'},
                'socket_timeout': {'$ref': '#/definitions/timeout'}
            },
            'required': ['hostname', 'port'],
            'additionalProperties': False
        },
        'redis-sentinel-config': {
            'type': 'object',
            'properties': {
                'hostname': {'type': 'string'},
                'port': {'type': 'integer'},
                'password': {'type': 'string'},
                'celery-db-index': {'type': 'integer'},
                'name': {'type': 'string'},
                'redis_socket_timeout': {'$ref': '#/definitions/timeout'},
                'sentinel_socket_timeout': {'$ref': '#/definitions/timeout'}
            },
            'required': ['hostname', 'port', 'name'],
            'additionalProperties': False
        },
        'interface-address': {
            'type': 'object',
            'properties': {
                'address': {'type': 'string'},
                'network': {'type': 'string'},
                'interface': {'type': 'string'},
                'router': {'type': 'string'}
            },
            'required': ['address', 'network', 'interface', 'router'],
            'additionalProperties': False
        },
        'oid': {
            'type': 'string',
            'pattern': r'^(\d+\.)*\d+$'
        },
        'gws-direct-counters': {
            'type': 'object',
            'properties': {
                'discards_in': {'$ref': '#/definitions/oid'},
                'discards_out': {'$ref': '#/definitions/oid'},
                'errors_in': {'$ref': '#/definitions/oid'},
                'errors_out': {'$ref': '#/definitions/oid'},
                'traffic_in': {'$ref': '#/definitions/oid'},
                'traffic_out': {'$ref': '#/definitions/oid'},
            },
            'additionalProperties': False
        },
        'gws-direct-interface': {
            'type': 'object',
            'properties': {
                'info': {'type': 'string'},
                'tag': {'type': 'string'},
                'counters': {'$ref': '#/definitions/gws-direct-counters'}
            },
            'required': ['tag', 'counters'],
            'additionalProperties': False
        },
        'gws-direct-host-v2': {
            'type': 'object',
            'properties': {
                'hostname': {'type': 'string'},
                'community': {'type': 'string'},
                'interfaces': {
                    'type': 'array',
                    'items': {'$ref': '#/definitions/gws-direct-interface'},
                    'minItems': 1
                }
            },
            'required': ['hostname', 'community', 'interfaces'],
            'additionalProperties': False
        },
        'snmp-v3-cred': {
            'type': 'object',
            'properties': {
                'protocol': {'enum': ['MD5', 'DES']},
                'password': {'type': 'string'}
            },
            'required': ['protocol', 'password'],
            'additionalProperties': False
        },
        'gws-direct-host-v3': {
            'type': 'object',
            'properties': {
                'hostname': {'type': 'string'},
                'sec-name': {'type': 'string'},
                'auth': {'$ref': '#/definitions/snmp-v3-cred'},
                'priv': {'$ref': '#/definitions/snmp-v3-cred'},
                'interfaces': {
                    'type': 'array',
                    'items': {'$ref': '#/definitions/gws-direct-interface'},
                    'minItems': 1
                }
            },
            'required': ['hostname', 'sec-name', 'interfaces'],
            'additionalProperties': False
        },
        'gws-direct-nren-isp': {
            'type': 'object',
            'properties': {
                'nren': {'type': 'string'},
                'isp': {
                    'type': 'string',
                    'enum': ['Cogent', 'Telia', 'CenturyLink', 'GTT']
                },
                'hosts': {
                    'type': 'array',
                    'items': {
                        'oneOf': [
                            {'$ref': '#/definitions/gws-direct-host-v2'},
                            {'$ref': '#/definitions/gws-direct-host-v3'},
                        ]
                    },
                    'minItems': 1
                }
            },
            'required': ['nren', 'isp', 'hosts'],
            'additionalProperties': False
        },
        'gws-direct': {
            'type': 'array',
            'items': {'$ref': '#/definitions/gws-direct-nren-isp'}
        },
        'nren-asn-map': {
            'type': 'array',
            'items': {
                'type': 'object',
                'properties': {
                    'nren': {'type': 'string'},
                    'asn': {'type': 'integer'}
                },
                'required': ['nren', 'asn'],
                'additionalProperties': False
            },
        },
        'aai': {
            'type': 'object',
            'properties': {
                'discovery_endpoint_url': {'type': 'string'},
                'inventory_provider': {'type': 'object',
                                       'properties': {
                                           'client_id': {'type': 'string'},
                                           'secret': {'type': 'string'}
                                       },
                                       'required': ['client_id', 'secret'],
                                       'additionalProperties': False}
            },
            'required': ['discovery_endpoint_url', 'inventory_provider'],
            'additionalProperties': False
        },
        'orchestrator': {
            'type': 'object',
            'properties': {
                'url': {'type': 'string'},
            },
            'required': ['url'],
            'additionalProperties': False
        },
    },

    'type': 'object',
    'properties': {
        'ssh': {'$ref': '#/definitions/ssh-credentials'},
        'nokia-ssh': {'$ref': '#/definitions/nokia-ssh-credentials'},
        'redis': {'$ref': '#/definitions/redis-credentials'},
        'sentinel': {'$ref': '#/definitions/redis-sentinel-config'},
        'ims': {'$ref': '#/definitions/ims'},
        'otrs-export': {'$ref': '#/definitions/otrs-export'},
        'redis-databases': {
            'type': 'array',
            'minItems': 1,
            'items': {'type': 'integer'}
        },
        'lab-routers': {
            'type': 'object',
            'properties': {
                '^[a-zA-Z0-9.-]+$': {
                    'type': 'string',
                    'enum': ['nokia', 'juniper']
                }
            }
        },
        'unmanaged-interfaces': {
            'type': 'array',
            'items': {'$ref': '#/definitions/interface-address'}
        },
        'gws-direct': {'$ref': '#/definitions/gws-direct'},
        'nren-asn-map': {'$ref': '#/definitions/nren-asn-map'},
        'nokia-community-inventory-provider': {'type': 'string'},
        'nokia-community-dashboard': {'type': 'string'},
        'nokia-community-brian': {'type': 'string'},
        'aai': {'$ref': '#/definitions/aai'},
        'orchestrator': {'$ref': '#/definitions/orchestrator'}
    },
    'oneOf': [
        {
            'required': [
                'ssh',
                'nokia-ssh',
                'redis',
                'redis-databases',
                'ims',
                'gws-direct',
                'nren-asn-map',
                'aai',
                'orchestrator']
        },
        {
            'required': [
                'ssh',
                'nokia-ssh',
                'sentinel',
                'redis-databases',
                'ims',
                'gws-direct',
                'nren-asn-map',
                'aai',
                'orchestrator']
        }
    ],
    'additionalProperties': False
}


def load(f):
    """
    Loads, validates and returns configuration parameters.

    Input is validated against this jsonschema:

    .. asjson:: inventory_provider.config.CONFIG_SCHEMA

    :param f: file-like object that produces the config file
    :return: a dict containing the parsed configuration parameters
    """
    config = json.loads(f.read())
    jsonschema.validate(config, CONFIG_SCHEMA)
    return config