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 java.util.ArrayList;
12 import java.util.List;
15 import org.apache.commons.lang3.exception.ExceptionUtils;
16 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
17 import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
18 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
19 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
20 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
21 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
22 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
23 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SourceDestKeyLcaf;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
29 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;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.ItrRloc;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import com.google.common.base.Preconditions;
48 import com.google.common.collect.Sets;
50 public class MapResolver implements IMapResolverAsync {
51 protected static final Logger LOG = LoggerFactory.getLogger(MapResolver.class);
53 private static final int TTL_RLOC_TIMED_OUT = 1;
54 private static final int TTL_NO_RLOC_KNOWN = 15;
56 private IMappingService mapService;
57 private boolean subscriptionService;
58 private String elpPolicy;
59 private IMapRequestResultHandler requestHandler;
60 private boolean authenticate = true;
62 public MapResolver(IMappingService mapService, boolean smr, String elpPolicy,
63 IMapRequestResultHandler requestHandler) {
64 Preconditions.checkNotNull(mapService);
65 this.subscriptionService = smr;
66 this.mapService = mapService;
67 this.elpPolicy = elpPolicy;
68 this.requestHandler = requestHandler;
71 public void handleMapRequest(MapRequest request) {
72 // SMRs and RLOC probes are directed towards xTRs and we're a Map-Resolver here, so ignore them
73 if (request.isSmr() != null && request.isSmr()) {
74 LOG.debug("Map-Resolver ignoring incoming SMR control message.");
77 if (request.isProbe() != null && request.isProbe()) {
78 LOG.debug("Map-Resolver ignoring incoming RLOC probe control message.");
82 if (request.getSourceEid() != null) {
83 srcEid = request.getSourceEid().getEid();
85 MapReplyBuilder replyBuilder = new MapReplyBuilder();
86 replyBuilder.setEchoNonceEnabled(false);
87 replyBuilder.setProbe(false);
88 replyBuilder.setSecurityEnabled(false);
89 replyBuilder.setNonce(request.getNonce());
90 replyBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
91 for (EidItem eidRecord : request.getEidItem()) {
92 MappingRecord mapping = (MappingRecord) mapService.getMapping(srcEid,
94 if (mapping != null) {
95 List<ItrRloc> itrRlocs = request.getItrRloc();
96 if (itrRlocs != null && itrRlocs.size() != 0) {
97 if (subscriptionService) {
98 updateSubscribers(itrRlocs.get(0).getRloc(), eidRecord.getEid(), mapping.getEid(), srcEid);
100 mapping = updateLocators(mapping, itrRlocs);
102 mapping = fixIfNotSDRequest(mapping, eidRecord.getEid());
104 mapping = getNegativeMapping(eidRecord.getEid());
106 replyBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(mapping).build());
108 requestHandler.handleMapReply(replyBuilder.build());
111 private MappingRecord getNegativeMapping(Eid eid) {
112 MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
113 recordBuilder.setAuthoritative(false);
114 recordBuilder.setMapVersion((short) 0);
115 recordBuilder.setEid(eid);
116 recordBuilder.setAction(Action.NativelyForward);
117 if (authenticate && mapService.getAuthenticationKey(eid) != null) {
118 recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
120 recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
122 return recordBuilder.build();
125 private void updateSubscribers(Rloc itrRloc, Eid reqEid, Eid mapEid, Eid srcEid) {
126 SubscriberRLOC subscriberRloc = new SubscriberRLOC(itrRloc, srcEid);
127 Eid subscribedEid = mapEid;
129 // If the eid in the matched mapping is SourceDest and the requested eid IS NOT then we subscribe itrRloc only
130 // to dst from the src/dst since that what's been requested. Note though that any updates to to the src/dst
131 // mapping will be pushed to dst as well (see sendSMRs in MapServer)
132 if (mapEid.getAddressType().equals(SourceDestKeyLcaf.class)
133 && !reqEid.getAddressType().equals(SourceDestKeyLcaf.class)) {
134 subscribedEid = SourceDestKeyHelper.getDstBinary(mapEid);
137 Set<SubscriberRLOC> subscribers = getSubscribers(subscribedEid);
138 if (subscribers == null) {
139 subscribers = Sets.newConcurrentHashSet();
140 } else if (subscribers.contains(subscriberRloc)) {
141 // If there is an entry already for this subscriberRloc, remove it, so that it gets the new
143 subscribers.remove(subscriberRloc);
145 if (LOG.isTraceEnabled()) {
146 LOG.trace("Adding new subscriber: " + LispAddressStringifier.getString(subscriberRloc.getSrcRloc()));
148 subscribers.add(subscriberRloc);
149 addSubscribers(subscribedEid, subscribers);
152 // Fixes mapping if request was for simple dst EID but the matched mapping is a SourceDest
153 private MappingRecord fixIfNotSDRequest(MappingRecord mapping, Eid dstEid) {
154 if (mapping.getEid().getAddress() instanceof SourceDestKey
155 && !(dstEid.getAddress() instanceof SourceDestKey)) {
156 return new MappingRecordBuilder(mapping).setEid(
157 SourceDestKeyHelper.getDstBinary(mapping.getEid())).build();
162 private boolean locatorsNeedFixing(List<LocatorRecord> locatorRecords) {
163 for (LocatorRecord record : locatorRecords) {
164 if (record.getRloc().getAddress() instanceof ExplicitLocatorPath) {
171 // Process locators according to configured policy
172 private MappingRecord updateLocators(MappingRecord mapping, List<ItrRloc> itrRlocs) {
173 // no fixing if elpPolicy is default
174 if (elpPolicy.equalsIgnoreCase("default")) {
178 List<LocatorRecord> locatorRecords = mapping.getLocatorRecord();
180 // if no updated is needed, just return the mapping
181 if (!locatorsNeedFixing(locatorRecords)) {
185 MappingRecordBuilder recordBuilder = new MappingRecordBuilder(mapping);
186 recordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
188 for (LocatorRecord record : locatorRecords) {
189 Rloc container = record.getRloc();
191 // For non-ELP RLOCs, or when ELP policy is default, or itrRlocs is null, just add the locator and be
193 if ((!(container.getAddress() instanceof ExplicitLocatorPath))
194 || elpPolicy.equalsIgnoreCase("default") || itrRlocs == null) {
195 recordBuilder.getLocatorRecord().add(
196 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
197 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
198 .setPriority(record.getPriority()).setMulticastWeight(record.getMulticastWeight())
199 .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
200 .setRloc(container).setLocatorId(record.getLocatorId()).build());
204 ExplicitLocatorPath teAddress = ((ExplicitLocatorPath) container.getAddress());
205 SimpleAddress nextHop = getNextELPHop(teAddress, itrRlocs);
206 if (nextHop != null) {
207 java.lang.Short priority = record.getPriority();
208 if (elpPolicy.equalsIgnoreCase("both")) {
209 recordBuilder.getLocatorRecord().add(
210 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
211 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
212 .setPriority(record.getPriority())
213 .setMulticastWeight(record.getMulticastWeight())
214 .setMulticastPriority(record.getMulticastPriority())
215 .setRouted(record.isRouted()).setRloc(container)
216 .setLocatorId(record.getLocatorId()).build());
217 // Make the priority of the added simple locator lower so that ELP is used by default if
218 // the xTR understands ELP. Exclude 255, since that means don't use for unicast forwarding
219 // XXX Complex cases like several ELPs with different priorities are not handled
220 if (priority != 254 || priority != 255) {
224 // Build and add the simple RLOC
225 recordBuilder.getLocatorRecord().add(
226 new LocatorRecordBuilder().setLocalLocator(record.isLocalLocator())
227 .setRlocProbed(record.isRlocProbed()).setWeight(record.getWeight())
228 .setPriority(priority).setMulticastWeight(record.getMulticastWeight())
229 .setMulticastPriority(record.getMulticastPriority()).setRouted(record.isRouted())
230 .setRloc(LispAddressUtil.toRloc(nextHop))
231 .setLocatorId(record.getLocatorId()).build());
234 } catch (ClassCastException cce) {
235 LOG.error("Class Cast Exception while building EidToLocatorRecord: {}", ExceptionUtils.getStackTrace(cce));
238 return recordBuilder.build();
241 private SimpleAddress getNextELPHop(ExplicitLocatorPath elp, List<ItrRloc> itrRlocs) {
242 SimpleAddress nextHop = null;
243 List<Hop> hops = elp.getExplicitLocatorPath().getHop();
245 if (hops != null && hops.size() > 0) {
246 // By default we return the first hop
247 nextHop = hops.get(0).getAddress();
248 for (Hop hop : hops) {
249 Address hopAddress = LispAddressUtil.addressFromSimpleAddress(hop.getAddress());
250 for (ItrRloc itrRloc : itrRlocs) {
251 if (itrRloc.getRloc().getAddress().equals(hopAddress)) {
252 int i = hops.indexOf(hop);
253 if (i < hops.size() - 1) {
254 nextHop = hops.get(i + 1).getAddress();
265 @SuppressWarnings("unchecked")
266 private Set<SubscriberRLOC> getSubscribers(Eid address) {
267 return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
270 private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
271 mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
275 public void setSubscriptionService(boolean smr) {
276 subscriptionService = smr;
280 public void setElpPolicy(String elpPolicy) {
281 this.elpPolicy = elpPolicy;
285 public void setShouldAuthenticate(boolean shouldAuthenticate) {
286 this.authenticate = shouldAuthenticate;