OpenSDN source code
etree.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 
7 
8 #include <stdio.h>
9 
10 #include <algorithm>
11 #include <string>
12 
13 
14 using std::copy;
15 using std::string;
16 
17 ETree::ETree(bool leaf, int label) {
20  data_[2] = leaf ? 0x01 : 0x0; // Leaf Indication
21  data_[3] = 0x00; // Reserved
22  data_[4] = 0x00; // Reserved
23  put_value(&data_[5], 3, (label<<4)); // leaf label
24 }
25 
26 ETree::ETree(const bytes_type &data) {
27  copy(data.begin(), data.end(), data_.begin());
28 }
29 
30 bool ETree::leaf() const {
31  return (data_[2] & 0x1);
32 }
33 
34 int ETree::label() const {
35  uint8_t data[ETree::kSize];
36  copy(data_.begin(), data_.end(), &data[0]);
37  if (data[0] == BgpExtendedCommunityType::Evpn &&
39  uint32_t value = get_value(data + 5, 3);
40  return value >> 4;
41  }
42  return false;
43 }
44 
45 std::string ETree::ToString() {
46  char temp[50];
47  snprintf(temp, sizeof(temp), "etree:%s:%d",
48  (leaf() ? "leaf":"root"), label());
49  return string(temp);
50 }
static const int kSize
Definition: etree.h:19
bool leaf() const
Definition: etree.cc:30
boost::array< uint8_t, kSize > bytes_type
Definition: etree.h:20
ETree(bool leaf, int label=0)
Definition: etree.cc:17
std::string ToString()
Definition: etree.cc:45
int label() const
Definition: etree.cc:34
bytes_type data_
Definition: etree.h:39
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:56
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:40