mirror of
https://gitee.com/willfree/mlsr.git
synced 2026-10-04 19:42:02 +08:00
411 lines
11 KiB
Go
411 lines
11 KiB
Go
// Package lsa
|
|
// @Author: Wang Feng
|
|
// @Description: 保存邻接信息的列表
|
|
// @Version: 0.1.0
|
|
// @Date: 2022/3/9 22:30
|
|
// @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
|
//
|
|
|
|
package lsa
|
|
|
|
import (
|
|
"errors"
|
|
"minlib/component"
|
|
"minlib/encoding"
|
|
"mlsr/extensions"
|
|
)
|
|
|
|
// 为邻接信息的数组起一个别称
|
|
type LsaAdjacencyContainer []*AdjLsaAdjacencyInfo
|
|
|
|
//
|
|
// AdjLsaAdjacenctList
|
|
// @Description: 保存邻接信息的列表
|
|
//
|
|
type AdjLsaAdjacenctList struct {
|
|
encoding.SelfEncodingBase
|
|
lsaAdjacencys LsaAdjacencyContainer
|
|
}
|
|
|
|
//
|
|
// insert
|
|
// @Description: 插入一条邻接信息
|
|
// @receiver l
|
|
// @param info
|
|
//
|
|
func (l *AdjLsaAdjacenctList) Insert(info *AdjLsaAdjacencyInfo) {
|
|
l.lsaAdjacencys = append(l.lsaAdjacencys, info)
|
|
}
|
|
|
|
//
|
|
// getAdjList
|
|
// @Description: 获取包含所有邻接信息的邻接表
|
|
// @receiver l
|
|
// @return []*AdjLsaAdjacencyInfo
|
|
//
|
|
func (l *AdjLsaAdjacenctList) GetAdjList() []*AdjLsaAdjacencyInfo {
|
|
return l.lsaAdjacencys
|
|
}
|
|
|
|
//
|
|
// isNeighbor
|
|
// @Description: 判断某个MIR标识符是否是邻居MIR
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @return bool
|
|
//
|
|
func (l *AdjLsaAdjacenctList) IsNeighbor(routerIdentifier *component.Identifier) bool {
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
if l.lsaAdjacencys[i].lsaNeighborRouterIdentifier.ToUri()==routerIdentifier.ToUri(){
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
//
|
|
// findIndexByRouterIdentifier
|
|
// @Description: 根据MIR标识符找到其在邻接表中的位置。
|
|
// todo: 需要注意的是,这里只会按序查找,并返回第一个找到的邻接信息。所有调用的函数都应知晓这一点。
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @return int
|
|
// @return error
|
|
//
|
|
func (l *AdjLsaAdjacenctList) findIndexByRouterIdentifier(routerIdentifier *component.Identifier) (int,error) {
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
if l.lsaAdjacencys[i].lsaNeighborRouterIdentifier.ToUri()==routerIdentifier.ToUri(){
|
|
return i,nil
|
|
}
|
|
}
|
|
return -1,errors.New("no found the identifier")
|
|
}
|
|
|
|
//
|
|
// IncrementTimedOutInterestCount
|
|
// @Description: 将指定MIR标识符对应的邻接信息中的失败的hello兴趣包次数加一。
|
|
// 注意,这里只对按序找到的第一个邻接信息加一
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @return error
|
|
//
|
|
func (l *AdjLsaAdjacenctList) IncrementTimedOutInterestCount(routerIdentifier *component.Identifier) error {
|
|
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
|
|
return err
|
|
}else{
|
|
l.lsaAdjacencys[index].InterestTimedOutNo++
|
|
return nil
|
|
}
|
|
}
|
|
|
|
//
|
|
// SetTimedOutInterestCount
|
|
// @Description: 设置指定MIR标识符对应的邻接信息中的失败的hello兴趣包次数。
|
|
// 注意,这里只对按序找到的第一个邻接信息进行设置
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @param count
|
|
// @return error
|
|
//
|
|
func (l *AdjLsaAdjacenctList) SetTimedOutInterestCount(routerIdentifier *component.Identifier, count uint32) error {
|
|
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
|
|
return err
|
|
}else{
|
|
l.lsaAdjacencys[index].InterestTimedOutNo=count
|
|
return nil
|
|
}
|
|
}
|
|
|
|
//
|
|
// GetTimedOutInterestCount
|
|
// @Description: 获取指定MIR标识符对应的邻接信息中的失败的hello兴趣包次数
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @return uint32
|
|
// @return error
|
|
//
|
|
func (l *AdjLsaAdjacenctList) GetTimedOutInterestCount(routerIdentifier *component.Identifier) (uint32,error) {
|
|
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
|
|
return 0,err
|
|
}else{
|
|
return l.lsaAdjacencys[index].InterestTimedOutNo,nil
|
|
}
|
|
}
|
|
|
|
//
|
|
// GetStatusOfNeighbor
|
|
// @Description: 获取指定MIR标识符对应MIR的状态
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @return int
|
|
// @return error
|
|
//
|
|
func (l *AdjLsaAdjacenctList) GetStatusOfNeighbor(routerIdentifier *component.Identifier) (int,error) {
|
|
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
|
|
return -2,err
|
|
}else{
|
|
return l.lsaAdjacencys[index].Status,nil
|
|
}
|
|
}
|
|
|
|
//
|
|
// SetStatusOfNeighbor
|
|
// @Description: 设置指定MIR标识符对应MIR的状态
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @param status
|
|
// @return error
|
|
//
|
|
func (l *AdjLsaAdjacenctList) SetStatusOfNeighbor(routerIdentifier *component.Identifier,status int) error {
|
|
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
|
|
return err
|
|
}else{
|
|
l.lsaAdjacencys[index].Status=status
|
|
return nil
|
|
}
|
|
}
|
|
|
|
//
|
|
// isAdjLsaBuildable
|
|
// @Description: 当所有邻居的状态可知时,此表可用于构建邻接LSA。
|
|
// 当邻居状态是ACTIVE或INACTIVE时,就表示此邻居状态可知;
|
|
// 当一个邻居还在被联络,即在发送hello兴趣包给它时,则此邻居状态为UNKNOWN。
|
|
// 在Nlsr中,本函数的代码实现与代码注释不一致,后面可以进一步研究其原因,甚至可以提交issue给nlsr项目组。
|
|
// @receiver l
|
|
// @param interestRetryNo 联系邻居的hello兴趣包的最大重传次数
|
|
// @return bool 此邻接表的实体对象是否能够用于构建一个邻接LSA
|
|
//
|
|
func (l *AdjLsaAdjacenctList) IsAdjLsaBuildable(interestRetryNo uint32) bool {
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
switch l.lsaAdjacencys[i].Status{
|
|
case STATUS_UNKNOWN:
|
|
if l.lsaAdjacencys[i].InterestTimedOutNo<interestRetryNo{
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
//
|
|
// GetAdjacency
|
|
// @Description: 根据路由器标识符返回其对应的那条邻接信息
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @return *AdjLsaAdjacencyInfo
|
|
// @return error
|
|
//
|
|
func (l *AdjLsaAdjacenctList) GetAdjacency(routerIdentifier *component.Identifier) (*AdjLsaAdjacencyInfo,error){
|
|
if index,err:=l.findIndexByRouterIdentifier(routerIdentifier);err!=nil{
|
|
return nil,err
|
|
}else{
|
|
return l.lsaAdjacencys[index],nil
|
|
}
|
|
}
|
|
|
|
//
|
|
// GetNumOfActiveNeighbor
|
|
// @Description: 获取状态为ACTIVE的邻居MIR的数目
|
|
// @receiver l
|
|
// @return int
|
|
//
|
|
func (l *AdjLsaAdjacenctList) GetNumOfActiveNeighbor() int {
|
|
numbers:=0
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
switch l.lsaAdjacencys[i].Status{
|
|
case STATUS_ACTIVE:
|
|
numbers++
|
|
}
|
|
}
|
|
return numbers
|
|
}
|
|
|
|
//
|
|
// GetNumOfINActiveNeighbor
|
|
// @Description: 获取状态为INACTIVE的邻居MIR的数目
|
|
// @receiver l
|
|
// @return int
|
|
//
|
|
func (l *AdjLsaAdjacenctList) GetNumOfINActiveNeighbor() int {
|
|
numbers:=0
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
switch l.lsaAdjacencys[i].Status{
|
|
case STATUS_INACTIVE:
|
|
numbers++
|
|
}
|
|
}
|
|
return numbers
|
|
}
|
|
|
|
//
|
|
// GetNumOfUnknownNeighbor
|
|
// @Description: 获取状态为Unknown的邻居MIR的数目
|
|
// @receiver l
|
|
// @return int
|
|
//
|
|
func (l *AdjLsaAdjacenctList) GetNumOfUnknownNeighbor() int {
|
|
numbers:=0
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
switch l.lsaAdjacencys[i].Status{
|
|
case STATUS_UNKNOWN:
|
|
numbers++
|
|
}
|
|
}
|
|
return numbers
|
|
}
|
|
|
|
//
|
|
// Size
|
|
// @Description: 邻接信息的条数
|
|
// @receiver l
|
|
// @return int
|
|
//
|
|
func (l *AdjLsaAdjacenctList) Size() int {
|
|
return len(l.lsaAdjacencys)
|
|
}
|
|
|
|
//
|
|
// Reset
|
|
// @Description: 清空所有邻接信息
|
|
// @receiver l
|
|
//
|
|
func (l *AdjLsaAdjacenctList) Reset() {
|
|
l.lsaAdjacencys=[]*AdjLsaAdjacencyInfo{}
|
|
}
|
|
|
|
//
|
|
// FindAdjacencyByRouterIdentifier
|
|
// @Description: 获取指定路由器标识的所有邻接信息条目。
|
|
// 这就表明一个邻接表中可能有包含同一个MIR标识的多个邻接信息条目,
|
|
// 前面的一些设置或获取操作均只对按序查到的第一个邻接信息条目进行操作。
|
|
// todo:后面应根据需求,增加实现对所有符合条件的邻接信息进行操作的函数。
|
|
// @receiver l
|
|
// @param routerIdentifier
|
|
// @return []*AdjLsaAdjacencyInfo
|
|
//
|
|
func (l *AdjLsaAdjacenctList) FindAdjacencyByRouterIdentifier(routerIdentifier *component.Identifier) []*AdjLsaAdjacencyInfo {
|
|
var infos []*AdjLsaAdjacencyInfo
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
if l.lsaAdjacencys[i].lsaNeighborRouterIdentifier.ToUri()==routerIdentifier.ToUri(){
|
|
infos=append(infos,l.lsaAdjacencys[i])
|
|
}
|
|
}
|
|
return infos
|
|
}
|
|
|
|
//
|
|
// FindAdjacencyByLogicFaceId
|
|
// @Description: 获取指定LogicFaceId的所有邻接信息条目
|
|
// @receiver l
|
|
// @param id
|
|
// @return []*AdjLsaAdjacencyInfo
|
|
//
|
|
func (l *AdjLsaAdjacenctList) FindAdjacencyByLogicFaceId(id *AdjLsaLogicFaceId) []*AdjLsaAdjacencyInfo {
|
|
var infos []*AdjLsaAdjacencyInfo
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
if l.lsaAdjacencys[i].LogicFaceId()==id.Value(){
|
|
infos=append(infos,l.lsaAdjacencys[i])
|
|
}
|
|
}
|
|
return infos
|
|
}
|
|
|
|
//
|
|
// FindAdjacencyByLogicFaceUri
|
|
// @Description: 获取指定logicfaceuri的所有邻接信息条目。
|
|
// @receiver l
|
|
// @param uri
|
|
// @return []*AdjLsaAdjacencyInfo
|
|
//
|
|
func (l *AdjLsaAdjacenctList) FindAdjacencyByLogicFaceUri(uri *AdjLsaLogicFaceUri) []*AdjLsaAdjacencyInfo {
|
|
var infos []*AdjLsaAdjacencyInfo
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
if l.lsaAdjacencys[i].AdjLsaLogicFaceUri.Value()==uri.Value(){
|
|
infos=append(infos,l.lsaAdjacencys[i])
|
|
}
|
|
}
|
|
return infos
|
|
}
|
|
|
|
func (l *AdjLsaAdjacenctList) getLogicFaceId(uri *AdjLsaLogicFaceUri) (uint64,error) {
|
|
for i:=0; i<len(l.lsaAdjacencys); i++ {
|
|
if l.lsaAdjacencys[i].AdjLsaLogicFaceUri.Value()==uri.Value(){
|
|
return l.lsaAdjacencys[i].AdjLsaLogicFaceId.Value(),nil
|
|
}
|
|
}
|
|
return 0,errors.New("not found the AdjLsaLogicFaceUri in the list")
|
|
}
|
|
|
|
//
|
|
// WireEncode
|
|
// @Description: 线速编码
|
|
// @receiver l
|
|
// @param encoder
|
|
// @return int
|
|
// @return error
|
|
//
|
|
func (l *AdjLsaAdjacenctList) WireEncode(encoder *encoding.Encoder) (int,error) {
|
|
// 判断列表长度,为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 {
|
|
// 检查 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
|
|
} |