Skip to content

Commit

Permalink
Quick update: generate fine-grained bgp level pid
Browse files Browse the repository at this point in the history
Signed-off-by: jensenzhang <jingxuan.n.zhang@gmail.com>
  • Loading branch information
fno2010 committed Jun 5, 2020
1 parent a07d9dc commit 626b792
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.OriginatorId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.as.path.Segments;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.BgpRib;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.RibId;
Expand All @@ -46,6 +47,9 @@
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -84,6 +88,7 @@ private void registerBGPListener() {

private void updateNetworkMap() {
final ReadWriteTransaction wrx = dataBroker.newReadWriteTransaction();
LOG.debug("Computing network map from BGP IPv4 routes...");
List<Map> networkMap = computeNetworkMapByBgpIpv4();
LOG.info("Putting auto generated network-map to manual map config...");
ManualMapsUtils.createResourceNetworkMap(contextId, networkMapConfig.getResourceId().getValue(),
Expand Down Expand Up @@ -119,18 +124,17 @@ private List<Map> computeNetworkMapByBgpIpv4() {
if (optional.isPresent()) {
Tables table = optional.get();
if (table.getRoutes() instanceof Ipv4RoutesCase) {
LOG.debug("Computing PIDs from BGP IPv4 routes...");
java.util.Map<String, List<IpPrefix>> pids = getPIDClusters((Ipv4RoutesCase) table.getRoutes());
for (java.util.Map.Entry<String, List<IpPrefix>> entry : pids.entrySet()) {
String pidName = entry.getKey();
List<IpPrefix> prefixList = entry.getValue();
networkMap.add(new MapBuilder()
.setPid(new PidName(pidName))
.setEndpointAddressGroup(Arrays.asList(new EndpointAddressGroup[]{
new EndpointAddressGroupBuilder()
.setAddressType(new EndpointAddressType(EndpointAddressType.Enumeration.Ipv4))
.setEndpointPrefix(prefixList)
.build()
}))
.setEndpointAddressGroup(Arrays.asList(new EndpointAddressGroupBuilder()
.setAddressType(new EndpointAddressType(EndpointAddressType.Enumeration.Ipv4))
.setEndpointPrefix(prefixList)
.build()))
.build());
}
} else {
Expand All @@ -139,7 +143,7 @@ private List<Map> computeNetworkMapByBgpIpv4() {
} else {
LOG.error("BGP Local RIB not found");
}
} catch (InterruptedException | ExecutionException e) {
} catch (Exception e) {
e.printStackTrace();
}
return networkMap;
Expand All @@ -152,21 +156,32 @@ private java.util.Map<String, List<IpPrefix>> getPIDClusters(Ipv4RoutesCase rout
continue;
}
List<Segments> segments = route.getAttributes().getAsPath().getSegments();
String pidName = "PID0";
LOG.debug("get route segments");
OriginatorId originatorId = route.getAttributes().getOriginatorId();
LOG.debug("get route originator");
String asNumber = "0";
if (segments != null && !segments.isEmpty()) {
List<AsNumber> asSequence = segments.get(segments.size() - 1).getAsSequence();
if (asSequence != null && !asSequence.isEmpty()) {
pidName = "PID" + asSequence.get(asSequence.size() - 1).getValue().toString();
asNumber = asSequence.get(asSequence.size() - 1).getValue().toString();
} else {
List<AsNumber> asSet = segments.get(segments.size() - 1).getAsSet();
if (asSet != null && !asSet.isEmpty()) {
pidName = "PID" + String.join("-",
asNumber = String.join("-",
(String[]) Arrays.stream((AsNumber[]) asSet.toArray())
.map(s -> s.getValue().toString())
.toArray());
}
}
}
LOG.debug("get route first-hop as number: {}", asNumber);
String originator = "0";
if (originatorId != null) {
originator = originatorId.getOriginator().getValue();
}
LOG.debug("get route first-hop ip: {}", originator);
String pidName = "PID" + getHashedPIDName(asNumber, originator);
LOG.debug("generator PID name: {}", pidName);
if (!pids.containsKey(pidName)) {
pids.put(pidName, new LinkedList<>());
}
Expand All @@ -175,6 +190,23 @@ private java.util.Map<String, List<IpPrefix>> getPIDClusters(Ipv4RoutesCase rout
return pids;
}

private String getHashedPIDName(String asNumber, String originator) {
return asNumber + ":" + getHexStrByIp(originator);
}

private String getHexStrByIp(String ipstr) {
String hex = "00000000";
try {
InetAddress ipaddr = InetAddress.getByName(ipstr);
byte[] addr = ipaddr.getAddress();
BigInteger bint = new BigInteger(1, addr);
hex = String.format("%0" + (addr.length << 1) + "x", bint);
} catch (UnknownHostException e) {
LOG.debug("Illegal IP address");
}
return hex;
}

private boolean isInternalPrefix(Ipv4Prefix prefix) {
// TODO: detect internal prefixes
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

import com.google.common.base.Optional;
import com.google.common.util.concurrent.SettableFuture;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import org.opendaylight.alto.basic.simpleird.SimpleIrdUtils;
import org.opendaylight.alto.core.resourcepool.ResourcepoolUtils;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
Expand Down Expand Up @@ -50,6 +44,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
* SimpleIrdEntryListener listens only to creation/deletion/update of the
* top-level container for simple Ird.
Expand Down

0 comments on commit 626b792

Please sign in to comment.