@@ -16,15 +16,15 @@ module.exports = async ({ aristaApi, mongoSingle, interfacesCollection, workerDa
1616 } ) ;
1717
1818 // fetch leases once
19- const leases = await mongoSingle . get ( "leases" ) || [ ] ;
20- const leasesByMac = Object . fromEntries ( leases . map ( lease => [ lease . mac , lease ] ) ) ;
19+ const leases = ( await mongoSingle . get ( "leases" ) ) || [ ] ;
20+ const leasesByMac = Object . fromEntries ( leases . map ( ( lease ) => [ lease . mac , lease ] ) ) ;
2121
2222 // process ARP / FDB-like info
2323 const arpByInterface = { } ;
24- ( arpResult ?. ipV4Neighbors || [ ] ) . forEach ( eachArp => {
24+ ( arpResult ?. ipV4Neighbors || [ ] ) . forEach ( ( eachArp ) => {
2525 const mac = parseHex ( eachArp . hwAddress ) . toUpperCase ( ) ;
2626 const interfaceArray = eachArp . interface . split ( ", " ) ;
27- interfaceArray . forEach ( iface => {
27+ interfaceArray . forEach ( ( iface ) => {
2828 if ( ! arpByInterface [ iface ] ) arpByInterface [ iface ] = [ ] ;
2929 arpByInterface [ iface ] . push ( leasesByMac [ mac ] || { mac } ) ;
3030 } ) ;
@@ -35,8 +35,8 @@ module.exports = async ({ aristaApi, mongoSingle, interfacesCollection, workerDa
3535 updateOne : {
3636 filter : { interfaceId } ,
3737 update : { $set : { fdb : data } } ,
38- upsert : false
39- }
38+ upsert : false ,
39+ } ,
4040 } ) ) ;
4141 if ( arpOps . length ) await interfacesCollection . bulkWrite ( arpOps ) ;
4242 logger . debug ( `updated db with arp details for ${ arpOps . length } interface(s)` ) ;
@@ -53,7 +53,7 @@ module.exports = async ({ aristaApi, mongoSingle, interfacesCollection, workerDa
5353
5454 // process LLDP info
5555 const lldpByInterface = { } ;
56- ( lldpResult ?. lldpNeighbors || [ ] ) . forEach ( eachNeighbor => {
56+ ( lldpResult ?. lldpNeighbors || [ ] ) . forEach ( ( eachNeighbor ) => {
5757 const interfaceId = eachNeighbor . port ;
5858 if ( ! lldpByInterface [ interfaceId ] ) lldpByInterface [ interfaceId ] = { } ;
5959
@@ -72,20 +72,39 @@ module.exports = async ({ aristaApi, mongoSingle, interfacesCollection, workerDa
7272 }
7373 } ) ;
7474
75- // bulk update LLDP info
76- const lldpOps = Object . entries ( lldpByInterface ) . map ( ( [ interfaceId , data ] ) => ( {
77- updateOne : {
78- filter : { interfaceId } ,
79- update : { $set : { lldp : data } } ,
80- upsert : false
81- }
82- } ) ) ;
83- if ( lldpOps . length ) await interfacesCollection . bulkWrite ( lldpOps ) ;
84- logger . debug ( `updated db with lldp details for ${ lldpOps . length } interface(s)` ) ;
75+ const activeInterfaceIds = Object . keys ( lldpByInterface ) ;
76+ const lldpClearFilter = {
77+ lldp : { $exists : true } ,
78+ } ;
8579
80+ if ( activeInterfaceIds . length ) {
81+ lldpClearFilter . interfaceId = { $nin : activeInterfaceIds } ;
82+ }
8683
84+ // bulk update LLDP info
85+ const lldpOps = [
86+ {
87+ updateMany : {
88+ filter : lldpClearFilter ,
89+ update : {
90+ $unset : {
91+ lldp : "" ,
92+ } ,
93+ } ,
94+ } ,
95+ } ,
96+ ...Object . entries ( lldpByInterface ) . map ( ( [ interfaceId , data ] ) => ( {
97+ updateOne : {
98+ filter : { interfaceId } ,
99+ update : { $set : { lldp : data } } ,
100+ upsert : false ,
101+ } ,
102+ } ) ) ,
103+ ] ;
104+ await interfacesCollection . bulkWrite ( lldpOps , { ordered : false } ) ;
105+ logger . debug ( `updated db with lldp details for ${ activeInterfaceIds . length } interface(s)` ) ;
87106 } catch ( err ) {
88107 logger . error ( `failed: ${ err . message } ` ) ;
89108 throw err ;
90109 }
91- } ;
110+ } ;
0 commit comments