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.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.interfaces.dao.SubKeys;
35 import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
36 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
38 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
39 import org.opendaylight.lispflowmapping.interfaces.lisp.SmrEvent;
40 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
41 import org.opendaylight.lispflowmapping.lisp.authentication.LispAuthenticationUtil;
42 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
43 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
44 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
45 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
46 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
47 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
48 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
69 import org.opendaylight.yangtools.concepts.ListenerRegistration;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
73 public class MapServer implements IMapServerAsync, OdlMappingserviceListener, ISmrNotificationListener {
75 protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
76 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};
77 private IMappingService mapService;
78 private boolean subscriptionService;
79 private IMapNotifyHandler notifyHandler;
80 private NotificationService notificationService;
81 private ListenerRegistration<MapServer> mapServerListenerRegistration;
82 private SmrScheduler scheduler;
84 public MapServer(IMappingService mapService, boolean subscriptionService,
85 IMapNotifyHandler notifyHandler, NotificationService notificationService) {
86 Preconditions.checkNotNull(mapService);
87 this.mapService = mapService;
88 this.subscriptionService = subscriptionService;
89 this.notifyHandler = notifyHandler;
90 this.notificationService = notificationService;
91 if (notificationService != null) {
92 notificationService.registerNotificationListener(this);
94 scheduler = new SmrScheduler();
98 public void setSubscriptionService(boolean subscriptionService) {
99 this.subscriptionService = subscriptionService;
102 @SuppressWarnings("unchecked")
103 public void handleMapRegister(MapRegister mapRegister) {
104 boolean mappingUpdated = false;
105 boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled();
106 Set<SubscriberRLOC> subscribers = null;
107 MappingRecord oldMapping;
110 if (!mapRegister.isXtrSiteIdPresent() || mapRegister.getXtrId() == null) {
111 LOG.error("Merge bit is set in Map-Register, but xTR-ID is not present. Will not merge.");
113 } else if (Arrays.equals(mapRegister.getXtrId().getValue(), ALL_ZEROES_XTR_ID)) {
114 LOG.warn("Merge bit is set in Map-Register, but xTR-ID is all zeroes.");
118 for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
119 MappingRecord mapping = record.getMappingRecord();
120 Eid eid = mapping.getEid();
121 MappingData mappingData = new MappingData(mapping, System.currentTimeMillis());
122 mappingData.setMergeEnabled(merge);
123 mappingData.setXtrId(mapRegister.getXtrId());
125 oldMapping = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid));
126 mapService.addMapping(MappingOrigin.Southbound, eid, getSiteId(mapRegister), mappingData);
128 if (subscriptionService) {
129 MappingRecord newMapping = merge
130 ? getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid)) : mapping;
132 if (mappingChanged(oldMapping, newMapping)) {
133 if (LOG.isDebugEnabled()) {
134 LOG.debug("Mapping update occured for {} SMRs will be sent for its subscribers.",
135 LispAddressStringifier.getString(mapping.getEid()));
137 subscribers = getSubscribers(eid);
138 if (oldMapping != null && !oldMapping.getEid().equals(eid)) {
139 subscribers = addParentSubscribers(eid, subscribers);
141 sendSmrs(eid, subscribers);
142 mappingUpdated = true;
146 if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
147 LOG.trace("MapRegister wants MapNotify");
148 MapNotifyBuilder builder = new MapNotifyBuilder();
149 List<TransportAddress> rlocs = null;
151 Set<IpAddressBinary> notifyRlocs = new HashSet<IpAddressBinary>();
152 List<MappingRecordItem> mergedMappings = new ArrayList<MappingRecordItem>();
153 for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
154 MappingRecord mapping = record.getMappingRecord();
155 MappingRecord currentRecord = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound,
157 mergedMappings.add(new MappingRecordItemBuilder().setMappingRecord(currentRecord).build());
158 Set<IpAddressBinary> sourceRlocs = (Set<IpAddressBinary>) mapService.getData(
159 MappingOrigin.Southbound, mapping.getEid(), SubKeys.SRC_RLOCS);
160 if (sourceRlocs != null) {
161 notifyRlocs.addAll(sourceRlocs);
164 MapNotifyBuilderHelper.setFromMapRegisterAndMappingRecordItems(builder, mapRegister, mergedMappings);
165 // send map-notify to merge group only when mapping record is changed
166 if (mappingUpdated) {
167 rlocs = getTransportAddresses(notifyRlocs);
170 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
172 List<MappingRecordItem> mappings = builder.getMappingRecordItem();
173 if (mappings != null && mappings.get(0) != null && mappings.get(0).getMappingRecord() != null
174 && mappings.get(0).getMappingRecord().getEid() != null) {
175 MappingAuthkey authkey = mapService.getAuthenticationKey(mappings.get(0).getMappingRecord().getEid());
176 if (authkey != null) {
177 builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(),
178 authkey.getKeyString()));
181 notifyHandler.handleMapNotify(builder.build(), rlocs);
185 private static List<TransportAddress> getTransportAddresses(Set<IpAddressBinary> addresses) {
186 List<TransportAddress> rlocs = new ArrayList<TransportAddress>();
187 for (IpAddressBinary address : addresses) {
188 TransportAddressBuilder tab = new TransportAddressBuilder();
189 tab.setIpAddress(address);
190 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
191 rlocs.add(tab.build());
196 private static SiteId getSiteId(MapRegister mapRegister) {
197 return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
200 private static MappingRecord getMappingRecord(MappingData mappingData) {
201 return (mappingData != null) ? mappingData.getRecord() : null;
205 public void onMappingChanged(MappingChanged notification) {
206 if (subscriptionService) {
207 Eid eid = notification.getMappingRecord().getEid();
208 if (mapService.isMaster()) {
209 sendSmrs(eid, getSubscribers(eid));
211 if (notification.getChangeType().equals(MappingChange.Removed)) {
212 removeSubscribers(eid);
217 private static boolean mappingChanged(MappingRecord oldMapping, MappingRecord newMapping) {
218 // We only check for fields we care about
219 // XXX: This code needs to be checked and updated when the YANG model for MappingRecord is modified
220 Preconditions.checkNotNull(newMapping, "The new mapping should never be null");
221 if (oldMapping == null) {
222 LOG.trace("mappingChanged(): old mapping is null");
224 } else if (!Objects.equals(oldMapping.getEid(), newMapping.getEid())) {
225 LOG.trace("mappingChanged(): EID");
227 } else if (!Objects.equals(oldMapping.getLocatorRecord(), newMapping.getLocatorRecord())) {
228 LOG.trace("mappingChanged(): RLOC");
230 } else if (!Objects.equals(oldMapping.getAction(), newMapping.getAction())) {
231 LOG.trace("mappingChanged(): action");
233 } else if (!Objects.equals(oldMapping.getRecordTtl(), newMapping.getRecordTtl())) {
234 LOG.trace("mappingChanged(): TTL");
236 } else if (!Objects.equals(oldMapping.getMapVersion(), newMapping.getMapVersion())) {
237 LOG.trace("mappingChanged(): mapping version");
243 private void sendSmrs(Eid eid, Set<SubscriberRLOC> subscribers) {
244 handleSmr(eid, subscribers);
246 // For SrcDst LCAF also send SMRs to Dst prefix
247 if (eid.getAddress() instanceof SourceDestKey) {
248 Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid);
249 Set<SubscriberRLOC> dstSubs = getSubscribers(dstAddr);
250 handleSmr(dstAddr, dstSubs);
254 private void handleSmr(Eid eid, Set<SubscriberRLOC> subscribers) {
255 if (subscribers == null) {
258 final MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress()));
259 LOG.trace("Built SMR packet: " + mrb.build().toString());
261 scheduler.scheduleSmrs(mrb, subscribers.iterator());
262 addSubscribers(eid, subscribers);
265 @SuppressWarnings("unchecked")
266 private Set<SubscriberRLOC> getSubscribers(Eid address) {
267 return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
270 private Set<SubscriberRLOC> addParentSubscribers(Eid eid, Set<SubscriberRLOC> subscribers) {
271 Eid parentPrefix = mapService.getParentPrefix(eid);
272 if (parentPrefix == null) {
276 Set<SubscriberRLOC> parentSubscribers = getSubscribers(parentPrefix);
277 if (parentSubscribers != null) {
278 if (subscribers != null) {
279 subscribers.addAll(parentSubscribers);
281 subscribers = parentSubscribers;
287 private void removeSubscribers(Eid address) {
288 mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
291 private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
292 mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
295 private static InetAddress getLocalAddress() {
297 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
298 while (interfaces.hasMoreElements()) {
299 NetworkInterface current = interfaces.nextElement();
300 LOG.debug("Interface " + current.toString());
301 if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
304 Enumeration<InetAddress> addresses = current.getInetAddresses();
305 while (addresses.hasMoreElements()) {
306 InetAddress currentAddr = addresses.nextElement();
307 // Skip loopback and link local addresses
308 if (currentAddr.isLoopbackAddress() || currentAddr.isLinkLocalAddress()) {
311 LOG.debug(currentAddr.getHostAddress());
315 } catch (SocketException se) {
316 LOG.debug("Caught socket exception", se);
322 public void onSmrInvokedReceived(SmrEvent event) {
323 scheduler.smrReceived(event);
327 * Task scheduler is responsible for resending SMR messages to a subscriber (xTR)
328 * {@value ConfigIni#LISP_SMR_RETRY_COUNT} times, or until {@link ISmrNotificationListener#onSmrInvokedReceived}
331 private class SmrScheduler {
332 final int cpuCores = Runtime.getRuntime().availableProcessors();
333 private final ThreadFactory threadFactory = new ThreadFactoryBuilder()
334 .setNameFormat("smr-executor-%d").build();
335 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(cpuCores * 2, threadFactory);
336 private final Map<IpAddressBinary, Map<Eid, ScheduledFuture<?>>> subscriberFutureMap = Maps.newConcurrentMap();
338 void scheduleSmrs(MapRequestBuilder mrb, Iterator<SubscriberRLOC> subscribers) {
339 // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a SubscriberRLOC
341 while (subscribers.hasNext()) {
342 SubscriberRLOC subscriber = subscribers.next();
343 if (subscriber.timedOut()) {
344 LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString());
345 subscribers.remove();
347 final ScheduledFuture<?> future = executor.scheduleAtFixedRate(new CancellableRunnable(
348 mrb, subscriber), 0L, ConfigIni.getInstance().getSmrTimeout(), TimeUnit.MILLISECONDS);
349 final IpAddressBinary subscriberAddress = LispAddressUtil
350 .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress());
352 if (subscriberFutureMap.containsKey(subscriberAddress)) {
353 subscriberFutureMap.get(subscriberAddress).put(mrb.getSourceEid().getEid(), future);
355 final Map<Eid, ScheduledFuture<?>> eidFutureMap = Maps.newConcurrentMap();
356 eidFutureMap.put(mrb.getSourceEid().getEid(), future);
357 subscriberFutureMap.put(subscriberAddress, eidFutureMap);
363 void smrReceived(SmrEvent event) {
364 final List<IpAddressBinary> subscriberAddressList = event.getSubscriberAddressList();
365 for (IpAddressBinary subscriberAddress : subscriberAddressList) {
366 final Map<Eid, ScheduledFuture<?>> eidFutureMap = subscriberFutureMap.get(subscriberAddress);
367 if (eidFutureMap != null) {
368 final ScheduledFuture<?> future = eidFutureMap.get(event.getEid());
369 if (future != null && !future.isCancelled()) {
370 future.cancel(false);
371 LOG.trace("SMR-invoked MapRequest received, scheduled task for subscriber {} with nonce {} has "
372 + "been canceled", subscriberAddress.toString(), event.getNonce());
373 eidFutureMap.remove(event.getEid());
375 if (eidFutureMap.isEmpty()) {
376 subscriberFutureMap.remove(subscriberAddress);
382 private final class CancellableRunnable implements Runnable {
383 private MapRequestBuilder mrb;
384 private SubscriberRLOC subscriber;
385 private int executionCount = 1;
387 CancellableRunnable(MapRequestBuilder mrb, SubscriberRLOC subscriber) {
389 this.subscriber = subscriber;
392 @SuppressWarnings("checkstyle:IllegalCatch")
395 final IpAddressBinary subscriberAddress = LispAddressUtil
396 .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress());
398 // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked
399 // Map-Request. To ensure consistent behavior it is set to the value used to originally request
401 if (executionCount <= ConfigIni.getInstance().getSmrRetryCount()) {
402 mrb.setEidItem(new ArrayList<EidItem>());
403 mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build());
404 notifyHandler.handleSMR(mrb.build(), subscriber.getSrcRloc());
405 LOG.trace("{}. attempt to send SMR for MapRequest " + mrb.getSourceEid().getEid()
406 + " to subscriber " + subscriber.getSrcRloc(), executionCount);
408 LOG.trace("Cancelling execution of a SMR Map-Request after {} failed attempts.",
410 cancelAndRemove(subscriberAddress);
412 } catch (Exception e) {
413 LOG.error("Errors encountered while handling SMR:", e);
414 cancelAndRemove(subscriberAddress);
419 private void cancelAndRemove(IpAddressBinary subscriberAddress) {
420 final Map<Eid, ScheduledFuture<?>> eidFutureMap = subscriberFutureMap.get(subscriberAddress);
421 final Eid eid = mrb.getSourceEid().getEid();
422 if (eidFutureMap.containsKey(eid)) {
423 eidFutureMap.get(eid).cancel(false);
425 eidFutureMap.remove(eid);
426 if (eidFutureMap.isEmpty()) {
427 subscriberFutureMap.remove(subscriberAddress);