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 4379 : ETree::ETree(bool leaf, int label) { 17 4379 : data_[0] = BgpExtendedCommunityType::Evpn; 18 4379 : data_[1] = BgpExtendedCommunityEvpnSubType::ETree; 19 4379 : data_[2] = leaf ? 0x01 : 0x0; // Leaf Indication 20 4379 : data_[3] = 0x00; // Reserved 21 4379 : data_[4] = 0x00; // Reserved 22 4379 : put_value(&data_[5], 3, (label<<4)); // leaf label 23 4379 : } 24 : 25 82552 : ETree::ETree(const bytes_type &data) { 26 82552 : copy(data.begin(), data.end(), data_.begin()); 27 82549 : } 28 : 29 82551 : bool ETree::leaf() const { 30 82551 : return (data_[2] & 0x1); 31 : } 32 : 33 8 : int ETree::label() const { 34 : uint8_t data[ETree::kSize]; 35 8 : copy(data_.begin(), data_.end(), &data[0]); 36 8 : if (data[0] == BgpExtendedCommunityType::Evpn && 37 8 : data[1] == BgpExtendedCommunityEvpnSubType::ETree) { 38 8 : uint32_t value = get_value(data + 5, 3); 39 8 : return value >> 4; 40 : } 41 0 : return false; 42 : } 43 : 44 8 : std::string ETree::ToString() { 45 : char temp[50]; 46 16 : snprintf(temp, sizeof(temp), "etree:%s:%d", 47 8 : (leaf() ? "leaf":"root"), label()); 48 8 : return string(temp); 49 : }