Esempio n. 1
0
def query_node_relationship_by_id(node_id):
    database_config = current_app.config['SERVER_CONFIG']['database']

    print("[api_v1_app] /search: connecting to neo4j...")
    database = Graph("{protocol}://{ip}:{port}".format(
        protocol=database_config['connection']['protocol'],
        ip=database_config['connection']['ip'],
        port=database_config['connection']['port']),
                     user=database_config['auth']['user'],
                     password=database_config['auth']['password'])

    cursor = database.run(
        f'MATCH (a)-[r]-(b) WHERE id(a)={node_id} RETURN a,r,b')
    records = cursor.data()

    if len(records) == 0:
        result = {
            'app': 'npdp-server',
            'api': 'node/ids/relationship',
            'timestamp': time.time(),
            'data': {
                'status': 'error',
                'message': "can't find relationships by id.",
                'request': {
                    'id': node_id
                }
            }
        }
        return jsonify(result)

    relationships = []

    for record in records:
        relation = record['r']
        relation_dict = get_relation_dict(relation)
        start_node = relation.start_node()
        start_node_dict = get_node_dict(start_node)
        end_node = relation.end_node()
        end_node_dict = get_node_dict(end_node)

        if start_node_dict['id'] == node_id:
            relation_dict['end_node'] = end_node_dict
        else:
            relation_dict['start_node'] = start_node_dict
        relationships.append(relation_dict)

    result = {
        'app': 'npdp-server',
        'api': 'node/ids/relationship',
        'timestamp': time.time(),
        'data': {
            'status': 'ok',
            'request': {
                'id': node_id
            },
            'relationships': relationships
        }
    }

    return jsonify(result)
Esempio n. 2
0
def query_ftp_server_products(node_id):
    database_config = current_app.config['SERVER_CONFIG']['database']

    print("[api_v1_app] /search: connecting to neo4j...")
    database = Graph("{protocol}://{ip}:{port}".format(
        protocol=database_config['connection']['protocol'],
        ip=database_config['connection']['ip'],
        port=database_config['connection']['port']),
                     user=database_config['auth']['user'],
                     password=database_config['auth']['password'])

    cursor = database.run(f'MATCH (a:OperationSystem)-[]-(b:ProductSet)'
                          f'-[]-(t:TransmissionTask)-[]-(f:FTPServer) '
                          f'WHERE id(f)={node_id} RETURN DISTINCT a, b')
    records = cursor.data()

    if len(records) == 0:
        result = {
            'app': 'npdp-server',
            'api': 'ftp-server/ids/products',
            'timestamp': time.time(),
            'data': {
                'status': 'error',
                'message': "can't find products.",
                'request': {
                    'id': node_id
                }
            }
        }
        return jsonify(result)

    products = []

    for record in records:
        operation_system = record['a']
        operation_system_dict = get_node_dict(operation_system)
        product_set = record['b']
        product_set_dict = get_node_dict(product_set)

        products.append({
            'operation_system': operation_system_dict,
            'product_set': product_set_dict
        })

    result = {
        'app': 'npdp-server',
        'api': 'ftp-server/ids/products',
        'timestamp': time.time(),
        'data': {
            'status': 'ok',
            'request': {
                'id': node_id
            },
            'products': products
        }
    }

    return jsonify(result)
Esempio n. 3
0
def query_operation_system_product_destinations(operation_system_id,
                                                product_set_id):
    database_config = current_app.config['SERVER_CONFIG']['database']

    print("[api_v1_app] /search: connecting to neo4j...")
    database = Graph("{protocol}://{ip}:{port}".format(
        protocol=database_config['connection']['protocol'],
        ip=database_config['connection']['ip'],
        port=database_config['connection']['port']),
                     user=database_config['auth']['user'],
                     password=database_config['auth']['password'])

    cursor = database.run(
        f'MATCH (a:OperationSystem)-[]-(ps:ProductSet)-[]-(:TransmissionTask)-[]-(d:Destination)'
        f'WHERE id(a)={operation_system_id} and id(ps)={product_set_id} '
        f'RETURN DISTINCT d')
    records = cursor.data()

    if len(records) == 0:
        result = {
            'app': 'npdp-server',
            'api': 'operation-system/ids/products/id/destinations',
            'timestamp': time.time(),
            'data': {
                'status': 'error',
                'message': "can't find destinations.",
                'request': {
                    'operation_system_id': operation_system_id,
                    'product_set_id': product_set_id
                }
            }
        }
        return jsonify(result)

    destinations = []

    for record in records:
        destination = record['d']
        destination_dict = get_node_dict(destination)

        destinations.append(destination_dict)

    result = {
        'app': 'npdp-server',
        'api': 'operation-system/ids/products/id/destinations',
        'timestamp': time.time(),
        'data': {
            'status': 'ok',
            'request': {
                'operation_system_id': operation_system_id,
                'product_set_id': product_set_id
            },
            'destinations': destinations
        }
    }

    return jsonify(result)
Esempio n. 4
0
def search():
    print(request.json)
    search_type = request.json['search_type']
    search_input = request.json['search_input']

    database_config = current_app.config['SERVER_CONFIG']['database']

    print("[api_v1_app] /search: connecting to neo4j...")
    database = Graph("{protocol}://{ip}:{port}".format(
        protocol=database_config['connection']['protocol'],
        ip=database_config['connection']['ip'],
        port=database_config['connection']['port']),
                     user=database_config['auth']['user'],
                     password=database_config['auth']['password'])

    selector = NodeSelector(database)
    if search_type == "operation_system":
        records = selector.select("OperationSystem") \
            .where("(any(prop in keys(_) where _[prop] CONTAINS '{input}'))".format(input=search_input))
    elif search_type == "ftp":
        records = selector.select("FTPServer") \
            .where("(any(prop in keys(_) where _[prop] CONTAINS '{input}'))".format(input=search_input))
    elif search_type == "product":
        records = selector.select("ProductSet") \
            .where("(any(prop in keys(_) where _[prop] CONTAINS '{input}'))".format(input=search_input))
    elif search_type == "all":
        records = selector.select() \
            .where("(any(prop in keys(_) where _[prop] CONTAINS '{input}'))".format(input=search_input))
    else:
        result = {
            'app': 'npdp-server',
            'api': 'search',
            'timestamp': time.time(),
            'data': {
                'status': 'error',
                'message': 'type not supported.',
                'request': request.form
            }
        }
        return jsonify(result)

    result_list = []
    for record in records:
        node_dict = get_node_dict(record)
        result_list.append(node_dict)
    result = {
        'app': 'npdp-server',
        'api': 'search',
        'timestamp': time.time(),
        'data': {
            'status': 'ok',
            'request': request.form,
            'results': result_list
        }
    }

    return jsonify(result)