Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "bgp/extended-community/etree.h" 6 : 7 : #include <stdio.h> 8 : 9 : #include <algorithm> 10 : #include <string> 11 : 12 : 13 : using std::copy; 14 : using std::string; 15 : 16 4370 : ETree::ETree(bool leaf, int label) { 17 4370 : data_[0] = BgpExtendedCommunityType::Evpn; 18 4371 : data_[1] = BgpExtendedCommunityEvpnSubType::ETree; 19 4371 : data_[2] = leaf ? 0x01 : 0x0; // Leaf Indication 20 4371 : data_[3] = 0x00; // Reserved 21 4371 : data_[4] = 0x00; // Reserved 22 4371 : put_value(&data_[5], 3, (label<<4)); // leaf label 23 4371 : } 24 : 25 82269 : ETree::ETree(const bytes_type &data) { 26 82269 : copy(data.begin(), data.end(), data_.begin()); 27 82263 : } 28 : 29 82265 : bool ETree::leaf() const { 30 82265 : return (data_[2] & 0x1); 31 : } 32 : 33 10 : int ETree::label() const { 34 : uint8_t data[ETree::kSize]; 35 10 : copy(data_.begin(), data_.end(), &data[0]); 36 10 : if (data[0] == BgpExtendedCommunityType::Evpn && 37 10 : data[1] == BgpExtendedCommunityEvpnSubType::ETree) { 38 10 : uint32_t value = get_value(data + 5, 3); 39 10 : return value >> 4; 40 : } 41 0 : return false; 42 : } 43 : 44 10 : std::string ETree::ToString() { 45 : char temp[50]; 46 20 : snprintf(temp, sizeof(temp), "etree:%s:%d", 47 10 : (leaf() ? "leaf":"root"), label()); 48 10 : return string(temp); 49 : }