diff --git a/alto-basic/auto-maps/impl/src/main/java/org/opendaylight/alto/basic/impl/AltoAutoMapsBgpIpv4Updater.java b/alto-basic/auto-maps/impl/src/main/java/org/opendaylight/alto/basic/impl/AltoAutoMapsBgpIpv4Updater.java index 2917be03..70e95b05 100644 --- a/alto-basic/auto-maps/impl/src/main/java/org/opendaylight/alto/basic/impl/AltoAutoMapsBgpIpv4Updater.java +++ b/alto-basic/auto-maps/impl/src/main/java/org/opendaylight/alto/basic/impl/AltoAutoMapsBgpIpv4Updater.java @@ -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; @@ -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; @@ -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 networkMap = computeNetworkMapByBgpIpv4(); LOG.info("Putting auto generated network-map to manual map config..."); ManualMapsUtils.createResourceNetworkMap(contextId, networkMapConfig.getResourceId().getValue(), @@ -119,18 +124,17 @@ private List computeNetworkMapByBgpIpv4() { if (optional.isPresent()) { Tables table = optional.get(); if (table.getRoutes() instanceof Ipv4RoutesCase) { + LOG.debug("Computing PIDs from BGP IPv4 routes..."); java.util.Map> pids = getPIDClusters((Ipv4RoutesCase) table.getRoutes()); for (java.util.Map.Entry> entry : pids.entrySet()) { String pidName = entry.getKey(); List 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 { @@ -139,7 +143,7 @@ private List computeNetworkMapByBgpIpv4() { } else { LOG.error("BGP Local RIB not found"); } - } catch (InterruptedException | ExecutionException e) { + } catch (Exception e) { e.printStackTrace(); } return networkMap; @@ -152,21 +156,32 @@ private java.util.Map> getPIDClusters(Ipv4RoutesCase rout continue; } List 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 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 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<>()); } @@ -175,6 +190,23 @@ private java.util.Map> 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; diff --git a/alto-basic/simple-ird/impl/src/main/java/org/opendaylight/alto/basic/impl/SimpleIrdEntryListener.java b/alto-basic/simple-ird/impl/src/main/java/org/opendaylight/alto/basic/impl/SimpleIrdEntryListener.java index ab283409..af35b19e 100644 --- a/alto-basic/simple-ird/impl/src/main/java/org/opendaylight/alto/basic/impl/SimpleIrdEntryListener.java +++ b/alto-basic/simple-ird/impl/src/main/java/org/opendaylight/alto/basic/impl/SimpleIrdEntryListener.java @@ -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; @@ -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.