1
0
mirror of https://gitee.com/willfree/mlsr.git synced 2026-06-05 16:49:32 +08:00

增加支持邻接LSA与json字符串互相转换

This commit is contained in:
free will
2022-05-26 21:13:57 +08:00
parent 1efd9c63aa
commit a25c524c68
9 changed files with 374 additions and 176 deletions
+143 -21
View File
@@ -9,30 +9,152 @@
package lsa
import (
"encoding/json"
"minlib/component"
"minlib/encoding"
"mlsr/extensions"
)
//
// AdjLsa
// @Description: 邻接LSA
//
type AdjLsa struct {
encoding.SelfEncodingBase
LsaBase
AdjLsaAdjacenctList
}
//
// innerAdjLsa
// @Description: 内置结构体之:邻接LSA
//
type innerAdjLsa struct {
LsaTypeInt int
LsaOriginRouter string
LsaSequenceNumber uint64
LsaExpirationTime uint64
AdjacencyInfoList []innerAdjacencyInfo
}
//
// innerAdjacencyInfo
// @Description: 内置结构体之:邻接信息
//
type innerAdjacencyInfo struct {
LinkCost uint64
LogicFaceUri string
LogicFaceId uint64
NeighborRouter string
InterestTimedOutNo uint32
Status int
}
//
// parseAdjLsaToInner
// @Description: 邻接LSA转内置结构体
// @param aLsa
// @return innerAdjLsa
// @return error
//
func parseAdjLsaToInner(aLsa *AdjLsa) (innerAdjLsa, error) {
innerALSA := innerAdjLsa{}
innerALSA.LsaTypeInt = int(aLsa.GetType())
innerALSA.LsaOriginRouter = aLsa.LsaOriginRouterIdentifier.ToUri()
innerALSA.LsaSequenceNumber = aLsa.GetSeqNo()
innerALSA.LsaExpirationTime = aLsa.GetExpirationTime()
for i := 0; i < len(aLsa.lsaAdjacencys); i++ {
info := innerAdjacencyInfo{}
info.LinkCost = aLsa.lsaAdjacencys[i].LinkCost()
info.LogicFaceUri = aLsa.lsaAdjacencys[i].LogicFaceUri()
info.LogicFaceId = aLsa.lsaAdjacencys[i].LogicFaceId()
info.NeighborRouter = aLsa.lsaAdjacencys[i].lsaNeighborRouterIdentifier.ToUri()
info.InterestTimedOutNo = aLsa.lsaAdjacencys[i].InterestTimedOutNo
info.Status = aLsa.lsaAdjacencys[i].Status
innerALSA.AdjacencyInfoList = append(innerALSA.AdjacencyInfoList, info)
}
return innerALSA, nil
}
//
// parseInnerToAdjLsa
// @Description: 内置结构体转邻接LSA
// @param inner
// @param alsa
// @return error
//
func parseInnerToAdjLsa(inner *innerAdjLsa, alsa *AdjLsa) error {
routerIdent, err := component.CreateIdentifierByString(inner.LsaOriginRouter)
if err != nil {
return err
}
alsa.SetOriginRouter(routerIdent)
alsa.SetSeqNo(inner.LsaSequenceNumber)
alsa.SetExpirationTime(inner.LsaExpirationTime)
for i := 0; i < len(inner.AdjacencyInfoList); i++ {
realInfo := new(AdjLsaAdjacencyInfo)
realInfo.SetLinkCost(inner.AdjacencyInfoList[i].LinkCost)
realInfo.SetLogicFaceUri(inner.AdjacencyInfoList[i].LogicFaceUri)
realInfo.SetLogicFaceId(inner.AdjacencyInfoList[i].LogicFaceId)
iden, err2 := component.CreateIdentifierByString(
inner.AdjacencyInfoList[i].NeighborRouter)
if err2 != nil {
return err2
}
realInfo.SetNeighborRouterIdentifier(iden)
realInfo.InterestTimedOutNo = inner.AdjacencyInfoList[i].InterestTimedOutNo
realInfo.Status = inner.AdjacencyInfoList[i].Status
alsa.lsaAdjacencys = append(alsa.lsaAdjacencys, realInfo)
}
return nil
}
//
// LoadFromJsonString
// @Description: 从json字符串中加载AdjLsa
// @receiver a
// @param jsonBytes
// @return error
//
func (a *AdjLsa) LoadFromJsonString(jsonBytes []byte) error {
// 转中间结构体
inner := innerAdjLsa{}
err := json.Unmarshal(jsonBytes, &inner)
if err != nil {
return err
}
// 转实际结构体
err = parseInnerToAdjLsa(&inner, a)
if err != nil {
return err
}
return nil
}
//
// GetJsonString
// @Description: AdjLsa转json
// @receiver a
// @return []byte
// @return error
//
func (a *AdjLsa) GetJsonString() ([]byte, error) {
innerAdjLsa, err := parseAdjLsaToInner(a)
if err != nil {
return nil, err
}
b, err := json.Marshal(innerAdjLsa)
if err != nil {
return nil, err
}
return b, nil
}
func (a *AdjLsa) GetType() LsaType {
return LsaADJACENCYType
}
func (a *AdjLsa) GetJsonString() ([]byte,error) {
// todo
return nil,nil
}
func (a *AdjLsa) LoadFromJsonString(jsonBytes []byte) error{
//todo
return nil
}
//
// GetAdjList
// @Description: 获取邻接信息表
@@ -89,22 +211,22 @@ func (a *AdjLsa) ToString() string {
// @return int
// @return error
//
func (a *AdjLsa) WireEncode(encoder *encoding.Encoder) (int,error) {
func (a *AdjLsa) WireEncode(encoder *encoding.Encoder) (int, error) {
totalLength := 0
// TLV-Value
tmpLen,err := a.AdjLsaAdjacenctList.WireEncode(encoder)
tmpLen, err := a.AdjLsaAdjacenctList.WireEncode(encoder)
if err != nil {
return 0, err
}
totalLength+=tmpLen
tmpLen,err = a.LsaBase.WireEncode(encoder)
totalLength += tmpLen
tmpLen, err = a.LsaBase.WireEncode(encoder)
if err != nil {
return 0, err
}
totalLength+=tmpLen
totalLength += tmpLen
// TLV-Length
tmpLen,err = encoder.PrependVarNumber(encoding.VlInt(totalLength))
tmpLen, err = encoder.PrependVarNumber(encoding.VlInt(totalLength))
if err != nil {
return 0, err
}
@@ -117,7 +239,7 @@ func (a *AdjLsa) WireEncode(encoder *encoding.Encoder) (int,error) {
}
totalLength += tmpLen
return totalLength,nil
return totalLength, nil
}
//
@@ -139,17 +261,17 @@ func (a *AdjLsa) WireDecode(block *encoding.Block) error {
}
// 提取参数
for _, parameter := range block.GetSubElements(){
for _, parameter := range block.GetSubElements() {
switch parameter.GetType() {
case extensions.TlvMlsrLsaBase:
if err:= a.LsaBase.WireDecode(parameter); err != nil {
if err := a.LsaBase.WireDecode(parameter); err != nil {
return err
}
case extensions.TlvMlsrAdjLsaAdjacencyList:
if err:= a.AdjLsaAdjacenctList.WireDecode(parameter); err != nil {
if err := a.AdjLsaAdjacenctList.WireDecode(parameter); err != nil {
return err
}
}
}
return nil
}
}
+33 -33
View File
@@ -16,19 +16,19 @@ import (
)
const (
STATUS_UNKNOWN = -1
STATUS_UNKNOWN = -1
STATUS_INACTIVE = 0
STATUS_ACTIVE = 1
STATUS_ACTIVE = 1
)
type AdjLsaAdjacencyInfo struct {
AdjLsaLinkCost // 两个路由器之间的链路成本
AdjLsaLogicFaceUri // MIR层级对LogicFace的字符串描述,如udp://1.0.0.2,或ether:aa.aa.aa.aa...
AdjLsaLogicFaceId // MIR给LogicFace分配的uint64类型的id,如182828nlsr可通过该id定位到哪个logicface负责传该链路数据
AdjLsaLinkCost // 两个路由器之间的链路成本
AdjLsaLogicFaceUri // MIR层级对LogicFace的字符串描述,如udp://1.0.0.2,或ether:aa.aa.aa.aa...
AdjLsaLogicFaceId // MIR给LogicFace分配的uint64类型的id,如182828nlsr可通过该id定位到哪个logicface负责传该链路数据
lsaNeighborRouterIdentifier *component.Identifier // NLSR已配置的邻居路由器标识符,如/min/pkusz/group1/router1
InterestTimedOutNo uint32 // 自从收到最后一个回复之后,记录已经失败的hello兴趣包的数目
Status int // 表示邻居路由器的状态:未知-1、未激活0、激活1
InterestTimedOutNo uint32 // 自从收到最后一个回复之后,记录已经失败的hello兴趣包的数目
Status int // 表示邻居路由器的状态:未知-1、未激活0、激活1
}
//
@@ -45,7 +45,7 @@ func NewAdjLsaAdjacencyInfo(adjLsaLinkCost AdjLsaLinkCost, adjLsaLogicFaceUri Ad
faceId AdjLsaLogicFaceId, m_lsaNeighborRouterIdentifier *component.Identifier,
m_interestTimedOutNo uint32, m_status int) *AdjLsaAdjacencyInfo {
return &AdjLsaAdjacencyInfo{AdjLsaLinkCost: adjLsaLinkCost, AdjLsaLogicFaceUri: adjLsaLogicFaceUri,
AdjLsaLogicFaceId: faceId,lsaNeighborRouterIdentifier: m_lsaNeighborRouterIdentifier,
AdjLsaLogicFaceId: faceId, lsaNeighborRouterIdentifier: m_lsaNeighborRouterIdentifier,
InterestTimedOutNo: m_interestTimedOutNo, Status: m_status}
}
@@ -65,7 +65,7 @@ func NewAdjLsaAdjacencyInfoByInstance(adjLsaLinkCost *AdjLsaLinkCost, adjLsaLogi
m_interestTimedOutNo uint32, m_status int) *AdjLsaAdjacencyInfo {
adjRes := AdjLsaAdjacencyInfo{}
adjRes.SetLinkCost(adjLsaLinkCost.LinkCost())
adjRes.setLogicFaceUri(adjLsaLogicFaceUri.LogicFaceUri())
adjRes.SetLogicFaceUri(adjLsaLogicFaceUri.LogicFaceUri())
adjRes.SetLogicFaceId(faceId.LogicFaceId())
adjRes.SetNeighborRouterIdentifier(m_lsaNeighborRouterIdentifier)
adjRes.InterestTimedOutNo = m_interestTimedOutNo
@@ -91,7 +91,7 @@ func (l *AdjLsaAdjacencyInfo) GetNeighborRouterIdentifier() *component.Identifie
//
func (l *AdjLsaAdjacencyInfo) SetNeighborRouterIdentifier(routerIdentifier *component.Identifier) {
// todo: 验证identifier,如类型、字段是否为空等
l.lsaNeighborRouterIdentifier= routerIdentifier
l.lsaNeighborRouterIdentifier = routerIdentifier
}
//
@@ -103,7 +103,7 @@ func (l *AdjLsaAdjacencyInfo) SetNeighborRouterIdentifier(routerIdentifier *comp
//
func (l *AdjLsaAdjacencyInfo) Compare(routerIdentifier *component.Identifier) bool {
// todo: 验证identifier,如类型、字段是否为空等
if routerIdentifier.ToUri()==l.lsaNeighborRouterIdentifier.ToUri() {
if routerIdentifier.ToUri() == l.lsaNeighborRouterIdentifier.ToUri() {
return true
}
return false
@@ -117,7 +117,7 @@ func (l *AdjLsaAdjacencyInfo) Compare(routerIdentifier *component.Identifier) bo
// @return bool
//
func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUri(faceUri *AdjLsaLogicFaceUri) bool {
if faceUri.LogicFaceUri()==l.LogicFaceUri(){
if faceUri.LogicFaceUri() == l.LogicFaceUri() {
return true
}
return false
@@ -131,7 +131,7 @@ func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUri(faceUri *AdjLsaLogicFaceUri) b
// @return bool
//
func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUriString(faceUriString string) bool {
if faceUriString==l.LogicFaceUri(){
if faceUriString == l.LogicFaceUri() {
return true
}
return false
@@ -145,7 +145,7 @@ func (l *AdjLsaAdjacencyInfo) CompareLogicFaceUriString(faceUriString string) bo
// @return bool
//
func (l *AdjLsaAdjacencyInfo) CompareLogicFaceId(faceId uint64) bool {
if faceId==l.LogicFaceId(){
if faceId == l.LogicFaceId() {
return true
}
return false
@@ -163,7 +163,7 @@ func (l *AdjLsaAdjacencyInfo) ToString() string {
resString += ", LogicFaceUri: "
resString += l.LogicFaceUri()
resString += ", LogicFaceId: "
resString += strconv.FormatUint(l.LogicFaceId(),10)
resString += strconv.FormatUint(l.LogicFaceId(), 10)
resString += ", NeighborRouterIdentifier: "
resString += l.lsaNeighborRouterIdentifier.ToUri()
resString += ", InterestTimedOutNo: "
@@ -182,41 +182,41 @@ func (l *AdjLsaAdjacencyInfo) ToString() string {
// @return int
// @return error
//
func (l *AdjLsaAdjacencyInfo) WireEncode(encoder *encoding.Encoder) (int,error) {
func (l *AdjLsaAdjacencyInfo) WireEncode(encoder *encoding.Encoder) (int, error) {
totalLength := 0
// TLV-Value
if tmpLen,err := l.AdjLsaLinkCost.WireEncode(encoder); err!=nil{
if tmpLen, err := l.AdjLsaLinkCost.WireEncode(encoder); err != nil {
return 0, err
}else{
} else {
totalLength += tmpLen
}
if tmpLen,err := l.AdjLsaLogicFaceUri.WireEncode(encoder); err!=nil{
if tmpLen, err := l.AdjLsaLogicFaceUri.WireEncode(encoder); err != nil {
return 0, err
}else{
} else {
totalLength += tmpLen
}
if tmpLen,err := l.lsaNeighborRouterIdentifier.WireEncode(encoder); err!=nil{
if tmpLen, err := l.lsaNeighborRouterIdentifier.WireEncode(encoder); err != nil {
return 0, err
}else{
} else {
totalLength += tmpLen
}
// TLV-Length
if tmpLen,err := encoder.PrependVarNumber(encoding.VlInt(totalLength)); err!=nil{
if tmpLen, err := encoder.PrependVarNumber(encoding.VlInt(totalLength)); err != nil {
return 0, err
}else{
} else {
totalLength += tmpLen
}
// TLV-Type
if tmpLen,err := encoder.PrependVarNumber(extensions.TlvMlsrAdjLsaAdjacencyInfo); err!=nil{
if tmpLen, err := encoder.PrependVarNumber(extensions.TlvMlsrAdjLsaAdjacencyInfo); err != nil {
return 0, err
}else{
} else {
totalLength += tmpLen
}
return totalLength,nil
return totalLength, nil
}
//
@@ -238,23 +238,23 @@ func (l *AdjLsaAdjacencyInfo) WireDecode(block *encoding.Block) error {
}
// 提取参数
for _, parameter := range block.GetSubElements(){
for _, parameter := range block.GetSubElements() {
switch parameter.GetType() {
case encoding.TlvIdentifier:
iden:=component.Identifier{}
if err:= iden.WireDecode(parameter); err != nil {
iden := component.Identifier{}
if err := iden.WireDecode(parameter); err != nil {
return err
}
l.lsaNeighborRouterIdentifier = &iden
case extensions.TlvMlsrAdjLsaLogicFaceUri:
if err:= l.AdjLsaLogicFaceUri.WireDecode(parameter); err != nil {
if err := l.AdjLsaLogicFaceUri.WireDecode(parameter); err != nil {
return err
}
case extensions.TlvMlsrAdjLsaLinkCost:
if err:= l.AdjLsaLinkCost.WireDecode(parameter); err != nil {
if err := l.AdjLsaLinkCost.WireDecode(parameter); err != nil {
return err
}
}
}
return nil
}
}
+23 -23
View File
@@ -21,18 +21,18 @@ import (
// @param t
//
func TestNewAdjLsaAdjacencyInfo(t *testing.T) {
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
fmt.Println("cost: ",adjInfo.LinkCost())
fmt.Println("uri: ",adjInfo.LogicFaceUri())
fmt.Println("id: ",adjInfo.LogicFaceId())
fmt.Println("identifier: ",iden.ToUri())
iden, _ := component.CreateIdentifierByString("/pku/router")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
fmt.Println("cost: ", adjInfo.LinkCost())
fmt.Println("uri: ", adjInfo.LogicFaceUri())
fmt.Println("id: ", adjInfo.LogicFaceId())
fmt.Println("identifier: ", iden.ToUri())
fmt.Println(adjInfo.ToString())
}
@@ -43,16 +43,16 @@ func TestNewAdjLsaAdjacencyInfo(t *testing.T) {
//
func TestAdjLsaAdjacencyInfo_Compare(t *testing.T) {
// 先构造
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
// 再测试:全部为true则pass
iden2,_ := component.CreateIdentifierByString("/pku/router")
iden2, _ := component.CreateIdentifierByString("/pku/router")
fmt.Println(adjInfo.Compare(iden2))
fmt.Println(adjInfo.CompareLogicFaceId(77))
fmt.Println(adjInfo.CompareLogicFaceUriString("uri"))
@@ -66,14 +66,14 @@ func TestAdjLsaAdjacencyInfo_Compare(t *testing.T) {
//
func TestAdjLsaAdjacencyInfo_WireEncode(t *testing.T) {
// 先构造
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
// 构造编码器
encoder := encoding.Encoder{}
@@ -95,8 +95,8 @@ func TestAdjLsaAdjacencyInfo_WireEncode(t *testing.T) {
return
}
fmt.Println(cnt)
for i:=0;i < len(res);i++{
fmt.Print(res[i],", ")
for i := 0; i < len(res); i++ {
fmt.Print(res[i], ", ")
}
fmt.Println()
// 241, 1, 169, 33, 101, 19, 100, 2, 0, 47, 100, 4, 0, 112, 107, 117, 100, 7, 0, 114, 111, 117, 116, 101, 114, 241, 1, 168, 3, 117, 114, 105, 241, 1, 167, 1, 100,
@@ -127,4 +127,4 @@ func TestAdjLsaAdjacencyInfo_WireDecode(t *testing.T) {
return
}
fmt.Println(adjInfo.ToString())
}
}
+37 -37
View File
@@ -22,21 +22,21 @@ import (
//
func TestAdjLsaAdjacenctList_GetAdjList(t *testing.T) {
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
// 构造Info列表
adjInfoList := new(AdjLsaAdjacenctList)
adjInfoList.Insert(adjInfo)
// 再加第二个info
iden2,_ := component.CreateIdentifierByString("/pku/router2")
iden2, _ := component.CreateIdentifierByString("/pku/router2")
adjInfo.SetNeighborRouterIdentifier(iden2)
adjInfoList.Insert(adjInfo)
@@ -51,20 +51,20 @@ func TestAdjLsaAdjacenctList_GetAdjList(t *testing.T) {
//
func TestAdjLsaAdjacenctList_IsNeighbor(t *testing.T) {
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
adjInfoList := new(AdjLsaAdjacenctList)
adjInfoList.Insert(adjInfo)
// 测试某路由器是否是邻居节点
fmt.Println(adjInfoList.IsNeighbor(iden))
iden2,_ := component.CreateIdentifierByString("/pku/router2")
iden2, _ := component.CreateIdentifierByString("/pku/router2")
fmt.Println(adjInfoList.IsNeighbor(iden2))
}
@@ -75,14 +75,14 @@ func TestAdjLsaAdjacenctList_IsNeighbor(t *testing.T) {
//
func TestAdjLsaAdjacenctList_IncrementTimedOutInterestCount(t *testing.T) {
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
adjInfoList := new(AdjLsaAdjacenctList)
adjInfoList.Insert(adjInfo)
@@ -105,14 +105,14 @@ func TestAdjLsaAdjacenctList_IncrementTimedOutInterestCount(t *testing.T) {
//
func TestAdjLsaAdjacenctList_GetStatusOfNeighbor(t *testing.T) {
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
adjInfoList := new(AdjLsaAdjacenctList)
adjInfoList.Insert(adjInfo)
@@ -120,7 +120,7 @@ func TestAdjLsaAdjacenctList_GetStatusOfNeighbor(t *testing.T) {
fmt.Println(adjInfoList.GetStatusOfNeighbor(iden))
// 设置再获取
adjInfoList.SetStatusOfNeighbor(iden,2)
adjInfoList.SetStatusOfNeighbor(iden, 2)
fmt.Println(adjInfoList.GetStatusOfNeighbor(iden))
}
@@ -131,14 +131,14 @@ func TestAdjLsaAdjacenctList_GetStatusOfNeighbor(t *testing.T) {
//
func TestAdjLsaAdjacenctList_FindAdjacencyByRouterIdentifier(t *testing.T) {
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
adjInfoList := new(AdjLsaAdjacenctList)
adjInfoList.Insert(adjInfo)
@@ -156,19 +156,19 @@ func TestAdjLsaAdjacenctList_FindAdjacencyByRouterIdentifier(t *testing.T) {
//
func TestAdjLsaAdjacenctList_WireEncode(t *testing.T) {
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
// 构造Info列表
adjInfoList := new(AdjLsaAdjacenctList)
adjInfoList.Insert(adjInfo)
// 再加第二个info
iden2,_ := component.CreateIdentifierByString("/pku/router2")
iden2, _ := component.CreateIdentifierByString("/pku/router2")
adjInfo2 := AdjLsaAdjacencyInfo{
AdjLsaLinkCost: adjInfo.AdjLsaLinkCost,
AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri,
@@ -200,8 +200,8 @@ func TestAdjLsaAdjacenctList_WireEncode(t *testing.T) {
return
}
fmt.Println(cnt)
for i:=0;i < len(res);i++{
fmt.Print(res[i],", ")
for i := 0; i < len(res); i++ {
fmt.Print(res[i], ", ")
}
fmt.Println()
}
@@ -233,4 +233,4 @@ func TestAdjLsaAdjacenctList_WireDecode(t *testing.T) {
return
}
fmt.Println(base.ToString())
}
}
+4 -4
View File
@@ -33,12 +33,12 @@ func (l *AdjLsaLogicFaceUri) LogicFaceUri() string {
}
//
// setLogicFaceUri
// SetLogicFaceUri
// @Description: 设置LogicFaceUri
// @receiver l
// @param logicFaceUri
//
func (l *AdjLsaLogicFaceUri) setLogicFaceUri(logicFaceUri string) {
func (l *AdjLsaLogicFaceUri) SetLogicFaceUri(logicFaceUri string) {
l.CommonString.SetValue(logicFaceUri)
}
@@ -50,7 +50,7 @@ func (l *AdjLsaLogicFaceUri) setLogicFaceUri(logicFaceUri string) {
// @return int
// @return error
//
func (l *AdjLsaLogicFaceUri) WireEncode(encoder *encoding.Encoder) (int,error) {
func (l *AdjLsaLogicFaceUri) WireEncode(encoder *encoding.Encoder) (int, error) {
l.CommonString.SetTlvType(extensions.TlvMlsrAdjLsaLogicFaceUri)
return l.CommonString.WireEncode(encoder)
}
@@ -64,7 +64,7 @@ func (l *AdjLsaLogicFaceUri) WireEncode(encoder *encoding.Encoder) (int,error) {
//
func (l *AdjLsaLogicFaceUri) WireDecode(block *encoding.Block) error {
// 检查type是否正确
if err := encoding.ExpectType(block.GetType(),extensions.TlvMlsrAdjLsaLogicFaceUri); err!=nil {
if err := encoding.ExpectType(block.GetType(), extensions.TlvMlsrAdjLsaLogicFaceUri); err != nil {
return err
}
return l.CommonString.WireDecode(block)
+3 -3
View File
@@ -21,7 +21,7 @@ import (
//
func TestAdjLsaLogicFaceUri_LogicFaceUri(t *testing.T) {
testCPU := AdjLsaLogicFaceUri{}
testCPU.setLogicFaceUri("url")
testCPU.SetLogicFaceUri("url")
fmt.Println(testCPU.LogicFaceUri())
}
@@ -33,7 +33,7 @@ func TestAdjLsaLogicFaceUri_LogicFaceUri(t *testing.T) {
func TestAdjLsaLogicFaceUri_WireEncode(t *testing.T) {
testCPU := new(AdjLsaLogicFaceUri)
fmt.Println(testCPU.LogicFaceUri())
testCPU.setLogicFaceUri("url")
testCPU.SetLogicFaceUri("url")
fmt.Println(testCPU.LogicFaceUri())
//testCPC.SetTlvType(5)
fmt.Println(testCPU.Value())
@@ -72,7 +72,7 @@ func TestAdjLsaLogicFaceUri_WireDecode(t *testing.T) {
fmt.Println("interest", testCPU.Value())
//testCPC.SetTlvType(5)
fmt.Println(testCPU.TlvType())
testCPU.setLogicFaceUri("url")
testCPU.SetLogicFaceUri("url")
fmt.Println("interest", testCPU.Value())
fmt.Println("interest", testCPU.IsInitial())
var encoder encoding.Encoder
+69 -17
View File
@@ -9,12 +9,64 @@
package lsa
import (
"encoding/json"
"fmt"
common2 "minlib/common"
"minlib/component"
"minlib/encoding"
"testing"
)
func TestAdjLsa_JsonConv(t *testing.T) {
// 构造基本部分
base := new(AdjLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku")
// 构造邻接信息部分
// 先构造一个info
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
base.Insert(adjInfo)
// 再加第二个info
iden2, _ := component.CreateIdentifierByString("/pku/router2")
adjInfo2 := AdjLsaAdjacencyInfo{
AdjLsaLinkCost: adjInfo.AdjLsaLinkCost,
AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri,
AdjLsaLogicFaceId: adjInfo.AdjLsaLogicFaceId,
lsaNeighborRouterIdentifier: iden2,
InterestTimedOutNo: 111,
Status: 0,
}
base.Insert(&adjInfo2)
// 打印该adjlsa
common2.LogInfo(base.ToString())
// 转byte[]类型的json
jsonBytes, err := json.Marshal(&base)
if err != nil {
common2.LogInfo("邻接LSA json 转换失败")
}
common2.LogInfo("直接用Marshal将邻接LSA为Json: " + string(jsonBytes))
jsonBytes, err = base.GetJsonString()
common2.LogInfo("通过中间结构体将邻接LSA转为json: " + string(jsonBytes))
// 从Json转回来
newAdjLsa := new(AdjLsa)
err = newAdjLsa.LoadFromJsonString(jsonBytes)
if err != nil {
common2.LogInfo("邻接LSA json 转换失败")
}
common2.LogInfo("通过中间结构体将json转为邻接LSA: " + newAdjLsa.ToString())
}
//
// TestAdjLsa_New
// @Description: 构造
@@ -25,20 +77,20 @@ func TestAdjLsa_New(t *testing.T) {
base := new(AdjLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku")
base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku")
// 构造邻接信息部分
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
base.Insert(adjInfo)
// 再加第二个info
iden2,_ := component.CreateIdentifierByString("/pku/router2")
iden2, _ := component.CreateIdentifierByString("/pku/router2")
adjInfo2 := AdjLsaAdjacencyInfo{
AdjLsaLinkCost: adjInfo.AdjLsaLinkCost,
AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri,
@@ -63,20 +115,20 @@ func TestAdjLsa_WireEncode(t *testing.T) {
base := new(AdjLsa)
base.SetLsaExpirationTime(2000)
base.SetLsaSequenceNumber(1234)
base.LsaOriginRouterIdentifier,_ = component.CreateIdentifierByString("/min/pku")
base.LsaOriginRouterIdentifier, _ = component.CreateIdentifierByString("/min/pku")
// 构造邻接信息部分
// 先构造一个info
linkCost :=AdjLsaLinkCost{}
linkCost := AdjLsaLinkCost{}
linkCost.SetLinkCost(100)
faceUri := AdjLsaLogicFaceUri{}
faceUri.setLogicFaceUri("uri")
faceId :=AdjLsaLogicFaceId{}
faceUri.SetLogicFaceUri("uri")
faceId := AdjLsaLogicFaceId{}
faceId.SetLogicFaceId(77)
iden,_ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost,faceUri,faceId,iden,123,1)
iden, _ := component.CreateIdentifierByString("/pku/router1")
adjInfo := NewAdjLsaAdjacencyInfo(linkCost, faceUri, faceId, iden, 123, 1)
base.Insert(adjInfo)
// 再加第二个info
iden2,_ := component.CreateIdentifierByString("/pku/router2")
iden2, _ := component.CreateIdentifierByString("/pku/router2")
adjInfo2 := AdjLsaAdjacencyInfo{
AdjLsaLinkCost: adjInfo.AdjLsaLinkCost,
AdjLsaLogicFaceUri: adjInfo.AdjLsaLogicFaceUri,
@@ -107,8 +159,8 @@ func TestAdjLsa_WireEncode(t *testing.T) {
return
}
fmt.Println(cnt)
for i:=0;i < len(res);i++{
fmt.Print(res[i],", ")
for i := 0; i < len(res); i++ {
fmt.Print(res[i], ", ")
}
fmt.Println()
}
@@ -143,4 +195,4 @@ func TestAdjLsa_WireDecode(t *testing.T) {
return
}
fmt.Println(base.ToString())
}
}
+53 -38
View File
@@ -15,6 +15,10 @@ import (
"mlsr/extensions"
)
//
// NameLsa
// @Description: 名称LSA
//
type NameLsa struct {
encoding.SelfEncodingBase
LsaBase
@@ -23,19 +27,23 @@ type NameLsa struct {
//
// innerNameLsa
// @Description: 转json所用的中间结构体
// @Description: 内置结构体之:名称LSA
//
type innerNameLsa struct {
LsaTypeInt int
LsaOriginRouter string
LsaTypeInt int
LsaOriginRouter string
LsaSequenceNumber uint64
LsaExpirationTime uint64
NamePrefixList []innerNamePrefixInfo
NamePrefixList []innerNamePrefixInfo
}
//
// innerNamePrefixInfo
// @Description: 内置结构体之:名称前缀信息
//
type innerNamePrefixInfo struct {
NamePrefix string
Sources []string
Sources []string
}
//
@@ -45,19 +53,19 @@ type innerNamePrefixInfo struct {
// @return innerNameLsa
// @return error
//
func parseNameLsaToInner(nLsa *NameLsa) (innerNameLsa,error) {
func parseNameLsaToInner(nLsa *NameLsa) (innerNameLsa, error) {
innerNLSA := innerNameLsa{}
innerNLSA.LsaTypeInt = int(nLsa.GetType())
innerNLSA.LsaOriginRouter = nLsa.LsaOriginRouterIdentifier.ToUri()
innerNLSA.LsaSequenceNumber = nLsa.GetSeqNo()
innerNLSA.LsaExpirationTime = nLsa.GetExpirationTime()
for i:=0;i<len(nLsa.lsaNamePrefixs);i++{
for i := 0; i < len(nLsa.lsaNamePrefixs); i++ {
info := innerNamePrefixInfo{}
info.Sources = nLsa.lsaNamePrefixs[i].Sources
info.NamePrefix = nLsa.lsaNamePrefixs[i].NamePrefix.ToUri()
innerNLSA.NamePrefixList = append(innerNLSA.NamePrefixList, info)
}
return innerNLSA,nil
return innerNLSA, nil
}
//
@@ -67,37 +75,44 @@ func parseNameLsaToInner(nLsa *NameLsa) (innerNameLsa,error) {
// @param nlsa
// @return error
//
func parseInnerToNameLsa(inner *innerNameLsa,nlsa *NameLsa) error {
routerIdent,err := component.CreateIdentifierByString(inner.LsaOriginRouter)
if err!=nil{
func parseInnerToNameLsa(inner *innerNameLsa, nlsa *NameLsa) error {
routerIdent, err := component.CreateIdentifierByString(inner.LsaOriginRouter)
if err != nil {
return err
}
nlsa.SetOriginRouter(routerIdent)
nlsa.SetSeqNo(inner.LsaSequenceNumber)
nlsa.SetExpirationTime(inner.LsaExpirationTime)
for i:=0;i<len(inner.NamePrefixList);i++{
for i := 0; i < len(inner.NamePrefixList); i++ {
realInfo := new(NameLsaNamePrefixInfo)
realInfo.Sources=inner.NamePrefixList[i].Sources
realInfo.NamePrefix,err = component.CreateIdentifierByString(
realInfo.Sources = inner.NamePrefixList[i].Sources
realInfo.NamePrefix, err = component.CreateIdentifierByString(
inner.NamePrefixList[i].NamePrefix)
if err!=nil{
if err != nil {
return err
}
nlsa.lsaNamePrefixs = append(nlsa.lsaNamePrefixs,realInfo)
nlsa.lsaNamePrefixs = append(nlsa.lsaNamePrefixs, realInfo)
}
return nil
}
//
// LoadFromJsonString
// @Description: 从Json字符串中加载名称LSA
// @receiver n
// @param jsonBytes
// @return error
//
func (n *NameLsa) LoadFromJsonString(jsonBytes []byte) error {
// 转中间结构体
inner := innerNameLsa{}
err := json.Unmarshal(jsonBytes,&inner)
if err!= nil{
err := json.Unmarshal(jsonBytes, &inner)
if err != nil {
return err
}
// 转实际结构体
err = parseInnerToNameLsa(&inner,n)
if err!= nil{
err = parseInnerToNameLsa(&inner, n)
if err != nil {
return err
}
@@ -111,16 +126,16 @@ func (n *NameLsa) LoadFromJsonString(jsonBytes []byte) error {
// @return []byte
// @return error
//
func (n *NameLsa) GetJsonString() ([]byte,error) {
innerNameLsa,err := parseNameLsaToInner(n)
if err!= nil {
func (n *NameLsa) GetJsonString() ([]byte, error) {
innerNameLsa, err := parseNameLsaToInner(n)
if err != nil {
return nil, err
}
b,err := json.Marshal(innerNameLsa)
if err!= nil {
b, err := json.Marshal(innerNameLsa)
if err != nil {
return nil, err
}
return b,nil
return b, nil
}
//
@@ -149,7 +164,7 @@ func (n *NameLsa) GetNamePrefixList() []*NameLsaNamePrefixInfo {
// @receiver n
// @param info
//
func (n *NameLsa) AddNamePrefixInfo(info *NameLsaNamePrefixInfo) {
func (n *NameLsa) AddNamePrefixInfo(info *NameLsaNamePrefixInfo) {
n.NameLsaNamePrefixList.InsertNamePrefixInfo(info)
}
@@ -176,22 +191,22 @@ func (n *NameLsa) ToString() string {
// @return int
// @return error
//
func (n *NameLsa) WireEncode(encoder *encoding.Encoder) (int,error) {
func (n *NameLsa) WireEncode(encoder *encoding.Encoder) (int, error) {
totalLength := 0
// TLV-Value
tmpLen,err := n.NameLsaNamePrefixList.WireEncode(encoder)
tmpLen, err := n.NameLsaNamePrefixList.WireEncode(encoder)
if err != nil {
return 0, err
}
totalLength+=tmpLen
tmpLen,err = n.LsaBase.WireEncode(encoder)
totalLength += tmpLen
tmpLen, err = n.LsaBase.WireEncode(encoder)
if err != nil {
return 0, err
}
totalLength+=tmpLen
totalLength += tmpLen
// TLV-Length
tmpLen,err = encoder.PrependVarNumber(encoding.VlInt(totalLength))
tmpLen, err = encoder.PrependVarNumber(encoding.VlInt(totalLength))
if err != nil {
return 0, err
}
@@ -204,7 +219,7 @@ func (n *NameLsa) WireEncode(encoder *encoding.Encoder) (int,error) {
}
totalLength += tmpLen
return totalLength,nil
return totalLength, nil
}
//
@@ -226,17 +241,17 @@ func (n *NameLsa) WireDecode(block *encoding.Block) error {
}
// 提取参数
for _, parameter := range block.GetSubElements(){
for _, parameter := range block.GetSubElements() {
switch parameter.GetType() {
case extensions.TlvMlsrLsaBase:
if err:= n.LsaBase.WireDecode(parameter); err != nil {
if err := n.LsaBase.WireDecode(parameter); err != nil {
return err
}
case extensions.TlvMlsrNameLsaNamePrefixList:
if err:= n.NameLsaNamePrefixList.WireDecode(parameter); err != nil {
if err := n.NameLsaNamePrefixList.WireDecode(parameter); err != nil {
return err
}
}
}
return nil
}
}
+9
View File
@@ -0,0 +1,9 @@
// Package route
// @Author: Wang Feng
// @Description:
// @Version: 0.1.0
// @Date: 2022/5/26 12:10
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
//
package route