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.Maps;
13 import com.google.common.util.concurrent.ThreadFactoryBuilder;
14 import java.net.InetAddress;
15 import java.net.NetworkInterface;
16 import java.net.SocketException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Enumeration;
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
24 import java.util.Objects;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.ScheduledExecutorService;
28 import java.util.concurrent.ScheduledFuture;
29 import java.util.concurrent.ThreadFactory;
30 import java.util.concurrent.TimeUnit;
31 import org.apache.commons.lang3.BooleanUtils;
32 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
33 import org.opendaylight.lispflowmapping.config.ConfigIni;
34 import org.opendaylight.lispflowmapping.implementation.util.LoggingUtil;
35 import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil;
36 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
37 import org.opendaylight.lispflowmapping.interfaces.dao.Subscriber;
38 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
39 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
40 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
41 import org.opendaylight.lispflowmapping.interfaces.lisp.SmrEvent;
42 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
43 import org.opendaylight.lispflowmapping.lisp.authentication.LispAuthenticationUtil;
44 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
45 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
46 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
47 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
48 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
49 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
50 import org.opendaylight.lispflowmapping.lisp.util.MappingRecordUtil;
51 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
71 import org.opendaylight.yangtools.concepts.ListenerRegistration;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
75 public class MapServer implements IMapServerAsync, OdlMappingserviceListener, ISmrNotificationListener {
77 protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
78 private static final byte[] ALL_ZEROES_XTR_ID = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
79 private IMappingService mapService;
80 private boolean subscriptionService;
81 private IMapNotifyHandler notifyHandler;
82 private NotificationService notificationService;
83 private ListenerRegistration<MapServer> mapServerListenerRegistration;
84 private SmrScheduler scheduler;
86 public MapServer(IMappingService mapService, boolean subscriptionService,
87 IMapNotifyHandler notifyHandler, NotificationService notificationService) {
88 Preconditions.checkNotNull(mapService);
89 this.mapService = mapService;
90 this.subscriptionService = subscriptionService;
91 this.notifyHandler = notifyHandler;
92 this.notificationService = notificationService;
93 if (notificationService != null) {
94 notificationService.registerNotificationListener(this);
96 scheduler = new SmrScheduler();
100 public void setSubscriptionService(boolean subscriptionService) {
101 this.subscriptionService = subscriptionService;
104 @SuppressWarnings("unchecked")
105 public void handleMapRegister(MapRegister mapRegister) {
106 boolean mappingUpdated = false;
107 boolean oldMappingRemoved = false;
108 boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled();
109 Set<Subscriber> subscribers = null;
110 MappingRecord oldMapping;
113 if (!mapRegister.isXtrSiteIdPresent() || mapRegister.getXtrId() == null) {
114 LOG.error("Merge bit is set in Map-Register, but xTR-ID is not present. Will not merge.");
116 } else if (Arrays.equals(mapRegister.getXtrId().getValue(), ALL_ZEROES_XTR_ID)) {
117 LOG.warn("Merge bit is set in Map-Register, but xTR-ID is all zeroes.");
121 for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
122 MappingRecord mapping = record.getMappingRecord();
123 Eid eid = mapping.getEid();
124 MappingData mappingData = new MappingData(mapping, System.currentTimeMillis());
125 mappingData.setMergeEnabled(merge);
126 mappingData.setXtrId(mapRegister.getXtrId());
128 oldMapping = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid));
129 mapService.addMapping(MappingOrigin.Southbound, eid, getSiteId(mapRegister), mappingData);
130 if (oldMapping != null && MappingRecordUtil.isNegativeMapping(oldMapping)) {
131 mapService.removeMapping(MappingOrigin.Southbound, oldMapping.getEid());
132 oldMappingRemoved = true;
135 if (subscriptionService) {
136 MappingRecord newMapping = merge
137 ? getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid)) : mapping;
139 if (mappingChanged(oldMapping, newMapping)) {
140 if (LOG.isDebugEnabled()) {
141 LOG.debug("Mapping update occured for {} SMRs will be sent for its subscribers.",
142 LispAddressStringifier.getString(mapping.getEid()));
144 subscribers = getSubscribers(eid);
145 if (oldMapping != null && !oldMapping.getEid().equals(eid)) {
146 subscribers = addParentSubscribers(eid, subscribers);
148 LoggingUtil.logSubscribers(LOG, eid, subscribers);
149 handleSmr(eid, subscribers);
150 if (oldMapping != null && oldMappingRemoved && !oldMapping.getEid().equals(eid)) {
151 subscribers = getSubscribers(oldMapping.getEid());
152 LoggingUtil.logSubscribers(LOG, oldMapping.getEid(), subscribers);
153 handleSmr(oldMapping.getEid(), subscribers);
155 mappingUpdated = true;
159 if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
160 LOG.trace("MapRegister wants MapNotify");
161 MapNotifyBuilder builder = new MapNotifyBuilder();
162 List<TransportAddress> rlocs = null;
164 Set<IpAddressBinary> notifyRlocs = new HashSet<IpAddressBinary>();
165 List<MappingRecordItem> mergedMappings = new ArrayList<MappingRecordItem>();
166 for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
167 MappingRecord mapping = record.getMappingRecord();
168 MappingRecord currentRecord = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound,
170 mergedMappings.add(new MappingRecordItemBuilder().setMappingRecord(currentRecord).build());
171 Set<IpAddressBinary> sourceRlocs = (Set<IpAddressBinary>) mapService.getData(
172 MappingOrigin.Southbound, mapping.getEid(), SubKeys.SRC_RLOCS);
173 if (sourceRlocs != null) {
174 notifyRlocs.addAll(sourceRlocs);
177 MapNotifyBuilderHelper.setFromMapRegisterAndMappingRecordItems(builder, mapRegister, mergedMappings);
178 // send map-notify to merge group only when mapping record is changed
179 if (mappingUpdated) {
180 rlocs = getTransportAddresses(notifyRlocs);
183 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
185 List<MappingRecordItem> mappings = builder.getMappingRecordItem();
186 if (mappings != null && mappings.get(0) != null && mappings.get(0).getMappingRecord() != null
187 && mappings.get(0).getMappingRecord().getEid() != null) {
188 MappingAuthkey authkey = mapService.getAuthenticationKey(mappings.get(0).getMappingRecord().getEid());
189 if (authkey != null) {
190 builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(),
191 authkey.getKeyString()));
194 notifyHandler.handleMapNotify(builder.build(), rlocs);
198 private static List<TransportAddress> getTransportAddresses(Set<IpAddressBinary> addresses) {
199 List<TransportAddress> rlocs = new ArrayList<TransportAddress>();
200 for (IpAddressBinary address : addresses) {
201 TransportAddressBuilder tab = new TransportAddressBuilder();
202 tab.setIpAddress(address);
203 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
204 rlocs.add(tab.build());
209 private static SiteId getSiteId(MapRegister mapRegister) {
210 return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
213 private static MappingRecord getMappingRecord(MappingData mappingData) {
214 return (mappingData != null) ? mappingData.getRecord() : null;
218 public void onMappingChanged(MappingChanged notification) {
219 LOG.trace("MappingChanged event of type: `{}'", notification.getChangeType());
220 if (subscriptionService) {
221 Eid eid = notification.getMappingRecord().getEid();
222 Set<Subscriber> subscribers = MSNotificationInputUtil.toSubscriberSet(notification.getSubscriberItem());
223 LoggingUtil.logSubscribers(LOG, eid, subscribers);
224 if (mapService.isMaster()) {
225 sendSmrs(eid, subscribers);
226 if (eid.getAddress() instanceof SourceDestKey) {
227 Set<Subscriber> dstSubscribers = MSNotificationInputUtil.toSubscriberSetFromDst(
228 notification.getDstSubscriberItem());
229 LoggingUtil.logSubscribers(LOG, SourceDestKeyHelper.getDstBinary(eid), dstSubscribers);
230 sendSmrs(SourceDestKeyHelper.getDstBinary(eid), dstSubscribers);
236 private static boolean mappingChanged(MappingRecord oldMapping, MappingRecord newMapping) {
237 // We only check for fields we care about
238 // XXX: This code needs to be checked and updated when the YANG model for MappingRecord is modified
239 Preconditions.checkNotNull(newMapping, "The new mapping should never be null");
240 if (oldMapping == null) {
241 LOG.trace("mappingChanged(): old mapping is null");
243 } else if (!Objects.equals(oldMapping.getEid(), newMapping.getEid())) {
244 LOG.trace("mappingChanged(): EID");
246 } else if (!Objects.equals(oldMapping.getLocatorRecord(), newMapping.getLocatorRecord())) {
247 LOG.trace("mappingChanged(): RLOC");
249 } else if (!Objects.equals(oldMapping.getAction(), newMapping.getAction())) {
250 LOG.trace("mappingChanged(): action");
252 } else if (!Objects.equals(oldMapping.getRecordTtl(), newMapping.getRecordTtl())) {
253 LOG.trace("mappingChanged(): TTL");
255 } else if (!Objects.equals(oldMapping.getMapVersion(), newMapping.getMapVersion())) {
256 LOG.trace("mappingChanged(): mapping version");
262 private void handleSmr(Eid eid, Set<Subscriber> subscribers) {
263 sendSmrs(eid, subscribers);
265 // For SrcDst LCAF also send SMRs to Dst prefix
266 if (eid.getAddress() instanceof SourceDestKey) {
267 Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid);
268 Set<Subscriber> dstSubs = getSubscribers(dstAddr);
269 sendSmrs(dstAddr, dstSubs);
273 private void sendSmrs(Eid eid, Set<Subscriber> subscribers) {
274 if (subscribers == null) {
277 final MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress()));
278 LOG.trace("Built SMR packet: " + mrb.build().toString());
280 scheduler.scheduleSmrs(mrb, subscribers.iterator());
283 @SuppressWarnings("unchecked")
284 private Set<Subscriber> getSubscribers(Eid address) {
285 return (Set<Subscriber>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
288 private Set<Subscriber> addParentSubscribers(Eid eid, Set<Subscriber> subscribers) {
289 Eid parentPrefix = mapService.getParentPrefix(eid);
290 if (parentPrefix == null) {
294 Set<Subscriber> parentSubscribers = getSubscribers(parentPrefix);
295 if (parentSubscribers != null) {
296 if (subscribers != null) {
297 subscribers.addAll(parentSubscribers);
299 subscribers = parentSubscribers;
305 private void addSubscribers(Eid address, Set<Subscriber> subscribers) {
306 mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
309 private static InetAddress getLocalAddress() {
311 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
312 while (interfaces.hasMoreElements()) {
313 NetworkInterface current = interfaces.nextElement();
314 LOG.trace("Interface " + current.toString());
315 if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
318 Enumeration<InetAddress> addresses = current.getInetAddresses();
319 while (addresses.hasMoreElements()) {
320 InetAddress currentAddr = addresses.nextElement();
321 // Skip loopback and link local addresses
322 if (currentAddr.isLoopbackAddress() || currentAddr.isLinkLocalAddress()) {
325 LOG.debug(currentAddr.getHostAddress());
329 } catch (SocketException se) {
330 LOG.debug("Caught socket exception", se);
336 public void onSmrInvokedReceived(SmrEvent event) {
337 scheduler.smrReceived(event);
341 * Task scheduler is responsible for resending SMR messages to a subscriber (xTR)
342 * {@value ConfigIni#LISP_SMR_RETRY_COUNT} times, or until {@link ISmrNotificationListener#onSmrInvokedReceived}
345 private class SmrScheduler {
346 final int cpuCores = Runtime.getRuntime().availableProcessors();
347 private final ThreadFactory threadFactory = new ThreadFactoryBuilder()
348 .setNameFormat("smr-executor-%d").build();
349 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(cpuCores * 2, threadFactory);
350 private final Map<IpAddressBinary, Map<Eid, ScheduledFuture<?>>> subscriberFutureMap = Maps.newConcurrentMap();
352 void scheduleSmrs(MapRequestBuilder mrb, Iterator<Subscriber> subscribers) {
353 // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a Subscriber
355 while (subscribers.hasNext()) {
356 Subscriber subscriber = subscribers.next();
357 if (subscriber.timedOut()) {
358 LOG.debug("Lazy removing expired subscriber entry " + subscriber.getString());
359 subscribers.remove();
361 final Eid srcEid = mrb.getSourceEid().getEid();
362 final ScheduledFuture<?> future = executor.scheduleAtFixedRate(new CancellableRunnable(
363 mrb, subscriber), 0L, ConfigIni.getInstance().getSmrTimeout(), TimeUnit.MILLISECONDS);
364 final IpAddressBinary subscriberAddress = LispAddressUtil
365 .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress());
367 if (subscriberFutureMap.containsKey(subscriberAddress)) {
368 subscriberFutureMap.get(subscriberAddress).put(srcEid, future);
370 final Map<Eid, ScheduledFuture<?>> eidFutureMap = Maps.newConcurrentMap();
371 eidFutureMap.put(srcEid, future);
372 subscriberFutureMap.put(subscriberAddress, eidFutureMap);
378 void smrReceived(SmrEvent event) {
379 final List<IpAddressBinary> subscriberAddressList = event.getSubscriberAddressList();
380 for (IpAddressBinary subscriberAddress : subscriberAddressList) {
381 final Map<Eid, ScheduledFuture<?>> eidFutureMap = subscriberFutureMap.get(subscriberAddress);
382 if (eidFutureMap != null) {
383 final ScheduledFuture<?> future = eidFutureMap.get(event.getEid());
384 if (future != null && !future.isCancelled()) {
386 LOG.debug("SMR-invoked MapRequest received, scheduled task for subscriber {}, EID {} with"
387 + " nonce {} has been cancelled", subscriberAddress.toString(),
388 LispAddressStringifier.getString(event.getEid()), event.getNonce());
389 eidFutureMap.remove(event.getEid());
391 if (eidFutureMap.isEmpty()) {
392 subscriberFutureMap.remove(subscriberAddress);
398 private final class CancellableRunnable implements Runnable {
399 private MapRequestBuilder mrb;
400 private Subscriber subscriber;
401 private int executionCount = 1;
403 CancellableRunnable(MapRequestBuilder mrb, Subscriber subscriber) {
405 this.subscriber = subscriber;
408 @SuppressWarnings("checkstyle:IllegalCatch")
411 final IpAddressBinary subscriberAddress = LispAddressUtil
412 .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress());
413 final Eid srcEid = mrb.getSourceEid().getEid();
416 // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked
417 // Map-Request. To ensure consistent behavior it is set to the value used to originally request
419 if (executionCount <= ConfigIni.getInstance().getSmrRetryCount()) {
421 mrb.setEidItem(new ArrayList<EidItem>());
422 mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build());
423 notifyHandler.handleSMR(mrb.build(), subscriber.getSrcRloc());
424 if (LOG.isTraceEnabled()) {
425 LOG.trace("Attempt #{} to send SMR for EID {} to subscriber {}, source EID {}",
427 LispAddressStringifier.getString(mrb.getSourceEid().getEid()),
428 LispAddressStringifier.getString(subscriber.getSrcRloc()),
429 LispAddressStringifier.getString(mrb.getEidItem().get(0).getEid()));
433 LOG.trace("Cancelling execution of a SMR Map-Request after {} failed attempts.",
435 cancelAndRemove(subscriberAddress, srcEid);
438 } catch (Exception e) {
439 LOG.error("Errors encountered while handling SMR:", e);
440 cancelAndRemove(subscriberAddress, srcEid);
446 private void cancelAndRemove(IpAddressBinary subscriberAddress, Eid eid) {
447 final Map<Eid, ScheduledFuture<?>> eidFutureMap = subscriberFutureMap.get(subscriberAddress);
448 if (eidFutureMap == null) {
449 LOG.warn("Couldn't find subscriber {} in SMR scheduler internal list", subscriberAddress);
453 if (eidFutureMap.containsKey(eid)) {
454 ScheduledFuture<?> eidFuture = eidFutureMap.get(eid);
455 eidFutureMap.remove(eid);
456 eidFuture.cancel(false);
458 if (eidFutureMap.isEmpty()) {
459 subscriberFutureMap.remove(subscriberAddress);