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

Commit c58ccf6

Browse files
committed
Bug fix to address work order receipt flow in work order sync mode execution.
Signed-off-by: Ramakrishna Srinivasamurthy <[email protected]>
1 parent 1c42159 commit c58ccf6

File tree

3 files changed

+83
-63
lines changed

3 files changed

+83
-63
lines changed

enclave_manager/avalon_enclave_manager/singleton/enclave_manager.py

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -299,51 +299,52 @@ def __update_receipt(self, kv_helper, wo_id, wo_json_resp):
299299
the receipt.
300300
"""
301301
receipt_entry = kv_helper.get("wo-receipts", wo_id)
302-
if receipt_entry:
303-
update_type = None
304-
if "error" in wo_json_resp and \
305-
wo_json_resp["error"]["code"] != \
306-
WorkOrderStatus.PENDING.value:
307-
update_type = ReceiptCreateStatus.FAILED.value
308-
else:
309-
update_type = ReceiptCreateStatus.PROCESSED.value
310-
receipt_obj = WorkOrderReceiptRequest()
311-
wo_receipt = receipt_obj.update_receipt(
312-
wo_id,
313-
update_type,
314-
wo_json_resp,
315-
self.private_key
316-
)
317-
updated_receipt = None
318-
# load previous updates to receipt
319-
updates_to_receipt = kv_helper.get("wo-receipt-updates", wo_id)
320-
# If it is first update to receipt
321-
if updates_to_receipt is None:
322-
updated_receipt = []
323-
else:
324-
updated_receipt = json.loads(updates_to_receipt)
325-
# Get the last update to receipt
326-
last_receipt = updated_receipt[len(updated_receipt) - 1]
327-
328-
# If receipt updateType is completed,
329-
# then no further update allowed
330-
if last_receipt["updateType"] == \
331-
ReceiptCreateStatus.COMPLETED.value:
332-
logger.info(
333-
"Receipt for the workorder id %s is completed " +
334-
"and no further updates are allowed",
335-
wo_id)
336-
return
337-
updated_receipt.append(wo_receipt)
338-
339-
# Since receipts_json is jrpc request updating only params object.
340-
kv_helper.set("wo-receipt-updates", wo_id, json.dumps(
341-
updated_receipt))
342-
logger.info("Receipt for the workorder id %s is updated to %s",
343-
wo_id, wo_receipt)
302+
# If receipt is not created yet, add tag "receiptUpdates" to
303+
# Receipt entry and update it
304+
if receipt_entry is None:
305+
receipt_entry = {
306+
"params": {
307+
"receiptUpdates": []
308+
}
309+
}
310+
# load previous updates to receipt
311+
receipt_update_entry = receipt_entry["params"]["receiptUpdates"]
312+
update_type = None
313+
if "error" in wo_json_resp and \
314+
wo_json_resp["error"]["code"] != \
315+
WorkOrderStatus.PENDING.value:
316+
update_type = ReceiptCreateStatus.FAILED.value
344317
else:
345-
logger.info("Work order receipt is not created, " +
346-
"so skipping the update")
318+
update_type = ReceiptCreateStatus.PROCESSED.value
319+
receipt_obj = WorkOrderReceiptRequest()
320+
wo_receipt = receipt_obj.update_receipt(
321+
wo_id,
322+
update_type,
323+
wo_json_resp,
324+
self.private_key
325+
)
326+
327+
# If it is first update to receipt
328+
if len(receipt_update_entry) > 0:
329+
# Get the last update to receipt
330+
last_receipt = receipt_update_entry[len(receipt_update_entry) - 1]
331+
# If receipt updateType is completed,
332+
# then no further update allowed
333+
if last_receipt["updateType"] == \
334+
ReceiptCreateStatus.COMPLETED.value:
335+
logger.info(
336+
"Receipt for the workorder id %s is completed " +
337+
"and no further updates are allowed",
338+
wo_id)
339+
return
340+
receipt_update_entry.append(wo_receipt)
341+
342+
# Since receipts_json is jrpc request updating only params object.
343+
receipt_entry["receiptUpdates"] = receipt_update_entry
344+
kv_helper.set("wo-receipts", wo_id, json.dumps(
345+
receipt_entry))
346+
logger.info("Receipt for the workorder id %s is updated to %s",
347+
wo_id, wo_receipt)
347348

348349

349350
# -----------------------------------------------------------------

listener/avalon_listener/tcs_workorder_receipt_handler.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def WorkOrderReceiptCreate(self, **params):
7272
wo_id = params["workOrderId"]
7373
input_json_str = params["raw"]
7474
input_value = json.loads(input_json_str)
75-
75+
# Check if work order id exists
7676
wo_request = self.kv_helper.get("wo-requests", wo_id)
7777
if wo_request is None:
7878
raise JSONRPCDispatchException(
@@ -82,12 +82,22 @@ def WorkOrderReceiptCreate(self, **params):
8282
)
8383
else:
8484
wo_receipt = self.kv_helper.get("wo-receipts", wo_id)
85-
if wo_receipt is None:
85+
wo_receipt = json.loads(wo_receipt)
86+
if wo_receipt and "workOrderId" not in wo_receipt["params"]:
8687
status, err_msg = \
8788
self.__validate_work_order_receipt_create_req(
8889
input_value, wo_request)
8990
if status is True:
90-
self.kv_helper.set("wo-receipts", wo_id, input_json_str)
91+
# If receiptUpdates doesn't exists then
92+
# create an entry with empty array
93+
if "receiptUpdates" not in wo_receipt["params"]:
94+
input_value["params"]["receiptUpdates"] = []
95+
else:
96+
input_value["params"]["receiptUpdates"] = \
97+
wo_receipt["receiptUpdates"]
98+
self.kv_helper.set("wo-receipts", wo_id, json.dumps(
99+
input_value
100+
))
91101
raise JSONRPCDispatchException(
92102
JRPCErrorCodes.SUCCESS,
93103
"Receipt created successfully"
@@ -186,11 +196,14 @@ def WorkOrderReceiptUpdate(self, **params):
186196
status, err_msg = self.__validate_work_order_receipt_update_req(
187197
input_value)
188198
if status is True:
199+
value = json.loads(value)
189200
# Load previous updates to receipt
190-
updates_to_receipt = \
191-
self.kv_helper.get("wo-receipt-updates", wo_id)
201+
if "receiptUpdates" in value["params"]:
202+
updates_to_receipt = value["params"]["receiptUpdates"]
203+
else:
204+
updates_to_receipt = []
192205
# If it is first update to receipt
193-
if updates_to_receipt is None:
206+
if len(updates_to_receipt) == 0:
194207
updated_receipt = []
195208
else:
196209
updated_receipt = json.loads(updates_to_receipt)
@@ -220,8 +233,9 @@ def WorkOrderReceiptUpdate(self, **params):
220233
" is not allowed"
221234
)
222235
updated_receipt.append(input_value)
223-
self.kv_helper.set("wo-receipt-updates", wo_id,
224-
json.dumps(updated_receipt))
236+
input_value["receiptUpdates"] = updated_receipt
237+
self.kv_helper.set("wo-receipts", wo_id,
238+
json.dumps(input_value))
225239
raise JSONRPCDispatchException(
226240
JRPCErrorCodes.SUCCESS,
227241
"Receipt updated successfully"
@@ -377,15 +391,19 @@ def WorkOrderReceiptRetrieve(self, **params):
377391
value = self.kv_helper.get("wo-receipts", wo_id)
378392
if value:
379393
receipt = json.loads(value)
380-
receipt_updates = self.kv_helper.get("wo-receipt-updates", wo_id)
381-
if receipt_updates is None:
394+
if "receiptUpdates" in receipt["params"]:
395+
receipt_updates = receipt["params"]["receiptUpdates"]
396+
else:
397+
receipt_updates = []
398+
if len(receipt_updates) == 0:
399+
# If there is no updates to receipt
400+
# then current status is same as create status
382401
receipt["params"]["receiptCurrentStatus"] = \
383402
receipt["params"]["receiptCreateStatus"]
384403
else:
385-
receipt_updates_json = json.loads(receipt_updates)
386-
# Get the recent update to receipt
387-
last_receipt = receipt_updates_json[len(receipt_updates_json)
388-
- 1]
404+
# Get the latest update to receipt
405+
last_receipt = receipt_updates[len(receipt_updates)
406+
- 1]
389407
receipt["params"]["receiptCurrentStatus"] = \
390408
last_receipt["updateType"]
391409
return receipt["params"]
@@ -420,10 +438,12 @@ def WorkOrderReceiptUpdateRetrieve(self, **params):
420438
# starts from 1
421439
update_index = input_params["updateIndex"]
422440
# Load list of updates to the receipt
423-
receipt_updates = self.kv_helper.get("wo-receipt-updates", wo_id)
441+
receipt_entry = self.kv_helper.get("wo-receipts", wo_id)
424442

425-
if receipt_updates:
426-
receipt_updates_json = json.loads(receipt_updates)
443+
if receipt_entry:
444+
receipt_entry_json = json.loads(receipt_entry)
445+
receipt_updates_json = \
446+
receipt_entry_json["params"]["receiptUpdates"]
427447
total_updates = len(receipt_updates_json)
428448
if update_index <= 0:
429449
raise JSONRPCDispatchException(

tools/run_tests.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ done
9999

100100
#yell "Start testing echo client with reading registry from blockchain................"
101101
#yell "#------------------------------------------------------------------------------------------------"
102-
#try $echo_client_path/echo_client.py -m "Hello world" -rs -dh
103102

104103
yell "Start testing echo client with service uri ................"
105104
yell "#------------------------------------------------------------------------------------------------"
106-
try $echo_client_path/echo_client.py -m "Hello world" -s "http://$LISTENER_URL:1947" -dh
105+
try $echo_client_path/echo_client.py -m "Hello world" -s "http://$LISTENER_URL:1947" -dh -rs
107106

108107
yell "Start testing generic client for echo workload ................"
109108
yell "#------------------------------------------------------------------------------------------------"

0 commit comments

Comments
 (0)