Skip to content
This repository was archived by the owner on Jan 27, 2022. It is now read-only.

Commit 25cc026

Browse files
committed
Abstract database calls from direct listener handlers
Abstract out calls using database/table names in handlers for direct mode listener. The segregated layer may contain some logic if it has to deal only with lower/database calls for it. Hence abstracting some deatils from the layer above. This thin layer can also be used in the Proxy mode of operations. Signed-off-by: Rajeev Ranjan <[email protected]>
1 parent 3cc5722 commit 25cc026

File tree

10 files changed

+570
-198
lines changed

10 files changed

+570
-198
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2020 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
__all__ = []
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2020 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
__all__ = []
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Copyright 2020 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import time
16+
import json
17+
import logging
18+
from error_code.error_status import WorkOrderStatus
19+
from error_code.enclave_error import EnclaveError
20+
21+
from jsonrpc.exceptions import JSONRPCDispatchException
22+
23+
logger = logging.getLogger(__name__)
24+
25+
26+
class WorkOrderLmdbHelper:
27+
"""
28+
WorkOrderDBHelper helps listener or other client facing modules
29+
to interact with the kv storage for queries related to work
30+
order processing.
31+
"""
32+
# ------------------------------------------------------------------------------------------------
33+
34+
def __init__(self, kv_helper):
35+
"""
36+
Function to perform init activity
37+
Parameters:
38+
- kv_helper is a object of lmdb database
39+
"""
40+
41+
self.kv_helper = kv_helper
42+
43+
# ---------------------------------------------------------------------------------------------
44+
def get_wo_result(self, wo_id):
45+
"""
46+
Function to get work-order result from lmdb
47+
Parameters:
48+
- wo_id is the id of the work-order for which result is
49+
requested.
50+
Returns jrpc response as defined in EEA spec 6.1.2
51+
"""
52+
53+
# Work order is processed if it is in wo-response table
54+
value = self.kv_helper.get("wo-responses", wo_id)
55+
if value:
56+
response = json.loads(value)
57+
if 'result' in response:
58+
return response['result']
59+
60+
# response without a result should have an error
61+
err_code = response["error"]["code"]
62+
err_msg = response["error"]["message"]
63+
if err_code == EnclaveError.ENCLAVE_ERR_VALUE:
64+
err_code = WorkOrderStatus.INVALID_PARAMETER_FORMAT_OR_VALUE
65+
elif err_code == EnclaveError.ENCLAVE_ERR_UNKNOWN:
66+
err_code = WorkOrderStatus.UNKNOWN_ERROR
67+
else:
68+
err_code = WorkOrderStatus.FAILED
69+
raise JSONRPCDispatchException(err_code, err_msg)
70+
71+
if(self.kv_helper.get("wo-timestamps", wo_id) is not None):
72+
# work order is yet to be processed
73+
raise JSONRPCDispatchException(
74+
WorkOrderStatus.PENDING,
75+
"Work order result is yet to be updated")
76+
77+
# work order not in 'wo-timestamps' table
78+
raise JSONRPCDispatchException(
79+
WorkOrderStatus.INVALID_PARAMETER_FORMAT_OR_VALUE,
80+
"Work order Id not found in the database. Hence invalid parameter")
81+
82+
# ---------------------------------------------------------------------------------------------
83+
def submit_wo(self, wo_id, input_json_str):
84+
"""
85+
Function to submit a new work-order
86+
Parameters:
87+
- wo_id: id of work-order being submitted
88+
- input_json_str: The actual work-order as received
89+
from the requester
90+
"""
91+
92+
if(self.kv_helper.get("wo-timestamps", wo_id) is None):
93+
94+
# Create a new work order entry.
95+
# Don't change the order of table updation.
96+
# The order is important for clean up if the TCS is restarted in
97+
# the middle.
98+
epoch_time = str(time.time())
99+
100+
# Update the tables
101+
self.kv_helper.set("wo-timestamps", wo_id, epoch_time)
102+
self.kv_helper.set("wo-requests", wo_id, input_json_str)
103+
self.kv_helper.set("wo-scheduled", wo_id, input_json_str)
104+
105+
raise JSONRPCDispatchException(
106+
WorkOrderStatus.PENDING,
107+
"Work order is computing. Please query for WorkOrderGetResult \
108+
to view the result")
109+
110+
# Workorder id already exists
111+
raise JSONRPCDispatchException(
112+
WorkOrderStatus.INVALID_PARAMETER_FORMAT_OR_VALUE,
113+
"Work order id already exists in the database. \
114+
Hence invalid parameter")
115+
116+
def clear_a_processed_wo(self):
117+
"""
118+
Function that clears one processed work-order from
119+
database
120+
"""
121+
work_orders = self.kv_helper.lookup("wo-timestamps")
122+
for id in work_orders:
123+
124+
# If work order is processed then remove from table
125+
if (self.kv_helper.get("wo-processed", id) is not None):
126+
self.kv_helper.remove("wo-processed", id)
127+
self.kv_helper.remove("wo-requests", id)
128+
self.kv_helper.remove("wo-responses", id)
129+
self.kv_helper.remove("wo-receipts", id)
130+
self.kv_helper.remove("wo-timestamps", id)
131+
return id
132+
133+
def cleanup_hindered_wo(self):
134+
"""
135+
This function is meant to do a boot time cleanup
136+
Returns the list of work-order to be processed and the count
137+
"""
138+
workorder_count = 0
139+
workorder_list = []
140+
work_orders = self.kv_helper.lookup("wo-timestamps")
141+
for wo_id in work_orders:
142+
143+
if (self.kv_helper.get("wo-scheduled", wo_id) is None and
144+
self.kv_helper.get("wo-processing", wo_id) is None and
145+
self.kv_helper.get("wo-processed", wo_id) is None):
146+
147+
if (self.kv_helper.get("wo-requests", wo_id) is not None):
148+
self.kv_helper.remove("wo-requests", wo_id)
149+
150+
if (self.kv_helper.get("wo-responses", wo_id) is not None):
151+
self.kv_helper.remove("wo-responses", wo_id)
152+
153+
if (self.kv_helper.get("wo-receipts", wo_id) is not None):
154+
self.kv_helper.remove("wo-receipts", wo_id)
155+
156+
# TODO: uncomment after fixing lmbd error
157+
self.kv_helper.remove("wo-timestamps", wo_id)
158+
159+
else:
160+
# Add to the internal FIFO
161+
workorder_list.append(wo_id)
162+
workorder_count += 1
163+
164+
return workorder_list, workorder_count
165+
# ---------------------------------------------------------------------------------------------
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2020 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
17+
logger = logging.getLogger(__name__)
18+
19+
20+
class WorkerEncryptionKeyLmdbHelper:
21+
"""
22+
WorkerEncryptionKeyDBHelper helps listener or other client
23+
facing modules to interact with the kv storage for queries
24+
related to encryption key. It implements all low level db
25+
calls which aides in the logical flows for getting/setting
26+
encryption keys.
27+
"""
28+
# ------------------------------------------------------------------------------------------------
29+
30+
def __init__(self, kv_helper):
31+
"""
32+
Function to perform init activity
33+
Parameters:
34+
- kv_helper is a object of lmdb database
35+
"""
36+
self.kv_helper = kv_helper
37+
38+
# ---------------------------------------------------------------------------------------------
39+
def get_worker_with_id(self, worker_id):
40+
"""
41+
Function to get worker corresponding to supplied worker id
42+
Parameters:
43+
- worker_id: id of worker being looked for
44+
Returns worker corresponding to key
45+
"""
46+
47+
return self.kv_helper.get_worker("workers", worker_id)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2020 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
17+
logger = logging.getLogger(__name__)
18+
19+
20+
class WorkerRegistryLmdbHelper:
21+
"""
22+
WorkerRegistryDBHelper helps listener or other client facing modules
23+
to interact with the kv storage for queries related to worker registry.
24+
"""
25+
# ------------------------------------------------------------------------------------------------
26+
27+
def __init__(self, kv_helper):
28+
"""
29+
Function to perform init activity
30+
Parameters:
31+
- kv_helper is a object of lmdb database
32+
"""
33+
34+
self.kv_helper = kv_helper
35+
# ------------------------------------------------------------------------------------------------
36+
37+
def get_worker_with_id(self, worker_id):
38+
"""
39+
Function to get worker corresponding to supplied worker id
40+
Parameters:
41+
- worker_id: id of worker being looked for
42+
Returns worker corresponding to key
43+
"""
44+
return self.kv_helper.get("workers", worker_id)
45+
46+
# ------------------------------------------------------------------------------------------------
47+
48+
def save_worker(self, worker_id, worker_details):
49+
"""
50+
Function to save a worker with given id and details
51+
Parameters:
52+
- worker_id: id of worker to be saved
53+
- worker_details: Details of worker to be saved
54+
"""
55+
self.kv_helper.set("workers", worker_id, worker_details)
56+
57+
# ------------------------------------------------------------------------------------------------
58+
def get_all_workers(self):
59+
"""
60+
Function to retrieve all workers from database
61+
Returns a list of all workers in the 'workers' table
62+
"""
63+
return self.kv_helper.lookup("workers")
64+
# ------------------------------------------------------------------------------------------------
65+
66+
def cleanup_registries(self):
67+
"""
68+
Function to clean up all registries from the database
69+
"""
70+
organisation_id = self.kv_helper.lookup("registries")
71+
for o_id in organisation_id:
72+
self.kv_helper.remove("registries", o_id)
73+
# ------------------------------------------------------------------------------------------------
74+
75+
def save_registry(self, reg_id, registry_details):
76+
"""
77+
Function to save/create a new registry in the database
78+
Parameters:
79+
- reg_id: Id of registry to be saved/updated
80+
- registry_details: Details of new/updated registry
81+
"""
82+
return self.kv_helper.set("registries", reg_id, registry_details)

0 commit comments

Comments
 (0)