LCOV - code coverage report
Current view: top level - vnsw/agent/resource_manager - mpls_index.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 34 138 24.6 %
Date: 2026-08-03 02:19:58 Functions: 10 20 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
       3             :  */
       4             : 
       5             : #include "cmn/agent.h"
       6             : #include "cmn/agent_cmn.h"
       7             : #include "resource_manager/resource_manager.h"
       8             : #include "resource_manager/resource_table.h"
       9             : #include "resource_manager/resource_cmn.h"
      10             : #include "resource_manager/mpls_index.h"
      11             : #include "resource_manager/resource_manager_types.h"
      12             : #include "resource_manager/resource_backup.h"
      13             : #include "base/time_util.h"
      14             : #include <oper/nexthop.h>
      15             : 
      16             : SandeshTraceBufferPtr InterfaceMplsDataTraceBuf(SandeshTraceBufferCreate
      17             :                                                ("InterfaceMplsData", 5000));
      18             : SandeshTraceBufferPtr VrfMplsDataTraceBuf(SandeshTraceBufferCreate
      19             :                                           ("VrfMplsData", 5000));
      20             : SandeshTraceBufferPtr VlanMplsDataTraceBuf(SandeshTraceBufferCreate
      21             :                                            ("VlanMplsData", 5000));
      22             : SandeshTraceBufferPtr RouteMplsDataTraceBuf(SandeshTraceBufferCreate
      23             :                                             ("RouteMplsData", 5000));
      24             : 
      25          85 : MplsIndexResourceKey::MplsIndexResourceKey(ResourceManager *rm,
      26          85 :                                            Type type) :
      27          85 :     IndexResourceKey(rm, Resource::MPLS_INDEX), type_(type) {
      28          85 : }
      29             : 
      30          85 : MplsIndexResourceKey::~MplsIndexResourceKey() {
      31          85 : }
      32             : 
      33             : // NextHop resource key
      34          78 : NexthopIndexResourceKey::NexthopIndexResourceKey(ResourceManager *rm,
      35          78 :                                                  NextHopKey *nh_key) :
      36          78 :     MplsIndexResourceKey(rm, MplsIndexResourceKey::NEXTHOP) {
      37          78 :     nh_key_.reset(nh_key);
      38          78 : }
      39             : 
      40         156 : NexthopIndexResourceKey::~NexthopIndexResourceKey() {
      41         156 : }
      42             : 
      43        1597 : bool NexthopIndexResourceKey::IsLess(const ResourceKey &rhs) const {
      44        1597 :     const MplsIndexResourceKey *mpls_key = static_cast<const
      45             :         MplsIndexResourceKey *>(&rhs);
      46             :     // Return if index resource key types are different
      47        1597 :     if (mpls_key->type() != type())
      48          55 :         return mpls_key->type() < type();
      49             : 
      50        1542 :     const NexthopIndexResourceKey *nh_rkey = static_cast<const
      51             :         NexthopIndexResourceKey *>(&rhs);
      52        1542 :     const NextHopKey *nh_key1 = GetNhKey();
      53        1542 :     const NextHopKey *nh_key2 = nh_rkey->GetNhKey();
      54        1542 :     return nh_key1->IsLess(*nh_key2);
      55             : }
      56             : 
      57             : //Backup the Nexthop Resource Data
      58           0 : void NexthopIndexResourceKey::Backup(ResourceData *data, uint16_t op) {
      59           0 :     const NextHopKey *nh_key = GetNhKey();
      60           0 :     switch(nh_key->GetType()) {
      61           0 :     case NextHop::INTERFACE:
      62           0 :         BackupInterfaceResource(data, op);
      63           0 :         break;
      64           0 :     case NextHop::VLAN:
      65           0 :         BackupVlanResource(data, op);
      66           0 :         break;
      67           0 :     case NextHop::VRF:
      68           0 :         BackupVrfResource(data, op);
      69           0 :         break;
      70           0 :     default:
      71           0 :         assert(0);
      72             :     }
      73           0 : }
      74             : 
      75             : //Backup Interface resource data as sandesh encoded format
      76           0 : void NexthopIndexResourceKey::BackupInterfaceResource(ResourceData *data,
      77             :                                                       uint16_t op) {
      78           0 :     IndexResourceData *index_data = static_cast<IndexResourceData *>(data);
      79             :     const InterfaceNHKey *itfnh_key = static_cast<const InterfaceNHKey *>(
      80           0 :                                       GetNhKey());
      81           0 :     string operation;
      82           0 :     if (op == ResourceBackupReq::DEL) {
      83             :         rm()->backup_mgr()->
      84           0 :            sandesh_maps().DeleteInterfaceMplsResourceEntry(index_data->index());
      85           0 :         operation = "DELETE";
      86             :     } else {
      87           0 :         InterfaceIndexResource backup_data;
      88           0 :         backup_data.set_type(
      89           0 :             (itfnh_key->intf_type() == Interface::VM_INTERFACE)?"vmi":"inet");
      90           0 :         backup_data.set_uuid(UuidToString(itfnh_key->GetUuid()));
      91           0 :         backup_data.set_name(itfnh_key->name());
      92           0 :         backup_data.set_policy(itfnh_key->GetPolicy());
      93           0 :         backup_data.set_flags(itfnh_key->flags());
      94           0 :         backup_data.set_mac(itfnh_key->dmac().ToString());
      95           0 :         backup_data.set_time_stamp(UTCTimestampUsec());
      96             :         rm()->backup_mgr()->
      97           0 :             sandesh_maps().AddInterfaceMplsResourceEntry(index_data->index(),
      98             :                                                          backup_data);
      99           0 :         operation = "ADD";
     100           0 :     }
     101             : 
     102           0 :     INTERFACE_MPLS_DATA_TRACE(InterfaceMplsDataTraceBuf,
     103             :                               (itfnh_key->intf_type() == Interface::VM_INTERFACE)?"vmi":"inet",
     104             :                               UuidToString(itfnh_key->GetUuid()),
     105             :                               itfnh_key->name(), itfnh_key->GetPolicy(),
     106             :                               itfnh_key->dmac().ToString(), index_data->index(),
     107             :                               operation);
     108             :     //TODO may be API to insert in map can be added and that will incr sequence
     109             :     //number internally.
     110           0 :     rm()->backup_mgr()->sandesh_maps().interface_mpls_index_table().
     111           0 :         TriggerBackup();
     112           0 : }
     113             : 
     114             : //Backup Vrf resource data as sandesh encoded format
     115           0 : void NexthopIndexResourceKey::BackupVrfResource(ResourceData *data,
     116             :                                                 uint16_t op) {
     117           0 :     IndexResourceData *index_data = static_cast<IndexResourceData *>(data);
     118           0 :     const VrfNHKey *vrfnh_key = static_cast<const VrfNHKey *>(GetNhKey());
     119           0 :     string operation;
     120           0 :     if (op == ResourceBackupReq::DEL) {
     121           0 :         rm()->backup_mgr()->sandesh_maps().DeleteVrfMplsResourceEntry(
     122             :                 index_data->index());
     123           0 :         operation = "DELETE";
     124             :     } else {
     125           0 :         VrfMplsResource backup_data;
     126           0 :         backup_data.set_name(vrfnh_key->GetVrfName());
     127           0 :         backup_data.set_bridge_nh(vrfnh_key->GetBridgeNh());
     128           0 :         backup_data.set_time_stamp(UTCTimestampUsec());
     129           0 :         rm()->backup_mgr()->sandesh_maps().AddVrfMplsResourceEntry(
     130             :                 index_data->index(), backup_data);
     131           0 :         operation = "ADD";
     132           0 :     }
     133             :     //TODO may be API to insert in map can be added and that will incr sequence
     134             :     //number internally.
     135           0 :     VRF_MPLS_DATA_TRACE(VrfMplsDataTraceBuf, vrfnh_key->GetVrfName(),
     136             :                         vrfnh_key->GetBridgeNh(), index_data->index(), operation);
     137           0 :     rm()->backup_mgr()->sandesh_maps().vrf_mpls_index_table().
     138           0 :         TriggerBackup();
     139           0 : }
     140             : 
     141             : //Backup Vlan resource data as sandesh encoded format
     142           0 : void NexthopIndexResourceKey::BackupVlanResource(ResourceData *data,
     143             :                                                  uint16_t op) {
     144           0 :     IndexResourceData *index_data = static_cast<IndexResourceData *>(data);
     145           0 :     const VlanNHKey *vlan_nh_key = static_cast<const VlanNHKey *>(GetNhKey());
     146           0 :     string operation;
     147           0 :     if (op == ResourceBackupReq::DEL) {
     148           0 :         rm()->backup_mgr()->sandesh_maps().DeleteVlanMplsResourceEntry(
     149             :                 index_data->index());
     150           0 :         operation = "DELETE";
     151             :     } else {
     152           0 :         operation = "ADD";
     153           0 :         VlanMplsResource backup_data;
     154           0 :         backup_data.set_uuid(UuidToString(vlan_nh_key->GetUuid()));
     155           0 :         backup_data.set_tag(vlan_nh_key->vlan_tag());
     156           0 :         backup_data.set_time_stamp(UTCTimestampUsec());
     157           0 :         rm()->backup_mgr()->sandesh_maps().AddVlanMplsResourceEntry(
     158             :                 index_data->index(), backup_data);
     159           0 :     }
     160             :     //TODO may be API to insert in map can be added and that will incr sequence
     161             :     //number internally.
     162           0 :     VLAN_MPLS_DATA_TRACE(VlanMplsDataTraceBuf,
     163             :                         UuidToString(vlan_nh_key->GetUuid()),
     164             :                         vlan_nh_key->vlan_tag(), index_data->index(), operation);
     165           0 :     rm()->backup_mgr()->sandesh_maps().vlan_mpls_index_table().
     166           0 :         TriggerBackup();
     167           0 : }
     168             : 
     169           7 : RouteMplsResourceKey::RouteMplsResourceKey(ResourceManager *rm,
     170             :                                              const std::string &vrf_name,
     171           7 :                                              const std::string route_key) :
     172             :     MplsIndexResourceKey(rm, MplsIndexResourceKey::ROUTE),
     173           7 :     vrf_name_(vrf_name), route_key_(route_key) {
     174           7 : }
     175             : 
     176          21 : RouteMplsResourceKey::~RouteMplsResourceKey() {
     177          14 : }
     178             : 
     179          92 : bool RouteMplsResourceKey::IsLess(const ResourceKey &rhs) const {
     180          92 :     const MplsIndexResourceKey *mpls_key = static_cast<const
     181             :         MplsIndexResourceKey *>(&rhs);
     182          92 :     if (mpls_key->type() != type())
     183          58 :         return mpls_key->type() < type();
     184             : 
     185          34 :     const RouteMplsResourceKey *route_key = static_cast<const
     186             :         RouteMplsResourceKey*>(&rhs);
     187          34 :     if (route_key->vrf_name_ != vrf_name_)
     188           0 :         return (route_key->vrf_name_ < vrf_name_);
     189          34 :     return (route_key->route_key_ < route_key_);
     190             : }
     191             : 
     192             : //Backup the Route Mpls Resource Data as Sandesh encoded format.
     193           0 : void RouteMplsResourceKey::Backup(ResourceData *data, uint16_t op) {
     194           0 :     IndexResourceData *index_data = static_cast<IndexResourceData *>(data);
     195           0 :     string operation;
     196           0 :     if (op == ResourceBackupReq::DEL) {
     197           0 :         rm()->backup_mgr()->sandesh_maps().DeleteRouteMplsResourceEntry(
     198             :                 index_data->index());
     199           0 :         operation = "DELETE";
     200             :     } else {
     201           0 :         RouteMplsResource backup_data;
     202           0 :         backup_data.set_vrf_name(vrf_name_);
     203           0 :         backup_data.set_route_prefix(route_key_);
     204           0 :         backup_data.set_time_stamp(UTCTimestampUsec());
     205           0 :         rm()->backup_mgr()->sandesh_maps().AddRouteMplsResourceEntry(
     206             :                 index_data->index(), backup_data);
     207           0 :         operation = "ADD";
     208           0 :     }
     209             :     //TODO may be API to insert in map can be added and that will incr sequence
     210             :     //number internally.
     211           0 :     ROUTE_MPLS_DATA_TRACE(RouteMplsDataTraceBuf, vrf_name_, route_key_,
     212             :                           index_data->index(), operation);
     213           0 :     rm()->backup_mgr()->sandesh_maps().route_mpls_index_table().
     214           0 :         TriggerBackup();
     215           0 : }
     216             : 
     217           0 : TestMplsResourceKey::TestMplsResourceKey(ResourceManager *rm,
     218           0 :                                          const std::string &name) :
     219           0 :     MplsIndexResourceKey(rm, MplsIndexResourceKey::TEST), name_(name) {
     220           0 : }
     221             : 
     222           0 : TestMplsResourceKey::~TestMplsResourceKey() {
     223           0 : }
     224             : 
     225           0 : bool TestMplsResourceKey::IsLess(const ResourceKey &rhs) const {
     226           0 :     const MplsIndexResourceKey *mpls_key = static_cast<const
     227             :         MplsIndexResourceKey *>(&rhs);
     228           0 :     if (mpls_key->type() != type())
     229           0 :         return mpls_key->type() < type();
     230             : 
     231           0 :     const TestMplsResourceKey *test_key = static_cast<const
     232             :         TestMplsResourceKey*>(&rhs);
     233           0 :     return (test_key->name_ < name_);
     234             : }

Generated by: LCOV version 1.14