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.lisp.ISmrNotificationListener;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.SmrEvent;
23 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
24 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
25 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
26 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
27 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6Afi;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
37 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;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6BinaryAfi;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6PrefixBinaryAfi;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 public class MapResolver implements IMapResolverAsync {
60 protected static final Logger LOG = LoggerFactory.getLogger(MapResolver.class);
62 private static final int TTL_RLOC_TIMED_OUT = 1;
63 private static final int TTL_NO_RLOC_KNOWN = 15;
65 private IMappingService mapService;
66 private boolean subscriptionService;
67 private String elpPolicy;
68 private IMapRequestResultHandler requestHandler;
69 private boolean authenticate = true;
70 private ISmrNotificationListener smrNotificationListener;
72 public MapResolver(IMappingService mapService, boolean smr, String elpPolicy,
73 IMapRequestResultHandler requestHandler) {
74 Preconditions.checkNotNull(mapService);
75 this.subscriptionService = smr;
76 this.mapService = mapService;
77 this.elpPolicy = elpPolicy;
78 this.requestHandler = requestHandler;
81 public void handleMapRequest(MapRequest request) {
82 // SMRs and RLOC probes are directed towards xTRs and we're a Map-Resolver here, so ignore them
83 if (request.isSmr() != null && request.isSmr()) {
84 LOG.debug("Map-Resolver ignoring incoming SMR control message.");
87 if (request.isProbe() != null && request.isProbe()) {
88 LOG.debug("Map-Resolver ignoring incoming RLOC probe control message.");
91 if (request.isSmrInvoked()) {
92 LOG.debug("SMR-invoked request received.");
93 for (EidItem eidItem : request.getEidItem()) {
94 final SmrEvent event = new SmrEvent(LispAddressUtil.addressBinariesFromItrRlocs(request.getItrRloc()),
95 eidItem.getEid(), request.getNonce());
96 smrNotificationListener.onSmrInvokedReceived(event);
100 if (request.getSourceEid() != null) {
101 srcEid = request.getSourceEid().getEid();
103 MapReplyBuilder replyBuilder = new MapReplyBuilder();
104 replyBuilder.setEchoNonceEnabled(false);
105 replyBuilder.setProbe(false);
106 replyBuilder.setSecurityEnabled(false);
107 replyBuilder.setNonce(request.getNonce());
108 replyBuilder.setMappingRecordItem(new ArrayList<>());
109 List<ItrRloc> itrRlocs = request.getItrRloc();
110 final IpAddressBinary sourceRloc = request.getSourceRloc();
112 for (EidItem eidRecord : request.getEidItem()) {
113 MappingData mappingData = mapService.getMapping(srcEid, eidRecord.getEid());
114 MappingRecord mapping;
115 if (mappingData == null) {
116 mapping = getNegativeMapping(eidRecord.getEid());
117 mapService.addMapping(MappingOrigin.Southbound, mapping.getEid(), null, new MappingData(mapping));
119 mapping = mappingData.getRecord();
122 if (itrRlocs != null && itrRlocs.size() != 0) {
123 if (subscriptionService) {
124 final Rloc resolvedRloc = resolveRloc(itrRlocs, sourceRloc);
125 updateSubscribers(resolvedRloc, eidRecord.getEid(), mapping.getEid(),
126 srcEid, mapping.getRecordTtl());
128 mapping = updateLocators(mapping, itrRlocs);
130 mapping = fixIfNotSDRequest(mapping, eidRecord.getEid());
131 replyBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(mapping).build());
133 requestHandler.handleMapReply(replyBuilder.build());
136 private static boolean isEqualIpVersion(IpAddressBinary srcRloc, Rloc rloc) {
137 if (srcRloc.getIpv4AddressBinary() != null) {
138 if (rloc.getAddressType() == Ipv4Afi.class
139 || rloc.getAddressType() == Ipv4BinaryAfi.class
140 || rloc.getAddressType() == Ipv4PrefixAfi.class
141 || rloc.getAddressType() == Ipv4PrefixBinaryAfi.class) {
144 } else if (rloc.getAddressType() == Ipv6Afi.class
145 || rloc.getAddressType() == Ipv6BinaryAfi.class
146 || rloc.getAddressType() == Ipv6PrefixAfi.class
147 || rloc.getAddressType() == Ipv6PrefixBinaryAfi.class) {
153 private Rloc resolveRloc(List<ItrRloc> itrRlocList, IpAddressBinary srcRloc) {
154 if (srcRloc == null) {
155 return itrRlocList.get(0).getRloc();
158 if (srcRloc.getIpv4AddressBinary() != null) {
159 srcRlocByte = srcRloc.getIpv4AddressBinary().getValue();
161 srcRlocByte = srcRloc.getIpv6AddressBinary().getValue();
164 Rloc equalIpvRloc = null;
165 for (ItrRloc itrRloc : itrRlocList) {
166 final Rloc rloc = itrRloc.getRloc();
167 final byte[] itrRlocByte = LispAddressUtil.ipAddressToByteArray(rloc.getAddress());
169 // return an Rloc equal to the source Rloc
170 if (itrRlocByte != null && LispAddressUtil.compareIpAddressByteArrays(srcRlocByte, itrRlocByte) == 0) {
173 // else lookup the first Rloc with identical Ip version
174 if (equalIpvRloc == null && isEqualIpVersion(srcRloc, rloc)) {
178 if (equalIpvRloc != null) {
181 // if none of the above, return the first Rloc
182 return itrRlocList.get(0).getRloc();
186 private MappingRecord getNegativeMapping(Eid eid) {
187 MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
188 recordBuilder.setAuthoritative(false);
189 recordBuilder.setMapVersion((short) 0);
190 recordBuilder.setEid(eid);
191 if (eid.getAddressType().equals(Ipv4PrefixBinaryAfi.class)
192 || eid.getAddressType().equals(Ipv6PrefixBinaryAfi.class)) {
193 Eid widestNegativePrefix = mapService.getWidestNegativePrefix(eid);
194 if (widestNegativePrefix != null) {
195 recordBuilder.setEid(widestNegativePrefix);
198 recordBuilder.setAction(Action.NativelyForward);
199 if (authenticate && mapService.getAuthenticationKey(eid) != null) {
200 recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
202 recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
204 return recordBuilder.build();
207 private void updateSubscribers(Rloc itrRloc, Eid reqEid, Eid mapEid, Eid srcEid, Integer recordTtl) {
208 SubscriberRLOC subscriberRloc = new SubscriberRLOC(itrRloc, srcEid,
209 SubscriberRLOC.recordTtlToSubscriberTime(recordTtl));
210 Eid subscribedEid = mapEid;
212 // If the eid in the matched mapping is SourceDest and the requested eid IS NOT then we subscribe itrRloc only
213 // to dst from the src/dst since that what's been requested. Note though that any updates to to the src/dst
214 // mapping will be pushed to dst as well (see sendSMRs in MapServer)
215 if (mapEid.getAddressType().equals(SourceDestKeyLcaf.class)
216 && !reqEid.getAddressType().equals(SourceDestKeyLcaf.class)) {
217 subscribedEid = SourceDestKeyHelper.getDstBinary(mapEid);
220 Set<SubscriberRLOC> subscribers = getSubscribers(subscribedEid);
221 if (subscribers == null) {
222 subscribers = Sets.newConcurrentHashSet();
223 } else if (subscribers.contains(subscriberRloc)) {
224 // If there is an entry already for this subscriberRloc, remove it, so that it gets the new
226 subscribers.remove(subscriberRloc);
228 if (LOG.isTraceEnabled()) {
229 LOG.trace("Adding new subscriber: " + LispAddressStringifier.getString(subscriberRloc.getSrcRloc()));
231 subscribers.add(subscriberRloc);
232 addSubscribers(subscribedEid, subscribers);
235 // Fixes mapping if request was for simple dst EID but the matched mapping is a SourceDest
236 private MappingRecord fixIfNotSDRequest(MappingRecord mapping, Eid dstEid) {
237 if (mapping.getEid().getAddress() instanceof SourceDestKey
238 && !(dstEid.getAddress() instanceof SourceDestKey)) {
239 return new MappingRecordBuilder(mapping).setEid(
240 SourceDestKeyHelper.getDstBinary(mapping.getEid())).build();
245 private boolean locatorsNeedFixing(List<LocatorRecord> locatorRecords) {
246 // no locators - no fixing needed ;)
247 if (locatorRecords == null) {
251 for (LocatorRecord record : locatorRecords) {
252 if (record.getRloc().getAddress() instanceof ExplicitLocatorPath) {
259 // Process locators according to configured policy
260 private MappingRecord updateLocators(MappingRecord mapping, List<ItrRloc> itrRlocs) {
261 // no fixing if elpPolicy is default
262 if (elpPolicy.equalsIgnoreCase("default")) {
266 List<LocatorRecord> locatorRecords = mapping.getLocatorRecord();
268 // if no updated is needed, just return the mapping
269 if (!locatorsNeedFixing(locatorRecords)) {
273 MappingRecordBuilder recordBuilder = new MappingRecordBuilder(mapping);
274 recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
276 for (LocatorRecord record : locatorRecords) {
277 Rloc container = record.getRloc();
279 // For non-ELP RLOCs, or when ELP policy is default, or itrRlocs is null, just add the locator and be
281 if ((!(container.getAddress() instanceof ExplicitLocatorPath))
282 || elpPolicy.equalsIgnoreCase("default") || itrRlocs == null) {
283 recordBuilder.getLocatorRecord().add(
284 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
285 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
286 .setPriority(record.getPriority()).setMulticastWeight(record.getMulticastWeight())
287 .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
288 .setRloc(container).setLocatorId(record.getLocatorId()).build());
292 ExplicitLocatorPath teAddress = ((ExplicitLocatorPath) container.getAddress());
293 SimpleAddress nextHop = getNextELPHop(teAddress, itrRlocs);
294 if (nextHop != null) {
295 java.lang.Short priority = record.getPriority();
296 if (elpPolicy.equalsIgnoreCase("both")) {
297 recordBuilder.getLocatorRecord().add(
298 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
299 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
300 .setPriority(record.getPriority())
301 .setMulticastWeight(record.getMulticastWeight())
302 .setMulticastPriority(record.getMulticastPriority())
303 .setRouted(record.isRouted()).setRloc(container)
304 .setLocatorId(record.getLocatorId()).build());
305 // Make the priority of the added simple locator lower so that ELP is used by default if
306 // the xTR understands ELP. Exclude 255, since that means don't use for unicast forwarding
307 // XXX Complex cases like several ELPs with different priorities are not handled
308 if (priority != 254 || priority != 255) {
312 // Build and add the simple RLOC
313 recordBuilder.getLocatorRecord().add(
314 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
315 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
316 .setPriority(priority).setMulticastWeight(record.getMulticastWeight())
317 .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
318 .setRloc(LispAddressUtil.toRloc(nextHop))
319 .setLocatorId(record.getLocatorId()).build());
322 } catch (ClassCastException cce) {
323 LOG.error("Class Cast Exception while building EidToLocatorRecord: {}", ExceptionUtils.getStackTrace(cce));
326 return recordBuilder.build();
329 private SimpleAddress getNextELPHop(ExplicitLocatorPath elp, List<ItrRloc> itrRlocs) {
330 SimpleAddress nextHop = null;
331 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
333 if (hops != null && hops.size() > 0) {
334 // By default we return the first hop
335 nextHop = hops.get(0).getAddress();
336 for (Hop hop : hops) {
337 Address hopAddress = LispAddressUtil.addressFromSimpleAddress(hop.getAddress());
338 for (ItrRloc itrRloc : itrRlocs) {
339 if (itrRloc.getRloc().getAddress().equals(hopAddress)) {
340 int iterator = hops.indexOf(hop);
341 if (iterator < hops.size() - 1) {
342 nextHop = hops.get(iterator + 1).getAddress();
353 @SuppressWarnings("unchecked")
354 private Set<SubscriberRLOC> getSubscribers(Eid address) {
355 return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
358 private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
359 mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
363 public void setSubscriptionService(boolean smr) {
364 subscriptionService = smr;
368 public void setElpPolicy(String elpPolicy) {
369 this.elpPolicy = elpPolicy;
373 public void setShouldAuthenticate(boolean shouldAuthenticate) {
374 this.authenticate = shouldAuthenticate;
378 public void setSmrNotificationListener(ISmrNotificationListener smrNotificationListener) {
379 this.smrNotificationListener = smrNotificationListener;