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 java.util.List;
13 import org.apache.commons.lang3.tuple.MutablePair;
14 import org.apache.commons.lang3.tuple.Pair;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
20 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
21 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
22 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
23 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
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.IMapServerAsync;
29 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
30 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 public class LispMappingService implements IFlowMapping, BindingAwareProvider, IMapRequestResultHandler,
62 IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
63 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
65 private volatile boolean shouldAuthenticate = true;
67 private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
68 private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
70 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
71 private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify =
72 new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
73 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest =
74 new ThreadLocal<Pair<MapRequest, TransportAddress>>();
76 private OdlLispSbService lispSB = null;
77 private IMapResolverAsync mapResolver;
78 private IMapServerAsync mapServer;
80 private IMappingService mapService;
81 private NotificationService notificationService;
82 private BindingAwareBroker broker;
83 private ProviderContext session;
85 public LispMappingService() {
86 LOG.debug("LispMappingService Module constructed!");
89 public void setBindingAwareBroker(BindingAwareBroker broker) {
93 public void setNotificationService(NotificationService ns) {
94 this.notificationService = ns;
97 public void setMappingService(IMappingService ms) {
98 LOG.trace("MappingService set in LispMappingService");
102 public boolean shouldUseSmr() {
106 public void setShouldUseSmr(boolean smr) {
108 if (mapServer != null) {
109 mapServer.setSubscriptionService(smr);
111 if (mapResolver != null) {
112 mapResolver.setSubscriptionService(smr);
114 ConfigIni.getInstance().setSmr(smr);
117 public NotificationService getNotificationService() {
118 return this.notificationService;
121 public void initialize() {
122 broker.registerProvider(this);
123 notificationService.registerNotificationListener(this);
124 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
125 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
128 public void basicInit() {
129 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
130 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
134 public void onSessionInitiated(ProviderContext session) {
135 LOG.info("Lisp Consumer session initialized!");
136 this.session = session;
137 LOG.info("LISP (RFC6830) Mapping Service init finished");
140 public MapReply handleMapRequest(MapRequest request) {
141 if (LOG.isDebugEnabled()) {
142 LOG.debug("DAO: Retrieving mapping for {}",
143 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
146 tlsMapReply.set(null);
147 tlsMapRequest.set(null);
148 mapResolver.handleMapRequest(request);
149 // After this invocation we assume that the thread local is filled with the reply
150 if (tlsMapRequest.get() != null) {
151 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
152 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
153 smrib.setTransportAddress(tlsMapRequest.get().getRight());
154 getLispSB().sendMapRequest(smrib.build());
157 return tlsMapReply.get();
161 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
162 if (LOG.isDebugEnabled()) {
163 LOG.debug("DAO: Adding mapping for {}",
164 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
165 .getMappingRecord().getEid()));
168 tlsMapNotify.set(null);
169 mapServer.handleMapRegister(mapRegister);
170 // After this invocation we assume that the thread local is filled with the reply
171 return tlsMapNotify.get();
174 public void setShouldAuthenticate(boolean shouldAuthenticate) {
175 this.shouldAuthenticate = shouldAuthenticate;
176 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
177 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
180 public boolean shouldAuthenticate() {
181 return shouldAuthenticate;
184 private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
185 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
186 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
187 smnib.setTransportAddress(address);
188 getLispSB().sendMapNotify(smnib.build());
192 public void onAddMapping(AddMapping mapRegisterNotification) {
193 Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
194 if (result != null && result.getLeft() != null) {
195 MapNotify mapNotify = result.getLeft();
196 List <TransportAddress> rlocs = result.getRight();
198 TransportAddressBuilder tab = new TransportAddressBuilder();
199 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
200 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
201 sendMapNotify(mapNotify, tab.build());
203 for (TransportAddress ta : rlocs) {
204 sendMapNotify(mapNotify, ta);
208 LOG.debug("Not sending Map-Notify");
213 public void onRequestMapping(RequestMapping mapRequestNotification) {
214 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
215 if (mapReply != null) {
216 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
217 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
218 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
219 getLispSB().sendMapReply(smrib.build());
221 LOG.warn("got null map reply");
226 public void onGotMapReply(GotMapReply notification) {
227 LOG.debug("Received GotMapReply notification, ignoring");
231 public void onGotMapNotify(GotMapNotify notification) {
232 LOG.debug("Received GotMapNotify notification, ignoring");
236 public void onXtrRequestMapping(XtrRequestMapping notification) {
237 LOG.debug("Received XtrRequestMapping notification, ignoring");
241 public void onXtrReplyMapping(XtrReplyMapping notification) {
242 LOG.debug("Received XtrReplyMapping notification, ignoring");
246 public void onMappingKeepAlive(MappingKeepAlive notification) {
247 final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
248 for (EidLispAddress eidLispAddress : cacheMetadata.getEidLispAddress()) {
249 final Eid eid = eidLispAddress.getEid();
250 final Long timestamp = cacheMetadata.getTimestamp();
251 LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
253 mapService.updateMappingRegistration(MappingOrigin.Southbound, eid, timestamp);
257 private OdlLispSbService getLispSB() {
258 if (lispSB == null) {
259 lispSB = session.getRpcService(OdlLispSbService.class);
265 public void handleMapReply(MapReply reply) {
266 tlsMapReply.set(reply);
270 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
271 tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
275 public void handleSMR(MapRequest smr, Rloc subscriber) {
276 if (LOG.isDebugEnabled()) {
277 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
278 LispAddressStringifier.getString(subscriber),
279 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
280 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
282 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
283 smrib.setMapRequest(new MapRequestBuilder(smr).build());
284 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
285 getLispSB().sendMapRequest(smrib.build());
290 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
291 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
294 public void destroy() {
295 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
301 public void close() throws Exception {