OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mpls_index.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include "cmn/agent.h"
6 #include "cmn/agent_cmn.h"
11 #include "resource_manager/resource_manager_types.h"
13 #include "base/time_util.h"
14 #include <oper/nexthop.h>
15 
17  ("InterfaceMplsData", 5000));
19  ("VrfMplsData", 5000));
21  ("VlanMplsData", 5000));
23  ("RouteMplsData", 5000));
24 
26  Type type) :
27  IndexResourceKey(rm, Resource::MPLS_INDEX), type_(type) {
28 }
29 
31 }
32 
33 // NextHop resource key
35  NextHopKey *nh_key) :
37  nh_key_.reset(nh_key);
38 }
39 
41 }
42 
44  const MplsIndexResourceKey *mpls_key = static_cast<const
45  MplsIndexResourceKey *>(&rhs);
46  // Return if index resource key types are different
47  if (mpls_key->type() != type())
48  return mpls_key->type() < type();
49 
50  const NexthopIndexResourceKey *nh_rkey = static_cast<const
51  NexthopIndexResourceKey *>(&rhs);
52  const NextHopKey *nh_key1 = GetNhKey();
53  const NextHopKey *nh_key2 = nh_rkey->GetNhKey();
54  return nh_key1->IsLess(*nh_key2);
55 }
56 
57 //Backup the Nexthop Resource Data
59  const NextHopKey *nh_key = GetNhKey();
60  switch(nh_key->GetType()) {
61  case NextHop::INTERFACE:
62  BackupInterfaceResource(data, op);
63  break;
64  case NextHop::VLAN:
65  BackupVlanResource(data, op);
66  break;
67  case NextHop::VRF:
68  BackupVrfResource(data, op);
69  break;
70  default:
71  assert(0);
72  }
73 }
74 
75 //Backup Interface resource data as sandesh encoded format
77  uint16_t op) {
78  IndexResourceData *index_data = static_cast<IndexResourceData *>(data);
79  const InterfaceNHKey *itfnh_key = static_cast<const InterfaceNHKey *>(
80  GetNhKey());
81  string operation;
82  if (op == ResourceBackupReq::DEL) {
83  rm()->backup_mgr()->
84  sandesh_maps().DeleteInterfaceMplsResourceEntry(index_data->index());
85  operation = "DELETE";
86  } else {
87  InterfaceIndexResource backup_data;
88  backup_data.set_type(
89  (itfnh_key->intf_type() == Interface::VM_INTERFACE)?"vmi":"inet");
90  backup_data.set_uuid(UuidToString(itfnh_key->GetUuid()));
91  backup_data.set_name(itfnh_key->name());
92  backup_data.set_policy(itfnh_key->GetPolicy());
93  backup_data.set_flags(itfnh_key->flags());
94  backup_data.set_mac(itfnh_key->dmac().ToString());
95  backup_data.set_time_stamp(UTCTimestampUsec());
96  rm()->backup_mgr()->
97  sandesh_maps().AddInterfaceMplsResourceEntry(index_data->index(),
98  backup_data);
99  operation = "ADD";
100  }
101 
102  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.
111  TriggerBackup();
112 }
113 
114 //Backup Vrf resource data as sandesh encoded format
116  uint16_t op) {
117  IndexResourceData *index_data = static_cast<IndexResourceData *>(data);
118  const VrfNHKey *vrfnh_key = static_cast<const VrfNHKey *>(GetNhKey());
119  string operation;
120  if (op == ResourceBackupReq::DEL) {
122  index_data->index());
123  operation = "DELETE";
124  } else {
125  VrfMplsResource backup_data;
126  backup_data.set_name(vrfnh_key->GetVrfName());
127  backup_data.set_bridge_nh(vrfnh_key->GetBridgeNh());
128  backup_data.set_time_stamp(UTCTimestampUsec());
130  index_data->index(), backup_data);
131  operation = "ADD";
132  }
133  //TODO may be API to insert in map can be added and that will incr sequence
134  //number internally.
135  VRF_MPLS_DATA_TRACE(VrfMplsDataTraceBuf, vrfnh_key->GetVrfName(),
136  vrfnh_key->GetBridgeNh(), index_data->index(), operation);
138  TriggerBackup();
139 }
140 
141 //Backup Vlan resource data as sandesh encoded format
143  uint16_t op) {
144  IndexResourceData *index_data = static_cast<IndexResourceData *>(data);
145  const VlanNHKey *vlan_nh_key = static_cast<const VlanNHKey *>(GetNhKey());
146  string operation;
147  if (op == ResourceBackupReq::DEL) {
149  index_data->index());
150  operation = "DELETE";
151  } else {
152  operation = "ADD";
153  VlanMplsResource backup_data;
154  backup_data.set_uuid(UuidToString(vlan_nh_key->GetUuid()));
155  backup_data.set_tag(vlan_nh_key->vlan_tag());
156  backup_data.set_time_stamp(UTCTimestampUsec());
158  index_data->index(), backup_data);
159  }
160  //TODO may be API to insert in map can be added and that will incr sequence
161  //number internally.
162  VLAN_MPLS_DATA_TRACE(VlanMplsDataTraceBuf,
163  UuidToString(vlan_nh_key->GetUuid()),
164  vlan_nh_key->vlan_tag(), index_data->index(), operation);
166  TriggerBackup();
167 }
168 
170  const std::string &vrf_name,
171  const std::string route_key) :
173  vrf_name_(vrf_name), route_key_(route_key) {
174 }
175 
177 }
178 
180  const MplsIndexResourceKey *mpls_key = static_cast<const
181  MplsIndexResourceKey *>(&rhs);
182  if (mpls_key->type() != type())
183  return mpls_key->type() < type();
184 
185  const RouteMplsResourceKey *route_key = static_cast<const
186  RouteMplsResourceKey*>(&rhs);
187  if (route_key->vrf_name_ != vrf_name_)
188  return (route_key->vrf_name_ < vrf_name_);
189  return (route_key->route_key_ < route_key_);
190 }
191 
192 //Backup the Route Mpls Resource Data as Sandesh encoded format.
193 void RouteMplsResourceKey::Backup(ResourceData *data, uint16_t op) {
194  IndexResourceData *index_data = static_cast<IndexResourceData *>(data);
195  string operation;
196  if (op == ResourceBackupReq::DEL) {
198  index_data->index());
199  operation = "DELETE";
200  } else {
201  RouteMplsResource backup_data;
202  backup_data.set_vrf_name(vrf_name_);
203  backup_data.set_route_prefix(route_key_);
204  backup_data.set_time_stamp(UTCTimestampUsec());
206  index_data->index(), backup_data);
207  operation = "ADD";
208  }
209  //TODO may be API to insert in map can be added and that will incr sequence
210  //number internally.
211  ROUTE_MPLS_DATA_TRACE(RouteMplsDataTraceBuf, vrf_name_, route_key_,
212  index_data->index(), operation);
214  TriggerBackup();
215 }
216 
218  const std::string &name) :
219  MplsIndexResourceKey(rm, MplsIndexResourceKey::TEST), name_(name) {
220 }
221 
223 }
224 
225 bool TestMplsResourceKey::IsLess(const ResourceKey &rhs) const {
226  const MplsIndexResourceKey *mpls_key = static_cast<const
227  MplsIndexResourceKey *>(&rhs);
228  if (mpls_key->type() != type())
229  return mpls_key->type() < type();
230 
231  const TestMplsResourceKey *test_key = static_cast<const
232  TestMplsResourceKey*>(&rhs);
233  return (test_key->name_ < name_);
234 }
VlanMplsBackUpResourceTable & vlan_mpls_index_table()
Definition: sandesh_map.h:273
const bool & GetBridgeNh() const
Definition: nexthop.h:1401
virtual ~NexthopIndexResourceKey()
Definition: mpls_index.cc:40
virtual bool IsLess(const ResourceKey &rhs) const
Definition: mpls_index.cc:179
RouteMplsBackUpResourceTable & route_mpls_index_table()
Definition: sandesh_map.h:277
void AddRouteMplsResourceEntry(uint32_t index, RouteMplsResource data)
Definition: sandesh_map.cc:738
void BackupVrfResource(ResourceData *data, uint16_t op)
Definition: mpls_index.cc:115
virtual void Backup(ResourceData *data, uint16_t op)
Definition: mpls_index.cc:58
MplsIndexResourceKey(ResourceManager *rm, Type type)
Definition: mpls_index.cc:25
void AddVlanMplsResourceEntry(uint32_t index, VlanMplsResource data)
Definition: sandesh_map.cc:729
void AddVrfMplsResourceEntry(uint32_t index, VrfMplsResource data)
Definition: sandesh_map.cc:720
Type type() const
Definition: mpls_index.h:36
const boost::uuids::uuid & GetUuid() const
Definition: nexthop.h:1211
std::unique_ptr< NextHopKey > nh_key_
Definition: mpls_index.h:55
static std::string UuidToString(const boost::uuids::uuid &id)
Definition: string_util.h:138
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:18
bool GetPolicy() const
Definition: nexthop.h:500
virtual ~MplsIndexResourceKey()
Definition: mpls_index.cc:30
const std::string name_
Definition: mpls_index.h:86
std::string ToString() const
Definition: mac_address.cc:53
RouteMplsResourceKey(ResourceManager *rm, const std::string &vrf_name, const std::string route_str)
Definition: mpls_index.cc:169
NextHop::Type GetType() const
Definition: nexthop.h:499
NexthopIndexResourceKey(ResourceManager *rm, NextHopKey *nh_key)
Definition: mpls_index.cc:34
const std::string & name() const
Definition: nexthop.h:1212
const boost::uuids::uuid & GetUuid() const
Definition: nexthop.h:1503
ResourceManager * rm()
void BackupVlanResource(ResourceData *data, uint16_t op)
Definition: mpls_index.cc:142
uint8_t type
Definition: load_balance.h:109
void DeleteVlanMplsResourceEntry(uint32_t index)
Definition: sandesh_map.cc:734
const std::string vrf_name_
Definition: mpls_index.h:71
bool IsLess(const NextHopKey &rhs) const
Definition: nexthop.h:501
void DeleteRouteMplsResourceEntry(uint32_t index)
Definition: sandesh_map.cc:743
virtual bool IsLess(const ResourceKey &rhs) const
Definition: mpls_index.cc:43
const std::string & GetVrfName() const
Definition: nexthop.h:1400
ResourceBackupManager * backup_mgr()
InterfaceMplsBackUpResourceTable & interface_mpls_index_table()
Definition: sandesh_map.h:265
virtual bool IsLess(const ResourceKey &rhs) const
Definition: mpls_index.cc:225
SandeshTraceBufferPtr InterfaceMplsDataTraceBuf
virtual ~RouteMplsResourceKey()
Definition: mpls_index.cc:176
virtual ~TestMplsResourceKey()
Definition: mpls_index.cc:222
virtual void Backup(ResourceData *data, uint16_t op)
Definition: mpls_index.cc:193
static uint64_t UTCTimestampUsec()
Definition: time_util.h:13
const NextHopKey * GetNhKey() const
Definition: mpls_index.h:53
const std::string route_key_
Definition: mpls_index.h:72
ResourceSandeshMaps & sandesh_maps()
uint32_t index() const
void BackupInterfaceResource(ResourceData *data, uint16_t op)
Definition: mpls_index.cc:76
const uint8_t & flags() const
Definition: nexthop.h:1216
const Interface::Type & intf_type() const
Definition: nexthop.h:1213
void DeleteVrfMplsResourceEntry(uint32_t index)
Definition: sandesh_map.cc:725
SandeshTraceBufferPtr VlanMplsDataTraceBuf
SandeshTraceBufferPtr VrfMplsDataTraceBuf
TestMplsResourceKey(ResourceManager *rm, const std::string &name)
Definition: mpls_index.cc:217
const uint16_t vlan_tag() const
Definition: nexthop.h:1505
SandeshTraceBufferPtr RouteMplsDataTraceBuf
VrfMplsBackUpResourceTable & vrf_mpls_index_table()
Definition: sandesh_map.h:269
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
Definition: sandesh_trace.h:46
const MacAddress & dmac() const
Definition: nexthop.h:1217