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