github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/optiontemplatemap/option_template_viptela_tloc.go (about)

     1  package optiontemplatemap
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net"
     7  	"sync"
     8  )
     9  
    10  /*
    11    _____________________________________________________________________________
    12    |                 Field                   |    ID | Ent.ID | Offset |  Size |
    13    -----------------------------------------------------------------------------
    14    | TLOC TABLE OVERLAY SESSION ID           | 12435 |      9 |      0 |     4 |
    15    | tloc local color                        | 12437 |      9 |      4 |    16 |
    16    | tloc remote color                       | 12439 |      9 |     20 |    16 |
    17    | tloc tunnel protocol                    | 12440 |      9 |     36 |     8 |
    18    | tloc local system ip address            | 12436 |      9 |     44 |     4 |
    19    | tloc remote system ip address           | 12438 |      9 |     48 |     4 |
    20    -----------------------------------------------------------------------------
    21  
    22  
    23  12432,9,overlay_session_id_input,unsigned32
    24  12433,9,overlay_session_id_output,unsigned32
    25  12434,9,routing_vrf_service,unsigned32
    26  12435,9,tloc_table_overlay_session_id,unsigned32
    27  12436,9,tloc_local_system_ip_address,ipv4Address
    28  12437,9,tloc_local_color,string
    29  12438,9,tloc_remote_system_ip_address,ipv4Address
    30  12439,9,tloc_remote_color,string
    31  12440,9,tloc_tunnel_protocol,string
    32  
    33    Client: Option options bfd-metrics-table
    34    Exporter Format: IPFIX (Version 10)
    35    Template ID    : 260
    36    Source ID      : 6
    37    Record Size    : 49
    38    Template layout
    39    _____________________________________________________________________________
    40    |                 Field                   |    ID | Ent.ID | Offset |  Size |
    41    -----------------------------------------------------------------------------
    42    | TLOC TABLE OVERLAY SESSION ID           | 12435 |      9 |      0 |     4 |
    43    | IP DSCP                                 |   195 |        |      4 |     1 |
    44    | bfd loss                                | 12527 |      9 |      5 |     4 |
    45    | bfd pfr update ts                       | 12530 |      9 |      9 |     8 |
    46    | bfd avg latency                         | 12528 |      9 |     17 |     8 |
    47    | bfd avg jitter                          | 12529 |      9 |     25 |     8 |
    48    | bfd rx cnt                              | 12531 |      9 |     33 |     8 |
    49    | bfd tx cnt                              | 12532 |      9 |     41 |     8 |
    50    -----------------------------------------------------------------------------
    51  
    52  
    53  */
    54  
    55  type TLOCData struct {
    56  	Protocol    uint64
    57  	LocalIP     net.IP
    58  	LocalColor  string
    59  	RemoteIP    net.IP
    60  	RemoteColor string
    61  	Loss        uint32
    62  	Jitter      uint32
    63  	Latency     uint32
    64  }
    65  
    66  func (t *TLOCData) String() string {
    67  	return fmt.Sprintf("[%s]-Local:%s:%s--Remote:%s:%s", t.Protocol, t.LocalIP, t.LocalColor, t.RemoteIP, t.RemoteColor)
    68  }
    69  
    70  type TLOCMapKey struct {
    71  	AgentID   string
    72  	SessionID uint32
    73  }
    74  
    75  var ViptelaTLocTable sync.Map
    76  
    77  func UpdateTLOCDatabase(AgentID string, sessionID uint32, tloc *TLOCData) error {
    78  	key := TLOCMapKey{
    79  		AgentID:   AgentID,
    80  		SessionID: sessionID,
    81  	}
    82  	data := TLOCData{
    83  		Protocol:    tloc.Protocol,
    84  		LocalIP:     tloc.LocalIP,
    85  		LocalColor:  tloc.LocalColor,
    86  		RemoteIP:    tloc.RemoteIP,
    87  		RemoteColor: tloc.RemoteColor,
    88  		Loss:        tloc.Loss,
    89  		Jitter:      tloc.Jitter,
    90  		Latency:     tloc.Latency,
    91  	}
    92  	ViptelaTLocTable.Store(key, data)
    93  	return nil
    94  }
    95  
    96  func FetchTLOCDatabaseByIndex(AgentID string, sessionID uint32) (TLOCData, error) {
    97  	var err error
    98  	var i TLOCData
    99  	key := TLOCMapKey{
   100  		AgentID:   AgentID,
   101  		SessionID: sessionID,
   102  	}
   103  
   104  	value, ok := ViptelaTLocTable.Load(key)
   105  	if ok {
   106  		i, valid := value.(TLOCData)
   107  		if !valid {
   108  			err = errors.New("invalid type assertion")
   109  			return i, err
   110  		} else {
   111  			return i, nil
   112  		}
   113  	} else {
   114  		err = errors.New("TLOC-Session-ID does not found in database")
   115  		return i, err
   116  	}
   117  }
   118  
   119  func ShowTLOCDatabase() {
   120  	ViptelaTLocTable.Range(func(k, v interface{}) bool {
   121  		value := v.(TLOCData)
   122  		key := k.(TLOCMapKey)
   123  		fmt.Printf("TLOC Session Map [%10s] | %d  %s \n", key.AgentID, key.SessionID, value.String())
   124  		return true
   125  	})
   126  }
   127  
   128  func UpdateViptelaTLOCMap(d map[string]interface{}, AgentID string) error {
   129  	var err error
   130  	sessionid, has_sessionid := d["tloc_table_overlay_session_id"]
   131  	local_ip, has_local_ip := d["tloc_local_system_ip_address"]
   132  	local_color, has_local_color := d["tloc_local_color"]
   133  	_, has_bfd := d["cisco_sdwan_bfd_update_ts"]
   134  
   135  	if has_sessionid && has_local_ip && has_local_color {
   136  		//is Option Template, update db
   137  		remote_ip, _ := d["tloc_remote_system_ip_address"]
   138  		remote_color, _ := d["tloc_remote_color"]
   139  		protocol_id, _ := d["tloc_tunnel_protocol"]
   140  
   141  
   142  		key := TLOCMapKey {
   143  			AgentID: AgentID,
   144  			SessionID: sessionid.(uint32),
   145  		}
   146  		tlocdata := &TLOCData{
   147  			RemoteIP:    remote_ip.(net.IP),
   148  			RemoteColor: remote_color.(string),
   149  			LocalIP:     local_ip.(net.IP),
   150  			LocalColor:  local_color.(string),
   151  			Protocol:    protocol_id.(uint64),
   152  		}
   153  		oldT , exist := 	ViptelaTLocTable.Load(key)
   154  		if exist {
   155  			old := oldT.(TLOCData)
   156  			tlocdata.Latency = old.Latency
   157  			tlocdata.Jitter = old.Jitter
   158  			tlocdata.Loss = old.Loss
   159  		}
   160  		err = UpdateTLOCDatabase(AgentID, sessionid.(uint32), tlocdata)
   161  	}
   162  
   163  	if has_sessionid && has_bfd {
   164  
   165  		key := TLOCMapKey {
   166  			AgentID: AgentID,
   167  			SessionID: sessionid.(uint32),
   168  		}
   169  
   170  		dataT , exist := ViptelaTLocTable.Load(key)
   171  		if exist {
   172  			data := dataT.(TLOCData)
   173  
   174  			r, err := FetchTLOCDatabaseByIndex(AgentID,sessionid.(uint32) )
   175  			if err == nil {
   176  				d["TLOCInfo"] = r
   177  			}
   178  			//update template measurement to data record
   179  			latencyT, ok := d["cisco_sdwan_bfd_latency"]
   180  			if ok {
   181  				data.Latency = uint32(latencyT.(uint64))
   182  			}
   183  			jitterT, ok := d["cisco_sdwan_bfd_jitter"]
   184  			if ok {
   185  				data.Jitter = uint32(jitterT.(uint64))
   186  			}
   187  			lossT, ok := d["cisco_sdwan_bfd_loss"]
   188  			if ok {
   189  				data.Loss = lossT.(uint32)
   190  			}
   191  			err = UpdateTLOCDatabase(AgentID, sessionid.(uint32), &data)
   192  		}
   193  	}
   194  
   195  	inputID, hasInputID := d["overlay_session_id_input"]
   196  	if hasInputID {
   197  		r, err := FetchTLOCDatabaseByIndex(AgentID, inputID.(uint32))
   198  		if err == nil {
   199  			d["input_TLOC_Session"] = r
   200  		}
   201  	}
   202  
   203  	outputID, hasOutputID := d["overlay_session_id_output"]
   204  	if hasOutputID {
   205  		r, err := FetchTLOCDatabaseByIndex(AgentID, outputID.(uint32))
   206  		if err == nil {
   207  			d["output_TLOC_Session"] = r
   208  		}
   209  	}
   210  	return err
   211  }