FM的代码,自己mark下
///
// System : WIBRO
// Subsystem : OAM
// Process : FM
// Class : AaaStateManager
// File : AaaStateManager.cpp
// Description :
// See Also : AaaStateManager.h
// Author : cleartool lshist AaaStateManager.cpp
// Created : cleartool lshist AaaStateManager.cpp
// Revision History : cleartool lshist AaaStateManager.cpp
///
#if defined(__MIPS__) || defined(__ARM__) || defined(__PPC85XX__)|| defined(__x86__)||defined(_OAM_BLADE_)
#include <cstdio>
#include <cstring>
#include <string>
#include "msg_id_oam.h"
#include "cpsofm_msg.h"
#include "mbox_id_oam.h"
#include "mbox_id_cps.h"
#include "conf_def.h"
#include "cp_rel.h"
#include "wibro_ras_rel.h"
#include "em_interface.h"
#include "ofm_def.h"
#include "BoardCode.h"
#include "BoardConf.h"
#include "DateTime.h"
#include "Debug.h"
#include "EnumUtil.h"
#include "EventManager.h"
#include "AaaStateManager.h"
#include "PldRelation.h"
#include "SelfConf.h"
#include "StringUtil.h"
#include "SystemConf.h"
#include "GmsClient.h"
#include "TrapManager.h"
#include "IpcClient.h"
#include "MwUtil.h"
#include "SelfConf.h"
#include "AlarmGroup.h"
namespace fm
{
using namespace std;
struct AaaAlarmMapItem
{
word16 alarm;
char sub[128];
};
///
// AaaStateManager class members
int AaaStateManager::cid = StringUtil::hashCode("AaaStateManager");
AaaStateManager* AaaStateManager::instance = NULL;
AaaStateManager::AaaStateManager()
: SnmpCommandHandlerBase(sizeof(AaaStateInfo), _msg_ReqUsiAaaState_BsmOfm, _msg_RspUsiAaaState_OfmBsm)
{
SnmpCommandHandlerBase::req[_msg_ReqUsiAaaState_BsmOfm] = "sts-Aaa ";
// Initialize SNMP table.
AaaStateInfo v;
table.keys.add(SnmpColumn(&v, &v.AaaId));
// Initialize SNMP table row helper.
table.rows.helper(SnmpRowHelper(&Provider::valid, &Provider::eval, &Provider::get));
// Initialize SNMP table row.
Provider::table = &table;
// Subscribe event delegate.
_evt.delegate(EID_FM_INIT_COMPLETED,
EventDelegate(this, &AaaStateManager::onInitCompleted));
// Subscribe event delegate.
_chn.delegate( _msg_NotiAaaState_UsiOfm, ChannelDelegate(this, &AaaStateManager::onAaaStateChanged));
// Register console command.
instance = this;
for (word32 i = 0; i < FM_MAX_AAA_COUNT; i++)
{
ude[i].set(i);
}
dusRegisterCmd("simAaareport", 9, onCommand2, "sim Aaa state");
dusRegisterCmd("disAaasts", 9, onCommand, "Display Aaa state");
}
AaaStateManager::~AaaStateManager()
{
instance = NULL;
Provider::table = NULL;
}
void AaaStateManager::onInitCompleted(void* params)
{
notiCpsOfmInfo();
}
void AaaStateManager::onAaaStateChanged(IChannelClient& client)
{
if (client.receiveBuffer() == NULL)
{
Debug::print(cid, "ex: invalid; CallFaultTrapNotifier:_msg_FaultReport_AscOfm/n");
return;
}
//send Trap
AaaStateInfo noti = {0,};
AaaStateReport_UsiOfm_Type revData ={0,};
memcpy(&revData, client.receiveBuffer(),
min(client.messageLength(), sizeof(AaaStateReport_UsiOfm_Type)));
DateTime::fill(noti.AaacTimestamp, 11, DateTime::now());
int AaaIdx = Read16(revData.AaaId);
ude[AaaIdx].pre_state = ude[AaaIdx].val.AaaState;
ude[AaaIdx].val.AaaId = AaaIdx;
noti.AaaId = AaaIdx;
ude[AaaIdx].val.AaaIpVer = Read32(revData.AaaIp.IpVer);
noti.AaaIpVer = Read32(revData.AaaIp.IpVer);
if( noti.AaaIpVer == IPV4)
{
ude[AaaIdx].val.AaaIpV4 =Read32(revData.AaaIp.Ip.V4);
noti.AaaIpV4 = Read32(revData.AaaIp.Ip.V4);
}
else
{
for(int n = 0 ; n < 16 ; n++)
{
ude[AaaIdx].val.AaaIpV6[n] = revData.AaaIp.Ip.V6[n];
noti.AaaIpV6[n] = revData.AaaIp.Ip.V6[n];
}
}
ude[AaaIdx].val.AaaState = Read8(revData.AaaState);
noti.AaaState = Read8(revData.AaaState);
memcpy(&(ude[AaaIdx].val.ReleaseReasonStr), &(revData.ReleaseReasonStr),sizeof(revData.ReleaseReasonStr));
memcpy(&(noti.ReleaseReasonStr), &(revData.ReleaseReasonStr),sizeof(revData.ReleaseReasonStr));
memcpy(&(ude[AaaIdx].val.AaacTimestamp), &(noti.AaacTimestamp),sizeof(noti.AaacTimestamp));
Provider* pv = find(AaaIdx);
if(NULL != pv)
{
pv->val.AaaId = AaaIdx;
pv->val.AaaIpVer = noti.AaaIpVer;
if( pv->val.AaaIpVer == IPV4)
{
pv->val.AaaIpV4 =noti.AaaIpV4;
}
else
{
for(int n = 0 ; n < 16 ; n++)
{
pv->val.AaaIpV6[n] = noti.AaaIpV6[n];
}
}
pv->val.AaaState = Read8(noti.AaaState);
memcpy(&(pv->val.ReleaseReasonStr), &(noti.ReleaseReasonStr),sizeof(noti.ReleaseReasonStr));
memcpy(&(pv->val.AaacTimestamp), &(noti.AaacTimestamp),sizeof(noti.AaacTimestamp));
pv->sndStat();
}
}
AaaStateManager::Provider* AaaStateManager::find(word16 AaaId)
{
for (word32 i = 0; i < FM_MAX_AAA_COUNT; i++)
{
bool match = (ude[i].val.AaaId == AaaId);
if (match)
{
return &ude[i];
}
}
return NULL;
}
void AaaStateManager::display()
{
for (word32 i = 0; i < table.rows.count(); i++)
{
Provider* p = (Provider*)table.rows[i];
if (p == NULL)
{
continue;
}
if (!p->valid())
{
continue;
}
p->eval();
AaaStateInfo* val = (AaaStateInfo*)p->get();
if (val == NULL)
{
continue;
}
char sid[32] = {0,};
if (val->AaaId != 0xFFFF)
{
snprintf(sid, 31, "Aaa-%02d", val->AaaId);
}
else
{
snprintf(sid, 31, "Aaa-**-**");
}
dusPrint("fm", PL_CMD, "%s-%d /n",
sid,val->AaaState);
}
}
int AaaStateManager::onCommand(int argc, char** argv)
{
try
{
if (instance != NULL)
{
instance->display();
}
dusPrint("fm", PL_CMD, "ok/n");
}
catch (exception& ex)
{
dusPrint("fm", PL_CMD, "ex: %s/n", ex.what());
}
catch (...)
{
dusPrint("fm", PL_CMD, "ex: unknown/n");
}
return 0;
}
int AaaStateManager::onCommand2(int argc, char** argv)
{
int choice = atoi(argv[1]);
try
{
if (instance != NULL)
{
AaaStateReport_UsiOfm_Type noti_msg;
memset(¬i_msg.MsgHdr, 0, sizeof(MsgHeader_Type));
noti_msg.MsgHdr.msg_id = Write16(_msg_NotiAaaState_UsiOfm);
noti_msg.AaaId = Write16(choice);
noti_msg.AaaIp.IpVer = Write32(4);
if( Read32(noti_msg.AaaIp.IpVer) == IPV4)
{
noti_msg.AaaIp.Ip.V4 =Write32(1835600026);
}
noti_msg.AaaState = atoi(argv[2]);
IpcClient::sendTo(¬i_msg, sizeof(AaaStateReport_UsiOfm_Type), MwUtil::ip(SelfConf::shelf, SelfConf::slot, 0),OFM_RECV_IPC_MBOX) ;
}
dusPrint("fm", PL_CMD, "ok/n");
}
catch (exception& ex)
{
dusPrint("fm", PL_CMD, "ex: %s/n", ex.what());
}
catch (...)
{
dusPrint("fm", PL_CMD, "ex: unknown/n");
}
return 0;
}
void* AaaStateManager::create()
{
// Register alarm type.
EnumUtil::alarm(OFM_ALM_AAA_DOWN, "Aaadown");
// Register alarm group.
AlarmGroup::set(OFM_ALM_AAA_DOWN, FM_ALG_COMM);
switch (SelfConf::procType)
{
// MMP(24), RMP(24), MRMP(24)
case MMP:
return new AaaStateManager();
default:
return NULL;
}
}
void AaaStateManager::notiCpsOfmInfo()
{
MsgHeader_Type noti_msg;
memset(¬i_msg, 0, sizeof(MsgHeader_Type));
noti_msg.msg_id = Write16(_msg_OfmStateInd_OfmCps);
IpcClient::sendTo(¬i_msg, sizeof(MsgHeader_Type), MwUtil::ip(SelfConf::shelf, SelfConf::slot, 0), ARC_INT_OAM_MBOX) ;
}
///
// AaaStateManager::Provider class members
SnmpTable* AaaStateManager::Provider::table = NULL;
AaaStateManager::Provider::Provider()
{
_rid = 0xFFFF;
_stateCount = 0;
_alarm = NULL;
memset(&val, 0xFF, sizeof(AaaStateInfo));
}
AaaStateManager::Provider::~Provider()
{
try
{
reset();
}
catch (...)
{
}
}
void AaaStateManager::Provider::set(word16 AaaId)
{
if ((_rid == AaaId) && (_alarm != NULL))
{
Debug::print(cid, "nop[Aaa-%02d];AaaStateManager/n", _rid);
return;
}
_rid = AaaId;
if (!valid())
{
return;
}
val.AaaId = AaaId;
val.AaaState = FM_OP_UNKNOWN;
char sub[128];
snprintf(sub, 128, "/Aaa_%d", _rid);
_stateCount = 1;
_alarmCount = 1;
_alarm = new AlarmMapItem*[_alarmCount];
_alarm[0] =
&AlarmMap::item(SelfConf::shelf, SelfConf::slot, sub, OFM_ALM_AAA_DOWN, OFM_SEV_MAJOR);
_alarm[0]->flag = FM_ADF_ACTIVE;
_alarm[0]->filtered(false);
}
int AaaStateManager::Provider::sndStat()
{
if(pre_state != val.AaaState)
{
SnmpAaaStateChange noti;
memset(¬i, 0, sizeof(SnmpAaaStateChange));
noti.msgHeader.msg_id = Write16(_msg_UsiAaaStateInd_OfmOagx);
noti.body.AaaId = Write16(val.AaaId);
noti.body.AaaIpVer = Write32(val.AaaIpVer);
if( val.AaaIpVer == IPV4)
{
noti.body.AaaIpV4 =Write32(val.AaaIpV4);
}
else
{
for(int n = 0 ; n < 16 ; n++)
{
noti.body.AaaIpV6[n] = val.AaaIpV6[n];
}
}
noti.body.AaaState = Write8(val.AaaState);
memcpy(&(noti.body.ReleaseReasonStr), &(val.ReleaseReasonStr),sizeof(val.ReleaseReasonStr));
DateTime::fill(noti.body.AaacTimestamp, 11, DateTime::now());
//TrapManager::onTrapSend(¬i, sizeof(SnmpAaaStateChange));
//GmsClient::sendTo(¬i, sizeof(SnmpAaaStateChange), MwUtil::addr(MwUtil::localAddress(), OAGM_TRAP_MBOX_ID), WebEMT);
TrapManager::send(¬i, sizeof(SnmpAaaStateChange), display, WebEMT);
Debug::print(DateTime::now(), cid, "AaaStateManager::sendAaaStatus/n");
timeval ts = DateTime::now().timeval();
_alarm[0]->ts = ts;
if(val.AaaState != FM_OP_ENABLED)
{
_alarm[0]->update(true);
}
else
{
if(pre_state != FM_OP_UNKNOWN)
_alarm[0]->update(false);
}
}
if ((table != NULL)&&(pre_state == FM_OP_UNKNOWN))
{
table->rows.add(this);
}
return 0;
}
void AaaStateManager::Provider::reset()
{
if (table != NULL)
{
table->rows.remove(this);
}
_stateCount = 0;
_alarm = NULL;
_alarmCount = 0;
_rid = 0xFFFF;
memset(&val,0 , sizeof(AaaStateInfo));
val.AaaId = 0xFFFF;
val.AaaState = 0xFF;
}
bool AaaStateManager::Provider::valid()
{
return (_rid < FM_MAX_AAA_COUNT);
}
void AaaStateManager::Provider::eval()
{
if (!valid())
{
return;
}
}
void* AaaStateManager::Provider::get()
{
memcpy(&htonval,&val,sizeof(val));
htonval.AaaId = Write16(val.AaaId);
htonval.AaaIpVer = Write32(val.AaaIpVer);
htonval.AaaIpV4 = Write32(val.AaaIpV4);
return &htonval;
}
void AaaStateManager::Provider::display(void* buffer, word32 len, bool enabled)
{
if ((buffer == NULL) || (len != sizeof(SnmpAaaStateChange)))
{
return;
}
SnmpAaaStateChange& noti = *((SnmpAaaStateChange*)buffer);
DateTime::fill(noti.body.AaacTimestamp, 11, DateTime::now());
DateTime now = DateTime::now();
string cur;
char sid[32] = {0,};
if (noti.body.AaaId != 0xFFFF)
{
snprintf(sid, 31, "Aaa-%02d", noti.body.AaaId);
}
else
{
snprintf(sid, 31, "Aaa-**-**");
}
if(noti.body.AaaState == 1)
cur = "enable";
else
cur = "disable";
Debug::print(now, cid, "Aaa_noti %c%s-%02d%s/n",
enabled ? ' ' : 'x',
sid,
noti.body.AaaId, cur.c_str());
if (!enabled)
{
return;
}
// Append to state log.
FILE* fp = TrapManager::statelog.fp();
if (fp == NULL)
{
return;
}
const byte* ts = noti.body.AaacTimestamp;
fprintf(fp, "[%s] %04d%02d%02d%02d%02d%02d %s-%02d -%2d%s/n",
now.toString().substr(0, 19).c_str(),
*(word16*)ts, ts[2], ts[3], ts[4], ts[5], ts[6],
sid,
noti.body.AaaId,
noti.body.AaaState,cur.c_str());
fflush(fp);
}
}
#endif