Do not publish OdlLispSbService to SR
[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.OdlLispSbService;
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.SendMapReplyInputBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
64 import org.opendaylight.yangtools.concepts.Registration;
65 import org.osgi.service.component.annotations.Activate;
66 import org.osgi.service.component.annotations.Component;
67 import org.osgi.service.component.annotations.Deactivate;
68 import org.osgi.service.component.annotations.Reference;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72 @Singleton
73 @Component(service = {IFlowMapping.class, IMapRequestResultHandler.class, IMapNotifyHandler.class},
74         immediate = true, property = "type=default")
75 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
76         IMapNotifyHandler, AutoCloseable, ClusterSingletonService {
77     private static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
78     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
79             LISPFLOWMAPPING_ENTITY_NAME);
80
81     private static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
82
83     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
84     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
85
86     // These are non-final for testing
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     private IMapResolverAsync mapResolver;
91     private MapServer mapServer;
92     private OdlLispSbService lispSB;
93
94     private final IMappingService mapService;
95     private final ClusterSingletonServiceProvider clusterSingletonService;
96     private final NotificationService notificationService;
97     private final Registration listenerRegistration;
98     private final Registration cssRegistration;
99
100     @Inject
101     @Activate
102     public LispMappingService(@Reference final IMappingService mappingService,
103             @Reference final ClusterSingletonServiceProvider clusterSingletonService,
104             @Reference final RpcConsumerRegistry rpcService, @Reference final NotificationService notificationService) {
105         this.mapService = mappingService;
106         this.lispSB = rpcService.getRpcService(OdlLispSbService.class);
107         this.clusterSingletonService = clusterSingletonService;
108         this.notificationService = notificationService;
109
110         // initialize
111         listenerRegistration = notificationService.registerCompositeListener(new CompositeListener(Set.of(
112                 new CompositeListener.Component<>(AddMapping.class, this::onAddMapping),
113                 new CompositeListener.Component<>(GotMapNotify.class, this::onGotMapNotify),
114                 new CompositeListener.Component<>(RequestMapping.class, this::onRequestMapping),
115                 new CompositeListener.Component<>(GotMapReply.class, this::onGotMapReply),
116                 new CompositeListener.Component<>(XtrRequestMapping.class, this::onXtrRequestMapping),
117                 new CompositeListener.Component<>(XtrReplyMapping.class, this::onXtrReplyMapping),
118                 new CompositeListener.Component<>(MappingKeepAlive.class, this::onMappingKeepAlive))));
119
120         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
121         mapServer = new MapServer(mapService, smr, this, notificationService);
122         cssRegistration = clusterSingletonService.registerClusterSingletonService(this);
123         mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
124         LOG.info("LISP (RFC6830) Mapping Service initialized");
125     }
126
127     public boolean shouldUseSmr() {
128         return this.smr;
129     }
130
131     @Override
132     public void setShouldUseSmr(boolean shouldUseSmr) {
133         this.smr = shouldUseSmr;
134         if (mapServer != null) {
135             mapServer.setSubscriptionService(shouldUseSmr);
136         }
137         if (mapResolver != null) {
138             mapResolver.setSubscriptionService(shouldUseSmr);
139         }
140         ConfigIni.getInstance().setSmr(shouldUseSmr);
141     }
142
143     public NotificationService getNotificationService() {
144         return this.notificationService;
145     }
146
147     @Override
148     public MapReply handleMapRequest(MapRequest request) {
149         if (LOG.isDebugEnabled()) {
150             LOG.debug("LISP: Retrieving mapping for {}",
151                     LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
152         }
153
154         tlsMapReply.set(null);
155         tlsMapRequest.set(null);
156         mapResolver.handleMapRequest(request);
157         // After this invocation we assume that the thread local is filled with the reply
158         if (tlsMapRequest.get() != null) {
159             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
160             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
161             smrib.setTransportAddress(tlsMapRequest.get().getRight());
162             getLispSB().sendMapRequest(smrib.build());
163             return null;
164         } else {
165             return tlsMapReply.get();
166         }
167     }
168
169     @Override
170     public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
171         if (LOG.isDebugEnabled()) {
172             LOG.debug("LISP: Adding mapping for {}",
173                     LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
174                             .getMappingRecord().getEid()));
175         }
176
177         tlsMapNotify.set(null);
178         mapServer.handleMapRegister(mapRegister);
179         // After this invocation we assume that the thread local is filled with the reply
180         return tlsMapNotify.get();
181     }
182
183     public void setShouldAuthenticate(boolean shouldAuthenticate) {
184         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
185     }
186
187     private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
188         SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
189         smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
190         smnib.setTransportAddress(address);
191         getLispSB().sendMapNotify(smnib.build());
192     }
193
194     @VisibleForTesting
195     void onAddMapping(AddMapping mapRegisterNotification) {
196         Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
197         if (result != null && result.getLeft() != null) {
198             MapNotify mapNotify = result.getLeft();
199             List<TransportAddress> rlocs = result.getRight();
200             if (rlocs == null) {
201                 TransportAddressBuilder tab = new TransportAddressBuilder();
202                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
203                 tab.setPort(new PortNumber(LispMessage.PORT_NUMBER));
204                 sendMapNotify(mapNotify, tab.build());
205             } else {
206                 for (TransportAddress ta : rlocs) {
207                     sendMapNotify(mapNotify, ta);
208                 }
209             }
210         } else {
211             LOG.debug("Not sending Map-Notify");
212         }
213     }
214
215     @VisibleForTesting
216     void onRequestMapping(RequestMapping mapRequestNotification) {
217         MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
218         if (mapReply != null) {
219             SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
220             smrib.setMapReply(new MapReplyBuilder(mapReply).build());
221             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
222             getLispSB().sendMapReply(smrib.build());
223         } else {
224             LOG.debug("handleMapRequest: Got null MapReply");
225         }
226     }
227
228     @VisibleForTesting
229     void onGotMapReply(GotMapReply notification) {
230         LOG.debug("Received GotMapReply notification, ignoring");
231     }
232
233     @VisibleForTesting
234     void onGotMapNotify(GotMapNotify notification) {
235         LOG.debug("Received GotMapNotify notification, ignoring");
236     }
237
238     @VisibleForTesting
239     void onXtrRequestMapping(XtrRequestMapping notification) {
240         LOG.debug("Received XtrRequestMapping notification, ignoring");
241     }
242
243     @VisibleForTesting
244     void onXtrReplyMapping(XtrReplyMapping notification) {
245         LOG.debug("Received XtrReplyMapping notification, ignoring");
246     }
247
248     @VisibleForTesting
249     void onMappingKeepAlive(MappingKeepAlive notification) {
250         final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
251         for (EidLispAddress eidLispAddress : cacheMetadata.nonnullEidLispAddress().values()) {
252             final Eid eid = eidLispAddress.getEid();
253             final XtrId xtrId = cacheMetadata.getXtrId();
254             final Long timestamp = cacheMetadata.getTimestamp();
255             LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
256                     timestamp);
257             mapService.refreshMappingRegistration(eid, xtrId, timestamp);
258         }
259     }
260
261     private OdlLispSbService getLispSB() {
262         return lispSB;
263     }
264
265     @Override
266     public void handleMapReply(MapReply reply) {
267         tlsMapReply.set(reply);
268     }
269
270     @Override
271     public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
272         tlsMapNotify.set(new MutablePair<>(notify, rlocs));
273     }
274
275     @Override
276     public void handleSMR(MapRequest smrMapRequest, Rloc subscriber) {
277         if (LOG.isDebugEnabled()) {
278             LOG.debug("Sending SMR Map-Request to {} with Source-EID {} and EID Record {} (reversed)",
279                     LispAddressStringifier.getString(subscriber),
280                     LispAddressStringifier.getString(smrMapRequest.getSourceEid().getEid()),
281                     LispAddressStringifier.getString(smrMapRequest.getEidItem().get(0).getEid()));
282         }
283         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
284         smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
285         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
286         getLispSB().sendMapRequest(smrib.build());
287
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 }