summaryrefslogtreecommitdiff
path: root/src-py/rhizi_api.py
blob: 7a6a44e966d7fb9fb00abf49b51f9ef5e8fd600d (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
Rhizi web API
"""
import os
import db_controller as dbc
import json
import logging
from flask import jsonify

from flask import Flask
from flask import request
from flask import make_response

log = logging.getLogger('rhizi')

class FlaskExt(Flask):
    """
    Flask server customization
    """

    def make_default_options_response(self):
        # sup = super(Flask, self)
        ret = Flask.make_default_options_response(self)
        ret.headers['Access-Control-Allow-Origin'] = '*'
        ret.headers['Access-Control-Allow-Headers'] = "Origin, Content-Type, Accept, Authorization"
        return ret

webapp = FlaskExt(__name__)
webapp.debug = True

# injected: DB controller
db_ctl = None

def __sanitize_input(*args, **kw_args):
    pass

def __response_wrap(data=None, error=None):
    """
    wrap response data/errors as dict - this should always be used when returning
    data to allow easy return of list objects, assist in error case distinction, etc. 
    """
    return dict(data=data, error=error)

def __common_resp_handle(data=None, error=None):
    """
    provide common response handling
    """
    ret_data = __response_wrap(data, error)
    resp = jsonify(ret_data)

    resp.headers['Access-Control-Allow-Origin'] = '*'

    # more response processing

    return resp

def __common_exec(op, on_success=__common_resp_handle):
    try:
        op_ret = db_ctl.exec_op(op)
        return on_success(op_ret)
    except Exception as e:
        return __common_resp_handle('exception raised: add_node_set')

@webapp.route("/load/node-set-by-id", methods=['POST'])
def load_node_set_by_id_attr():
    """
    load node-set by ID attribute
    
    @param id_set: list of node ids to match id attribute against
    @return: a list of nodes whose id attribute matches 'id' or
            an empty list if the requested node is not found
    @raise exception: on error
    """
    req_json = request.get_json()
    id_set = req_json['id_set']

    __sanitize_input(id_set)

    return __load_node_set_by_id_attr_common(id_set)

def __load_node_set_by_id_attr_common(id_set):
    """
    @param f_k: optional attribute filter key
    @param f_vset: possible key values to match against
    """
    op = dbc.DBO_match_node_set_by_id_attribute(id_set=id_set)
    try:
        n_set = db_ctl.exec_op(op)
        return __common_resp_handle(data=n_set)
    except Exception as e:
        log.exception(e)
        return __common_resp_handle(error='unable to load node with ids: {0}'.format(id_set))

@webapp.route("/load/link-set/by_link_ptr_set", methods=['POST'])
def load_link_set_by_link_ptr_set():

    def deserialize_param_set(param_json):
        l_ptr_set_raw = param_json['link_ptr_set']

        __sanitize_input(l_ptr_set_raw)

        l_ptr_set = []
        for lptr_dict in l_ptr_set_raw:
            src_id = lptr_dict.get('src_id')
            dst_id = lptr_dict.get('dst_id')
            l_ptr_set += [Link.Link_Ptr(src_id=src_id, dst_id=dst_id) ]

        return l_ptr_set

    l_ptr_set = deserialize_param_set(request.get_json())

    op = dbc.DBO_load_link_set.init_from_link_ptr_set(l_ptr_set)
    try:
        l_set = db_ctl.exec_op(op)
        return __common_resp_handle(data=l_set)
    except Exception as e:
        log.exception(e)
        return __common_resp_handle(error='unable to load link set')

@webapp.route("/graph/topo-diff-commit", methods=['POST'])
def topo_diff_commit():
    """
    commit a graph topology diff
    """
    topo_diff_dict = request.get_json()['topo_diff']
    __sanitize_input(topo_diff_dict)

    topo_diff = Topo_Diff.from_dict(topo_diff_dict)

    op = dbc.DBO_topo_diff_commit(topo_diff)
    return __common_exec(op)

@webapp.route("/graph/attr-diff-commit", methods=['POST'])
def attr_diff_commit():
    """
    commit a graph attribute diff
    """
    attr_diff = request.get_json()['attr_diff']
    __sanitize_input(attr_diff)

    op = dbc.DBO_attr_diff_commit(attr_diff)
    return __common_exec(op)

@webapp.route("/add/node-set", methods=['POST'])
def add_node_set():
    """
    @param node_map: node type to node map, eg. { 'Skill': { 'name': 'kung-fu' } }
    """
    node_map = request.get_json()['node_map']
    __sanitize_input(node_map)

    op = dbc.DBO_add_node_set(node_map)
    try:
        n_set = db_ctl.exec_op(op)
        return __common_resp_handle(n_set)
    except Exception as e:
        return __common_resp_handle('exception raised: add_node_set')