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;
12 import org.apache.commons.lang3.tuple.MutablePair;
13 import org.apache.commons.lang3.tuple.Pair;
14 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
15 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
16 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
17 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
18 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
19 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
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.mappingservice.IMappingService;
26 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
58 IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
59 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
61 private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
62 private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
64 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
65 private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify =
66 new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
67 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest =
68 new ThreadLocal<Pair<MapRequest, TransportAddress>>();
70 private final OdlLispSbService lispSB;
71 private IMapResolverAsync mapResolver;
72 private IMapServerAsync mapServer;
74 private final IMappingService mapService;
75 private final NotificationService notificationService;
77 public LispMappingService(final NotificationService notificationService,
78 final IMappingService mappingService,
79 final OdlLispSbService odlLispService) {
81 this.notificationService = notificationService;
82 this.mapService = mappingService;
83 this.lispSB = odlLispService;
84 LOG.debug("LispMappingService Module constructed!");
87 public boolean shouldUseSmr() {
91 public void setShouldUseSmr(boolean smr) {
93 if (mapServer != null) {
94 mapServer.setSubscriptionService(smr);
96 if (mapResolver != null) {
97 mapResolver.setSubscriptionService(smr);
99 ConfigIni.getInstance().setSmr(smr);
102 public NotificationService getNotificationService() {
103 return this.notificationService;
106 public void initialize() {
107 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
108 mapServer = new MapServer(mapService, smr, this, notificationService);
109 LOG.info("LISP (RFC6830) Mapping Service init finished");
112 public void basicInit() {
113 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
114 mapServer = new MapServer(mapService, smr, this, notificationService);
117 public MapReply handleMapRequest(MapRequest request) {
118 if (LOG.isDebugEnabled()) {
119 LOG.debug("DAO: Retrieving mapping for {}",
120 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
123 tlsMapReply.set(null);
124 tlsMapRequest.set(null);
125 mapResolver.handleMapRequest(request);
126 // After this invocation we assume that the thread local is filled with the reply
127 if (tlsMapRequest.get() != null) {
128 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
129 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
130 smrib.setTransportAddress(tlsMapRequest.get().getRight());
131 getLispSB().sendMapRequest(smrib.build());
134 return tlsMapReply.get();
138 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
139 if (LOG.isDebugEnabled()) {
140 LOG.debug("DAO: Adding mapping for {}",
141 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
142 .getMappingRecord().getEid()));
145 tlsMapNotify.set(null);
146 mapServer.handleMapRegister(mapRegister);
147 // After this invocation we assume that the thread local is filled with the reply
148 return tlsMapNotify.get();
151 public void setShouldAuthenticate(boolean shouldAuthenticate) {
152 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
155 private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
156 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
157 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
158 smnib.setTransportAddress(address);
159 getLispSB().sendMapNotify(smnib.build());
163 public void onAddMapping(AddMapping mapRegisterNotification) {
164 Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
165 if (result != null && result.getLeft() != null) {
166 MapNotify mapNotify = result.getLeft();
167 List <TransportAddress> rlocs = result.getRight();
169 TransportAddressBuilder tab = new TransportAddressBuilder();
170 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
171 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
172 sendMapNotify(mapNotify, tab.build());
174 for (TransportAddress ta : rlocs) {
175 sendMapNotify(mapNotify, ta);
179 LOG.debug("Not sending Map-Notify");
184 public void onRequestMapping(RequestMapping mapRequestNotification) {
185 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
186 if (mapReply != null) {
187 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
188 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
189 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
190 getLispSB().sendMapReply(smrib.build());
192 LOG.warn("got null map reply");
197 public void onGotMapReply(GotMapReply notification) {
198 LOG.debug("Received GotMapReply notification, ignoring");
202 public void onGotMapNotify(GotMapNotify notification) {
203 LOG.debug("Received GotMapNotify notification, ignoring");
207 public void onXtrRequestMapping(XtrRequestMapping notification) {
208 LOG.debug("Received XtrRequestMapping notification, ignoring");
212 public void onXtrReplyMapping(XtrReplyMapping notification) {
213 LOG.debug("Received XtrReplyMapping notification, ignoring");
217 public void onMappingKeepAlive(MappingKeepAlive notification) {
218 final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
219 for (EidLispAddress eidLispAddress : cacheMetadata.getEidLispAddress()) {
220 final Eid eid = eidLispAddress.getEid();
221 final Long timestamp = cacheMetadata.getTimestamp();
222 LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
224 mapService.updateMappingRegistration(MappingOrigin.Southbound, eid, timestamp);
228 private OdlLispSbService getLispSB() {
233 public void handleMapReply(MapReply reply) {
234 tlsMapReply.set(reply);
238 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
239 tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
243 public void handleSMR(MapRequest smr, Rloc subscriber) {
244 if (LOG.isDebugEnabled()) {
245 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
246 LispAddressStringifier.getString(subscriber),
247 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
248 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
250 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
251 smrib.setMapRequest(new MapRequestBuilder(smr).build());
252 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
253 getLispSB().sendMapRequest(smrib.build());
258 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
259 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
262 public void destroy() {
263 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
269 public void close() throws Exception {