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.controller.md.sal.binding.api.NotificationService;
17 import org.opendaylight.lispflowmapping.config.ConfigIni;
18 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
19 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
20 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
21 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
27 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
28 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
29 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
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<MapReply>();
77 private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify =
78 new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
79 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest =
80 new ThreadLocal<Pair<MapRequest, TransportAddress>>();
82 private final OdlLispSbService lispSB;
83 private IMapResolverAsync mapResolver;
84 private IMapServerAsync mapServer;
86 private final IMappingService mapService;
87 private final NotificationService notificationService;
89 public LispMappingService(final NotificationService notificationService,
90 final IMappingService mappingService,
91 final OdlLispSbService odlLispService, final ClusterSingletonServiceProvider clusterSingletonService) {
93 this.notificationService = notificationService;
94 this.mapService = mappingService;
95 this.lispSB = odlLispService;
96 this.clusterSingletonService = clusterSingletonService;
97 LOG.debug("LispMappingService Module constructed!");
100 public boolean shouldUseSmr() {
104 public void setShouldUseSmr(boolean shouldUseSmr) {
105 this.smr = shouldUseSmr;
106 if (mapServer != null) {
107 mapServer.setSubscriptionService(shouldUseSmr);
109 if (mapResolver != null) {
110 mapResolver.setSubscriptionService(shouldUseSmr);
112 ConfigIni.getInstance().setSmr(shouldUseSmr);
115 public NotificationService getNotificationService() {
116 return this.notificationService;
119 public void initialize() {
120 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
121 mapServer = new MapServer(mapService, smr, this, notificationService);
122 this.clusterSingletonService.registerClusterSingletonService(this);
123 mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
124 LOG.info("LISP (RFC6830) Mapping Service init finished");
127 public void basicInit() {
128 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
129 mapServer = new MapServer(mapService, smr, this, notificationService);
130 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();
154 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
155 if (LOG.isDebugEnabled()) {
156 LOG.debug("LISP: Adding mapping for {}",
157 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
158 .getMappingRecord().getEid()));
161 tlsMapNotify.set(null);
162 mapServer.handleMapRegister(mapRegister);
163 // After this invocation we assume that the thread local is filled with the reply
164 return tlsMapNotify.get();
167 public void setShouldAuthenticate(boolean shouldAuthenticate) {
168 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
171 private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
172 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
173 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
174 smnib.setTransportAddress(address);
175 getLispSB().sendMapNotify(smnib.build());
179 public void onAddMapping(AddMapping mapRegisterNotification) {
180 Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
181 if (result != null && result.getLeft() != null) {
182 MapNotify mapNotify = result.getLeft();
183 List<TransportAddress> rlocs = result.getRight();
185 TransportAddressBuilder tab = new TransportAddressBuilder();
186 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
187 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
188 sendMapNotify(mapNotify, tab.build());
190 for (TransportAddress ta : rlocs) {
191 sendMapNotify(mapNotify, ta);
195 LOG.debug("Not sending Map-Notify");
200 public void onRequestMapping(RequestMapping mapRequestNotification) {
201 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
202 if (mapReply != null) {
203 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
204 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
205 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
206 getLispSB().sendMapReply(smrib.build());
208 LOG.debug("handleMapRequest: Got null MapReply");
213 public void onGotMapReply(GotMapReply notification) {
214 LOG.debug("Received GotMapReply notification, ignoring");
218 public void onGotMapNotify(GotMapNotify notification) {
219 LOG.debug("Received GotMapNotify notification, ignoring");
223 public void onXtrRequestMapping(XtrRequestMapping notification) {
224 LOG.debug("Received XtrRequestMapping notification, ignoring");
228 public void onXtrReplyMapping(XtrReplyMapping notification) {
229 LOG.debug("Received XtrReplyMapping notification, ignoring");
233 public void onMappingKeepAlive(MappingKeepAlive notification) {
234 final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
235 for (EidLispAddress eidLispAddress : cacheMetadata.getEidLispAddress()) {
236 final Eid eid = eidLispAddress.getEid();
237 final XtrId xtrId = cacheMetadata.getXtrId();
238 final Long timestamp = cacheMetadata.getTimestamp();
239 LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
241 mapService.refreshMappingRegistration(eid, xtrId, timestamp);
245 private OdlLispSbService getLispSB() {
250 public void handleMapReply(MapReply reply) {
251 tlsMapReply.set(reply);
255 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
256 tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
260 public void handleSMR(MapRequest smrMapRequest, Rloc subscriber) {
261 if (LOG.isDebugEnabled()) {
262 LOG.debug("Sending SMR Map-Request to {} with Source-EID {} and EID Record {} (reversed)",
263 LispAddressStringifier.getString(subscriber),
264 LispAddressStringifier.getString(smrMapRequest.getSourceEid().getEid()),
265 LispAddressStringifier.getString(smrMapRequest.getEidItem().get(0).getEid()));
267 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
268 smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
269 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
270 getLispSB().sendMapRequest(smrib.build());
275 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
276 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
279 private void destroy() {
280 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
286 public void close() throws Exception {
288 clusterSingletonService.close();
292 public void instantiateServiceInstance() {
293 mapService.setIsMaster(true);
297 public ListenableFuture<Void> closeServiceInstance() {
298 if (mapService != null) {
299 mapService.setIsMaster(false);
301 return Futures.<Void>immediateFuture(null);
305 public ServiceGroupIdentifier getIdentifier() {
306 return SERVICE_GROUP_IDENTIFIER;