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