c9b28994441bbef82e16cac1a9326094c958a8b1
[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 NotificationService notificationService;
100     private final Registration listenerRegistration;
101     private final Registration cssRegistration;
102
103     @Inject
104     @Activate
105     public LispMappingService(@Reference final IMappingService mappingService,
106             @Reference final ClusterSingletonServiceProvider clusterSingletonService,
107             @Reference final RpcConsumerRegistry rpcService, @Reference final NotificationService notificationService) {
108         this.mapService = mappingService;
109         sendMapRequest = rpcService.getRpc(SendMapRequest.class);
110         sendMapReply = rpcService.getRpc(SendMapReply.class);
111         sendMapNotify = rpcService.getRpc(SendMapNotify.class);
112         this.notificationService = notificationService;
113
114         // initialize
115         listenerRegistration = notificationService.registerCompositeListener(new CompositeListener(Set.of(
116                 new CompositeListener.Component<>(AddMapping.class, this::onAddMapping),
117                 new CompositeListener.Component<>(GotMapNotify.class, this::onGotMapNotify),
118                 new CompositeListener.Component<>(RequestMapping.class, this::onRequestMapping),
119                 new CompositeListener.Component<>(GotMapReply.class, this::onGotMapReply),
120                 new CompositeListener.Component<>(XtrRequestMapping.class, this::onXtrRequestMapping),
121                 new CompositeListener.Component<>(XtrReplyMapping.class, this::onXtrReplyMapping),
122                 new CompositeListener.Component<>(MappingKeepAlive.class, this::onMappingKeepAlive))));
123         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
124         mapServer = new MapServer(mapService, smr, this, notificationService);
125         cssRegistration = clusterSingletonService.registerClusterSingletonService(this);
126         mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
127         LOG.info("LISP (RFC6830) Mapping Service initialized");
128     }
129
130     public boolean shouldUseSmr() {
131         return this.smr;
132     }
133
134     @Override
135     public void setShouldUseSmr(boolean shouldUseSmr) {
136         this.smr = shouldUseSmr;
137         if (mapServer != null) {
138             mapServer.setSubscriptionService(shouldUseSmr);
139         }
140         if (mapResolver != null) {
141             mapResolver.setSubscriptionService(shouldUseSmr);
142         }
143         ConfigIni.getInstance().setSmr(shouldUseSmr);
144     }
145
146     public NotificationService getNotificationService() {
147         return this.notificationService;
148     }
149
150     @Override
151     public MapReply handleMapRequest(MapRequest request) {
152         if (LOG.isDebugEnabled()) {
153             LOG.debug("LISP: Retrieving mapping for {}",
154                     LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
155         }
156
157         tlsMapReply.set(null);
158         tlsMapRequest.set(null);
159         mapResolver.handleMapRequest(request);
160         // After this invocation we assume that the thread local is filled with the reply
161         if (tlsMapRequest.get() != null) {
162             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
163             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
164             smrib.setTransportAddress(tlsMapRequest.get().getRight());
165             sendMapRequest.invoke(smrib.build());
166             return null;
167         } else {
168             return tlsMapReply.get();
169         }
170     }
171
172     @Override
173     public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
174         if (LOG.isDebugEnabled()) {
175             LOG.debug("LISP: Adding mapping for {}",
176                     LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
177                             .getMappingRecord().getEid()));
178         }
179
180         tlsMapNotify.set(null);
181         mapServer.handleMapRegister(mapRegister);
182         // After this invocation we assume that the thread local is filled with the reply
183         return tlsMapNotify.get();
184     }
185
186     public void setShouldAuthenticate(boolean shouldAuthenticate) {
187         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
188     }
189
190     private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
191         SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
192         smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
193         smnib.setTransportAddress(address);
194         sendMapNotify.invoke(smnib.build());
195     }
196
197     @VisibleForTesting
198     void onAddMapping(AddMapping mapRegisterNotification) {
199         Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
200         if (result != null && result.getLeft() != null) {
201             MapNotify mapNotify = result.getLeft();
202             List<TransportAddress> rlocs = result.getRight();
203             if (rlocs == null) {
204                 TransportAddressBuilder tab = new TransportAddressBuilder();
205                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
206                 tab.setPort(new PortNumber(LispMessage.PORT_NUMBER));
207                 sendMapNotify(mapNotify, tab.build());
208             } else {
209                 for (TransportAddress ta : rlocs) {
210                     sendMapNotify(mapNotify, ta);
211                 }
212             }
213         } else {
214             LOG.debug("Not sending Map-Notify");
215         }
216     }
217
218     @VisibleForTesting
219     void onRequestMapping(RequestMapping mapRequestNotification) {
220         MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
221         if (mapReply != null) {
222             SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
223             smrib.setMapReply(new MapReplyBuilder(mapReply).build());
224             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
225             sendMapReply.invoke(smrib.build());
226         } else {
227             LOG.debug("handleMapRequest: Got null MapReply");
228         }
229     }
230
231     @VisibleForTesting
232     void onGotMapReply(GotMapReply notification) {
233         LOG.debug("Received GotMapReply notification, ignoring");
234     }
235
236     @VisibleForTesting
237     void onGotMapNotify(GotMapNotify notification) {
238         LOG.debug("Received GotMapNotify notification, ignoring");
239     }
240
241     @VisibleForTesting
242     void onXtrRequestMapping(XtrRequestMapping notification) {
243         LOG.debug("Received XtrRequestMapping notification, ignoring");
244     }
245
246     @VisibleForTesting
247     void onXtrReplyMapping(XtrReplyMapping notification) {
248         LOG.debug("Received XtrReplyMapping notification, ignoring");
249     }
250
251     @VisibleForTesting
252     void onMappingKeepAlive(MappingKeepAlive notification) {
253         final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
254         for (EidLispAddress eidLispAddress : cacheMetadata.nonnullEidLispAddress().values()) {
255             final Eid eid = eidLispAddress.getEid();
256             final XtrId xtrId = cacheMetadata.getXtrId();
257             final Long timestamp = cacheMetadata.getTimestamp();
258             LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
259                     timestamp);
260             mapService.refreshMappingRegistration(eid, xtrId, timestamp);
261         }
262     }
263
264     @Override
265     public void handleMapReply(MapReply reply) {
266         tlsMapReply.set(reply);
267     }
268
269     @Override
270     public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
271         tlsMapNotify.set(new MutablePair<>(notify, rlocs));
272     }
273
274     @Override
275     public void handleSMR(MapRequest smrMapRequest, Rloc subscriber) {
276         if (LOG.isDebugEnabled()) {
277             LOG.debug("Sending SMR Map-Request to {} with Source-EID {} and EID Record {} (reversed)",
278                     LispAddressStringifier.getString(subscriber),
279                     LispAddressStringifier.getString(smrMapRequest.getSourceEid().getEid()),
280                     LispAddressStringifier.getString(smrMapRequest.getEidItem().get(0).getEid()));
281         }
282         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
283         smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
284         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
285         sendMapRequest.invoke(smrib.build());
286     }
287
288     @Override
289     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
290         tlsMapRequest.set(new MutablePair<>(mapRequest, transportAddress));
291     }
292
293     private void destroy() {
294         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
295         mapResolver = null;
296         if (mapServer != null) {
297             mapServer.close();
298             mapServer = null;
299         }
300     }
301
302     @Deactivate
303     @PreDestroy
304     @Override
305     public void close() throws Exception {
306         destroy();
307         cssRegistration.close();
308         listenerRegistration.close();
309     }
310
311     @Override
312     public void instantiateServiceInstance() {
313         mapService.setIsMaster(true);
314     }
315
316     @Override
317     public ListenableFuture<Void> closeServiceInstance() {
318         if (mapService != null) {
319             mapService.setIsMaster(false);
320         }
321         return Futures.<Void>immediateFuture(null);
322     }
323
324     @Override
325     public ServiceGroupIdentifier getIdentifier() {
326         return SERVICE_GROUP_IDENTIFIER;
327     }
328 }