Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement RouterMac extended community
Change-Id: I50f29f797842c7554d7f0f0a6b13397e33444a7c Partial-Bug: 1636654
- Loading branch information
Nischal Sheth
committed
Oct 26, 2016
1 parent
1474bf4
commit 71f4086
Showing
10 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#include "bgp/extended-community/router_mac.h" | ||
|
||
#include <algorithm> | ||
#include <string> | ||
|
||
using std::copy; | ||
using std::string; | ||
|
||
RouterMac::RouterMac(const MacAddress &mac_addr) { | ||
data_[0] = BgpExtendedCommunityType::Evpn; | ||
data_[1] = BgpExtendedCommunityEvpnSubType::RouterMac; | ||
copy(mac_addr.GetData(), mac_addr.GetData() + 6, data_.begin() + 2); | ||
} | ||
|
||
RouterMac::RouterMac(const bytes_type &data) { | ||
copy(data.begin(), data.end(), data_.begin()); | ||
} | ||
|
||
MacAddress RouterMac::mac_address() const { | ||
uint8_t data[RouterMac::kSize]; | ||
copy(data_.begin(), data_.end(), &data[0]); | ||
if (data[0] == BgpExtendedCommunityType::Evpn && | ||
data[1] == BgpExtendedCommunityEvpnSubType::RouterMac) { | ||
return MacAddress(&data[2]); | ||
} | ||
return MacAddress::kZeroMac; | ||
} | ||
|
||
string RouterMac::ToString() { | ||
return string("rtrmac:") + mac_address().ToString(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#ifndef SRC_BGP_EXTENDED_COMMUNITY_ROUTER_MAC_H_ | ||
#define SRC_BGP_EXTENDED_COMMUNITY_ROUTER_MAC_H_ | ||
|
||
#include <boost/array.hpp> | ||
#include <boost/system/error_code.hpp> | ||
|
||
#include <string> | ||
|
||
#include "base/parse_object.h" | ||
#include "bgp/extended-community/types.h" | ||
#include "net/mac_address.h" | ||
|
||
class RouterMac { | ||
public: | ||
static const int kSize = 8; | ||
typedef boost::array<uint8_t, kSize> bytes_type; | ||
|
||
explicit RouterMac(const MacAddress &mac_addr); | ||
explicit RouterMac(const bytes_type &data); | ||
|
||
MacAddress mac_address() const; | ||
|
||
const bytes_type &GetExtCommunity() const { return data_; } | ||
|
||
const uint64_t GetExtCommunityValue() const { | ||
return get_value(data_.begin(), 8); | ||
} | ||
std::string ToString(); | ||
|
||
private: | ||
bytes_type data_; | ||
}; | ||
|
||
#endif // SRC_BGP_EXTENDED_COMMUNITY_ROUTER_MAC_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. | ||
*/ | ||
|
||
#include "bgp/extended-community/router_mac.h" | ||
|
||
#include "bgp/community.h" | ||
#include "testing/gunit.h" | ||
|
||
using namespace std; | ||
|
||
class RouterMacTest : public ::testing::Test { | ||
}; | ||
|
||
TEST_F(RouterMacTest, ByteArray_1) { | ||
RouterMac::bytes_type data = { { | ||
BgpExtendedCommunityType::Evpn, | ||
BgpExtendedCommunityEvpnSubType::RouterMac, | ||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06 | ||
} }; | ||
RouterMac router_mac(data); | ||
EXPECT_TRUE(ExtCommunity::is_router_mac(router_mac.GetExtCommunity())); | ||
EXPECT_EQ("rtrmac:01:02:03:04:05:06", router_mac.ToString()); | ||
} | ||
|
||
TEST_F(RouterMacTest, ByteArray_2) { | ||
RouterMac::bytes_type data = { { | ||
BgpExtendedCommunityType::Evpn, | ||
BgpExtendedCommunityEvpnSubType::RouterMac, | ||
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f | ||
} }; | ||
RouterMac router_mac(data); | ||
EXPECT_TRUE(ExtCommunity::is_router_mac(router_mac.GetExtCommunity())); | ||
EXPECT_EQ("rtrmac:0a:0b:0c:0d:0e:0f", router_mac.ToString()); | ||
} | ||
|
||
TEST_F(RouterMacTest, MacAddress_1) { | ||
boost::system::error_code ec; | ||
MacAddress mac_addr = MacAddress::FromString("01:02:03:04:05:06", &ec); | ||
EXPECT_EQ(0, ec.value()); | ||
RouterMac router_mac(mac_addr); | ||
EXPECT_TRUE(ExtCommunity::is_router_mac(router_mac.GetExtCommunity())); | ||
EXPECT_EQ("rtrmac:01:02:03:04:05:06", router_mac.ToString()); | ||
EXPECT_EQ(mac_addr, router_mac.mac_address()); | ||
RouterMac router_mac2(router_mac.GetExtCommunity()); | ||
EXPECT_EQ(router_mac.GetExtCommunityValue(), | ||
router_mac2.GetExtCommunityValue()); | ||
} | ||
|
||
TEST_F(RouterMacTest, MacAddress_2) { | ||
boost::system::error_code ec; | ||
MacAddress mac_addr = MacAddress::FromString("0a:0b:0c:0d:0e:0f", &ec); | ||
EXPECT_EQ(0, ec.value()); | ||
RouterMac router_mac(mac_addr); | ||
EXPECT_TRUE(ExtCommunity::is_router_mac(router_mac.GetExtCommunity())); | ||
EXPECT_EQ("rtrmac:0a:0b:0c:0d:0e:0f", router_mac.ToString()); | ||
EXPECT_EQ(mac_addr, router_mac.mac_address()); | ||
RouterMac router_mac2(router_mac.GetExtCommunity()); | ||
EXPECT_EQ(router_mac.GetExtCommunityValue(), | ||
router_mac2.GetExtCommunityValue()); | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
::testing::InitGoogleTest(&argc, argv); | ||
int result = RUN_ALL_TESTS(); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters