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