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