2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.lispflowmapping.implementation.lisp;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Sets;
13 import java.util.ArrayList;
14 import java.util.List;
16 import org.apache.commons.lang3.exception.ExceptionUtils;
17 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
18 import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
19 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
20 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
21 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
22 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
23 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
24 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
34 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;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6BinaryAfi;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6PrefixBinaryAfi;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class MapResolver implements IMapResolverAsync {
57 protected static final Logger LOG = LoggerFactory.getLogger(MapResolver.class);
59 private static final int TTL_RLOC_TIMED_OUT = 1;
60 private static final int TTL_NO_RLOC_KNOWN = 15;
62 private IMappingService mapService;
63 private boolean subscriptionService;
64 private String elpPolicy;
65 private IMapRequestResultHandler requestHandler;
66 private boolean authenticate = true;
68 public MapResolver(IMappingService mapService, boolean smr, String elpPolicy,
69 IMapRequestResultHandler requestHandler) {
70 Preconditions.checkNotNull(mapService);
71 this.subscriptionService = smr;
72 this.mapService = mapService;
73 this.elpPolicy = elpPolicy;
74 this.requestHandler = requestHandler;
77 public void handleMapRequest(MapRequest request) {
78 // SMRs and RLOC probes are directed towards xTRs and we're a Map-Resolver here, so ignore them
79 if (request.isSmr() != null && request.isSmr()) {
80 LOG.debug("Map-Resolver ignoring incoming SMR control message.");
83 if (request.isProbe() != null && request.isProbe()) {
84 LOG.debug("Map-Resolver ignoring incoming RLOC probe control message.");
88 if (request.getSourceEid() != null) {
89 srcEid = request.getSourceEid().getEid();
91 MapReplyBuilder replyBuilder = new MapReplyBuilder();
92 replyBuilder.setEchoNonceEnabled(false);
93 replyBuilder.setProbe(false);
94 replyBuilder.setSecurityEnabled(false);
95 replyBuilder.setNonce(request.getNonce());
96 replyBuilder.setMappingRecordItem(new ArrayList<>());
97 List<ItrRloc> itrRlocs = request.getItrRloc();
98 final IpAddressBinary sourceRloc = request.getSourceRloc();
100 for (EidItem eidRecord : request.getEidItem()) {
101 MappingRecord mapping = (MappingRecord) mapService.getMapping(srcEid,
103 if (mapping != null) {
104 if (itrRlocs != null && itrRlocs.size() != 0) {
105 if (subscriptionService) {
106 final Rloc resolvedRloc = resolveRloc(itrRlocs, sourceRloc);
107 updateSubscribers(resolvedRloc, eidRecord.getEid(), mapping.getEid(), srcEid);
109 mapping = updateLocators(mapping, itrRlocs);
111 mapping = fixIfNotSDRequest(mapping, eidRecord.getEid());
113 mapping = getNegativeMapping(eidRecord.getEid());
115 replyBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(mapping).build());
117 requestHandler.handleMapReply(replyBuilder.build());
120 private static boolean isEqualIpVersion(IpAddressBinary srcRloc, Rloc rloc) {
121 if (srcRloc.getIpv4AddressBinary() != null) {
122 if (rloc.getAddressType() == Ipv4Afi.class
123 || rloc.getAddressType() == Ipv4BinaryAfi.class
124 || rloc.getAddressType() == Ipv4PrefixAfi.class
125 || rloc.getAddressType() == Ipv4PrefixBinaryAfi.class) {
128 } else if (rloc.getAddressType() == Ipv6Afi.class
129 || rloc.getAddressType() == Ipv6BinaryAfi.class
130 || rloc.getAddressType() == Ipv6PrefixAfi.class
131 || rloc.getAddressType() == Ipv6PrefixBinaryAfi.class) {
137 private Rloc resolveRloc(List<ItrRloc> itrRlocList, IpAddressBinary srcRloc) {
138 if (srcRloc == null) {
139 return itrRlocList.get(0).getRloc();
142 if (srcRloc.getIpv4AddressBinary() != null) {
143 srcRlocByte = srcRloc.getIpv4AddressBinary().getValue();
145 srcRlocByte = srcRloc.getIpv6AddressBinary().getValue();
148 Rloc equalIpvRloc = null;
149 for (ItrRloc itrRloc : itrRlocList) {
150 final Rloc rloc = itrRloc.getRloc();
151 final byte[] itrRlocByte = LispAddressUtil.ipAddressToByteArray(rloc.getAddress());
153 // return an Rloc equal to the source Rloc
154 if (itrRlocByte != null && LispAddressUtil.compareIpAddressByteArrays(srcRlocByte, itrRlocByte) == 0) {
157 // else lookup the first Rloc with identical Ip version
158 if (equalIpvRloc == null && isEqualIpVersion(srcRloc, rloc)) {
162 if (equalIpvRloc != null) {
165 // if none of the above, return the first Rloc
166 return itrRlocList.get(0).getRloc();
170 private MappingRecord getNegativeMapping(Eid eid) {
171 MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
172 recordBuilder.setAuthoritative(false);
173 recordBuilder.setMapVersion((short) 0);
174 if (eid.getAddressType().equals(Ipv4PrefixBinaryAfi.class)
175 || eid.getAddressType().equals(Ipv6PrefixBinaryAfi.class)) {
176 recordBuilder.setEid(mapService.getWidestNegativePrefix(eid));
178 recordBuilder.setEid(eid);
180 recordBuilder.setAction(Action.NativelyForward);
181 if (authenticate && mapService.getAuthenticationKey(eid) != null) {
182 recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
184 recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
186 return recordBuilder.build();
189 private void updateSubscribers(Rloc itrRloc, Eid reqEid, Eid mapEid, Eid srcEid) {
190 SubscriberRLOC subscriberRloc = new SubscriberRLOC(itrRloc, srcEid);
191 Eid subscribedEid = mapEid;
193 // If the eid in the matched mapping is SourceDest and the requested eid IS NOT then we subscribe itrRloc only
194 // to dst from the src/dst since that what's been requested. Note though that any updates to to the src/dst
195 // mapping will be pushed to dst as well (see sendSMRs in MapServer)
196 if (mapEid.getAddressType().equals(SourceDestKeyLcaf.class)
197 && !reqEid.getAddressType().equals(SourceDestKeyLcaf.class)) {
198 subscribedEid = SourceDestKeyHelper.getDstBinary(mapEid);
201 Set<SubscriberRLOC> subscribers = getSubscribers(subscribedEid);
202 if (subscribers == null) {
203 subscribers = Sets.newConcurrentHashSet();
204 } else if (subscribers.contains(subscriberRloc)) {
205 // If there is an entry already for this subscriberRloc, remove it, so that it gets the new
207 subscribers.remove(subscriberRloc);
209 if (LOG.isTraceEnabled()) {
210 LOG.trace("Adding new subscriber: " + LispAddressStringifier.getString(subscriberRloc.getSrcRloc()));
212 subscribers.add(subscriberRloc);
213 addSubscribers(subscribedEid, subscribers);
216 // Fixes mapping if request was for simple dst EID but the matched mapping is a SourceDest
217 private MappingRecord fixIfNotSDRequest(MappingRecord mapping, Eid dstEid) {
218 if (mapping.getEid().getAddress() instanceof SourceDestKey
219 && !(dstEid.getAddress() instanceof SourceDestKey)) {
220 return new MappingRecordBuilder(mapping).setEid(
221 SourceDestKeyHelper.getDstBinary(mapping.getEid())).build();
226 private boolean locatorsNeedFixing(List<LocatorRecord> locatorRecords) {
227 for (LocatorRecord record : locatorRecords) {
228 if (record.getRloc().getAddress() instanceof ExplicitLocatorPath) {
235 // Process locators according to configured policy
236 private MappingRecord updateLocators(MappingRecord mapping, List<ItrRloc> itrRlocs) {
237 // no fixing if elpPolicy is default
238 if (elpPolicy.equalsIgnoreCase("default")) {
242 List<LocatorRecord> locatorRecords = mapping.getLocatorRecord();
244 // if no updated is needed, just return the mapping
245 if (!locatorsNeedFixing(locatorRecords)) {
249 MappingRecordBuilder recordBuilder = new MappingRecordBuilder(mapping);
250 recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
252 for (LocatorRecord record : locatorRecords) {
253 Rloc container = record.getRloc();
255 // For non-ELP RLOCs, or when ELP policy is default, or itrRlocs is null, just add the locator and be
257 if ((!(container.getAddress() instanceof ExplicitLocatorPath))
258 || elpPolicy.equalsIgnoreCase("default") || itrRlocs == null) {
259 recordBuilder.getLocatorRecord().add(
260 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
261 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
262 .setPriority(record.getPriority()).setMulticastWeight(record.getMulticastWeight())
263 .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
264 .setRloc(container).setLocatorId(record.getLocatorId()).build());
268 ExplicitLocatorPath teAddress = ((ExplicitLocatorPath) container.getAddress());
269 SimpleAddress nextHop = getNextELPHop(teAddress, itrRlocs);
270 if (nextHop != null) {
271 java.lang.Short priority = record.getPriority();
272 if (elpPolicy.equalsIgnoreCase("both")) {
273 recordBuilder.getLocatorRecord().add(
274 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
275 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
276 .setPriority(record.getPriority())
277 .setMulticastWeight(record.getMulticastWeight())
278 .setMulticastPriority(record.getMulticastPriority())
279 .setRouted(record.isRouted()).setRloc(container)
280 .setLocatorId(record.getLocatorId()).build());
281 // Make the priority of the added simple locator lower so that ELP is used by default if
282 // the xTR understands ELP. Exclude 255, since that means don't use for unicast forwarding
283 // XXX Complex cases like several ELPs with different priorities are not handled
284 if (priority != 254 || priority != 255) {
288 // Build and add the simple RLOC
289 recordBuilder.getLocatorRecord().add(
290 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
291 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
292 .setPriority(priority).setMulticastWeight(record.getMulticastWeight())
293 .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
294 .setRloc(LispAddressUtil.toRloc(nextHop))
295 .setLocatorId(record.getLocatorId()).build());
298 } catch (ClassCastException cce) {
299 LOG.error("Class Cast Exception while building EidToLocatorRecord: {}", ExceptionUtils.getStackTrace(cce));
302 return recordBuilder.build();
305 private SimpleAddress getNextELPHop(ExplicitLocatorPath elp, List<ItrRloc> itrRlocs) {
306 SimpleAddress nextHop = null;
307 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
309 if (hops != null && hops.size() > 0) {
310 // By default we return the first hop
311 nextHop = hops.get(0).getAddress();
312 for (Hop hop : hops) {
313 Address hopAddress = LispAddressUtil.addressFromSimpleAddress(hop.getAddress());
314 for (ItrRloc itrRloc : itrRlocs) {
315 if (itrRloc.getRloc().getAddress().equals(hopAddress)) {
316 int iterator = hops.indexOf(hop);
317 if (iterator < hops.size() - 1) {
318 nextHop = hops.get(iterator + 1).getAddress();
329 @SuppressWarnings("unchecked")
330 private Set<SubscriberRLOC> getSubscribers(Eid address) {
331 return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
334 private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
335 mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
339 public void setSubscriptionService(boolean smr) {
340 subscriptionService = smr;
344 public void setElpPolicy(String elpPolicy) {
345 this.elpPolicy = elpPolicy;
349 public void setShouldAuthenticate(boolean shouldAuthenticate) {
350 this.authenticate = shouldAuthenticate;