Skip to content

Commit

Permalink
Revise for review comments
Browse files Browse the repository at this point in the history
1. create separate file for NextHopKey and NextHopGroupKey
2. remove test_AclRuleRedirectToNexthop
  • Loading branch information
Tyler Li committed Sep 20, 2019
1 parent 06ab194 commit 698d10a
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 317 deletions.
62 changes: 1 addition & 61 deletions orchagent/neighorch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,10 @@
#include "intfsorch.h"

#include "ipaddress.h"
#include "tokenize.h"
#include "nexthopkey.h"

#define NHFLAGS_IFDOWN 0x1 // nexthop's outbound i/f is down

#define VRF_PREFIX "Vrf"
extern IntfsOrch *gIntfsOrch;

struct NextHopKey
{
IpAddress ip_address; // neighbor IP address
string alias; // incoming interface alias

NextHopKey() = default;
NextHopKey(const std::string &ipstr, const std::string &alias) : ip_address(ipstr), alias(alias) {}
NextHopKey(const IpAddress &ip, const std::string &alias) : ip_address(ip), alias(alias) {}
NextHopKey(const std::string &str)
{
if (str.find(',') != string::npos)
{
std::string err = "Error converting " + str + " to NextHop";
throw std::invalid_argument(err);
}
auto keys = tokenize(str, '|');
if (keys.size() == 1)
{
ip_address = keys[0];
alias = gIntfsOrch->getRouterIntfsAlias(ip_address);
}
else if (keys.size() == 2)
{
ip_address = keys[0];
alias = keys[1];
if (!alias.compare(0, strlen(VRF_PREFIX), VRF_PREFIX))
{
alias = gIntfsOrch->getRouterIntfsAlias(ip_address, alias);
}
}
else
{
std::string err = "Error converting " + str + " to NextHop";
throw std::invalid_argument(err);
}
}
const std::string to_string() const
{
return ip_address.to_string() + "|" + alias;
}

bool operator<(const NextHopKey &o) const
{
return tie(ip_address, alias) < tie(o.ip_address, o.alias);
}

bool operator==(const NextHopKey &o) const
{
return (ip_address == o.ip_address) && (alias == o.alias);
}

bool operator!=(const NextHopKey &o) const
{
return !(*this == o);
}
};

typedef NextHopKey NeighborEntry;

struct NextHopEntry
Expand Down
126 changes: 126 additions & 0 deletions orchagent/nexthopgroupkey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#ifndef SWSS_NEXTHOPGROUPKEY_H
#define SWSS_NEXTHOPGROUPKEY_H

#include "nexthopkey.h"

class NextHopGroupKey
{
public:
NextHopGroupKey() = default;

/* ip_string|if_alias separated by ',' */
NextHopGroupKey(const std::string &nexthops)
{
auto nhv = tokenize(nexthops, ',');
for (const auto &nh : nhv)
{
m_nexthops.insert(nh);
}
}

inline const std::set<NextHopKey> &getNextHops() const
{
return m_nexthops;
}

inline size_t getSize() const
{
return m_nexthops.size();
}

inline bool operator<(const NextHopGroupKey &o) const
{
return m_nexthops < o.m_nexthops;
}

inline bool operator==(const NextHopGroupKey &o) const
{
return m_nexthops == o.m_nexthops;
}

inline bool operator!=(const NextHopGroupKey &o) const
{
return !(*this == o);
}

void add(const std::string &ip, const std::string &alias)
{
m_nexthops.emplace(ip, alias);
}

void add(const std::string &nh)
{
m_nexthops.insert(nh);
}

void add(const NextHopKey &nh)
{
m_nexthops.insert(nh);
}

bool contains(const std::string &ip, const std::string &alias) const
{
NextHopKey nh(ip, alias);
return m_nexthops.find(nh) != m_nexthops.end();
}

bool contains(const std::string &nh) const
{
return m_nexthops.find(nh) != m_nexthops.end();
}

bool contains(const NextHopKey &nh) const
{
return m_nexthops.find(nh) != m_nexthops.end();
}

bool contains(const NextHopGroupKey &nhs) const
{
for (const auto &nh : nhs.getNextHops())
{
if (!contains(nh))
{
return false;
}
}
return true;
}

void remove(const std::string &ip, const std::string &alias)
{
NextHopKey nh(ip, alias);
m_nexthops.erase(nh);
}

void remove(const std::string &nh)
{
m_nexthops.erase(nh);
}

void remove(const NextHopKey &nh)
{
m_nexthops.erase(nh);
}

const std::string to_string() const
{
string nhs_str;

for (auto it = m_nexthops.begin(); it != m_nexthops.end(); ++it)
{
if (it != m_nexthops.begin())
{
nhs_str += ",";
}

nhs_str += it->to_string();
}

return nhs_str;
}

private:
std::set<NextHopKey> m_nexthops;
};

#endif /* SWSS_NEXTHOPGROUPKEY_H */
67 changes: 67 additions & 0 deletions orchagent/nexthopkey.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef SWSS_NEXTHOPKEY_H
#define SWSS_NEXTHOPKEY_H

#include "ipaddress.h"
#include "tokenize.h"

#define VRF_PREFIX "Vrf"
extern IntfsOrch *gIntfsOrch;

struct NextHopKey
{
IpAddress ip_address; // neighbor IP address
string alias; // incoming interface alias

NextHopKey() = default;
NextHopKey(const std::string &ipstr, const std::string &alias) : ip_address(ipstr), alias(alias) {}
NextHopKey(const IpAddress &ip, const std::string &alias) : ip_address(ip), alias(alias) {}
NextHopKey(const std::string &str)
{
if (str.find(',') != string::npos)
{
std::string err = "Error converting " + str + " to NextHop";
throw std::invalid_argument(err);
}
auto keys = tokenize(str, '|');
if (keys.size() == 1)
{
ip_address = keys[0];
alias = gIntfsOrch->getRouterIntfsAlias(ip_address);
}
else if (keys.size() == 2)
{
ip_address = keys[0];
alias = keys[1];
if (!alias.compare(0, strlen(VRF_PREFIX), VRF_PREFIX))
{
alias = gIntfsOrch->getRouterIntfsAlias(ip_address, alias);
}
}
else
{
std::string err = "Error converting " + str + " to NextHop";
throw std::invalid_argument(err);
}
}
const std::string to_string() const
{
return ip_address.to_string() + "|" + alias;
}

bool operator<(const NextHopKey &o) const
{
return tie(ip_address, alias) < tie(o.ip_address, o.alias);
}

bool operator==(const NextHopKey &o) const
{
return (ip_address == o.ip_address) && (alias == o.alias);
}

bool operator!=(const NextHopKey &o) const
{
return !(*this == o);
}
};

#endif /* SWSS_NEXTHOPKEY_H */
Loading

0 comments on commit 698d10a

Please sign in to comment.