ipoe: fluxo adicionado
Signed-off-by: Rodrigo Martins <rodrigo@martinsevieira.adv.br>
This commit is contained in:
1 parent
1427e6126a
commit
ef6f94067b
5 files changed
+8791
-8457
No files matched your search
@@ -1832,6 +1832,196 @@ class SSHManager {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configura IPoE em ONU Datacom: altera VLAN da ethernet e service-ports para 977
|
||||
* 1. Busca service-ports existentes
|
||||
* 2. Busca a ONU (slot/onuId) pelo serial
|
||||
* 3. Remove service-ports antigos
|
||||
* 4. Altera ethernet native vlan para 977
|
||||
* 5. Cria novos service-ports com VLAN 977
|
||||
* 6. Commit
|
||||
* @param {string} equipmentId - ID do equipamento OLT (ex: olt_dito)
|
||||
* @param {string} slot - Slot da ONU (ex: 1/1/1)
|
||||
* @param {string} onuId - ID da ONU (ex: 5)
|
||||
* @returns {Promise} - Promise com resultado da operação
|
||||
*/
|
||||
async configIPoEDatacom(equipmentId, slot, onuId) {
|
||||
console.log(`🔗 [IPoE Datacom] Iniciando - OLT: ${equipmentId}, Slot: ${slot}, ONU: ${onuId}`);
|
||||
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
const connection = await this.connect(equipmentId);
|
||||
if (!connection.success) {
|
||||
throw new Error(`Falha na conexão: ${connection.message}`);
|
||||
}
|
||||
|
||||
// Buscar service-ports existentes
|
||||
const spCommand = `show running-config service-port | context-match "gpon ${slot} onu ${onuId}"`;
|
||||
const spResult = await this.executeCommand(connection.connectionId, spCommand);
|
||||
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}`
|
||||
});
|
||||
}
|
||||
|
||||
console.log(`🔌 [IPoE Datacom] Service-ports encontrados: ${servicePorts.join(', ')}`);
|
||||
|
||||
const conn = this.connections.get(connection.connectionId);
|
||||
const VLAN_IPOE = 977;
|
||||
|
||||
conn.connection.shell((err, stream) => {
|
||||
if (err) {
|
||||
this.disconnect(connection.connectionId);
|
||||
reject(new Error(`Erro ao abrir shell: ${err.message}`));
|
||||
return;
|
||||
}
|
||||
|
||||
let output = '';
|
||||
let step = 0;
|
||||
let isComplete = false;
|
||||
let newServicePortId = null;
|
||||
|
||||
// Sequência de comandos:
|
||||
// 0: config
|
||||
// 1..N: no service-port X (remover antigos)
|
||||
// N+1: interface gpon {slot}
|
||||
// N+2: onu {onuId}
|
||||
// N+3: ethernet 1
|
||||
// N+4: native vlan vlan-id 977
|
||||
// N+5: top
|
||||
// N+6: service-port new
|
||||
// N+7: gpon {slot} onu {onuId} gem 1 match vlan vlan-id 977 action vlan replace vlan-id 977
|
||||
// N+8: commit
|
||||
// N+9: top
|
||||
// N+10: exit
|
||||
// N+11: exit
|
||||
const commands = [
|
||||
'config',
|
||||
...servicePorts.map(sp => `no service-port ${sp}`),
|
||||
`interface gpon ${slot}`,
|
||||
`onu ${onuId}`,
|
||||
'ethernet 1',
|
||||
`native vlan vlan-id ${VLAN_IPOE}`,
|
||||
'top',
|
||||
'service-port new',
|
||||
`gpon ${slot} onu ${onuId} gem 1 match vlan vlan-id ${VLAN_IPOE} action vlan replace vlan-id ${VLAN_IPOE}`,
|
||||
'commit',
|
||||
'top',
|
||||
'exit',
|
||||
'exit'
|
||||
];
|
||||
|
||||
const removeCount = servicePorts.length;
|
||||
const ifaceIdx = 1 + removeCount; // interface gpon
|
||||
const onuIdx = ifaceIdx + 1; // onu
|
||||
const ethIdx = onuIdx + 1; // ethernet 1
|
||||
const vlanIdx = ethIdx + 1; // native vlan
|
||||
const topAfterVlanIdx = vlanIdx + 1; // top (back to config)
|
||||
const spNewIdx = topAfterVlanIdx + 1; // service-port new
|
||||
const spCfgIdx = spNewIdx + 1; // gpon ... match vlan
|
||||
const commitIdx = spCfgIdx + 1; // commit
|
||||
const topAfterCommitIdx = commitIdx + 1; // top
|
||||
const exit1Idx = topAfterCommitIdx + 1; // exit
|
||||
const exit2Idx = exit1Idx + 1; // exit
|
||||
|
||||
stream.on('data', (data) => {
|
||||
const dataStr = data.toString();
|
||||
output += dataStr;
|
||||
console.log(`📨 [IPoE] Recebido: ${dataStr.trim()}`);
|
||||
|
||||
// Capturar novo service-port ID
|
||||
if (dataStr.includes('(config-service-port-')) {
|
||||
const match = dataStr.match(/\(config-service-port-(\d+)\)/);
|
||||
if (match && !newServicePortId) {
|
||||
newServicePortId = match[1];
|
||||
console.log(`✅ [IPoE] Novo service-port ID: ${newServicePortId}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (step < commands.length) {
|
||||
let shouldSend = false;
|
||||
|
||||
if (step === 0 && dataStr.includes('#')) {
|
||||
shouldSend = true; // prompt inicial → config
|
||||
} else if (step >= 1 && step <= removeCount && dataStr.includes('(config)#')) {
|
||||
shouldSend = true; // modo config → no service-port X
|
||||
} else if (step === ifaceIdx && dataStr.includes('(config)#')) {
|
||||
shouldSend = true; // modo config → interface gpon
|
||||
} else if (step === onuIdx && dataStr.includes('(config-gpon')) {
|
||||
shouldSend = true; // modo gpon → onu
|
||||
} else if (step === ethIdx && dataStr.includes('(config-gpon-onu')) {
|
||||
shouldSend = true; // modo onu → ethernet 1
|
||||
} else if (step === vlanIdx && dataStr.includes('(config-ethernet')) {
|
||||
shouldSend = true; // modo ethernet → native vlan
|
||||
} else if (step === topAfterVlanIdx && dataStr.includes('(config-ethernet')) {
|
||||
shouldSend = true; // após vlan → top
|
||||
} else if (step === spNewIdx && dataStr.includes('(config)#')) {
|
||||
shouldSend = true; // config → service-port new
|
||||
} else if (step === spCfgIdx && dataStr.includes('(config-service-port')) {
|
||||
shouldSend = true; // service-port → gpon ... match vlan
|
||||
} else if (step === commitIdx && dataStr.includes('(config-service-port')) {
|
||||
shouldSend = true; // service-port → commit
|
||||
} else if (step === topAfterCommitIdx && dataStr.includes('(config-service-port')) {
|
||||
shouldSend = true; // service-port → top
|
||||
} else if (step === exit1Idx && dataStr.includes('(config)#')) {
|
||||
shouldSend = true; // config → exit
|
||||
} else if (step === exit2Idx && dataStr.includes('#') && !dataStr.includes('(config')) {
|
||||
shouldSend = true; // prompt → exit
|
||||
}
|
||||
|
||||
if (shouldSend) {
|
||||
console.log(`📝 [IPoE] Comando ${step + 1}/${commands.length}: ${commands[step]}`);
|
||||
stream.write(commands[step] + '\n');
|
||||
step++;
|
||||
}
|
||||
} else if (!isComplete && dataStr.includes('#') && !dataStr.includes('(config')) {
|
||||
isComplete = true;
|
||||
setTimeout(() => { stream.end(); }, 1000);
|
||||
}
|
||||
});
|
||||
|
||||
stream.on('close', () => {
|
||||
this.disconnect(connection.connectionId);
|
||||
console.log(`✅ [IPoE Datacom] Concluído - ONU ${onuId} no slot ${slot}`);
|
||||
|
||||
resolve({
|
||||
success: true,
|
||||
message: `VLAN alterada para ${VLAN_IPOE} (IPoE)`,
|
||||
output: output.trim(),
|
||||
slot,
|
||||
onuId,
|
||||
vlanId: VLAN_IPOE,
|
||||
oldServicePorts: servicePorts,
|
||||
newServicePortId
|
||||
});
|
||||
});
|
||||
|
||||
stream.on('error', (err) => {
|
||||
this.disconnect(connection.connectionId);
|
||||
reject(new Error(`Erro no shell: ${err.message}`));
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
if (!stream.destroyed && !isComplete) {
|
||||
console.log(`⏰ [IPoE Datacom] Timeout`);
|
||||
stream.end();
|
||||
this.disconnect(connection.connectionId);
|
||||
reject(new Error('Timeout na operação IPoE'));
|
||||
}
|
||||
}, 90000);
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error(`❌ [IPoE Datacom] Erro:`, error);
|
||||
reject(new Error(`Erro IPoE Datacom: ${error.message}`));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SSHManager;
|
||||
Reference in new issue
Block a user