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.XtrReplyMapping;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
64 IMapNotifyHandler, OdlLispProtoListener, AutoCloseable, ClusterSingletonService {
65 public static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
66 public static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
67 LISPFLOWMAPPING_ENTITY_NAME);
70 protected 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 smr) {
106 if (mapServer != null) {
107 mapServer.setSubscriptionService(smr);
109 if (mapResolver != null) {
110 mapResolver.setSubscriptionService(smr);
112 ConfigIni.getInstance().setSmr(smr);
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("DAO: 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("DAO: 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 Long timestamp = cacheMetadata.getTimestamp();
238 LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
240 mapService.updateMappingRegistration(MappingOrigin.Southbound, eid, timestamp);
244 private OdlLispSbService getLispSB() {
249 public void handleMapReply(MapReply reply) {
250 tlsMapReply.set(reply);
254 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
255 tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
259 public void handleSMR(MapRequest smr, Rloc subscriber) {
260 if (LOG.isDebugEnabled()) {
261 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
262 LispAddressStringifier.getString(subscriber),
263 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
264 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
266 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
267 smrib.setMapRequest(new MapRequestBuilder(smr).build());
268 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
269 getLispSB().sendMapRequest(smrib.build());
274 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
275 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
278 public void destroy() {
279 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
285 public void close() throws Exception {
287 clusterSingletonService.close();
291 public void instantiateServiceInstance() {
292 mapService.setIsMaster(true);
296 public ListenableFuture<Void> closeServiceInstance() {
297 if (mapService != null) {
298 mapService.setIsMaster(false);
300 return Futures.<Void>immediateFuture(null);
304 public ServiceGroupIdentifier getIdentifier() {
305 return SERVICE_GROUP_IDENTIFIER;