Skip to content

Commit

Permalink
algoritmo arpService.addInfoGWAndTenantToDevices per aggiornare i dev…
Browse files Browse the repository at this point in the history
…ices nel DB (work in progress) #8
  • Loading branch information
mdslp committed Mar 27, 2020
1 parent bbd562c commit 36876b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
13 changes: 12 additions & 1 deletion backend/src/services/arpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
53 changes: 27 additions & 26 deletions backend/src/services/dnsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions backend/src/shared/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -29,6 +29,6 @@ fs.watchFile(tmpDirectoryLeases, (curr: any, prev: any) => {
);
let data = fs.readFileSync(tmpDirectoryLeases, 'utf8');
console.log("LEASES", leases(data));
})
});


0 comments on commit 36876b0

Please sign in to comment.