Do not use NotificationListener in LISP components
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.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;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.List;
15 import java.util.Set;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.apache.commons.lang3.tuple.MutablePair;
20 import org.apache.commons.lang3.tuple.Pair;
21 import org.opendaylight.lispflowmapping.config.ConfigIni;
22 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
23 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
24 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
27 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
28 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
29 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
30 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
31 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
32 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
33 import org.opendaylight.mdsal.binding.api.NotificationService;
34 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
35 import org.opendaylight.mdsal.binding.api.RpcProviderService;
36 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
37 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
38 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
65 import org.opendaylight.yangtools.concepts.Registration;
66 import org.osgi.service.component.annotations.Activate;
67 import org.osgi.service.component.annotations.Component;
68 import org.osgi.service.component.annotations.Deactivate;
69 import org.osgi.service.component.annotations.Reference;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72
73 @Singleton
74 @Component(service = {IFlowMapping.class, IMapRequestResultHandler.class, IMapNotifyHandler.class},
75         immediate = true, property = "type=default")
76 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
77         IMapNotifyHandler, AutoCloseable, ClusterSingletonService {
78     private static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
79     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
80             LISPFLOWMAPPING_ENTITY_NAME);
81
82     private static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
83
84     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
85     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
86
87     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<>();
88     private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify = new ThreadLocal<>();
89     private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<>();
90
91     private IMapResolverAsync mapResolver;
92     private MapServer mapServer;
93
94     private final IMappingService mapService;
95     private final OdlLispSbService lispSB;
96     private final ClusterSingletonServiceProvider clusterSingletonService;
97     private final NotificationService notificationService;
98     private final Registration rpcRegistration;
99     private final Registration listenerRegistration;
100
101     @Inject
102     @Activate
103     public LispMappingService(@Reference final IMappingService mappingService,
104             @Reference final OdlLispSbService odlLispService,
105             @Reference final ClusterSingletonServiceProvider clusterSingletonService,
106             @Reference final RpcProviderService rpcProviderService,
107             @Reference final NotificationService notificationService) {
108         this.mapService = mappingService;
109         this.lispSB = odlLispService;
110         this.clusterSingletonService = clusterSingletonService;
111         this.notificationService = notificationService;
112
113         // initialize
114         listenerRegistration = notificationService.registerCompositeListener(new CompositeListener(Set.of(
115                 new CompositeListener.Component<>(AddMapping.class, this::onAddMapping),
116                 new CompositeListener.Component<>(GotMapNotify.class, this::onGotMapNotify),
117                 new CompositeListener.Component<>(RequestMapping.class, this::onRequestMapping),
118                 new CompositeListener.Component<>(GotMapReply.class, this::onGotMapReply),
119                 new CompositeListener.Component<>(XtrRequestMapping.class, this::onXtrRequestMapping),
120                 new CompositeListener.Component<>(XtrReplyMapping.class, this::onXtrReplyMapping),
121                 new CompositeListener.Component<>(MappingKeepAlive.class, this::onMappingKeepAlive))));
122         rpcRegistration = rpcProviderService.registerRpcImplementation(OdlLispSbService.class, lispSB);
123
124         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
125         mapServer = new MapServer(mapService, smr, this, notificationService);
126         clusterSingletonService.registerClusterSingletonService(this);
127         mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
128         LOG.info("LISP (RFC6830) Mapping Service initialized");
129     }
130
131     public boolean shouldUseSmr() {
132         return this.smr;
133     }
134
135     @Override
136     public void setShouldUseSmr(boolean shouldUseSmr) {
137         this.smr = shouldUseSmr;
138         if (mapServer != null) {
139             mapServer.setSubscriptionService(shouldUseSmr);
140         }
141         if (mapResolver != null) {
142             mapResolver.setSubscriptionService(shouldUseSmr);
143         }
144         ConfigIni.getInstance().setSmr(shouldUseSmr);
145     }
146
147     public NotificationService getNotificationService() {
148         return this.notificationService;
149     }
150
151     @Override
152     public MapReply handleMapRequest(MapRequest request) {
153         if (LOG.isDebugEnabled()) {
154             LOG.debug("LISP: Retrieving mapping for {}",
155                     LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
156         }
157
158         tlsMapReply.set(null);
159         tlsMapRequest.set(null);
160         mapResolver.handleMapRequest(request);
161         // After this invocation we assume that the thread local is filled with the reply
162         if (tlsMapRequest.get() != null) {
163             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
164             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
165             smrib.setTransportAddress(tlsMapRequest.get().getRight());
166             getLispSB().sendMapRequest(smrib.build());
167             return null;
168         } else {
169             return tlsMapReply.get();
170         }
171     }
172
173     @Override
174     public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
175         if (LOG.isDebugEnabled()) {
176             LOG.debug("LISP: Adding mapping for {}",
177                     LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
178                             .getMappingRecord().getEid()));
179         }
180
181         tlsMapNotify.set(null);
182         mapServer.handleMapRegister(mapRegister);
183         // After this invocation we assume that the thread local is filled with the reply
184         return tlsMapNotify.get();
185     }
186
187     public void setShouldAuthenticate(boolean shouldAuthenticate) {
188         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
189     }
190
191     private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
192         SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
193         smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
194         smnib.setTransportAddress(address);
195         getLispSB().sendMapNotify(smnib.build());
196     }
197
198     @VisibleForTesting
199     void onAddMapping(AddMapping mapRegisterNotification) {
200         Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
201         if (result != null && result.getLeft() != null) {
202             MapNotify mapNotify = result.getLeft();
203             List<TransportAddress> rlocs = result.getRight();
204             if (rlocs == null) {
205                 TransportAddressBuilder tab = new TransportAddressBuilder();
206                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
207                 tab.setPort(new PortNumber(LispMessage.PORT_NUMBER));
208                 sendMapNotify(mapNotify, tab.build());
209             } else {
210                 for (TransportAddress ta : rlocs) {
211                     sendMapNotify(mapNotify, ta);
212                 }
213             }
214         } else {
215             LOG.debug("Not sending Map-Notify");
216         }
217     }
218
219     @VisibleForTesting
220     void onRequestMapping(RequestMapping mapRequestNotification) {
221         MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
222         if (mapReply != null) {
223             SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
224             smrib.setMapReply(new MapReplyBuilder(mapReply).build());
225             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
226             getLispSB().sendMapReply(smrib.build());
227         } else {
228             LOG.debug("handleMapRequest: Got null MapReply");
229         }
230     }
231
232     @VisibleForTesting
233     void onGotMapReply(GotMapReply notification) {
234         LOG.debug("Received GotMapReply notification, ignoring");
235     }
236
237     @VisibleForTesting
238     void onGotMapNotify(GotMapNotify notification) {
239         LOG.debug("Received GotMapNotify notification, ignoring");
240     }
241
242     @VisibleForTesting
243     void onXtrRequestMapping(XtrRequestMapping notification) {
244         LOG.debug("Received XtrRequestMapping notification, ignoring");
245     }
246
247     @VisibleForTesting
248     void onXtrReplyMapping(XtrReplyMapping notification) {
249         LOG.debug("Received XtrReplyMapping notification, ignoring");
250     }
251
252     @VisibleForTesting
253     void onMappingKeepAlive(MappingKeepAlive notification) {
254         final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
255         for (EidLispAddress eidLispAddress : cacheMetadata.nonnullEidLispAddress().values()) {
256             final Eid eid = eidLispAddress.getEid();
257             final XtrId xtrId = cacheMetadata.getXtrId();
258             final Long timestamp = cacheMetadata.getTimestamp();
259             LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
260                     timestamp);
261             mapService.refreshMappingRegistration(eid, xtrId, timestamp);
262         }
263     }
264
265     private OdlLispSbService getLispSB() {
266         return lispSB;
267     }
268
269     @Override
270     public void handleMapReply(MapReply reply) {
271         tlsMapReply.set(reply);
272     }
273
274     @Override
275     public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
276         tlsMapNotify.set(new MutablePair<>(notify, rlocs));
277     }
278
279     @Override
280     public void handleSMR(MapRequest smrMapRequest, Rloc subscriber) {
281         if (LOG.isDebugEnabled()) {
282             LOG.debug("Sending SMR Map-Request to {} with Source-EID {} and EID Record {} (reversed)",
283                     LispAddressStringifier.getString(subscriber),
284                     LispAddressStringifier.getString(smrMapRequest.getSourceEid().getEid()),
285                     LispAddressStringifier.getString(smrMapRequest.getEidItem().get(0).getEid()));
286         }
287         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
288         smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
289         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
290         getLispSB().sendMapRequest(smrib.build());
291
292     }
293
294     @Override
295     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
296         tlsMapRequest.set(new MutablePair<>(mapRequest, transportAddress));
297     }
298
299     private void destroy() {
300         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
301         mapResolver = null;
302         if (mapServer != null) {
303             mapServer.close();
304             mapServer = null;
305         }
306     }
307
308     @Deactivate
309     @PreDestroy
310     @Override
311     public void close() throws Exception {
312         destroy();
313         clusterSingletonService.close();
314         rpcRegistration.close();
315         listenerRegistration.close();
316     }
317
318     @Override
319     public void instantiateServiceInstance() {
320         mapService.setIsMaster(true);
321     }
322
323     @Override
324     public ListenableFuture<Void> closeServiceInstance() {
325         if (mapService != null) {
326             mapService.setIsMaster(false);
327         }
328         return Futures.<Void>immediateFuture(null);
329     }
330
331     @Override
332     public ServiceGroupIdentifier getIdentifier() {
333         return SERVICE_GROUP_IDENTIFIER;
334     }
335 }