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
9 package org.opendaylight.lispflowmapping.implementation;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.List;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.apache.commons.lang3.tuple.MutablePair;
20 import org.apache.commons.lang3.tuple.Pair;
21 import org.opendaylight.lispflowmapping.config.ConfigIni;
22 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
23 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
24 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
27 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
28 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
29 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
30 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
31 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
32 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
33 import org.opendaylight.mdsal.binding.api.NotificationService;
34 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
35 import org.opendaylight.mdsal.binding.api.RpcProviderService;
36 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
37 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
38 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
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.SendMapRequestInputBuilder;
65 import org.opendaylight.yangtools.concepts.Registration;
66 import org.osgi.service.component.annotations.Activate;
67 import org.osgi.service.component.annotations.Component;
68 import org.osgi.service.component.annotations.Deactivate;
69 import org.osgi.service.component.annotations.Reference;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
74 @Component(service = {IFlowMapping.class, IMapRequestResultHandler.class, IMapNotifyHandler.class},
75 immediate = true, property = "type=default")
76 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
77 IMapNotifyHandler, AutoCloseable, ClusterSingletonService {
78 private static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
79 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
80 LISPFLOWMAPPING_ENTITY_NAME);
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 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<>();
91 private IMapResolverAsync mapResolver;
92 private MapServer mapServer;
94 private final IMappingService mapService;
95 private final OdlLispSbService lispSB;
96 private final ClusterSingletonServiceProvider clusterSingletonService;
97 private final NotificationService notificationService;
98 private final Registration rpcRegistration;
99 private final Registration listenerRegistration;
103 public LispMappingService(@Reference final IMappingService mappingService,
104 @Reference final OdlLispSbService odlLispService,
105 @Reference final ClusterSingletonServiceProvider clusterSingletonService,
106 @Reference final RpcProviderService rpcProviderService,
107 @Reference final NotificationService notificationService) {
108 this.mapService = mappingService;
109 this.lispSB = odlLispService;
110 this.clusterSingletonService = clusterSingletonService;
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 rpcRegistration = rpcProviderService.registerRpcImplementation(OdlLispSbService.class, lispSB);
124 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
125 mapServer = new MapServer(mapService, smr, this, notificationService);
126 clusterSingletonService.registerClusterSingletonService(this);
127 mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
128 LOG.info("LISP (RFC6830) Mapping Service initialized");
131 public boolean shouldUseSmr() {
136 public void setShouldUseSmr(boolean shouldUseSmr) {
137 this.smr = shouldUseSmr;
138 if (mapServer != null) {
139 mapServer.setSubscriptionService(shouldUseSmr);
141 if (mapResolver != null) {
142 mapResolver.setSubscriptionService(shouldUseSmr);
144 ConfigIni.getInstance().setSmr(shouldUseSmr);
147 public NotificationService getNotificationService() {
148 return this.notificationService;
152 public MapReply handleMapRequest(MapRequest request) {
153 if (LOG.isDebugEnabled()) {
154 LOG.debug("LISP: Retrieving mapping for {}",
155 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
158 tlsMapReply.set(null);
159 tlsMapRequest.set(null);
160 mapResolver.handleMapRequest(request);
161 // After this invocation we assume that the thread local is filled with the reply
162 if (tlsMapRequest.get() != null) {
163 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
164 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
165 smrib.setTransportAddress(tlsMapRequest.get().getRight());
166 getLispSB().sendMapRequest(smrib.build());
169 return tlsMapReply.get();
174 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
175 if (LOG.isDebugEnabled()) {
176 LOG.debug("LISP: Adding mapping for {}",
177 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
178 .getMappingRecord().getEid()));
181 tlsMapNotify.set(null);
182 mapServer.handleMapRegister(mapRegister);
183 // After this invocation we assume that the thread local is filled with the reply
184 return tlsMapNotify.get();
187 public void setShouldAuthenticate(boolean shouldAuthenticate) {
188 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
191 private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
192 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
193 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
194 smnib.setTransportAddress(address);
195 getLispSB().sendMapNotify(smnib.build());
199 void onAddMapping(AddMapping mapRegisterNotification) {
200 Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
201 if (result != null && result.getLeft() != null) {
202 MapNotify mapNotify = result.getLeft();
203 List<TransportAddress> rlocs = result.getRight();
205 TransportAddressBuilder tab = new TransportAddressBuilder();
206 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
207 tab.setPort(new PortNumber(LispMessage.PORT_NUMBER));
208 sendMapNotify(mapNotify, tab.build());
210 for (TransportAddress ta : rlocs) {
211 sendMapNotify(mapNotify, ta);
215 LOG.debug("Not sending Map-Notify");
220 void onRequestMapping(RequestMapping mapRequestNotification) {
221 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
222 if (mapReply != null) {
223 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
224 smrib.setMapReply(new MapReplyBuilder(mapReply).build());
225 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
226 getLispSB().sendMapReply(smrib.build());
228 LOG.debug("handleMapRequest: Got null MapReply");
233 void onGotMapReply(GotMapReply notification) {
234 LOG.debug("Received GotMapReply notification, ignoring");
238 void onGotMapNotify(GotMapNotify notification) {
239 LOG.debug("Received GotMapNotify notification, ignoring");
243 void onXtrRequestMapping(XtrRequestMapping notification) {
244 LOG.debug("Received XtrRequestMapping notification, ignoring");
248 void onXtrReplyMapping(XtrReplyMapping notification) {
249 LOG.debug("Received XtrReplyMapping notification, ignoring");
253 void onMappingKeepAlive(MappingKeepAlive notification) {
254 final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
255 for (EidLispAddress eidLispAddress : cacheMetadata.nonnullEidLispAddress().values()) {
256 final Eid eid = eidLispAddress.getEid();
257 final XtrId xtrId = cacheMetadata.getXtrId();
258 final Long timestamp = cacheMetadata.getTimestamp();
259 LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
261 mapService.refreshMappingRegistration(eid, xtrId, timestamp);
265 private OdlLispSbService getLispSB() {
270 public void handleMapReply(MapReply reply) {
271 tlsMapReply.set(reply);
275 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
276 tlsMapNotify.set(new MutablePair<>(notify, rlocs));
280 public void handleSMR(MapRequest smrMapRequest, Rloc subscriber) {
281 if (LOG.isDebugEnabled()) {
282 LOG.debug("Sending SMR Map-Request to {} with Source-EID {} and EID Record {} (reversed)",
283 LispAddressStringifier.getString(subscriber),
284 LispAddressStringifier.getString(smrMapRequest.getSourceEid().getEid()),
285 LispAddressStringifier.getString(smrMapRequest.getEidItem().get(0).getEid()));
287 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
288 smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
289 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
290 getLispSB().sendMapRequest(smrib.build());
295 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
296 tlsMapRequest.set(new MutablePair<>(mapRequest, transportAddress));
299 private void destroy() {
300 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
302 if (mapServer != null) {
311 public void close() throws Exception {
313 clusterSingletonService.close();
314 rpcRegistration.close();
315 listenerRegistration.close();
319 public void instantiateServiceInstance() {
320 mapService.setIsMaster(true);
324 public ListenableFuture<Void> closeServiceInstance() {
325 if (mapService != null) {
326 mapService.setIsMaster(false);
328 return Futures.<Void>immediateFuture(null);
332 public ServiceGroupIdentifier getIdentifier() {
333 return SERVICE_GROUP_IDENTIFIER;