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 smr = ConfigIni.getInstance().smrIsSet();
66 private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
68 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
69 private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify =
70 new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
71 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest =
72 new ThreadLocal<Pair<MapRequest, TransportAddress>>();
74 private OdlLispSbService lispSB = null;
75 private IMapResolverAsync mapResolver;
76 private IMapServerAsync mapServer;
78 private IMappingService mapService;
79 private NotificationService notificationService;
80 private BindingAwareBroker broker;
81 private ProviderContext session;
83 public LispMappingService() {
84 LOG.debug("LispMappingService Module constructed!");
87 public void setBindingAwareBroker(BindingAwareBroker broker) {
91 public void setNotificationService(NotificationService ns) {
92 this.notificationService = ns;
95 public void setMappingService(IMappingService ms) {
96 LOG.trace("MappingService set in LispMappingService");
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 broker.registerProvider(this);
121 notificationService.registerNotificationListener(this);
122 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
123 mapServer = new MapServer(mapService, smr, this, notificationService);
126 public void basicInit() {
127 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
128 mapServer = new MapServer(mapService, smr, this, notificationService);
132 public void onSessionInitiated(ProviderContext session) {
133 LOG.info("Lisp Consumer session initialized!");
134 this.session = session;
135 LOG.info("LISP (RFC6830) Mapping Service init finished");
138 public MapReply handleMapRequest(MapRequest request) {
139 if (LOG.isDebugEnabled()) {
140 LOG.debug("DAO: Retrieving mapping for {}",
141 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
144 tlsMapReply.set(null);
145 tlsMapRequest.set(null);
146 mapResolver.handleMapRequest(request);
147 // After this invocation we assume that the thread local is filled with the reply
148 if (tlsMapRequest.get() != null) {
149 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
150 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
151 smrib.setTransportAddress(tlsMapRequest.get().getRight());
152 getLispSB().sendMapRequest(smrib.build());
155 return tlsMapReply.get();
159 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
160 if (LOG.isDebugEnabled()) {
161 LOG.debug("DAO: Adding mapping for {}",
162 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
163 .getMappingRecord().getEid()));
166 tlsMapNotify.set(null);
167 mapServer.handleMapRegister(mapRegister);
168 // After this invocation we assume that the thread local is filled with the reply
169 return tlsMapNotify.get();
172 public void setShouldAuthenticate(boolean shouldAuthenticate) {
173 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
176 private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
177 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
178 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
179 smnib.setTransportAddress(address);
180 getLispSB().sendMapNotify(smnib.build());
184 public void onAddMapping(AddMapping mapRegisterNotification) {
185 Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
186 if (result != null && result.getLeft() != null) {
187 MapNotify mapNotify = result.getLeft();
188 List <TransportAddress> rlocs = result.getRight();
190 TransportAddressBuilder tab = new TransportAddressBuilder();
191 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
192 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
193 sendMapNotify(mapNotify, tab.build());
195 for (TransportAddress ta : rlocs) {
196 sendMapNotify(mapNotify, ta);
200 LOG.debug("Not sending Map-Notify");
205 public void onRequestMapping(RequestMapping mapRequestNotification) {
206 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
207 if (mapReply != null) {
208 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
209 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
210 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
211 getLispSB().sendMapReply(smrib.build());
213 LOG.warn("got null map reply");
218 public void onGotMapReply(GotMapReply notification) {
219 LOG.debug("Received GotMapReply notification, ignoring");
223 public void onGotMapNotify(GotMapNotify notification) {
224 LOG.debug("Received GotMapNotify notification, ignoring");
228 public void onXtrRequestMapping(XtrRequestMapping notification) {
229 LOG.debug("Received XtrRequestMapping notification, ignoring");
233 public void onXtrReplyMapping(XtrReplyMapping notification) {
234 LOG.debug("Received XtrReplyMapping notification, ignoring");
238 public void onMappingKeepAlive(MappingKeepAlive notification) {
239 final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
240 for (EidLispAddress eidLispAddress : cacheMetadata.getEidLispAddress()) {
241 final Eid eid = eidLispAddress.getEid();
242 final Long timestamp = cacheMetadata.getTimestamp();
243 LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
245 mapService.updateMappingRegistration(MappingOrigin.Southbound, eid, timestamp);
249 private OdlLispSbService getLispSB() {
250 if (lispSB == null) {
251 lispSB = session.getRpcService(OdlLispSbService.class);
257 public void handleMapReply(MapReply reply) {
258 tlsMapReply.set(reply);
262 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
263 tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
267 public void handleSMR(MapRequest smr, Rloc subscriber) {
268 if (LOG.isDebugEnabled()) {
269 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
270 LispAddressStringifier.getString(subscriber),
271 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
272 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
274 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
275 smrib.setMapRequest(new MapRequestBuilder(smr).build());
276 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
277 getLispSB().sendMapRequest(smrib.build());
282 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
283 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
286 public void destroy() {
287 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
293 public void close() throws Exception {