diff --git a/extensions/TlvNlsrExtention.go b/extensions/TlvMlsrExtention.go similarity index 92% rename from extensions/TlvNlsrExtention.go rename to extensions/TlvMlsrExtention.go index 5e98d05..6af4ed3 100644 --- a/extensions/TlvNlsrExtention.go +++ b/extensions/TlvMlsrExtention.go @@ -23,5 +23,6 @@ const ( TlvMlsrAdjLsaLinkCost = 423 // 链路开销 TlvMlsrAdjLsaLogicFaceUri = 424 // LogicFace前缀 TlvMlsrAdjLsaAdjacencyInfo = 425 // 单条邻接LSA信息 + TlvMlsrAdjLsaAdjacencyList = 426 // 一个邻接LSA包含的所有邻接信息 ) diff --git a/lsa/AdjLsaAdjacencyList.go b/lsa/AdjLsaAdjacencyList.go index 88bc333..63eca3c 100644 --- a/lsa/AdjLsaAdjacencyList.go +++ b/lsa/AdjLsaAdjacencyList.go @@ -12,6 +12,7 @@ import ( "errors" "minlib/component" "minlib/encoding" + "mlsr/extensions" ) // 为邻接信息的数组起一个别称 @@ -335,11 +336,76 @@ func (l *AdjLsaAdjacenctList) getLogicFaceId(uri *AdjLsaLogicFaceUri) (uint64,er return -1,nil } -// todo:有待实现编码解码 +// +// WireEncode +// @Description: 线速编码 +// @receiver l +// @param encoder +// @return int +// @return error +// func (l *AdjLsaAdjacenctList) WireEncode(encoder *encoding.Encoder) (int,error) { - panic("") + // 判断列表长度,为0则抛出错误 + if len(l.lsaAdjacencys)==0 { + return 0,errors.New("Invalid AdjLsaAdjacencyList, expect at least one AdjLsaAdjacencyInfo!") + } + totalLength := 0 + // 编码 TLV-VALUE + // 反向遍历,可以保证解码的时候正向输出 + for i:=len(l.lsaAdjacencys)-1; i>=0; i-- { + if l.lsaAdjacencys[i]==nil { + continue + } + tmpLen,err := l.lsaAdjacencys[i].WireEncode(encoder) + if err != nil { + return 0, err + } + totalLength += tmpLen + } + + // 编码 TLV-LENGTH + tmpLen, err := encoder.PrependVarNumber(encoding.VlInt(totalLength)) + if err != nil { + return 0, err + } + totalLength += tmpLen + + // 编码 TLV-TYPE + tmpLen, err = encoder.PrependVarNumber(extensions.TlvMlsrAdjLsaAdjacencyList) + if err != nil { + return 0, err + } + totalLength += tmpLen + + return totalLength,nil } +// +// WireDecode +// @Description: 线速解码 +// @receiver l +// @param block +// @return error +// func (l *AdjLsaAdjacenctList) WireDecode(block *encoding.Block) error { - panic("") + // 检查 TLV-TYPE 是否正确 + if err := encoding.ExpectType(block.GetType(), extensions.TlvMlsrAdjLsaAdjacencyList); err != nil { + return err + } + + // 解析子 TLV + if err := block.ParseSubElements(); err != nil { + return err + } + adjacencyContainer := make(LsaAdjacencyContainer, len(block.GetSubElements())) + for index, element := range block.GetSubElements() { + var newAdjacencyInfo *AdjLsaAdjacencyInfo + err := newAdjacencyInfo.WireDecode(element) + if err != nil { + return err + } + adjacencyContainer[index] = newAdjacencyInfo + } + l.lsaAdjacencys = adjacencyContainer + return nil } \ No newline at end of file