X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=mappingservice%2Fimplementation%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Flispflowmapping%2Fimplementation%2Futil%2FMappingMergeUtil.java;h=540d08a5500c47acbaf407f3a08ce3898500b4bf;hb=4a99f1d24037d5b7947658b7360a8c53baeedbf6;hp=aab708237a1a2fe35a48b29dcc7652a3a2999fa7;hpb=d37a97737588593a2222c94011477c65de796e09;p=lispflowmapping.git diff --git a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/util/MappingMergeUtil.java b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/util/MappingMergeUtil.java index aab708237..540d08a55 100644 --- a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/util/MappingMergeUtil.java +++ b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/util/MappingMergeUtil.java @@ -7,27 +7,29 @@ */ package org.opendaylight.lispflowmapping.implementation.util; -import java.net.InetAddress; +import static org.opendaylight.yangtools.yang.common.UintConversions.fromJava; + +import com.google.common.base.Preconditions; import java.util.ArrayList; import java.util.Date; -import java.util.LinkedHashMap; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; - -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix; +import org.opendaylight.lispflowmapping.config.ConfigIni; +import org.opendaylight.lispflowmapping.lisp.type.MappingData; +import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; +import org.opendaylight.lispflowmapping.lisp.util.MaskUtil; +import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKey; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.source.dest.key.SourceDestKeyBuilder; -import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; -import org.opendaylight.lispflowmapping.lisp.util.MaskUtil; -import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper; -import org.opendaylight.lispflowmapping.implementation.config.ConfigIni; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder; @@ -37,16 +39,14 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rl import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Preconditions; - /** - * Utility class to implement merging of locator sets + * Utility class to implement merging of locator sets. * * @author Lorand Jakab * */ public final class MappingMergeUtil { - protected static final Logger LOG = LoggerFactory.getLogger(MappingMergeUtil.class); + private static final Logger LOG = LoggerFactory.getLogger(MappingMergeUtil.class); // Utility class, should not be instantiated private MappingMergeUtil() { @@ -76,10 +76,10 @@ public final class MappingMergeUtil { return newLocator; } - private static int compareLocators(LocatorRecord a, LocatorRecord b) { - InetAddress aInet = LispAddressUtil.ipAddressToInet(a.getRloc().getAddress()); - InetAddress bInet = LispAddressUtil.ipAddressToInet(b.getRloc().getAddress()); - return LispAddressUtil.compareInetAddresses(aInet, bInet); + private static int compareLocators(LocatorRecord one, LocatorRecord two) { + byte[] oneIp = LispAddressUtil.ipAddressToByteArray(one.getRloc().getAddress()); + byte[] twoIp = LispAddressUtil.ipAddressToByteArray(two.getRloc().getAddress()); + return LispAddressUtil.compareIpAddressByteArrays(oneIp, twoIp); } private static void mergeLocatorRecords(MappingRecordBuilder mrb, MappingRecord newRecord) { @@ -100,9 +100,7 @@ public final class MappingMergeUtil { Rloc newRloc = newLocator.getRloc(); if (locatorMap.containsKey(newRloc)) { // overlapping locator - if (locatorMap.get(newRloc).equals(newLocator)) { - continue; - } else { + if (!locatorMap.get(newRloc).equals(newLocator)) { LocatorRecord mergedLocator = mergeLocators(locatorMap.get(newRloc), newLocator); newLocatorList.add(mergedLocator); } @@ -113,32 +111,33 @@ public final class MappingMergeUtil { } // Build new merged and sorted locator set if need be - if (newLocatorList.size() != 0) { + if (!newLocatorList.isEmpty()) { List mergedLocators = new ArrayList(); - int mlIt = 0, lIt = 0; - while (mlIt < newLocatorList.size() && lIt < locators.size()) { - int cmp = compareLocators(locators.get(lIt), newLocatorList.get(mlIt)); + int mlocIt = 0; + int locIt = 0; + while (mlocIt < newLocatorList.size() && locIt < locators.size()) { + int cmp = compareLocators(locators.get(locIt), newLocatorList.get(mlocIt)); if (cmp < 0) { - mergedLocators.add(locators.get(lIt)); - lIt++; + mergedLocators.add(locators.get(locIt)); + locIt++; } else if (cmp > 0) { - mergedLocators.add(newLocatorList.get(mlIt)); - mlIt++; + mergedLocators.add(newLocatorList.get(mlocIt)); + mlocIt++; } else { // when a locator appears in both lists, keep the new (merged) one and skip the old - mergedLocators.add(newLocatorList.get(mlIt)); - mlIt++; - lIt++; + mergedLocators.add(newLocatorList.get(mlocIt)); + mlocIt++; + locIt++; } } - while (lIt < locators.size()) { - mergedLocators.add(locators.get(lIt)); - lIt++; + while (locIt < locators.size()) { + mergedLocators.add(locators.get(locIt)); + locIt++; } - while (mlIt < newLocatorList.size()) { - mergedLocators.add(newLocatorList.get(mlIt)); - mlIt++; + while (mlocIt < newLocatorList.size()) { + mergedLocators.add(newLocatorList.get(mlocIt)); + mlocIt++; } mrb.setLocatorRecord(mergedLocators); } @@ -162,29 +161,30 @@ public final class MappingMergeUtil { return mrb.build(); } - public static MappingRecord mergeXtrIdMappings(List records, List expiredMappings, - Set sourceRlocs) { + public static MappingData mergeXtrIdMappings(List mappingDataList, List expiredMappingDataList, + Set sourceRlocs) { MappingRecordBuilder mrb = null; XtrId xtrId = null; Long timestamp = Long.MAX_VALUE; - for (int i = 0; i < records.size(); i++) { - MappingRecord record = (MappingRecord) records.get(i); + for (int i = 0; i < mappingDataList.size(); i++) { + MappingData mappingData = (MappingData) mappingDataList.get(i); + MappingRecord record = mappingData.getRecord(); // Skip expired mappings and add them to a list to be returned to the caller - if (timestampIsExpired(record.getTimestamp())) { - expiredMappings.add(record.getXtrId()); + if (timestampIsExpired(mappingData.getTimestamp())) { + expiredMappingDataList.add(mappingData); continue; } if (mrb == null) { - mrb = new MappingRecordBuilder((MappingRecord) records.get(i)); + mrb = new MappingRecordBuilder(record); } // Save the oldest valid timestamp - if (record.getTimestamp() < timestamp) { - timestamp = record.getTimestamp(); - xtrId = record.getXtrId(); + if (mappingData.getTimestamp().getTime() < timestamp) { + timestamp = mappingData.getTimestamp().getTime(); + xtrId = mappingData.getXtrId(); } // Merge record fields and locators @@ -200,15 +200,19 @@ public final class MappingMergeUtil { return null; } mrb.setXtrId(xtrId); - mrb.setTimestamp(timestamp); - return mrb.build(); + return new MappingData(mrb.build(), new Date(timestamp)); } - public static boolean mappingIsExpired(MappingRecord mapping) { - Preconditions.checkNotNull(mapping, "mapping should not be null!"); - if (mapping.getTimestamp() != null) { - return timestampIsExpired(mapping.getTimestamp()); + /* + * The following three methods intentionally throw an exception when their argument is null, because they can't + * decide based on that to return true or false, so the calling function should do that and only call these with + * non-null arguments + */ + public static boolean mappingIsExpired(MappingData mappingData) { + Preconditions.checkNotNull(mappingData, "mapping should not be null!"); + if (mappingData.getTimestamp() != null) { + return timestampIsExpired(mappingData.getTimestamp()); } return false; } @@ -218,15 +222,20 @@ public final class MappingMergeUtil { return timestampIsExpired(timestamp.getTime()); } - public static boolean timestampIsExpired(Long timestamp) { + private static boolean timestampIsExpired(Long timestamp) { Preconditions.checkNotNull(timestamp, "timestamp should not be null!"); - if ((System.currentTimeMillis() - timestamp) > ConfigIni.getInstance().getRegistrationValiditySb() ) { + if ((System.currentTimeMillis() - timestamp) > ConfigIni.getInstance().getRegistrationValiditySb()) { return true; } return false; } - public static Object computeNbSbIntersection(MappingRecord nbMapping, MappingRecord sbMapping) { + public static MappingData computeNbSbIntersection(MappingData nbMappingData, + MappingData sbMappingData) { + return new MappingData(computeNbSbIntersection(nbMappingData.getRecord(), sbMappingData.getRecord())); + } + + private static MappingRecord computeNbSbIntersection(MappingRecord nbMapping, MappingRecord sbMapping) { // returns a MappingRecord which has the more specific EID, and intersection of locator records. // If locators intersection is empty, original NB mapping is returned. // The intersection is only computed for mappings with maskable EIDs. @@ -243,7 +252,7 @@ public final class MappingMergeUtil { if (nbMapping.getEid().getAddress() instanceof org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang .ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey) { nbMask = SourceDestKeyHelper.getDstMask(nbMapping.getEid()); - if ( nbMask < sbMask) { + if (nbMask < sbMask) { // We have to create a new SourceDest EID, where the source is same as the // one in NB record, and dest EID is the more specific from SB mapping record. @@ -295,11 +304,11 @@ public final class MappingMergeUtil { if (sbLocatorMap.containsKey(nbRloc)) { // common locator found. use the NB record as the common locator. - if (sbLocatorMap.get(nbRloc).getPriority() == (short) 255) { + if (sbLocatorMap.get(nbRloc).getPriority().toJava() == (short) 255) { // if SB locator has p == 255 then common locator takes all NB fields except for p // which must be set to 255 LocatorRecordBuilder lrb = new LocatorRecordBuilder(nbLocator); - lrb.setPriority((short) 255); + lrb.setPriority(fromJava((short) 255)); commonLocators.add(lrb.build()); } else { commonLocators.add(nbLocator);