|
| 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 | +# --------------------------------------------------------------------------------------------- |
0 commit comments