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 java.net.InetAddress;
13 import java.net.NetworkInterface;
14 import java.net.SocketException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Enumeration;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
23 import java.util.Objects;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.ScheduledExecutorService;
27 import java.util.concurrent.ScheduledFuture;
28 import java.util.concurrent.TimeUnit;
29 import org.apache.commons.lang3.BooleanUtils;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
31 import org.opendaylight.lispflowmapping.config.ConfigIni;
32 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
33 import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
34 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
35 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
36 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.SmrEvent;
38 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
39 import org.opendaylight.lispflowmapping.lisp.authentication.LispAuthenticationUtil;
40 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
41 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
42 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
43 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
44 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
45 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
67 import org.opendaylight.yangtools.concepts.ListenerRegistration;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
71 public class MapServer implements IMapServerAsync, OdlMappingserviceListener, ISmrNotificationListener {
73 protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
74 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};
75 private IMappingService mapService;
76 private boolean subscriptionService;
77 private IMapNotifyHandler notifyHandler;
78 private NotificationService notificationService;
79 private ListenerRegistration<MapServer> mapServerListenerRegistration;
80 private SmrScheduler scheduler;
82 public MapServer(IMappingService mapService, boolean subscriptionService,
83 IMapNotifyHandler notifyHandler, NotificationService notificationService) {
84 Preconditions.checkNotNull(mapService);
85 this.mapService = mapService;
86 this.subscriptionService = subscriptionService;
87 this.notifyHandler = notifyHandler;
88 this.notificationService = notificationService;
89 if (notificationService != null) {
90 notificationService.registerNotificationListener(this);
95 public void setSubscriptionService(boolean subscriptionService) {
96 this.subscriptionService = subscriptionService;
99 @SuppressWarnings("unchecked")
100 public void handleMapRegister(MapRegister mapRegister) {
101 boolean mappingUpdated = false;
102 boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled();
103 Set<SubscriberRLOC> subscribers = null;
104 MappingRecord oldMapping;
107 if (!mapRegister.isXtrSiteIdPresent() || mapRegister.getXtrId() == null) {
108 LOG.error("Merge bit is set in Map-Register, but xTR-ID is not present. Will not merge.");
110 } else if (Arrays.equals(mapRegister.getXtrId().getValue(), ALL_ZEROES_XTR_ID)) {
111 LOG.warn("Merge bit is set in Map-Register, but xTR-ID is all zeroes.");
115 for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
116 MappingRecord mapping = record.getMappingRecord();
118 oldMapping = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, mapping.getEid());
119 mapService.addMapping(MappingOrigin.Southbound, mapping.getEid(), getSiteId(mapRegister), mapping, merge);
121 if (subscriptionService) {
122 MappingRecord newMapping = merge
123 ? (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, mapping.getEid()) : mapping;
125 if (mappingChanged(oldMapping, newMapping)) {
126 if (LOG.isDebugEnabled()) {
127 LOG.debug("Mapping update occured for {} SMRs will be sent for its subscribers.",
128 LispAddressStringifier.getString(mapping.getEid()));
130 subscribers = getSubscribers(mapping.getEid());
131 sendSmrs(mapping, subscribers);
132 mappingUpdated = true;
136 if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
137 LOG.trace("MapRegister wants MapNotify");
138 MapNotifyBuilder builder = new MapNotifyBuilder();
139 List<TransportAddress> rlocs = null;
141 Set<IpAddressBinary> notifyRlocs = new HashSet<IpAddressBinary>();
142 List<MappingRecordItem> mergedMappings = new ArrayList<MappingRecordItem>();
143 for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
144 MappingRecord mapping = record.getMappingRecord();
145 MappingRecord currentRecord = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound,
147 mergedMappings.add(new MappingRecordItemBuilder().setMappingRecord(currentRecord).build());
148 Set<IpAddressBinary> sourceRlocs = (Set<IpAddressBinary>) mapService.getData(
149 MappingOrigin.Southbound, mapping.getEid(), SubKeys.SRC_RLOCS);
150 if (sourceRlocs != null) {
151 notifyRlocs.addAll(sourceRlocs);
154 MapNotifyBuilderHelper.setFromMapRegisterAndMappingRecordItems(builder, mapRegister, mergedMappings);
155 // send map-notify to merge group only when mapping record is changed
156 if (mappingUpdated) {
157 rlocs = getTransportAddresses(notifyRlocs);
160 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
162 List<MappingRecordItem> mappings = builder.getMappingRecordItem();
163 if (mappings != null && mappings.get(0) != null && mappings.get(0).getMappingRecord() != null
164 && mappings.get(0).getMappingRecord().getEid() != null) {
165 MappingAuthkey authkey = mapService.getAuthenticationKey(mappings.get(0).getMappingRecord().getEid());
166 if (authkey != null) {
167 builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(),
168 authkey.getKeyString()));
171 notifyHandler.handleMapNotify(builder.build(), rlocs);
175 private static List<TransportAddress> getTransportAddresses(Set<IpAddressBinary> addresses) {
176 List<TransportAddress> rlocs = new ArrayList<TransportAddress>();
177 for (IpAddressBinary address : addresses) {
178 TransportAddressBuilder tab = new TransportAddressBuilder();
179 tab.setIpAddress(address);
180 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
181 rlocs.add(tab.build());
186 private SiteId getSiteId(MapRegister mapRegister) {
187 return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
191 public void onMappingChanged(MappingChanged notification) {
192 if (subscriptionService) {
193 if (mapService.isMaster()) {
194 sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
196 if (notification.getChangeType().equals(MappingChange.Removed)) {
197 removeSubscribers(notification.getMappingRecord().getEid());
202 private static boolean mappingChanged(MappingRecord oldMapping, MappingRecord newMapping) {
203 // We only check for fields we care about
204 // XXX: This code needs to be checked and updated when the YANG model is modified
205 Preconditions.checkNotNull(newMapping, "The new mapping should never be null");
206 if (oldMapping == null) {
207 LOG.trace("mappingChanged(): old mapping is null");
209 } else if (!Objects.equals(oldMapping.getEid(), newMapping.getEid())) {
210 LOG.trace("mappingChanged(): EID");
212 } else if (!Objects.equals(oldMapping.getLocatorRecord(), newMapping.getLocatorRecord())) {
213 LOG.trace("mappingChanged(): RLOC");
215 } else if (!Objects.equals(oldMapping.getAction(), newMapping.getAction())) {
216 LOG.trace("mappingChanged(): action");
218 } else if (!Objects.equals(oldMapping.getRecordTtl(), newMapping.getRecordTtl())) {
219 LOG.trace("mappingChanged(): TTL");
221 } else if (!Objects.equals(oldMapping.getMapVersion(), newMapping.getMapVersion())) {
222 LOG.trace("mappingChanged(): mapping version");
228 private void sendSmrs(MappingRecord record, Set<SubscriberRLOC> subscribers) {
229 Eid eid = record.getEid();
230 handleSmr(eid, subscribers);
232 // For SrcDst LCAF also send SMRs to Dst prefix
233 if (eid.getAddress() instanceof SourceDestKey) {
234 Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid);
235 Set<SubscriberRLOC> dstSubs = getSubscribers(dstAddr);
236 MappingRecord newRecord = new MappingRecordBuilder(record).setEid(dstAddr).build();
237 handleSmr(newRecord.getEid(), dstSubs);
241 private void handleSmr(Eid eid, Set<SubscriberRLOC> subscribers) {
242 if (subscribers == null) {
245 final MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress()));
246 LOG.trace("Built SMR packet: " + mrb.build().toString());
248 scheduler = new SmrScheduler();
250 scheduler.scheduleSmrs(mrb, subscribers.iterator());
251 } catch (InterruptedException e) {
252 LOG.error("SMR Scheduler interrupted.", e);
254 addSubscribers(eid, subscribers);
257 @SuppressWarnings("unchecked")
258 private Set<SubscriberRLOC> getSubscribers(Eid address) {
259 return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
262 private void removeSubscribers(Eid address) {
263 mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
266 private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
267 mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
270 private static InetAddress getLocalAddress() {
272 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
273 while (interfaces.hasMoreElements()) {
274 NetworkInterface current = interfaces.nextElement();
275 LOG.debug("Interface " + current.toString());
276 if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
279 Enumeration<InetAddress> addresses = current.getInetAddresses();
280 while (addresses.hasMoreElements()) {
281 InetAddress currentAddr = addresses.nextElement();
282 // Skip loopback and link local addresses
283 if (currentAddr.isLoopbackAddress() || currentAddr.isLinkLocalAddress()) {
286 LOG.debug(currentAddr.getHostAddress());
290 } catch (SocketException se) {
291 LOG.debug("Caught socket exception", se);
297 public void onSmrInvokedReceived(SmrEvent event) {
298 scheduler.smrReceived(event.getSubscriberAddressList());
302 * Task scheduler is responsible for resending SMR messages to a subscriber (xTR)
303 * {@value ConfigIni#LISP_SMR_RETRY_COUNT} times, or until {@link ISmrNotificationListener#onSmrInvokedReceived}
306 private class SmrScheduler {
307 private ScheduledExecutorService executor;
310 * Scheduling SMRs is done in a single thread. If a use case demands better performance, this may be revisited
311 * and for multi-threading we will need ConcurrentHashMap here along with a multi-threaded executor.
313 private Map<IpAddressBinary, ScheduledFuture<?>> subscriberFutureMap = new HashMap<>();
314 private long smrNonce;
316 void scheduleSmrs(MapRequestBuilder mrb, Iterator<SubscriberRLOC> subscribers) throws InterruptedException {
317 executor = Executors.newSingleThreadScheduledExecutor();
318 smrNonce = mrb.getNonce();
320 // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a SubscriberRLOC
322 while (subscribers.hasNext()) {
323 SubscriberRLOC subscriber = subscribers.next();
324 if (subscriber.timedOut()) {
325 LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString());
326 subscribers.remove();
328 final ScheduledFuture<?> future = executor.scheduleAtFixedRate(new CancellableRunnable(
329 mrb, subscriber), 0L, ConfigIni.getInstance().getSmrTimeout(), TimeUnit.MILLISECONDS);
330 subscriberFutureMap.put(
331 LispAddressUtil.addressBinaryFromAddress(subscriber.getSrcRloc().getAddress()), future);
336 void smrReceived(List<IpAddressBinary> subscriberAddressList) {
337 ScheduledFuture<?> future;
338 for (IpAddressBinary subscriberAddress : subscriberAddressList) {
339 future = subscriberFutureMap.get(subscriberAddress);
340 if (future != null && !future.isCancelled()) {
341 future.cancel(false);
342 LOG.trace("SMR-invoked MapRequest received, scheduled task for subscriber {} with nonce {} has "
343 + "been canceled", subscriberAddress.toString(), smrNonce);
344 tryToShutDownExecutor();
349 private final class CancellableRunnable implements Runnable {
350 private MapRequestBuilder mrb;
351 private SubscriberRLOC subscriber;
352 private int executionCount = 1;
354 CancellableRunnable(MapRequestBuilder mrb, SubscriberRLOC subscriber) {
356 this.subscriber = subscriber;
359 @SuppressWarnings("checkstyle:IllegalCatch")
362 final IpAddressBinary subscriberAddress = LispAddressUtil
363 .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress());
365 // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked
366 // Map-Request. To ensure consistent behavior it is set to the value used to originally request
368 if (executionCount <= ConfigIni.getInstance().getSmrRetryCount()) {
369 mrb.setEidItem(new ArrayList<EidItem>());
370 mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build());
371 notifyHandler.handleSMR(mrb.build(), subscriber.getSrcRloc());
372 LOG.trace("{}. attempt to send SMR for MapRequest " + subscriber.getSrcEid(), executionCount);
374 LOG.trace("Cancelling execution of a SMR Map-Request after {} failed attempts.",
376 subscriberFutureMap.get(subscriberAddress).cancel(false);
377 tryToShutDownExecutor();
379 } catch (Exception e) {
380 LOG.error("Errors encountered while handling SMR:", e);
381 subscriberFutureMap.get(subscriberAddress).cancel(false);
383 tryToShutDownExecutor();
389 private void tryToShutDownExecutor() {
390 if (!executor.isShutdown() && areAllFuturesCanceled()) {
392 LOG.trace("SMR scheduler executor shutdown.");
396 private boolean areAllFuturesCanceled() {
397 for (Map.Entry<IpAddressBinary, ScheduledFuture<?>> entry : subscriberFutureMap.entrySet()) {
398 if (!entry.getValue().isCancelled()) {