OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mpls_ksync.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include <base/logging.h>
6 #include <oper/nexthop.h>
7 #include <oper/mirror_table.h>
8 #include <ksync/ksync_index.h>
13 #include <ksync/ksync_sock.h>
14 
16  uint32_t index) :
17  KSyncNetlinkDBEntry(index), ksync_obj_(obj), label_(me->label_),
18  nh_(NULL) {
19 }
20 
22  KSyncNetlinkDBEntry(kInvalidIndex), ksync_obj_(obj),
23  label_(mpls->label()), nh_(NULL) {
24 }
25 
27 }
28 
30  return ksync_obj_;
31 }
32 
33 bool MplsKSyncEntry::IsLess(const KSyncEntry &rhs) const {
34  const MplsKSyncEntry &entry = static_cast<const MplsKSyncEntry &>(rhs);
35  return label_ < entry.label_;
36 }
37 
38 std::string MplsKSyncEntry::ToString() const {
39  std::stringstream s;
40  NHKSyncEntry *next_hop = nh();
41 
42  s << "Mpls : " << label_ << " Index : " << GetIndex();
43  if (next_hop) {
44  s << next_hop->ToString();
45  } else {
46  s << "NextHop : NULL";
47  }
48  return s.str();
49 }
50 
52  bool ret = false;
53  const MplsLabel *mpls = static_cast<MplsLabel *>(e);
54 
55  NHKSyncObject *nh_object = ksync_obj_->ksync()->nh_ksync_obj();
56  if (mpls->nexthop() == NULL) {
57  LOG(DEBUG, "nexthop in mpls label is null");
58  assert(0);
59  }
60  NHKSyncEntry next_hop(nh_object, mpls->nexthop());
61  NHKSyncEntry *old_nh = nh();
62 
63  nh_ = nh_object->GetReference(&next_hop);
64  if (old_nh != nh()) {
65  ret = true;
66  }
67 
68  return ret;
69 };
70 
71 int MplsKSyncEntry::Encode(sandesh_op::type op, char *buf, int buf_len) {
72  vr_mpls_req encoder;
73  int encode_len;
74  NHKSyncEntry *next_hop = nh();
75 
76  encoder.set_h_op(op);
77  encoder.set_mr_label(label_);
78  encoder.set_mr_rid(0);
79  encoder.set_mr_nhid(next_hop->nh_id());
80  int error = 0;
81  encode_len = encoder.WriteBinary((uint8_t *)buf, buf_len, &error);
82  assert(error == 0);
83  assert(encode_len <= buf_len);
84  return encode_len;
85 }
86 
88  KSyncMplsInfo &info) const {
89  info.set_label(label_);
90  info.set_nh(nh()->nh_id());
91 
92  if (op == sandesh_op::ADD) {
93  info.set_operation("ADD/CHANGE");
94  } else {
95  info.set_operation("DELETE");
96  }
97 }
98 
99 int MplsKSyncEntry::AddMsg(char *buf, int buf_len) {
100  KSyncMplsInfo info;
101  FillObjectLog(sandesh_op::ADD, info);
102  KSYNC_TRACE(Mpls, GetObject(), info);
103 
104  return Encode(sandesh_op::ADD, buf, buf_len);
105 }
106 
107 int MplsKSyncEntry::ChangeMsg(char *buf, int buf_len){
108  KSyncMplsInfo info;
109  FillObjectLog(sandesh_op::ADD, info);
110  KSYNC_TRACE(Mpls, GetObject(), info);
111 
112  return Encode(sandesh_op::ADD, buf, buf_len);
113 }
114 
115 int MplsKSyncEntry::DeleteMsg(char *buf, int buf_len) {
116  KSyncMplsInfo info;
117  FillObjectLog(sandesh_op::DEL, info);
118  KSYNC_TRACE(Mpls, GetObject(), info);
119 
120  return Encode(sandesh_op::DEL, buf, buf_len);
121 }
122 
124  NHKSyncEntry *next_hop = nh();
125  if (!next_hop->IsResolved()) {
126  return next_hop;
127  }
128  return NULL;
129 }
130 
132  KSyncDBObject("KSync Mpls"), ksync_(ksync) {
133 }
134 
136 }
137 
140 }
141 
142 KSyncEntry *MplsKSyncObject::Alloc(const KSyncEntry *entry, uint32_t index) {
143  const MplsKSyncEntry *mpls = static_cast<const MplsKSyncEntry *>(entry);
144  MplsKSyncEntry *ksync = new MplsKSyncEntry(this, mpls, index);
145  return static_cast<KSyncEntry *>(ksync);
146 };
147 
149  const MplsLabel *mpls = static_cast<const MplsLabel *>(e);
150  MplsKSyncEntry *key = new MplsKSyncEntry(this, mpls);
151  return static_cast<KSyncEntry *>(key);
152 }
153 
154 void vr_mpls_req::Process(SandeshContext *context) {
155  AgentSandeshContext *ioc = static_cast<AgentSandeshContext *>(context);
156  ioc->MplsMsgHandler(this);
157 }
158 
#define KSYNC_TRACE(obj, parent,...)
Definition: ksync_object.h:304
KSyncEntryPtr nh_
Definition: mpls_ksync.h:43
void RegisterDb(DBTableBase *table)
virtual void MplsMsgHandler(vr_mpls_req *req)=0
bool IsResolved()
Agent * agent() const
Definition: ksync_init.h:39
MplsKSyncObject(KSync *ksync)
Definition: mpls_ksync.cc:131
const NextHop * nexthop() const
Definition: mpls.h:80
uint32_t nh_id() const
Definition: nexthop_ksync.h:58
MplsKSyncObject * ksync_obj_
Definition: mpls_ksync.h:41
virtual ~MplsKSyncObject()
Definition: mpls_ksync.cc:135
int Encode(sandesh_op::type op, char *buf, int buf_len)
Definition: mpls_ksync.cc:71
virtual std::string ToString() const
Definition: mpls_ksync.cc:38
MplsTable * mpls_table() const
Definition: agent.h:510
virtual KSyncEntry * UnresolvedReference()
Definition: mpls_ksync.cc:123
virtual std::string ToString() const
virtual bool Sync(DBEntry *e)
Definition: mpls_ksync.cc:51
KSync * ksync() const
Definition: mpls_ksync.h:52
KSync * ksync_
Definition: mpls_ksync.h:57
uint8_t type
Definition: load_balance.h:109
NHKSyncObject * nh_ksync_obj() const
Definition: ksync_init.h:43
virtual KSyncEntry * DBToKSyncEntry(const DBEntry *e)
Definition: mpls_ksync.cc:148
MplsKSyncEntry(MplsKSyncObject *obj, const MplsKSyncEntry *entry, uint32_t index)
Definition: mpls_ksync.cc:15
NHKSyncEntry * nh() const
Definition: mpls_ksync.h:26
virtual int ChangeMsg(char *buf, int buf_len)
Definition: mpls_ksync.cc:107
virtual int AddMsg(char *buf, int buf_len)
Definition: mpls_ksync.cc:99
KSyncEntry * GetReference(const KSyncEntry *key)
void FillObjectLog(sandesh_op::type op, KSyncMplsInfo &info) const
Definition: mpls_ksync.cc:87
void RegisterDBClients()
Definition: mpls_ksync.cc:138
virtual KSyncEntry * Alloc(const KSyncEntry *entry, uint32_t index)
Definition: mpls_ksync.cc:142
virtual bool IsLess(const KSyncEntry &rhs) const
Definition: mpls_ksync.cc:33
virtual int DeleteMsg(char *buf, int buf_len)
Definition: mpls_ksync.cc:115
Definition: mpls.h:52
KSyncDBObject * GetObject() const
Definition: mpls_ksync.cc:29
#define LOG(_Level, _Msg)
Definition: logging.h:33
size_t GetIndex() const
Definition: ksync_entry.h:156
uint32_t label_
Definition: mpls_ksync.h:42
virtual ~MplsKSyncEntry()
Definition: mpls_ksync.cc:26