datacom: fix service-port

Signed-off-by: Rodrigo Martins <rodrigo@martinsevieira.adv.br>
This commit is contained in:
Rodrigo Martins committed 2026-04-15 10:57:48 -03:00
1 parent abf9b71484
commit 6c048a0045
1 file changed
+43 -4
+43 -4
View File
@@ -1087,8 +1087,8 @@ class SSHManager {
}; };
} }
// Extrair todos os service-ports da saída // Extrair service-ports que pertencem EXATAMENTE à ONU (match exato, não substring)
const servicePorts = this.parseServicePorts(result.output); const servicePorts = this.parseServicePortsForOnu(result.output, slot, onuId);
if (servicePorts.length === 0) { if (servicePorts.length === 0) {
return { return {
@@ -1150,6 +1150,43 @@ class SSHManager {
return ports; return ports;
} }
/**
* Extrai service-ports que pertencem EXATAMENTE à ONU indicada.
* O context-match faz substring match, então "onu 3" bate em "onu 30", "onu 31", etc.
* Este método verifica a linha de configuração de cada bloco para garantir match exato.
* @param {string} output - Saída do comando show running-config service-port
* @param {string} slot - Slot da ONU (ex: 1/1/9)
* @param {string|number} onuId - ID exato da ONU
* @returns {string[]} - Array com os service-ports que pertencem exclusivamente à ONU
*/
parseServicePortsForOnu(output, slot, onuId) {
const escapedSlot = slot.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const escapedOnuId = String(onuId).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
// Garante que "onu 3" não case com "onu 30": exige espaço ou fim de linha após o ID
const onuPattern = new RegExp(`gpon\\s+${escapedSlot}\\s+onu\\s+${escapedOnuId}(?:\\s|$)`);
const lines = output.split('\n');
const ports = [];
let currentPort = null;
for (const line of lines) {
const portMatch = line.match(/^service-port\s+(\d+)/);
if (portMatch) {
currentPort = portMatch[1];
} else if (currentPort !== null) {
if (onuPattern.test(line)) {
if (!ports.includes(currentPort)) {
ports.push(currentPort);
}
}
// Cada bloco tem apenas uma linha de configuração; reseta após verificar
currentPort = null;
}
}
return ports;
}
/** /**
* Remove uma ONU específica (processo completo) * Remove uma ONU específica (processo completo)
* Busca automaticamente todos os service-ports e remove cada um deles. * Busca automaticamente todos os service-ports e remove cada um deles.
@@ -1177,7 +1214,8 @@ class SSHManager {
const searchCommand = `show running-config service-port | context-match "gpon ${slot} onu ${onuId}"`; const searchCommand = `show running-config service-port | context-match "gpon ${slot} onu ${onuId}"`;
console.log(`🔍 Buscando service-ports: ${searchCommand}`); console.log(`🔍 Buscando service-ports: ${searchCommand}`);
const spResult = await this.executeCommand(connection.connectionId, searchCommand); const spResult = await this.executeCommand(connection.connectionId, searchCommand);
const servicePorts = spResult.success ? this.parseServicePorts(spResult.output) : []; // Match exato: "onu 3" não pode casar com "onu 30", "onu 31", etc.
const servicePorts = spResult.success ? this.parseServicePortsForOnu(spResult.output, slot, onuId) : [];
if (servicePorts.length === 0) { if (servicePorts.length === 0) {
this.disconnect(connection.connectionId); this.disconnect(connection.connectionId);
@@ -1863,7 +1901,8 @@ class SSHManager {
// Buscar service-ports existentes // Buscar service-ports existentes
const spCommand = `show running-config service-port | context-match "gpon ${slot} onu ${onuId}"`; const spCommand = `show running-config service-port | context-match "gpon ${slot} onu ${onuId}"`;
const spResult = await this.executeCommand(connection.connectionId, spCommand); const spResult = await this.executeCommand(connection.connectionId, spCommand);
const servicePorts = spResult.success ? this.parseServicePorts(spResult.output) : []; // Match exato: "onu 3" não pode casar com "onu 30", "onu 31", etc.
const servicePorts = spResult.success ? this.parseServicePortsForOnu(spResult.output, slot, onuId) : [];
if (servicePorts.length === 0) { if (servicePorts.length === 0) {
this.disconnect(connection.connectionId); this.disconnect(connection.connectionId);