Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include "bgp/extended-community/esi_label.h" 7 : 8 : #include <stdio.h> 9 : 10 : #include <algorithm> 11 : #include <string> 12 : 13 : #include "base/parse_object.h" 14 : 15 : using std::copy; 16 : using std::fill; 17 : using std::string; 18 : 19 56 : EsiLabel::EsiLabel(const bytes_type &data) { 20 56 : copy(data.begin(), data.end(), data_.begin()); 21 56 : } 22 : 23 48 : EsiLabel::EsiLabel(bool single_active) { 24 48 : fill(data_.begin(), data_.end(), 0); 25 48 : data_[0] = BgpExtendedCommunityType::Evpn; 26 48 : data_[1] = BgpExtendedCommunityEvpnSubType::EsiMplsLabel; 27 48 : if (single_active) { 28 8 : data_[2] |= 0x01; 29 : } 30 48 : } 31 : 32 6 : string EsiLabel::flags() const { 33 6 : return (data_[2] & 0x01) ? string("sa") : string("aa"); 34 : } 35 : 36 6 : int EsiLabel::label() const { 37 12 : if (data_[0] == BgpExtendedCommunityType::Evpn && 38 6 : data_[1] == BgpExtendedCommunityEvpnSubType::EsiMplsLabel) { 39 6 : int value = get_value(&data_[5], 3); 40 6 : return value >> 4; 41 : } 42 0 : return 0; 43 : } 44 : 45 6 : string EsiLabel::ToString() const { 46 : char temp[50]; 47 6 : snprintf(temp, sizeof(temp), "esilabel:%s:%d", flags().c_str(), label()); 48 6 : return string(temp); 49 : }