2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.implementation;
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;
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;
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");
82 private static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
84 private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
85 private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
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;
97 private final IMappingService mapService;
98 private final NotificationService notificationService;
99 private final Registration listenerRegistration;
100 private final Registration cssRegistration;
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;
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");
129 public boolean shouldUseSmr() {
134 public void setShouldUseSmr(boolean shouldUseSmr) {
135 this.smr = shouldUseSmr;
136 if (mapServer != null) {
137 mapServer.setSubscriptionService(shouldUseSmr);
139 if (mapResolver != null) {
140 mapResolver.setSubscriptionService(shouldUseSmr);
142 ConfigIni.getInstance().setSmr(shouldUseSmr);
145 public NotificationService getNotificationService() {
146 return this.notificationService;
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()));
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());
167 return tlsMapReply.get();
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()));
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();
185 public void setShouldAuthenticate(boolean shouldAuthenticate) {
186 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
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());
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();
203 TransportAddressBuilder tab = new TransportAddressBuilder();
204 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
205 tab.setPort(new PortNumber(LispMessage.PORT_NUMBER));
206 sendMapNotify(mapNotify, tab.build());
208 for (TransportAddress ta : rlocs) {
209 sendMapNotify(mapNotify, ta);
213 LOG.debug("Not sending Map-Notify");
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());
226 LOG.debug("handleMapRequest: Got null MapReply");
231 void onGotMapReply(GotMapReply notification) {
232 LOG.debug("Received GotMapReply notification, ignoring");
236 void onGotMapNotify(GotMapNotify notification) {
237 LOG.debug("Received GotMapNotify notification, ignoring");
241 void onXtrRequestMapping(XtrRequestMapping notification) {
242 LOG.debug("Received XtrRequestMapping notification, ignoring");
246 void onXtrReplyMapping(XtrReplyMapping notification) {
247 LOG.debug("Received XtrReplyMapping notification, ignoring");
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),
259 mapService.refreshMappingRegistration(eid, xtrId, timestamp);
264 public void handleMapReply(MapReply reply) {
265 tlsMapReply.set(reply);
269 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
270 tlsMapNotify.set(new MutablePair<>(notify, rlocs));
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()));
281 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
282 smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
283 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
284 sendMapRequest.invoke(smrib.build());
288 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
289 tlsMapRequest.set(new MutablePair<>(mapRequest, transportAddress));
292 private void destroy() {
293 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
295 if (mapServer != null) {
304 public void close() throws Exception {
306 cssRegistration.close();
307 listenerRegistration.close();
311 public void instantiateServiceInstance() {
312 mapService.setIsMaster(true);
316 public ListenableFuture<Void> closeServiceInstance() {
317 if (mapService != null) {
318 mapService.setIsMaster(false);
320 return Futures.<Void>immediateFuture(null);
324 public ServiceGroupIdentifier getIdentifier() {
325 return SERVICE_GROUP_IDENTIFIER;