From 36876b0ea84ac150f8fde97a8268ac0a8fffece8 Mon Sep 17 00:00:00 2001 From: Melania Dello Spedale La Paglia Date: Fri, 27 Mar 2020 15:28:32 +0100 Subject: [PATCH] algoritmo arpService.addInfoGWAndTenantToDevices per aggiornare i devices nel DB (work in progress) #8 --- backend/src/services/arpService.ts | 13 +++++++- backend/src/services/dnsService.ts | 53 +++++++++++++++--------------- backend/src/shared/watcher.ts | 4 +-- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/backend/src/services/arpService.ts b/backend/src/services/arpService.ts index 27b4280..32a9de2 100644 --- a/backend/src/services/arpService.ts +++ b/backend/src/services/arpService.ts @@ -37,24 +37,33 @@ export default class PingService { } async addInfoGWAndTenantToDevices(arpData: any, contactedGW: any) { + // MAC address dei gateway che sono stati contattati const gwMacs = Object.keys(contactedGW); const leases = Utilities.getCurrentLeases(); if (arpData) { await Object.keys(arpData).forEach(async (interfaceKey: string) => { + console.log("interfaceKey", interfaceKey); const mac_addresses = arpData[interfaceKey].mac_addresses; if (mac_addresses && Object.keys(mac_addresses).length > 0) { let tenantRes = await tenantStore.findBy({ edge_interface_name: interfaceKey }); if (tenantRes && tenantRes.length == 1) { const tenant = tenantRes[0]; + console.log("tenant", tenant); + let currentGWId: number = null; await Object.keys(mac_addresses).forEach(async (key: string) => { + console.log("mac_address", key); + if (gwMacs.includes(key)) { let gwInterface: IDevice = { mac_address: key, tenant_id: tenant.id }; + console.log("gwInterface Device", gwInterface); + const gwRes = await deviceStore.findBy(gwInterface); gwInterface.is_gw = true; + // aggiunge o aggiorna il gateway nell DB (tabella devices) if (gwRes && gwRes.length == 1) { gwInterface.id = gwRes[0].id; // verificare result update @@ -66,14 +75,16 @@ export default class PingService { } else { // verificare result create const createRes = await deviceStore.create(gwInterface); + console.log("createRes", createRes); if (createRes) { currentGWId = createRes[0]; } } + return; // } - // ELSE??? no const ipaddrs: string[] = mac_addresses[key]; await ipaddrs.forEach(async (ip: string) => { + // se IP non รจ quello del Gateway if (ip != contactedGW[key]) { const currentLeases = leases.filter((val: ILease) => val.ip == ip); if (currentLeases && currentLeases.length == 1) { diff --git a/backend/src/services/dnsService.ts b/backend/src/services/dnsService.ts index a11d344..dadfd02 100644 --- a/backend/src/services/dnsService.ts +++ b/backend/src/services/dnsService.ts @@ -16,37 +16,38 @@ export default class DNSService { try { let prepareFileHost = ""; // const tenantRes = await tenantStore.findAll(); - // let tenantRes = await tenantStore.findBy({ edge_interface_name: cfg.default_tenant }); + let tenantRes = await tenantStore.findBy({ edge_interface_name: cfg.default_tenant }); - // if (tenantRes) { - let leasesData = Utilities.getCurrentLeases(); - // for (let i = 0; i < tenantRes.length; i++) { - // const tenant: ITenant = tenantRes[i]; - if (leasesData) { - for (let j = 0; j < leasesData.length; j++) { - const val = leasesData[j]; - let device: IDevice = { - mac_address: val.mac, - // tenant_id: tenant.id - }; + if (tenantRes && tenantRes.length > 0) { + let leasesData = Utilities.getCurrentLeases(); + // for (let i = 0; i < tenantRes.length; i++) { + // const tenant: ITenant = tenantRes[i]; + const tenant: ITenant = tenantRes[0]; + if (leasesData) { + for (let j = 0; j < leasesData.length; j++) { + const val = leasesData[j]; + let device: IDevice = { + mac_address: val.mac, + tenant_id: tenant.id + }; - const deviceRes = await deviceStore.findBy(device); - let devicePersistent = null; - if (deviceRes && deviceRes.length == 0) { - device.dns_name_auto = val.host || ""; - await deviceStore.create(device); - devicePersistent = device; - } else { - devicePersistent = deviceRes[0]; - } - if (devicePersistent) { - let dns_name = devicePersistent.dns_name_manual && devicePersistent.dns_name_manual != "" ? devicePersistent.dns_name_manual : devicePersistent.dns_name_auto; - prepareFileHost = prepareFileHost + `${val.ip} ${dns_name}\n`; + const deviceRes = await deviceStore.findBy(device); + let devicePersistent = null; + if (deviceRes && deviceRes.length == 0) { + device.dns_name_auto = val.host || ""; + await deviceStore.create(device); + devicePersistent = device; + } else { + devicePersistent = deviceRes[0]; + } + if (devicePersistent) { + let dns_name = devicePersistent.dns_name_manual && devicePersistent.dns_name_manual != "" ? devicePersistent.dns_name_manual : devicePersistent.dns_name_auto; + prepareFileHost = prepareFileHost + `${val.ip} ${dns_name}\n`; + } } } + // } } - // } - // } console.log("ContentFileHost", prepareFileHost); let pathFileHost = cfg.path_file_host ? cfg.path_file_host : "myhostfile"; Utilities.writeFile(pathFileHost, prepareFileHost); diff --git a/backend/src/shared/watcher.ts b/backend/src/shared/watcher.ts index 3aa53fc..a78c0f3 100644 --- a/backend/src/shared/watcher.ts +++ b/backend/src/shared/watcher.ts @@ -16,7 +16,7 @@ fs.watchFile(tmpDirectory, (curr: any, prev: any) => { `[${new Date().toLocaleString()}] Watching for file changes on: ${tmpDirectory}` ); arpService.execute(); -}) +}); let tmpDirectoryLeases = path.join(__dirname, '../../src/leases'); if (cfg.watcher && cfg.watcher.leases_path) { @@ -29,6 +29,6 @@ fs.watchFile(tmpDirectoryLeases, (curr: any, prev: any) => { ); let data = fs.readFileSync(tmpDirectoryLeases, 'utf8'); console.log("LEASES", leases(data)); -}) +});