1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

103 lines
2.0 KiB
React
Raw Normal View History

2018-10-03 15:30:41 +02:00
/*
* Communities helper
*/
/*
* Communities are represented as a nested object:
* {
* 1234: {
* 23: "community-leaf",
* 42: {
* 1: "large-community-leaf"
* }
* }
*/
2018-10-03 22:40:40 +02:00
/*
* Resolve a community description from the above described
* tree structure.
*/
2018-10-03 20:01:25 +02:00
export function resolveCommunity(base, community) {
let lookup = base;
for (const part of community) {
2018-10-03 15:30:41 +02:00
if (typeof(lookup) !== "object") {
return null;
}
2018-10-03 20:01:25 +02:00
let res = lookup[part];
2018-10-03 15:30:41 +02:00
if (!res) {
// Try the wildcard
if (lookup["*"]) {
res = lookup["*"]
} else {
return null; // We did everything we could
}
}
lookup = res;
}
return lookup;
}
2018-10-03 20:01:25 +02:00
/*
* Resolve all communities
*/
export function resolveCommunities(base, communities) {
let results = [];
for (const c of communities) {
const description = resolveCommunity(base, c);
if (description != null) {
results.push([c, description]);
}
}
return results;
}
2018-10-03 15:30:41 +02:00
/*
* Reject candidate helpers:
*
* - check if prefix is a reject candidate
* - make css classes
*/
2018-10-03 22:40:40 +02:00
export function isRejectCandidate(rejectCommunities, route) {
2018-10-03 15:30:41 +02:00
// Check if any reject candidate community is set
2018-10-03 22:40:40 +02:00
const communities = route.bgp.communities;
const largeCommunities = route.bgp.large_communities;
const resolved = resolveCommunities(
rejectCommunities, largeCommunities
);
2018-10-03 15:30:41 +02:00
2018-10-03 22:40:40 +02:00
return (resolved.length > 0);
2018-10-03 15:30:41 +02:00
}
2018-10-18 15:02:34 +02:00
/*
* Expand variables in string:
* "Test AS$0 rejects $2"
* will expand with [23, 42, 123] to
* "Test AS23 rejects 123"
*/
export function expandVars(str, vars) {
if (!str) {
return str; // We don't have to do anything.
}
var res = str;
vars.map((v, i) => {
res = res.replace(`$${i}`, v);
});
return res;
}
export function makeReadableCommunity(communities, community) {
const label = resolveCommunity(communities, community);
return expandVars(label, community);
}
export function communityRepr(community) {
return community.join(":");
}
2018-10-03 15:30:41 +02:00