OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bgp_origin_vn_path.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 
7 
8 #include <algorithm>
9 #include <string>
10 
11 #include "bgp/bgp_proto.h"
13 
14 using std::string;
15 using std::vector;
16 
18  char repr[80];
19  snprintf(repr, sizeof(repr), "OriginVnPath <code: %d, flags: %02x> : %zu",
20  code, flags, origin_vns.size());
21  return string(repr);
22 }
23 
25  int ret = BgpAttribute::CompareTo(rhs);
26  if (ret != 0)
27  return ret;
29  static_cast<const OriginVnPathSpec &>(rhs).origin_vns);
30  return 0;
31 }
32 
34  attr->set_origin_vn_path(this);
35 }
36 
38  return origin_vns.size() * sizeof(uint64_t);
39 }
40 
42  const OriginVnPathSpec spec)
43  : ovnpath_db_(ovnpath_db) {
44  refcount_ = 0;
45  for (vector<uint64_t>::const_iterator it = spec.origin_vns.begin();
46  it < spec.origin_vns.end(); ++it) {
47  OriginVnValue value;
48  put_value(value.data(), value.size(), *it);
49  origin_vns_.push_back(value);
50  }
51 }
52 
54  ovnpath_db_->Delete(this);
55 }
56 
58  OriginVnList::iterator it = origin_vns_.begin();
59  origin_vns_.insert(it, value);
60 }
61 
62 bool OriginVnPath::Contains(as_t asn, uint32_t vn_index) const {
63  if (asn <= 0xffff) {
64  OriginVn origin_vn(asn, vn_index);
65  return Contains(origin_vn.GetExtCommunity());
66  }
67  OriginVn origin_vn4(asn, AS_TRANS);
68  OriginVn origin_vn(AS_TRANS, vn_index);
69  return (Contains(origin_vn.GetExtCommunity()) &&
70  Contains(origin_vn4.GetExtCommunity()));
71 }
72 
73 bool OriginVnPath::Contains(const OriginVnValue &val) const {
74  OriginVn in_origin_vn(val);
75  int in_vn_index = in_origin_vn.IsGlobal() ? in_origin_vn.vn_index() : 0;
76  for (OriginVnList::const_iterator it = origin_vns_.begin();
77  it != origin_vns_.end(); ++it) {
78  if (*it == val)
79  return true;
80  if (in_vn_index == 0)
81  continue;
82  OriginVn origin_vn(*it);
83  if (origin_vn.vn_index() == in_vn_index)
84  return true;
85  }
86  return false;
87 }
88 
89 int OriginVnPath::CompareTo(const OriginVnPath &rhs) const {
90  KEY_COMPARE(origin_vns_.size(), rhs.origin_vns_.size());
91 
92  OriginVnList::const_iterator it1, it2;
93  for (it1 = origin_vns_.begin(), it2 = rhs.origin_vns_.begin();
94  it1 < origin_vns_.end(); ++it1, ++it2) {
95  if (*it1 < *it2) {
96  return -1;
97  }
98  if (*it1 > *it2) {
99  return 1;
100  }
101  }
102  return 0;
103 }
104 
106 }
107 
109  const OriginVnPath::OriginVnValue &value) {
110  OriginVnPath *clone;
111  if (ovnpath) {
112  clone = new OriginVnPath(*ovnpath);
113  } else {
114  clone = new OriginVnPath(this);
115  }
116  clone->Prepend(value);
117  return Locate(clone);
118 }
int CompareTo(const OriginVnPath &rhs) const
virtual void ToCanonical(BgpAttr *attr)
#define KEY_COMPARE(x, y)
Definition: util.h:70
OriginVnPathPtr PrependAndLocate(const OriginVnPath *ovnpath, const OriginVnPath::OriginVnValue &value)
uint8_t flags
Definition: bgp_attr_base.h:70
virtual size_t EncodeLength() const
uint32_t as_t
Definition: bgp_common.h:21
boost::intrusive_ptr< const OriginVnPath > OriginVnPathPtr
virtual int CompareTo(const BgpAttribute &rhs) const
void Delete(Type *attr)
std::vector< uint64_t > origin_vns
virtual void Remove()
bool IsGlobal() const
Definition: origin_vn.cc:134
OriginVnPathDB(BgpServer *server)
OriginVnPath(OriginVnPathDB *ovnpath_db)
virtual int CompareTo(const BgpAttribute &rhs_attr) const
int vn_index() const
Definition: origin_vn.cc:122
virtual std::string ToString() const
bool Contains(const OriginVnValue &value) const
const bytes_type & GetExtCommunity() const
Definition: origin_vn.h:33
OriginVnList origin_vns_
OriginVnPathDB * ovnpath_db_
void set_origin_vn_path(OriginVnPathPtr ovnpath)
Definition: bgp_attr.cc:1023
void Prepend(const OriginVnValue &value)
boost::array< uint8_t, 8 > OriginVnValue
tbb::atomic< int > refcount_
uint8_t code
Definition: bgp_attr_base.h:68
#define AS_TRANS
Definition: bgp_common.h:23
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:55