Checkstyle: fix issues and enforce on implementation
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapResolver.java
index 27c610ccd25a3a0c2033134c21409152a08540cc..7b983cb06b6b94430bae2a1051e5aa2e48ed1237 100644 (file)
 
 package org.opendaylight.lispflowmapping.implementation.lisp;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Sets;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.HashSet;
-import java.util.Map;
-import java.net.InetAddress;
-
-import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
-import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
-import org.opendaylight.lispflowmapping.implementation.util.MapRequestUtil;
-import org.opendaylight.lispflowmapping.implementation.util.MaskUtil;
-import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
-import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceSubscriberRLOC;
+import java.util.Set;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
+import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord.Action;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecordBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapreplymessage.MapReplyBuilder;
+import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
+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.SourceDestKeyLcaf;
+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.ExplicitLocatorPath;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6BinaryAfi;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6PrefixBinaryAfi;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
+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;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class MapResolver extends AbstractLispComponent implements IMapResolverAsync {
+public class MapResolver implements IMapResolverAsync {
+    protected static final Logger LOG = LoggerFactory.getLogger(MapResolver.class);
 
     private static final int TTL_RLOC_TIMED_OUT = 1;
     private static final int TTL_NO_RLOC_KNOWN = 15;
-    protected static final Logger logger = LoggerFactory.getLogger(MapResolver.class);
 
-    public MapResolver(ILispDAO dao) {
-        this(dao, true, true);
-    }
+    private IMappingService mapService;
+    private boolean subscriptionService;
+    private String elpPolicy;
+    private IMapRequestResultHandler requestHandler;
+    private boolean authenticate = true;
 
-    public MapResolver(ILispDAO dao, boolean authenticate, boolean iterateMask) {
-        super(dao, authenticate, iterateMask);
+    public MapResolver(IMappingService mapService, boolean smr, String elpPolicy,
+            IMapRequestResultHandler requestHandler) {
+        Preconditions.checkNotNull(mapService);
+        this.subscriptionService = smr;
+        this.mapService = mapService;
+        this.elpPolicy = elpPolicy;
+        this.requestHandler = requestHandler;
     }
 
-    public void handleMapRequest(MapRequest request, IMapRequestResultHandler callback) {
-        if (dao == null) {
-            logger.warn("handleMapRequest called while dao is uninitialized");
+    public void handleMapRequest(MapRequest request) {
+        // SMRs and RLOC probes are directed towards xTRs and we're a Map-Resolver here, so ignore them
+        if (request.isSmr() != null && request.isSmr()) {
+            LOG.debug("Map-Resolver ignoring incoming SMR control message.");
+            return;
+        }
+        if (request.isProbe() != null && request.isProbe()) {
+            LOG.debug("Map-Resolver ignoring incoming RLOC probe control message.");
             return;
         }
-        InetAddress itrRloc = MapRequestUtil.selectItrRloc(request);
-        logger.trace("Map-Request itrRloc is " + ((itrRloc == null) ? "MISSING" : itrRloc.toString()));
-        if (request.isPitr()) {
-            if (request.getEidRecord().size() > 0) {
-                EidRecord eid = request.getEidRecord().get(0);
-                Object result = getLocatorsSpecific(eid, ADDRESS_SUBKEY);
-                if (result != null && result instanceof MappingServiceRLOCGroup) {
-                    MappingServiceRLOCGroup locatorsGroup = (MappingServiceRLOCGroup) result;
-                    if (locatorsGroup != null && locatorsGroup.getRecords().size() > 0) {
-                        callback.handleNonProxyMapRequest(request,
-                                LispNotificationHelper.getTransportAddressFromContainer(locatorsGroup.getRecords().get(0).getLispAddressContainer()));
+        Eid srcEid = null;
+        if (request.getSourceEid() != null) {
+            srcEid = request.getSourceEid().getEid();
+        }
+        MapReplyBuilder replyBuilder = new MapReplyBuilder();
+        replyBuilder.setEchoNonceEnabled(false);
+        replyBuilder.setProbe(false);
+        replyBuilder.setSecurityEnabled(false);
+        replyBuilder.setNonce(request.getNonce());
+        replyBuilder.setMappingRecordItem(new ArrayList<>());
+        List<ItrRloc> itrRlocs = request.getItrRloc();
+        final IpAddressBinary sourceRloc = request.getSourceRloc();
+
+        for (EidItem eidRecord : request.getEidItem()) {
+            MappingRecord mapping = (MappingRecord) mapService.getMapping(srcEid,
+                    eidRecord.getEid());
+            if (mapping != null) {
+                if (itrRlocs != null && itrRlocs.size() != 0) {
+                    if (subscriptionService) {
+                        final Rloc resolvedRloc = resolveRloc(itrRlocs, sourceRloc);
+                        updateSubscribers(resolvedRloc, eidRecord.getEid(), mapping.getEid(), srcEid);
                     }
+                    mapping = updateLocators(mapping, itrRlocs);
                 }
+                mapping = fixIfNotSDRequest(mapping, eidRecord.getEid());
+            } else {
+                mapping = getNegativeMapping(eidRecord.getEid());
+            }
+            replyBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(mapping).build());
+        }
+        requestHandler.handleMapReply(replyBuilder.build());
+    }
+
+    private static boolean isEqualIpVersion(IpAddressBinary srcRloc, Rloc rloc) {
+        if (srcRloc.getIpv4AddressBinary() != null) {
+            if (rloc.getAddressType() == Ipv4Afi.class
+                    || rloc.getAddressType() == Ipv4BinaryAfi.class
+                    || rloc.getAddressType() == Ipv4PrefixAfi.class
+                    || rloc.getAddressType() == Ipv4PrefixBinaryAfi.class) {
+                return true;
             }
+        } else if (rloc.getAddressType() == Ipv6Afi.class
+                || rloc.getAddressType() == Ipv6BinaryAfi.class
+                || rloc.getAddressType() == Ipv6PrefixAfi.class
+                || rloc.getAddressType() == Ipv6PrefixBinaryAfi.class) {
+            return true;
+        }
+        return false;
+    }
 
+    private Rloc resolveRloc(List<ItrRloc> itrRlocList, IpAddressBinary srcRloc) {
+        if (srcRloc == null) {
+            return itrRlocList.get(0).getRloc();
+        }
+        byte[] srcRlocByte;
+        if (srcRloc.getIpv4AddressBinary() != null) {
+            srcRlocByte = srcRloc.getIpv4AddressBinary().getValue();
         } else {
-            MapReplyBuilder builder = new MapReplyBuilder();
-            builder.setEchoNonceEnabled(false);
-            builder.setProbe(false);
-            builder.setSecurityEnabled(false);
-            builder.setNonce(request.getNonce());
-            builder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
-            for (EidRecord eid : request.getEidRecord()) {
-                EidToLocatorRecordBuilder recordBuilder = new EidToLocatorRecordBuilder();
-                recordBuilder.setRecordTtl(0);
-                recordBuilder.setAction(Action.NoAction);
-                recordBuilder.setAuthoritative(false);
-                recordBuilder.setMapVersion((short) 0);
-                recordBuilder.setMaskLength(eid.getMask());
-                recordBuilder.setLispAddressContainer(eid.getLispAddressContainer());
-                recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
-                List<MappingServiceRLOCGroup> locators = getLocators(eid);
-                if (locators != null && locators.size() > 0) {
-                    addLocatorGroups(recordBuilder, locators);
-                    if (itrRloc != null) {
-                        MappingServiceSubscriberRLOC subscriberRloc = new MappingServiceSubscriberRLOC(itrRloc);
-                        HashSet<MappingServiceSubscriberRLOC> subscribers = getSubscribers(eid.getLispAddressContainer(), eid.getMask());
-                        if (subscribers == null) {
-                            subscribers = new HashSet<MappingServiceSubscriberRLOC>();
-                        } else if (subscribers.contains(subscriberRloc)) {
-                            /* If there is an entry already for this subscriberRloc, remove it, so that it gets the new timestamp */
-                            subscribers.remove(subscriberRloc);
-                        }
-                        IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
-                        subscribers.add(subscriberRloc);
-                        dao.put(key, new MappingEntry<HashSet<MappingServiceSubscriberRLOC>>(SUBSCRIBERS_SUBKEY, subscribers));
-                    }
-                } else {
-                    recordBuilder
-                            .setAction(org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord.Action.NativelyForward);
-                    if (shouldAuthenticate() && getPassword(eid.getLispAddressContainer(), eid.getMask()) != null) {
-                        recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
-                    } else {
-                        recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
+            srcRlocByte = srcRloc.getIpv6AddressBinary().getValue();
+        }
 
-                    }
-                }
-                builder.getEidToLocatorRecord().add(recordBuilder.build());
-            }
+        Rloc equalIpvRloc = null;
+        for (ItrRloc itrRloc : itrRlocList) {
+            final Rloc rloc = itrRloc.getRloc();
+            final byte[] itrRlocByte = LispAddressUtil.ipAddressToByteArray(rloc.getAddress());
 
-            callback.handleMapReply(builder.build());
+            // return an Rloc equal to the source Rloc
+            if (itrRlocByte != null && LispAddressUtil.compareIpAddressByteArrays(srcRlocByte, itrRlocByte) == 0) {
+                return rloc;
+            }
+            // else lookup the first Rloc with identical Ip version
+            if (equalIpvRloc == null && isEqualIpVersion(srcRloc, rloc)) {
+                equalIpvRloc = rloc;
+            }
+        }
+        if (equalIpvRloc != null) {
+            return equalIpvRloc;
+        } else {
+            // if none of the above, return the first Rloc
+            return itrRlocList.get(0).getRloc();
         }
     }
 
-    private List<MappingServiceRLOCGroup> getLocators(EidRecord eid) {
-        IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
-        Map<String, ?> locators = dao.get(key);
-        List<MappingServiceRLOCGroup> result = aggregateLocators(locators);
-        if (shouldIterateMask() && result.isEmpty() && MaskUtil.isMaskable(key.getEID().getAddress())) {
-            result = findMaskLocators(key);
+    private MappingRecord getNegativeMapping(Eid eid) {
+        MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
+        recordBuilder.setAuthoritative(false);
+        recordBuilder.setMapVersion((short) 0);
+        if (eid.getAddressType().equals(Ipv4PrefixBinaryAfi.class)
+                || eid.getAddressType().equals(Ipv6PrefixBinaryAfi.class)) {
+            recordBuilder.setEid(mapService.getWidestNegativePrefix(eid));
+        } else {
+            recordBuilder.setEid(eid);
         }
-        return result;
+        recordBuilder.setAction(Action.NativelyForward);
+        if (authenticate && mapService.getAuthenticationKey(eid) != null) {
+            recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
+        } else {
+            recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
+        }
+        return recordBuilder.build();
     }
 
-    private List<MappingServiceRLOCGroup> aggregateLocators(Map<String, ?> locators) {
-        List<MappingServiceRLOCGroup> result = new ArrayList<MappingServiceRLOCGroup>();
-        if (locators != null) {
-            for (Object value : locators.values()) {
-                if (value != null && value instanceof MappingServiceRLOCGroup) {
-                    if (!((MappingServiceRLOCGroup) value).getRecords().isEmpty()) {
-                        result.add((MappingServiceRLOCGroup) value);
-                    }
-                }
-            }
+    private void updateSubscribers(Rloc itrRloc, Eid reqEid, Eid mapEid, Eid srcEid) {
+        SubscriberRLOC subscriberRloc = new SubscriberRLOC(itrRloc, srcEid);
+        Eid subscribedEid = mapEid;
+
+        // If the eid in the matched mapping is SourceDest and the requested eid IS NOT then we subscribe itrRloc only
+        // to dst from the src/dst since that what's been requested. Note though that any updates to to the src/dst
+        // mapping will be pushed to dst as well (see sendSMRs in MapServer)
+        if (mapEid.getAddressType().equals(SourceDestKeyLcaf.class)
+                && !reqEid.getAddressType().equals(SourceDestKeyLcaf.class)) {
+            subscribedEid = SourceDestKeyHelper.getDstBinary(mapEid);
         }
-        return result;
-    }
 
-    private Object getLocatorsSpecific(EidRecord eid, String subkey) {
-        IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
-        Object locators = dao.getSpecific(key, subkey);
-        if (shouldIterateMask() && locators == null && MaskUtil.isMaskable(key.getEID().getAddress())) {
-            locators = findMaskLocatorsSpecific(key, subkey);
+        Set<SubscriberRLOC> subscribers = getSubscribers(subscribedEid);
+        if (subscribers == null) {
+            subscribers = Sets.newConcurrentHashSet();
+        } else if (subscribers.contains(subscriberRloc)) {
+            // If there is an entry already for this subscriberRloc, remove it, so that it gets the new
+            // timestamp
+            subscribers.remove(subscriberRloc);
+        }
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Adding new subscriber: " + LispAddressStringifier.getString(subscriberRloc.getSrcRloc()));
         }
-        return locators;
+        subscribers.add(subscriberRloc);
+        addSubscribers(subscribedEid, subscribers);
     }
 
-    private Object findMaskLocatorsSpecific(IMappingServiceKey key, String subkey) {
-        int mask = key.getMask();
-        while (mask > 0) {
-            key = MappingServiceKeyUtil.generateMappingServiceKey(
-                    new LispAddressContainerBuilder().setAddress(MaskUtil.normalize(key.getEID().getAddress(), mask)).build(), mask);
-            mask--;
-            Object locators = dao.getSpecific(key, subkey);
-            if (locators != null) {
-                return locators;
-            }
+    // Fixes mapping if request was for simple dst EID but the matched mapping is a SourceDest
+    private MappingRecord fixIfNotSDRequest(MappingRecord mapping, Eid dstEid) {
+        if (mapping.getEid().getAddress() instanceof SourceDestKey
+                && !(dstEid.getAddress() instanceof SourceDestKey)) {
+            return new MappingRecordBuilder(mapping).setEid(
+                    SourceDestKeyHelper.getDstBinary(mapping.getEid())).build();
         }
-        return null;
+        return mapping;
     }
 
-    private List<MappingServiceRLOCGroup> findMaskLocators(IMappingServiceKey key) {
-        int mask = key.getMask();
-        while (mask > 0) {
-            key = MappingServiceKeyUtil.generateMappingServiceKey(
-                    new LispAddressContainerBuilder().setAddress(MaskUtil.normalize(key.getEID().getAddress(), mask)).build(), mask);
-            mask--;
-            Map<String, ?> locators = dao.get(key);
-            if (locators != null) {
-                List<MappingServiceRLOCGroup> result = aggregateLocators(locators);
-                if (result != null && !result.isEmpty()) {
-                    return result;
-                }
+    private boolean locatorsNeedFixing(List<LocatorRecord> locatorRecords) {
+        for (LocatorRecord record : locatorRecords) {
+            if (record.getRloc().getAddress() instanceof ExplicitLocatorPath) {
+                return true;
             }
         }
-        return null;
+        return false;
     }
 
-    private void addLocatorGroups(EidToLocatorRecordBuilder recordBuilder, List<MappingServiceRLOCGroup> rlocs) {
-        for (MappingServiceRLOCGroup rloc : rlocs) {
-            addLocators(recordBuilder, rloc);
-            recordBuilder.setRecordTtl(rloc.getTtl());
+    // Process locators according to configured policy
+    private MappingRecord updateLocators(MappingRecord mapping, List<ItrRloc> itrRlocs) {
+        // no fixing if elpPolicy is default
+        if (elpPolicy.equalsIgnoreCase("default")) {
+            return mapping;
         }
-    }
 
-    private void addLocators(EidToLocatorRecordBuilder recordBuilder, MappingServiceRLOCGroup locatorObject) {
-        if (locatorObject == null) {
-            return;
+        List<LocatorRecord> locatorRecords = mapping.getLocatorRecord();
+
+        // if no updated is needed, just return the mapping
+        if (!locatorsNeedFixing(locatorRecords)) {
+            return mapping;
         }
+
+        MappingRecordBuilder recordBuilder = new MappingRecordBuilder(mapping);
+        recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
         try {
-            for (LocatorRecord record : locatorObject.getRecords()) {
-                recordBuilder.getLocatorRecord().add(
-                        new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator()).setRlocProbed(record.isRlocProbed())
-                                .setWeight(record.getWeight()).setPriority(record.getPriority()).setMulticastWeight(record.getMulticastWeight())
-                                .setMulticastPriority(record.getMulticastPriority()).setRouted(true)
-                                .setLispAddressContainer(record.getLispAddressContainer()).build());
+            for (LocatorRecord record : locatorRecords) {
+                Rloc container = record.getRloc();
+
+                // For non-ELP RLOCs, or when ELP policy is default, or itrRlocs is null, just add the locator and be
+                // done
+                if ((!(container.getAddress() instanceof ExplicitLocatorPath))
+                        || elpPolicy.equalsIgnoreCase("default") || itrRlocs == null) {
+                    recordBuilder.getLocatorRecord().add(
+                            new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
+                                    .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
+                                    .setPriority(record.getPriority()).setMulticastWeight(record.getMulticastWeight())
+                                    .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
+                                    .setRloc(container).setLocatorId(record.getLocatorId()).build());
+                    continue;
+                }
+
+                ExplicitLocatorPath teAddress = ((ExplicitLocatorPath) container.getAddress());
+                SimpleAddress nextHop = getNextELPHop(teAddress, itrRlocs);
+                if (nextHop != null) {
+                    java.lang.Short priority = record.getPriority();
+                    if (elpPolicy.equalsIgnoreCase("both")) {
+                        recordBuilder.getLocatorRecord().add(
+                                new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
+                                        .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
+                                        .setPriority(record.getPriority())
+                                        .setMulticastWeight(record.getMulticastWeight())
+                                        .setMulticastPriority(record.getMulticastPriority())
+                                        .setRouted(record.isRouted()).setRloc(container)
+                                        .setLocatorId(record.getLocatorId()).build());
+                        // Make the priority of the added simple locator lower so that ELP is used by default if
+                        // the xTR understands ELP. Exclude 255, since that means don't use for unicast forwarding
+                        // XXX Complex cases like several ELPs with different priorities are not handled
+                        if (priority != 254 || priority != 255) {
+                            priority++;
+                        }
+                    }
+                    // Build and add the simple RLOC
+                    recordBuilder.getLocatorRecord().add(
+                            new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
+                                    .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
+                                    .setPriority(priority).setMulticastWeight(record.getMulticastWeight())
+                                    .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
+                                    .setRloc(LispAddressUtil.toRloc(nextHop))
+                                    .setLocatorId(record.getLocatorId()).build());
+                }
             }
-            recordBuilder.setAction(locatorObject.getAction());
-            recordBuilder.setAuthoritative(locatorObject.isAuthoritative());
-            recordBuilder.setRecordTtl(locatorObject.getTtl());
         } catch (ClassCastException cce) {
+            LOG.error("Class Cast Exception while building EidToLocatorRecord: {}", ExceptionUtils.getStackTrace(cce));
         }
+
+        return recordBuilder.build();
     }
 
+    private SimpleAddress getNextELPHop(ExplicitLocatorPath elp, List<ItrRloc> itrRlocs) {
+        SimpleAddress nextHop = null;
+        List<Hop> hops = elp.getExplicitLocatorPath().getHop();
+
+        if (hops != null && hops.size() > 0) {
+            // By default we return the first hop
+            nextHop = hops.get(0).getAddress();
+            for (Hop hop : hops) {
+                Address hopAddress = LispAddressUtil.addressFromSimpleAddress(hop.getAddress());
+                for (ItrRloc itrRloc : itrRlocs) {
+                    if (itrRloc.getRloc().getAddress().equals(hopAddress)) {
+                        int iterator = hops.indexOf(hop);
+                        if (iterator < hops.size() - 1) {
+                            nextHop = hops.get(iterator + 1).getAddress();
+                            return nextHop;
+                        }
+                    }
+                }
+            }
+        }
+
+        return nextHop;
+    }
+
+    @SuppressWarnings("unchecked")
+    private Set<SubscriberRLOC> getSubscribers(Eid address) {
+        return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
+    }
+
+    private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
+        mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
+    }
+
+    @Override
+    public void setSubscriptionService(boolean smr) {
+        subscriptionService = smr;
+    }
+
+    @Override
+    public void setElpPolicy(String elpPolicy) {
+        this.elpPolicy = elpPolicy;
+    }
+
+    @Override
+    public void setShouldAuthenticate(boolean shouldAuthenticate) {
+        this.authenticate = shouldAuthenticate;
+    }
 }