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.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import org.apache.commons.lang3.tuple.MutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.opendaylight.lispflowmapping.config.ConfigIni;
17 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
18 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
19 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
20 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
21 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
26 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
27 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
28 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
29 import org.opendaylight.mdsal.binding.api.NotificationService;
30 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
31 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
32 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
64 IMapNotifyHandler, OdlLispProtoListener, AutoCloseable, ClusterSingletonService {
65 private static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
66 private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
67 LISPFLOWMAPPING_ENTITY_NAME);
70 private static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
71 private final ClusterSingletonServiceProvider clusterSingletonService;
73 private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
74 private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
76 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<>();
77 private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify = new ThreadLocal<>();
78 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<>();
80 private final OdlLispSbService lispSB;
81 private IMapResolverAsync mapResolver;
82 private IMapServerAsync mapServer;
84 private final IMappingService mapService;
85 private final NotificationService notificationService;
87 public LispMappingService(final NotificationService notificationService,
88 final IMappingService mappingService,
89 final OdlLispSbService odlLispService, final ClusterSingletonServiceProvider clusterSingletonService) {
91 this.notificationService = notificationService;
92 this.mapService = mappingService;
93 this.lispSB = odlLispService;
94 this.clusterSingletonService = clusterSingletonService;
95 LOG.debug("LispMappingService Module constructed!");
98 public boolean shouldUseSmr() {
103 public void setShouldUseSmr(boolean shouldUseSmr) {
104 this.smr = shouldUseSmr;
105 if (mapServer != null) {
106 mapServer.setSubscriptionService(shouldUseSmr);
108 if (mapResolver != null) {
109 mapResolver.setSubscriptionService(shouldUseSmr);
111 ConfigIni.getInstance().setSmr(shouldUseSmr);
114 public NotificationService getNotificationService() {
115 return this.notificationService;
118 public void initialize() {
119 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
120 mapServer = new MapServer(mapService, smr, this, notificationService);
121 this.clusterSingletonService.registerClusterSingletonService(this);
122 mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
123 LOG.info("LISP (RFC6830) Mapping Service init finished");
126 public void basicInit() {
127 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
128 mapServer = new MapServer(mapService, smr, this, notificationService);
129 mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
133 public MapReply handleMapRequest(MapRequest request) {
134 if (LOG.isDebugEnabled()) {
135 LOG.debug("LISP: Retrieving mapping for {}",
136 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
139 tlsMapReply.set(null);
140 tlsMapRequest.set(null);
141 mapResolver.handleMapRequest(request);
142 // After this invocation we assume that the thread local is filled with the reply
143 if (tlsMapRequest.get() != null) {
144 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
145 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
146 smrib.setTransportAddress(tlsMapRequest.get().getRight());
147 getLispSB().sendMapRequest(smrib.build());
150 return tlsMapReply.get();
155 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
156 if (LOG.isDebugEnabled()) {
157 LOG.debug("LISP: Adding mapping for {}",
158 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
159 .getMappingRecord().getEid()));
162 tlsMapNotify.set(null);
163 mapServer.handleMapRegister(mapRegister);
164 // After this invocation we assume that the thread local is filled with the reply
165 return tlsMapNotify.get();
168 public void setShouldAuthenticate(boolean shouldAuthenticate) {
169 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
172 private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
173 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
174 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
175 smnib.setTransportAddress(address);
176 getLispSB().sendMapNotify(smnib.build());
180 public void onAddMapping(AddMapping mapRegisterNotification) {
181 Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
182 if (result != null && result.getLeft() != null) {
183 MapNotify mapNotify = result.getLeft();
184 List<TransportAddress> rlocs = result.getRight();
186 TransportAddressBuilder tab = new TransportAddressBuilder();
187 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
188 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
189 sendMapNotify(mapNotify, tab.build());
191 for (TransportAddress ta : rlocs) {
192 sendMapNotify(mapNotify, ta);
196 LOG.debug("Not sending Map-Notify");
201 public void onRequestMapping(RequestMapping mapRequestNotification) {
202 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
203 if (mapReply != null) {
204 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
205 smrib.setMapReply(new MapReplyBuilder(mapReply).build());
206 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
207 getLispSB().sendMapReply(smrib.build());
209 LOG.debug("handleMapRequest: Got null MapReply");
214 public void onGotMapReply(GotMapReply notification) {
215 LOG.debug("Received GotMapReply notification, ignoring");
219 public void onGotMapNotify(GotMapNotify notification) {
220 LOG.debug("Received GotMapNotify notification, ignoring");
224 public void onXtrRequestMapping(XtrRequestMapping notification) {
225 LOG.debug("Received XtrRequestMapping notification, ignoring");
229 public void onXtrReplyMapping(XtrReplyMapping notification) {
230 LOG.debug("Received XtrReplyMapping notification, ignoring");
234 public void onMappingKeepAlive(MappingKeepAlive notification) {
235 final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
236 for (EidLispAddress eidLispAddress : cacheMetadata.nonnullEidLispAddress().values()) {
237 final Eid eid = eidLispAddress.getEid();
238 final XtrId xtrId = cacheMetadata.getXtrId();
239 final Long timestamp = cacheMetadata.getTimestamp();
240 LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
242 mapService.refreshMappingRegistration(eid, xtrId, timestamp);
246 private OdlLispSbService getLispSB() {
251 public void handleMapReply(MapReply reply) {
252 tlsMapReply.set(reply);
256 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
257 tlsMapNotify.set(new MutablePair<>(notify, rlocs));
261 public void handleSMR(MapRequest smrMapRequest, Rloc subscriber) {
262 if (LOG.isDebugEnabled()) {
263 LOG.debug("Sending SMR Map-Request to {} with Source-EID {} and EID Record {} (reversed)",
264 LispAddressStringifier.getString(subscriber),
265 LispAddressStringifier.getString(smrMapRequest.getSourceEid().getEid()),
266 LispAddressStringifier.getString(smrMapRequest.getEidItem().get(0).getEid()));
268 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
269 smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
270 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
271 getLispSB().sendMapRequest(smrib.build());
276 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
277 tlsMapRequest.set(new MutablePair<>(mapRequest, transportAddress));
280 private void destroy() {
281 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
287 public void close() throws Exception {
289 clusterSingletonService.close();
293 public void instantiateServiceInstance() {
294 mapService.setIsMaster(true);
298 public ListenableFuture<Void> closeServiceInstance() {
299 if (mapService != null) {
300 mapService.setIsMaster(false);
302 return Futures.<Void>immediateFuture(null);
306 public ServiceGroupIdentifier getIdentifier() {
307 return SERVICE_GROUP_IDENTIFIER;