2 * Copyright (c) 2014, 2017 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.Subscriber;
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.MappingRecordBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
58 public class MapResolver implements IMapResolverAsync {
59 protected static final Logger LOG = LoggerFactory.getLogger(MapResolver.class);
61 private IMappingService mapService;
62 private boolean subscriptionService;
63 private String elpPolicy;
64 private IMapRequestResultHandler requestHandler;
65 private boolean authenticate = true;
66 private ISmrNotificationListener smrNotificationListener;
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.");
87 if (request.isSmrInvoked()) {
88 LOG.debug("SMR-invoked request received.");
89 for (EidItem eidItem : request.getEidItem()) {
90 final SmrEvent event = new SmrEvent(LispAddressUtil.addressBinariesFromItrRlocs(request.getItrRloc()),
91 eidItem.getEid(), request.getNonce());
92 smrNotificationListener.onSmrInvokedReceived(event);
96 if (request.getSourceEid() != null) {
97 srcEid = request.getSourceEid().getEid();
99 MapReplyBuilder replyBuilder = new MapReplyBuilder();
100 replyBuilder.setEchoNonceEnabled(false);
101 replyBuilder.setProbe(false);
102 replyBuilder.setSecurityEnabled(false);
103 replyBuilder.setNonce(request.getNonce());
104 replyBuilder.setMappingRecordItem(new ArrayList<>());
105 List<ItrRloc> itrRlocs = request.getItrRloc();
106 final IpAddressBinary sourceRloc = request.getSourceRloc();
108 for (EidItem eidRecord : request.getEidItem()) {
109 MappingData mappingData = mapService.getMapping(srcEid, eidRecord.getEid());
110 MappingRecord mapping;
111 if (mappingData == null) {
112 mapping = mapService.addNegativeMapping(eidRecord.getEid()).getRecord();
114 mapping = mappingData.getRecord();
117 if (itrRlocs != null && itrRlocs.size() != 0) {
118 if (subscriptionService) {
119 final Rloc resolvedRloc = resolveRloc(itrRlocs, sourceRloc);
120 updateSubscribers(resolvedRloc, eidRecord.getEid(), mapping.getEid(),
121 srcEid, mapping.getRecordTtl());
123 mapping = updateLocators(mapping, itrRlocs);
125 mapping = fixIfNotSDRequest(mapping, eidRecord.getEid());
126 replyBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(mapping).build());
128 requestHandler.handleMapReply(replyBuilder.build());
131 private static boolean isEqualIpVersion(IpAddressBinary srcRloc, Rloc rloc) {
132 if (srcRloc.getIpv4AddressBinary() != null) {
133 if (rloc.getAddressType() == Ipv4Afi.class
134 || rloc.getAddressType() == Ipv4BinaryAfi.class
135 || rloc.getAddressType() == Ipv4PrefixAfi.class
136 || rloc.getAddressType() == Ipv4PrefixBinaryAfi.class) {
139 } else if (rloc.getAddressType() == Ipv6Afi.class
140 || rloc.getAddressType() == Ipv6BinaryAfi.class
141 || rloc.getAddressType() == Ipv6PrefixAfi.class
142 || rloc.getAddressType() == Ipv6PrefixBinaryAfi.class) {
148 private Rloc resolveRloc(List<ItrRloc> itrRlocList, IpAddressBinary srcRloc) {
149 if (srcRloc == null) {
150 return itrRlocList.get(0).getRloc();
153 if (srcRloc.getIpv4AddressBinary() != null) {
154 srcRlocByte = srcRloc.getIpv4AddressBinary().getValue();
156 srcRlocByte = srcRloc.getIpv6AddressBinary().getValue();
159 Rloc equalIpvRloc = null;
160 for (ItrRloc itrRloc : itrRlocList) {
161 final Rloc rloc = itrRloc.getRloc();
162 final byte[] itrRlocByte = LispAddressUtil.ipAddressToByteArray(rloc.getAddress());
164 // return an Rloc equal to the source Rloc
165 if (itrRlocByte != null && LispAddressUtil.compareIpAddressByteArrays(srcRlocByte, itrRlocByte) == 0) {
168 // else lookup the first Rloc with identical Ip version
169 if (equalIpvRloc == null && isEqualIpVersion(srcRloc, rloc)) {
173 if (equalIpvRloc != null) {
176 // if none of the above, return the first Rloc
177 return itrRlocList.get(0).getRloc();
181 private void updateSubscribers(Rloc itrRloc, Eid reqEid, Eid mapEid, Eid srcEid, Integer recordTtl) {
182 Subscriber subscriber = new Subscriber(itrRloc, srcEid, Subscriber.recordTtlToSubscriberTime(recordTtl));
183 Eid subscribedEid = mapEid;
185 // If the eid in the matched mapping is SourceDest and the requested eid IS NOT then we subscribe itrRloc only
186 // to dst from the src/dst since that what's been requested. Note though that any updates to to the src/dst
187 // mapping will be pushed to dst as well (see sendSMRs in MapServer)
188 if (mapEid.getAddressType().equals(SourceDestKeyLcaf.class)
189 && !reqEid.getAddressType().equals(SourceDestKeyLcaf.class)) {
190 subscribedEid = SourceDestKeyHelper.getDstBinary(mapEid);
193 Set<Subscriber> subscribers = getSubscribers(subscribedEid);
194 if (subscribers == null) {
195 subscribers = Sets.newConcurrentHashSet();
196 } else if (subscribers.contains(subscriber)) {
197 // If there is an entry already for this subscriber, remove it, so that it gets the new timestamp
198 subscribers.remove(subscriber);
200 if (LOG.isTraceEnabled()) {
201 LOG.trace("Adding new subscriber: " + LispAddressStringifier.getString(subscriber.getSrcRloc()));
203 subscribers.add(subscriber);
204 addSubscribers(subscribedEid, subscribers);
207 // Fixes mapping if request was for simple dst EID but the matched mapping is a SourceDest
208 private MappingRecord fixIfNotSDRequest(MappingRecord mapping, Eid dstEid) {
209 if (mapping.getEid().getAddress() instanceof SourceDestKey
210 && !(dstEid.getAddress() instanceof SourceDestKey)) {
211 return new MappingRecordBuilder(mapping).setEid(
212 SourceDestKeyHelper.getDstBinary(mapping.getEid())).build();
217 private boolean locatorsNeedFixing(List<LocatorRecord> locatorRecords) {
218 // no locators - no fixing needed ;)
219 if (locatorRecords == null) {
223 for (LocatorRecord record : locatorRecords) {
224 if (record.getRloc().getAddress() instanceof ExplicitLocatorPath) {
231 // Process locators according to configured policy
232 private MappingRecord updateLocators(MappingRecord mapping, List<ItrRloc> itrRlocs) {
233 // no fixing if elpPolicy is default
234 if (elpPolicy.equalsIgnoreCase("default")) {
238 List<LocatorRecord> locatorRecords = mapping.getLocatorRecord();
240 // if no updated is needed, just return the mapping
241 if (!locatorsNeedFixing(locatorRecords)) {
245 MappingRecordBuilder recordBuilder = new MappingRecordBuilder(mapping);
246 recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
248 for (LocatorRecord record : locatorRecords) {
249 Rloc container = record.getRloc();
251 // For non-ELP RLOCs, or when ELP policy is default, or itrRlocs is null, just add the locator and be
253 if ((!(container.getAddress() instanceof ExplicitLocatorPath))
254 || elpPolicy.equalsIgnoreCase("default") || itrRlocs == null) {
255 recordBuilder.getLocatorRecord().add(
256 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
257 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
258 .setPriority(record.getPriority()).setMulticastWeight(record.getMulticastWeight())
259 .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
260 .setRloc(container).setLocatorId(record.getLocatorId()).build());
264 ExplicitLocatorPath teAddress = ((ExplicitLocatorPath) container.getAddress());
265 SimpleAddress nextHop = getNextELPHop(teAddress, itrRlocs);
266 if (nextHop != null) {
267 java.lang.Short priority = record.getPriority();
268 if (elpPolicy.equalsIgnoreCase("both")) {
269 recordBuilder.getLocatorRecord().add(
270 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
271 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
272 .setPriority(record.getPriority())
273 .setMulticastWeight(record.getMulticastWeight())
274 .setMulticastPriority(record.getMulticastPriority())
275 .setRouted(record.isRouted()).setRloc(container)
276 .setLocatorId(record.getLocatorId()).build());
277 // Make the priority of the added simple locator lower so that ELP is used by default if
278 // the xTR understands ELP. Exclude 255, since that means don't use for unicast forwarding
279 // XXX Complex cases like several ELPs with different priorities are not handled
280 if (priority != 254 || priority != 255) {
284 // Build and add the simple RLOC
285 recordBuilder.getLocatorRecord().add(
286 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
287 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
288 .setPriority(priority).setMulticastWeight(record.getMulticastWeight())
289 .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
290 .setRloc(LispAddressUtil.toRloc(nextHop))
291 .setLocatorId(record.getLocatorId()).build());
294 } catch (ClassCastException cce) {
295 LOG.error("Class Cast Exception while building EidToLocatorRecord: {}", ExceptionUtils.getStackTrace(cce));
298 return recordBuilder.build();
301 private SimpleAddress getNextELPHop(ExplicitLocatorPath elp, List<ItrRloc> itrRlocs) {
302 SimpleAddress nextHop = null;
303 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
305 if (hops != null && hops.size() > 0) {
306 // By default we return the first hop
307 nextHop = hops.get(0).getAddress();
308 for (Hop hop : hops) {
309 Address hopAddress = LispAddressUtil.addressFromSimpleAddress(hop.getAddress());
310 for (ItrRloc itrRloc : itrRlocs) {
311 if (itrRloc.getRloc().getAddress().equals(hopAddress)) {
312 int iterator = hops.indexOf(hop);
313 if (iterator < hops.size() - 1) {
314 nextHop = hops.get(iterator + 1).getAddress();
325 @SuppressWarnings("unchecked")
326 private Set<Subscriber> getSubscribers(Eid address) {
327 return (Set<Subscriber>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
330 private void addSubscribers(Eid address, Set<Subscriber> subscribers) {
331 mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
335 public void setSubscriptionService(boolean smr) {
336 subscriptionService = smr;
340 public void setElpPolicy(String elpPolicy) {
341 this.elpPolicy = elpPolicy;
345 public void setShouldAuthenticate(boolean shouldAuthenticate) {
346 this.authenticate = shouldAuthenticate;
350 public void setSmrNotificationListener(ISmrNotificationListener smrNotificationListener) {
351 this.smrNotificationListener = smrNotificationListener;