Merge "Make part of the Subscribers object modeled"
[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 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;
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.Set;
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.Subscriber;
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;
72
73 public class MapServer implements IMapServerAsync, OdlMappingserviceListener, ISmrNotificationListener {
74
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;
83
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);
93         }
94         scheduler = new SmrScheduler();
95     }
96
97     @Override
98     public void setSubscriptionService(boolean subscriptionService) {
99         this.subscriptionService = subscriptionService;
100     }
101
102     @SuppressWarnings("unchecked")
103     public void handleMapRegister(MapRegister mapRegister) {
104         boolean mappingUpdated = false;
105         boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled();
106         Set<Subscriber> subscribers = null;
107         MappingRecord oldMapping;
108
109         if (merge) {
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.");
112                 merge = false;
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.");
115             }
116         }
117
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());
124
125             oldMapping = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid));
126             mapService.addMapping(MappingOrigin.Southbound, eid, getSiteId(mapRegister), mappingData);
127
128             if (subscriptionService) {
129                 MappingRecord newMapping = merge
130                         ? getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid)) : mapping;
131
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()));
136                     }
137                     subscribers = getSubscribers(eid);
138                     if (oldMapping != null && !oldMapping.getEid().equals(eid)) {
139                         subscribers = addParentSubscribers(eid, subscribers);
140                     }
141                     sendSmrs(eid, subscribers);
142                     mappingUpdated = true;
143                 }
144             }
145         }
146         if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
147             LOG.trace("MapRegister wants MapNotify");
148             MapNotifyBuilder builder = new MapNotifyBuilder();
149             List<TransportAddress> rlocs = null;
150             if (merge) {
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,
156                             mapping.getEid()));
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);
162                     }
163                 }
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);
168                 }
169             } else {
170                 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
171             }
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()));
179                 }
180             }
181             notifyHandler.handleMapNotify(builder.build(), rlocs);
182         }
183     }
184
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());
192         }
193         return rlocs;
194     }
195
196     private static SiteId getSiteId(MapRegister mapRegister) {
197         return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
198     }
199
200     private static MappingRecord getMappingRecord(MappingData mappingData) {
201         return (mappingData != null) ? mappingData.getRecord() : null;
202     }
203
204     @Override
205     public void onMappingChanged(MappingChanged notification) {
206         if (subscriptionService) {
207             Eid eid = notification.getMappingRecord().getEid();
208             if (mapService.isMaster()) {
209                 sendSmrs(eid, getSubscribers(eid));
210             }
211             if (notification.getChangeType().equals(MappingChange.Removed)) {
212                 removeSubscribers(eid);
213             }
214         }
215     }
216
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");
223             return true;
224         } else if (!Objects.equals(oldMapping.getEid(), newMapping.getEid())) {
225             LOG.trace("mappingChanged(): EID");
226             return true;
227         } else if (!Objects.equals(oldMapping.getLocatorRecord(), newMapping.getLocatorRecord())) {
228             LOG.trace("mappingChanged(): RLOC");
229             return true;
230         } else if (!Objects.equals(oldMapping.getAction(), newMapping.getAction())) {
231             LOG.trace("mappingChanged(): action");
232             return true;
233         } else if (!Objects.equals(oldMapping.getRecordTtl(), newMapping.getRecordTtl())) {
234             LOG.trace("mappingChanged(): TTL");
235             return true;
236         } else if (!Objects.equals(oldMapping.getMapVersion(), newMapping.getMapVersion())) {
237             LOG.trace("mappingChanged(): mapping version");
238             return true;
239         }
240         return false;
241     }
242
243     private void sendSmrs(Eid eid, Set<Subscriber> subscribers) {
244         handleSmr(eid, subscribers);
245
246         // For SrcDst LCAF also send SMRs to Dst prefix
247         if (eid.getAddress() instanceof SourceDestKey) {
248             Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid);
249             Set<Subscriber> dstSubs = getSubscribers(dstAddr);
250             handleSmr(dstAddr, dstSubs);
251         }
252     }
253
254     private void handleSmr(Eid eid, Set<Subscriber> subscribers) {
255         if (subscribers == null) {
256             return;
257         }
258         final MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress()));
259         LOG.trace("Built SMR packet: " + mrb.build().toString());
260
261         scheduler.scheduleSmrs(mrb, subscribers.iterator());
262         addSubscribers(eid, subscribers);
263     }
264
265     @SuppressWarnings("unchecked")
266     private Set<Subscriber> getSubscribers(Eid address) {
267         return (Set<Subscriber>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
268     }
269
270     private Set<Subscriber> addParentSubscribers(Eid eid, Set<Subscriber> subscribers) {
271         Eid parentPrefix = mapService.getParentPrefix(eid);
272         if (parentPrefix == null) {
273             return subscribers;
274         }
275
276         Set<Subscriber> parentSubscribers = getSubscribers(parentPrefix);
277         if (parentSubscribers != null) {
278             if (subscribers != null) {
279                 subscribers.addAll(parentSubscribers);
280             } else {
281                 subscribers = parentSubscribers;
282             }
283         }
284         return subscribers;
285     }
286
287     private void removeSubscribers(Eid address) {
288         mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
289     }
290
291     private void addSubscribers(Eid address, Set<Subscriber> subscribers) {
292         mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
293     }
294
295     private static InetAddress getLocalAddress() {
296         try {
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()) {
302                     continue;
303                 }
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()) {
309                         continue;
310                     }
311                     LOG.debug(currentAddr.getHostAddress());
312                     return currentAddr;
313                 }
314             }
315         } catch (SocketException se) {
316             LOG.debug("Caught socket exception", se);
317         }
318         return null;
319     }
320
321     @Override
322     public void onSmrInvokedReceived(SmrEvent event) {
323         scheduler.smrReceived(event);
324     }
325
326     /**
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}
329      * is triggered.
330      */
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();
337
338         void scheduleSmrs(MapRequestBuilder mrb, Iterator<Subscriber> subscribers) {
339             // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a Subscriber
340             // from a Set.
341             while (subscribers.hasNext()) {
342                 Subscriber subscriber = subscribers.next();
343                 if (subscriber.timedOut()) {
344                     LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString());
345                     subscribers.remove();
346                 } else {
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());
351
352                     if (subscriberFutureMap.containsKey(subscriberAddress)) {
353                         subscriberFutureMap.get(subscriberAddress).put(mrb.getSourceEid().getEid(), future);
354                     } else {
355                         final Map<Eid, ScheduledFuture<?>> eidFutureMap = Maps.newConcurrentMap();
356                         eidFutureMap.put(mrb.getSourceEid().getEid(), future);
357                         subscriberFutureMap.put(subscriberAddress, eidFutureMap);
358                     }
359                 }
360             }
361         }
362
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());
374                     }
375                     if (eidFutureMap.isEmpty()) {
376                         subscriberFutureMap.remove(subscriberAddress);
377                     }
378                 }
379             }
380         }
381
382         private final class CancellableRunnable implements Runnable {
383             private MapRequestBuilder mrb;
384             private Subscriber subscriber;
385             private int executionCount = 1;
386
387             CancellableRunnable(MapRequestBuilder mrb, Subscriber subscriber) {
388                 this.mrb = mrb;
389                 this.subscriber = subscriber;
390             }
391
392             @SuppressWarnings("checkstyle:IllegalCatch")
393             @Override
394             public void run() {
395                 final IpAddressBinary subscriberAddress = LispAddressUtil
396                         .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress());
397                 try {
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
400                     // a given mapping.
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);
407                     } else {
408                         LOG.trace("Cancelling execution of a SMR Map-Request after {} failed attempts.",
409                                 executionCount - 1);
410                         cancelAndRemove(subscriberAddress);
411                     }
412                 } catch (Exception e) {
413                     LOG.error("Errors encountered while handling SMR:", e);
414                     cancelAndRemove(subscriberAddress);
415                 }
416                 executionCount++;
417             }
418
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);
424                 }
425                 eidFutureMap.remove(eid);
426                 if (eidFutureMap.isEmpty()) {
427                     subscriberFutureMap.remove(subscriberAddress);
428                 }
429             }
430         }
431     }
432 }