From ea18d5c62a7ad3551a0a9b859bc4e6c62f5e9122 Mon Sep 17 00:00:00 2001 From: Valentina Schiavi Date: Tue, 5 Dec 2023 09:18:23 +0100 Subject: [PATCH] Use field "operation-key" in load.json to store authentication data Fixes #171 --- server/service/IndividualServicesService.js | 143 +++++++++++------- .../CyclicProcessService/cyclicProcess.js | 18 ++- 2 files changed, 103 insertions(+), 58 deletions(-) diff --git a/server/service/IndividualServicesService.js b/server/service/IndividualServicesService.js index 2635e60..9b0bce6 100644 --- a/server/service/IndividualServicesService.js +++ b/server/service/IndividualServicesService.js @@ -264,12 +264,10 @@ const RequestForDeleteEquipmentIntoElasticSearch = async function (mountName) { id: mountName }); - resolve(result); - if (Object.keys(result).length > 0) { resolve(result); } else { - resolve(); + resolve(null); } console.log('Remove mountName={' + mountName + '}'); } @@ -287,28 +285,33 @@ const findNotConnectedElements = async function (listJsonES, listJsonMD) { let listES; let listMD; - if (listJsonES == null) - resolve(null); - else { - listES = listJsonES["mount-name-list"]; - if (listJsonMD != null) { - listMD = listJsonMD["mount-name-list"]; - - // Filter the elements present in listES but not in listMD - let missingElements = listES.filter(element => !listMD.includes(element)); - - if (missingElements.length > 0) { - // Create a new JSON object with the result - const resultJSON = { - "mount-name-list": missingElements - }; - resolve(resultJSON); - } - else { - resolve(null); + try { + if (listJsonES == null) + resolve(null); + else { + listES = listJsonES["mount-name-list"]; + if (listJsonMD != null) { + listMD = listJsonMD["mount-name-list"]; + + // Filter the elements present in listES but not in listMD + let missingElements = listES.filter(element => !listMD.includes(element)); + + if (missingElements.length > 0) { + // Create a new JSON object with the result + const resultJSON = { + "mount-name-list": missingElements + }; + resolve(resultJSON); + } + else { + resolve(null); + } } } } + catch (error) { + reject(error) + } }); } @@ -316,11 +319,11 @@ function areEqualArray(listJsonES, listJsonMD) { let array1 = null; let array2 = null; - if (listJsonES != null) { + if (listJsonES != null && listJsonES != undefined) { array1 = listJsonES["mount-name-list"]; } - if (listJsonMD != null) { + if (listJsonMD != null && listJsonMD != undefined) { array2 = listJsonMD["mount-name-list"]; } @@ -353,6 +356,8 @@ exports.updateCurrentConnectedEquipment = async function (user, originator, xCor let result; let listDisconnectedEq = []; let oldConnectedListFromES = null; + let newConnectedListFromMwdi = null; + let listJsonDisconnectedEq = []; return new Promise(async function (resolve, reject) { try { //"mount-name-list" from ES @@ -368,34 +373,65 @@ exports.updateCurrentConnectedEquipment = async function (user, originator, xCor //MIDW applicationInfo let MIDWApplicationInfo = await EmbeddingCausesRequestForListOfApplicationsAtRo(user, originator, xCorrelator, traceIndicator, customerJourney); } - catch(error) { + catch (error) { console.error('MIDW application is not registered. Skypping'); } - //mountName - list from network/Mwdi - let newConnectedListFromMwdi = await EmbeddingCausesRequestForListOfDevicesAtMwdi(user, originator, xCorrelator, traceIndicator, customerJourney); + try { + //mountName - list from network/Mwdi + newConnectedListFromMwdi = await EmbeddingCausesRequestForListOfDevicesAtMwdi(user, originator, xCorrelator, traceIndicator, customerJourney); + } + catch (error) { + console.error('No Equipment connected. Retry to read...'); + newConnectedListFromMwdi = null; + } - //list of equipment that was connected (mac-address data in ES) but now that are not connected - const listJsonDisconnectedEq = await findNotConnectedElements(oldConnectedListFromES, newConnectedListFromMwdi); + if (newConnectedListFromMwdi != null) { + try { + //list of equipment that was connected (mac-address data in ES) but now that are not connected + listJsonDisconnectedEq = await findNotConnectedElements(oldConnectedListFromES, newConnectedListFromMwdi); + } + catch (error) { + listJsonDisconnectedEq = null; + console.error('No Equipment disconnected'); + } - //Write new "mountName - list" list into ES - if (areEqualArray(oldConnectedListFromES, newConnectedListFromMwdi) == false) { - result = await RequestForWriteListConnectedEquipmentIntoElasticSearch(newConnectedListFromMwdi); - } - if (listJsonDisconnectedEq != null) { - listDisconnectedEq = listJsonDisconnectedEq["mount-name-list"]; - console.log("Number of disconnected equipment:" + listDisconnectedEq.length + "=> remove mac-adress data from ES"); + //Write new "mount-name-list" list into ES + if (areEqualArray(oldConnectedListFromES, newConnectedListFromMwdi) == false) { + try { + result = await RequestForWriteListConnectedEquipmentIntoElasticSearch(newConnectedListFromMwdi); + } + catch (error) { + console.error('Error during updating operation of mount-name-list into db'); + } + } - //remove mac-address data in ES of equipment that are that are no longer connected - if (Array.isArray(listDisconnectedEq)) { - for (const elementToRemove of listDisconnectedEq) { - RequestForDeleteEquipmentIntoElasticSearch(elementToRemove); + if (listJsonDisconnectedEq != null) { + listDisconnectedEq = listJsonDisconnectedEq["mount-name-list"]; + console.log("Number of disconnected equipment:" + listDisconnectedEq.length + "=> remove mac-adress data from ES"); + + //remove mac-address data in ES of equipment that are that are no longer connected + try { + if (Array.isArray(listDisconnectedEq)) { + for (const elementToRemove of listDisconnectedEq) { + try{ + RequestForDeleteEquipmentIntoElasticSearch(elementToRemove); + } + catch(error) + { + console.error('Error during deleting operation of old mac address data into db (' + elementToRemove +')'); + } + } + } + } + catch (error) { + console.error('Error during deleting operation of old mac address data into db'); } } } - resolve(newConnectedListFromMwdi); + } catch (error) { reject(error); @@ -409,6 +445,7 @@ const EmbeddingCausesRequestForListOfApplicationsAtRo = async function (user, or return new Promise(async function (resolve, reject) { try { + let auth = "Basic YWRtaW46YWRtaW4="; let applicationNameAndHttpClient = await resolveApplicationNameAndHttpClientLtpUuidFromForwardingName('EmbeddingCausesRequestForListOfApplicationsAtRo'); @@ -435,9 +472,6 @@ const EmbeddingCausesRequestForListOfApplicationsAtRo = async function (user, or let finalUrl = "http://" + remoteTcpAddress["ip-address"]["ipv-4-address"] + ":" + remoteTcpPort + operationName; console.log("url = " + finalUrl); - //TO FIX - let auth = "Basic YWRtaW46YWRtaW4="; - let httpRequestHeader = new RequestHeader( user, originator, @@ -447,6 +481,9 @@ const EmbeddingCausesRequestForListOfApplicationsAtRo = async function (user, or operationKey ); + if (operationKey != "Operation key not yet provided.") + auth = httpRequestHeader['operationKey']; + let httpRequestHeaderAuth = { "content-type": httpRequestHeader['contentType'], "user": httpRequestHeader['user'], @@ -496,7 +533,7 @@ const EmbeddingCausesRequestForListOfApplicationsAtRo = async function (user, or const EmbeddingCausesRequestForListOfDevicesAtMwdi = async function (user, originator, xCorrelator, traceIndicator, customerJourney) { return new Promise(async function (resolve, reject) { try { - + let auth = "Basic YWRtaW46YWRtaW4="; let applicationNameAndHttpClient = await resolveApplicationNameAndHttpClientLtpUuidFromForwardingName('EmbeddingCausesRequestForListOfDevicesAtMwdi'); @@ -523,9 +560,6 @@ const EmbeddingCausesRequestForListOfDevicesAtMwdi = async function (user, origi let finalUrl = "http://" + remoteTcpAddress["ip-address"]["ipv-4-address"] + ":" + remoteTcpPort + operationName; console.log("url = " + finalUrl); - //TO FIX - let auth = "Basic YWRtaW46YWRtaW4="; - let httpRequestHeader = new RequestHeader( user, @@ -536,6 +570,10 @@ const EmbeddingCausesRequestForListOfDevicesAtMwdi = async function (user, origi operationKey ); + if (operationKey != "Operation key not yet provided.") + auth = httpRequestHeader['operationKey']; //#issue171 + + let httpRequestHeaderAuth = { "content-type": httpRequestHeader['contentType'], "user": httpRequestHeader['user'], @@ -563,11 +601,11 @@ const EmbeddingCausesRequestForListOfDevicesAtMwdi = async function (user, origi console.log("Number of connected devices = " + response.data['mount-name-list'].length); resolve(response.data); } catch (error) { - resolve(error); + reject(error); } } catch (error) { - resolve(error); + reject(error); } }); }; @@ -993,6 +1031,7 @@ async function PromptForUpdatingMacTableFromDeviceCausesUuidOfMacFdBeingSearched //STEP 2 async function PromptForUpdatingMacTableFromDeviceCausesMacTableBeingRetrievedFromDevice(mountName, user, originator, xCorrelator, traceIndicator, customerJourney) { try { + let auth = "Basic YWRtaW46YWRtaW4="; let applicationNameAndHttpClient = await resolveApplicationNameAndHttpClientLtpUuidFromForwardingName('PromptForUpdatingMacTableFromDeviceCausesMacTableBeingRetrievedFromDevice'); @@ -1037,8 +1076,8 @@ async function PromptForUpdatingMacTableFromDeviceCausesMacTableBeingRetrievedFr {} }; - //TO FIX - let auth = "Basic YWRtaW46YWRtaW4="; + if (operationKey != "Operation key not yet provided.") + auth = httpRequestHeader['operationKey']; //#issue171 let httpRequestHeaderAuth = { "content-type": httpRequestHeader['contentType'], diff --git a/server/service/individualServices/CyclicProcessService/cyclicProcess.js b/server/service/individualServices/CyclicProcessService/cyclicProcess.js index a330854..cf66536 100644 --- a/server/service/individualServices/CyclicProcessService/cyclicProcess.js +++ b/server/service/individualServices/CyclicProcessService/cyclicProcess.js @@ -228,7 +228,7 @@ function startTtlChecking() { if (slidingWindow.length == 0) { clearInterval(handle); handle = 0; // I just do this so I know I've cleared the interval - stop = false; + stop = false; MATRCycle(2); } } @@ -304,10 +304,10 @@ async function requestMessage(index) { printLog('Response from element ' + retObj['node-id'] + ' --> Dropped from Sliding Window. Timestamp: ' + Date.now(), print_log_level >= 2); slidingWindow.splice(elementIndex, 1); if (addNextDeviceListElementInWindow()) { - printLog('Add element ' + slidingWindow[slidingWindow.length - 1]['node-id'] + ' in Sliding Window and send request...', print_log_level >= 2); - printLog(printListDevice('Device List', deviceList), print_log_level >= 2); - printLog(printList('Sliding Window', slidingWindow), print_log_level >= 1); - requestMessage(slidingWindow.length - 1); + printLog('Add element ' + slidingWindow[slidingWindow.length - 1]['node-id'] + ' in Sliding Window and send request...', print_log_level >= 2); + printLog(printListDevice('Device List', deviceList), print_log_level >= 2); + printLog(printList('Sliding Window', slidingWindow), print_log_level >= 1); + requestMessage(slidingWindow.length - 1); } else { printLog(printListDevice('Device List', deviceList), print_log_level >= 2); @@ -345,6 +345,8 @@ module.exports.embeddingCausesCyclicRequestsForUpdatingMacTableFromDeviceAtMatr async function MATRCycle(logging_level) { + let deviceListMount = null; + printLog('*****************************************************************', print_log_level >= 1); printLog(' ', print_log_level >= 1); printLog(' START MATR CYCLE ', print_log_level >= 1); @@ -365,7 +367,10 @@ async function MATRCycle(logging_level) { let customerJourney = "Unknown value"; try { - let deviceListMount = await individualServices.updateCurrentConnectedEquipment(user, originator, xCorrelator, traceIndicator, customerJourney); + do { + deviceListMount = await individualServices.updateCurrentConnectedEquipment(user, originator, xCorrelator, traceIndicator, customerJourney); + } while (deviceListMount == null); + deviceList = deviceListMount['mount-name-list']; slidingWindowSize = (slidingWindowSizeDb > deviceList.length) ? deviceList.length : slidingWindowSizeDb; @@ -379,6 +384,7 @@ async function MATRCycle(logging_level) { printLog(printList('Sliding Window - MAIN', slidingWindow), print_log_level >= 1); startTtlChecking(); + } catch (error) { console.error("Error on MATR cycle: " + error);