Reliable SMR - executor shutdown update
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.lispflowmapping.implementation.lisp;
10
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;
22 import java.util.Map;
23 import java.util.Objects;
24 import java.util.Set;
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;
70
71 public class MapServer implements IMapServerAsync, OdlMappingserviceListener, ISmrNotificationListener {
72
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;
81
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);
91         }
92     }
93
94     @Override
95     public void setSubscriptionService(boolean subscriptionService) {
96         this.subscriptionService = subscriptionService;
97     }
98
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;
105
106         if (merge) {
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.");
109                 merge = false;
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.");
112             }
113         }
114
115         for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
116             MappingRecord mapping = record.getMappingRecord();
117
118             oldMapping = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, mapping.getEid());
119             mapService.addMapping(MappingOrigin.Southbound, mapping.getEid(), getSiteId(mapRegister), mapping, merge);
120
121             if (subscriptionService) {
122                 MappingRecord newMapping = merge
123                         ? (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, mapping.getEid()) : mapping;
124
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()));
129                     }
130                     subscribers = getSubscribers(mapping.getEid());
131                     sendSmrs(mapping, subscribers);
132                     mappingUpdated = true;
133                 }
134             }
135         }
136         if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
137             LOG.trace("MapRegister wants MapNotify");
138             MapNotifyBuilder builder = new MapNotifyBuilder();
139             List<TransportAddress> rlocs = null;
140             if (merge) {
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,
146                             mapping.getEid());
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);
152                     }
153                 }
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);
158                 }
159             } else {
160                 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
161             }
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()));
169                 }
170             }
171             notifyHandler.handleMapNotify(builder.build(), rlocs);
172         }
173     }
174
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());
182         }
183         return rlocs;
184     }
185
186     private SiteId getSiteId(MapRegister mapRegister) {
187         return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
188     }
189
190     @Override
191     public void onMappingChanged(MappingChanged notification) {
192         if (subscriptionService) {
193             if (mapService.isMaster()) {
194                 sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
195             }
196             if (notification.getChangeType().equals(MappingChange.Removed)) {
197                 removeSubscribers(notification.getMappingRecord().getEid());
198             }
199         }
200     }
201
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");
208             return true;
209         } else if (!Objects.equals(oldMapping.getEid(), newMapping.getEid())) {
210             LOG.trace("mappingChanged(): EID");
211             return true;
212         } else if (!Objects.equals(oldMapping.getLocatorRecord(), newMapping.getLocatorRecord())) {
213             LOG.trace("mappingChanged(): RLOC");
214             return true;
215         } else if (!Objects.equals(oldMapping.getAction(), newMapping.getAction())) {
216             LOG.trace("mappingChanged(): action");
217             return true;
218         } else if (!Objects.equals(oldMapping.getRecordTtl(), newMapping.getRecordTtl())) {
219             LOG.trace("mappingChanged(): TTL");
220             return true;
221         } else if (!Objects.equals(oldMapping.getMapVersion(), newMapping.getMapVersion())) {
222             LOG.trace("mappingChanged(): mapping version");
223             return true;
224         }
225         return false;
226     }
227
228     private void sendSmrs(MappingRecord record, Set<SubscriberRLOC> subscribers) {
229         Eid eid = record.getEid();
230         handleSmr(eid, subscribers);
231
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);
238         }
239     }
240
241     private void handleSmr(Eid eid, Set<SubscriberRLOC> subscribers) {
242         if (subscribers == null) {
243             return;
244         }
245         final MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress()));
246         LOG.trace("Built SMR packet: " + mrb.build().toString());
247
248         scheduler = new SmrScheduler();
249         scheduler.scheduleSmrs(mrb, subscribers.iterator());
250         addSubscribers(eid, subscribers);
251     }
252
253     @SuppressWarnings("unchecked")
254     private Set<SubscriberRLOC> getSubscribers(Eid address) {
255         return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
256     }
257
258     private void removeSubscribers(Eid address) {
259         mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
260     }
261
262     private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
263         mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
264     }
265
266     private static InetAddress getLocalAddress() {
267         try {
268             Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
269             while (interfaces.hasMoreElements()) {
270                 NetworkInterface current = interfaces.nextElement();
271                 LOG.debug("Interface " + current.toString());
272                 if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
273                     continue;
274                 }
275                 Enumeration<InetAddress> addresses = current.getInetAddresses();
276                 while (addresses.hasMoreElements()) {
277                     InetAddress currentAddr = addresses.nextElement();
278                     // Skip loopback and link local addresses
279                     if (currentAddr.isLoopbackAddress() || currentAddr.isLinkLocalAddress()) {
280                         continue;
281                     }
282                     LOG.debug(currentAddr.getHostAddress());
283                     return currentAddr;
284                 }
285             }
286         } catch (SocketException se) {
287             LOG.debug("Caught socket exception", se);
288         }
289         return null;
290     }
291
292     @Override
293     public void onSmrInvokedReceived(SmrEvent event) {
294         scheduler.smrReceived(event.getSubscriberAddressList());
295     }
296
297     /**
298      * Task scheduler is responsible for resending SMR messages to a subscriber (xTR)
299      * {@value ConfigIni#LISP_SMR_RETRY_COUNT} times, or until {@link ISmrNotificationListener#onSmrInvokedReceived}
300      * is triggered.
301      */
302     private class SmrScheduler {
303         private ScheduledExecutorService executor;
304
305         /**
306          * Scheduling SMRs is done in a single thread. If a use case demands better performance, this may be revisited
307          * and for multi-threading we will need ConcurrentHashMap here along with a multi-threaded executor.
308          */
309         private Map<IpAddressBinary, ScheduledFuture<?>> subscriberFutureMap = new HashMap<>();
310         private long smrNonce;
311
312         void scheduleSmrs(MapRequestBuilder mrb, Iterator<SubscriberRLOC> subscribers) {
313             executor = Executors.newSingleThreadScheduledExecutor();
314             smrNonce = mrb.getNonce();
315
316             // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a SubscriberRLOC
317             // from a Set.
318             while (subscribers.hasNext()) {
319                 SubscriberRLOC subscriber = subscribers.next();
320                 if (subscriber.timedOut()) {
321                     LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString());
322                     subscribers.remove();
323                 } else {
324                     final ScheduledFuture<?> future = executor.scheduleAtFixedRate(new CancellableRunnable(
325                             mrb, subscriber), 0L, ConfigIni.getInstance().getSmrTimeout(), TimeUnit.MILLISECONDS);
326                     subscriberFutureMap.put(
327                             LispAddressUtil.addressBinaryFromAddress(subscriber.getSrcRloc().getAddress()), future);
328                 }
329             }
330         }
331
332         void smrReceived(List<IpAddressBinary> subscriberAddressList) {
333             ScheduledFuture<?> future;
334             for (IpAddressBinary subscriberAddress : subscriberAddressList) {
335                 future = subscriberFutureMap.get(subscriberAddress);
336                 if (future != null && !future.isCancelled()) {
337                     future.cancel(false);
338                     LOG.trace("SMR-invoked MapRequest received, scheduled task for subscriber {} with nonce {} has "
339                             + "been canceled", subscriberAddress.toString(), smrNonce);
340                 }
341             }
342         }
343
344         private final class CancellableRunnable implements Runnable {
345             private MapRequestBuilder mrb;
346             private SubscriberRLOC subscriber;
347             private int executionCount = 1;
348
349             CancellableRunnable(MapRequestBuilder mrb, SubscriberRLOC subscriber) {
350                 this.mrb = mrb;
351                 this.subscriber = subscriber;
352             }
353
354             @SuppressWarnings("checkstyle:IllegalCatch")
355             @Override
356             public void run() {
357                 final IpAddressBinary subscriberAddress = LispAddressUtil
358                         .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress());
359                 try {
360                     // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked
361                     // Map-Request. To ensure consistent behavior it is set to the value used to originally request
362                     // a given mapping.
363                     if (executionCount <= ConfigIni.getInstance().getSmrRetryCount()) {
364                         mrb.setEidItem(new ArrayList<EidItem>());
365                         mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build());
366                         notifyHandler.handleSMR(mrb.build(), subscriber.getSrcRloc());
367                         LOG.trace("{}. attempt to send SMR for MapRequest " + subscriber.getSrcEid(), executionCount);
368                     } else {
369                         LOG.trace("Cancelling execution of a SMR Map-Request after {} failed attempts.",
370                                 executionCount - 1);
371                         subscriberFutureMap.get(subscriberAddress).cancel(false);
372                     }
373                 } catch (Exception e) {
374                     LOG.error("Errors encountered while handling SMR:", e);
375                     subscriberFutureMap.get(subscriberAddress).cancel(false);
376                 } finally {
377                     tryToShutDownExecutor();
378                 }
379                 executionCount++;
380             }
381         }
382
383         private void tryToShutDownExecutor() {
384             if (!executor.isShutdown() && areAllFuturesCanceled()) {
385                 executor.shutdown();
386                 LOG.trace("SMR scheduler executor shutdown.");
387             }
388         }
389
390         private boolean areAllFuturesCanceled() {
391             for (Map.Entry<IpAddressBinary, ScheduledFuture<?>> entry : subscriberFutureMap.entrySet()) {
392                 if (!entry.getValue().isCancelled()) {
393                     return false;
394                 }
395             }
396             return true;
397         }
398     }
399 }