datacom: remove multiplos services-ports

Signed-off-by: Rodrigo Bertoldo de Sousa Martins <rodrigo.sousa.577@gmail.com>
This commit is contained in:
rodrigo committed 2026-03-29 09:52:13 -03:00
1 parent 0aa2ff478a
commit dd2d026fa0
2 files changed
+103 -61

No files matched your search

+69 -34
View File
@@ -958,10 +958,10 @@ class SSHManager {
};
}
// Extrair service-port da saída
const servicePort = this.parseServicePort(result.output);
// Extrair todos os service-ports da saída
const servicePorts = this.parseServicePorts(result.output);
if (!servicePort) {
if (servicePorts.length === 0) {
return {
success: false,
message: `Service-port não encontrado para ONU ${onuId} no slot ${slot}`,
@@ -971,8 +971,9 @@ class SSHManager {
return {
success: true,
message: `Service-port encontrado: ${servicePort}`,
servicePort: servicePort,
message: `Service-port(s) encontrado(s): ${servicePorts.join(', ')}`,
servicePort: servicePorts[0], // compatibilidade retroativa
servicePorts: servicePorts,
slot: slot,
onuId: onuId,
output: result.output
@@ -989,34 +990,47 @@ class SSHManager {
}
/**
* Extrai o service-port da saída do comando
* Extrai o service-port da saída do comando (retorna apenas o primeiro)
* @param {string} output - Saída do comando show running-config service-port
* @returns {string|null} - Service-port encontrado ou null
*/
parseServicePort(output) {
const ports = this.parseServicePorts(output);
return ports.length > 0 ? ports[0] : null;
}
/**
* Extrai todos os service-ports da saída do comando
* @param {string} output - Saída do comando show running-config service-port
* @returns {string[]} - Array com todos os service-ports encontrados
*/
parseServicePorts(output) {
const lines = output.split('\n');
const ports = [];
for (const line of lines) {
// Procurar por "service-port" seguido de número
const match = line.match(/service-port\s+(\d+)/);
if (match) {
return match[1];
const port = match[1];
if (!ports.includes(port)) {
ports.push(port);
}
}
}
return null;
return ports;
}
/**
* Remove uma ONU específica (processo completo)
* Busca automaticamente todos os service-ports e remove cada um deles.
* @param {string} equipmentId - ID do equipamento OLT
* @param {string} slot - Slot da ONU (ex: 1/1/1)
* @param {string} onuId - ID da ONU
* @param {string} servicePort - Service-port da ONU
* @returns {Promise} - Promise com resultado da operação
*/
async removeOnu(equipmentId, slot, onuId, servicePort) {
console.log(`🗑️ Iniciando remoção de ONU - OLT: ${equipmentId}, Slot: ${slot}, ONU: ${onuId}, Service-port: ${servicePort}`);
async removeOnu(equipmentId, slot, onuId) {
console.log(`🗑️ Iniciando remoção de ONU - OLT: ${equipmentId}, Slot: ${slot}, ONU: ${onuId}`);
return new Promise(async (resolve, reject) => {
try {
@@ -1029,6 +1043,24 @@ class SSHManager {
}
console.log(`✅ Conectado! ConnectionId: ${connection.connectionId}`);
// Buscar todos os service-ports antes de abrir o shell interativo
const searchCommand = `show running-config service-port | context-match "gpon ${slot} onu ${onuId}"`;
console.log(`🔍 Buscando service-ports: ${searchCommand}`);
const spResult = await this.executeCommand(connection.connectionId, searchCommand);
const servicePorts = spResult.success ? this.parseServicePorts(spResult.output) : [];
if (servicePorts.length === 0) {
this.disconnect(connection.connectionId);
return resolve({
success: false,
message: `Nenhum service-port encontrado para ONU ${onuId} no slot ${slot}`,
slot: slot,
onuId: onuId
});
}
console.log(`🔌 Service-port(s) encontrado(s): ${servicePorts.join(', ')}`);
const conn = this.connections.get(connection.connectionId);
@@ -1044,24 +1076,36 @@ class SSHManager {
let step = 0;
let isComplete = false;
// Construir lista de comandos dinamicamente para N service-ports
// 0: config
// 1: interface gpon <slot>
// 2: no onu <onuId>
// 3: top
// 4..4+n-1: no service-port <spX> (um por service-port)
// 4+n: commit
// 4+n+1: top
// 4+n+2: exit
// 4+n+3: exit
const commands = [
'config',
`interface gpon ${slot}`,
`no onu ${onuId}`,
'top',
`no service-port ${servicePort}`,
...servicePorts.map(sp => `no service-port ${sp}`),
'commit',
'top',
'exit',
'exit'
];
// Índice do comando commit (usado como referência para os demais)
const commitIdx = 4 + servicePorts.length;
stream.on('data', (data) => {
const dataStr = data.toString();
output += dataStr;
console.log(`📨 Recebido: ${dataStr.trim()}`);
// Detectar prompts e enviar próximo comando
if (step < commands.length) {
let shouldSendCommand = false;
@@ -1072,30 +1116,21 @@ class SSHManager {
// No modo config - enviar interface
shouldSendCommand = true;
} else if (step === 2 && dataStr.includes('(config-gpon')) {
// No modo interface - enviar no onu
// No modo interface gpon - enviar no onu
shouldSendCommand = true;
} else if (step === 3 && dataStr.includes('(config-gpon')) {
// Após no onu - enviar top
shouldSendCommand = true;
} else if (step === 4 && dataStr.includes('(config)#')) {
// De volta ao config - enviar no service-port
} else if (step >= 4 && step <= commitIdx + 2 && dataStr.includes('(config)#')) {
// Remove todos os service-ports + commit + top + primeiro exit
shouldSendCommand = true;
} else if (step === 5 && dataStr.includes('(config)#')) {
// Após service-port - enviar commit
shouldSendCommand = true;
} else if (step === 6 && dataStr.includes('(config)#')) {
// Após commit - enviar top
shouldSendCommand = true;
} else if (step === 7 && dataStr.includes('(config)#')) {
// Após top - enviar exit
shouldSendCommand = true;
} else if (step === 8 && dataStr.includes('#') && !dataStr.includes('(config')) {
// Último exit - enviar exit final
} else if (step === commitIdx + 3 && dataStr.includes('#') && !dataStr.includes('(config')) {
// Último exit
shouldSendCommand = true;
}
if (shouldSendCommand) {
console.log(`📝 Enviando comando ${step + 1}: ${commands[step]}`);
console.log(`📝 Enviando comando ${step + 1}/${commands.length}: ${commands[step]}`);
stream.write(commands[step] + '\n');
step++;
}
@@ -1118,7 +1153,7 @@ class SSHManager {
output: output.trim(),
slot: slot,
onuId: onuId,
servicePort: servicePort
servicePorts: servicePorts
});
});
@@ -1145,7 +1180,6 @@ class SSHManager {
}
});
}
/**
* Processo completo de remoção de ONU pelo serial number
* @param {string} equipmentId - ID do equipamento OLT
@@ -1190,7 +1224,8 @@ class SSHManager {
serialNumber: serialNumber,
slot: findResult.onuInfo.slot,
onuId: findResult.onuInfo.onuId,
servicePort: servicePortResult.servicePort,
servicePort: servicePortResult.servicePorts[0], // compatibilidade retroativa
servicePorts: servicePortResult.servicePorts,
findOutput: findResult.output,
servicePortOutput: servicePortResult.output,
readyForRemoval: true