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 org.apache.commons.lang3.tuple.MutablePair;
12 import org.apache.commons.lang3.tuple.Pair;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
17 import org.opendaylight.lispflowmapping.implementation.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.lisp.type.LispMessage;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
27 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
28 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispProtoListener;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.LispAddressContainer;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transportaddress.TransportAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transportaddress.TransportAddressBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.LispSbService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class LispMappingService implements IFlowMapping, BindingAwareProvider, IMapRequestResultHandler,
55 IMapNotifyHandler, LispProtoListener, AutoCloseable {
56 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
58 private volatile boolean shouldAuthenticate = true;
60 private static final ConfigIni configIni = new ConfigIni();
61 private volatile boolean smr = configIni.smrIsSet();
62 private volatile String elpPolicy = configIni.getElpPolicy();
64 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
65 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
66 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
68 private LispSbService lispSB = null;
69 private IMapResolverAsync mapResolver;
70 private IMapServerAsync mapServer;
72 private IMappingService mapService;
73 private NotificationService notificationService;
74 private BindingAwareBroker broker;
75 private ProviderContext session;
77 public LispMappingService() {
78 LOG.debug("LispMappingService Module constructed!");
81 public void setBindingAwareBroker(BindingAwareBroker broker) {
85 public void setNotificationService(NotificationService ns) {
86 this.notificationService = ns;
89 public void setMappingService(IMappingService ms) {
90 LOG.trace("MappingService set in LispMappingService");
94 public boolean shouldUseSmr() {
98 public void setShouldUseSmr(boolean smr) {
100 if (mapServer != null) {
101 mapServer.setSubscriptionService(smr);
103 if (mapResolver != null) {
104 mapResolver.setSubscriptionService(smr);
108 public NotificationService getNotificationService() {
109 return this.notificationService;
112 public void initialize() {
113 broker.registerProvider(this);
114 notificationService.registerNotificationListener(this);
115 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
116 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
119 public void basicInit() {
120 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
121 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
125 public void onSessionInitiated(ProviderContext session) {
126 LOG.info("Lisp Consumer session initialized!");
127 this.session = session;
128 LOG.info("LISP (RFC6830) Mapping Service init finished");
131 public MapReply handleMapRequest(MapRequest request) {
132 LOG.debug("DAO: Retrieving mapping for {}",
133 LispAddressStringifier.getString(request.getEidRecord().get(0).getLispAddressContainer()));
135 tlsMapReply.set(null);
136 tlsMapRequest.set(null);
137 mapResolver.handleMapRequest(request);
138 // After this invocation we assume that the thread local is filled with the reply
139 if (tlsMapRequest.get() != null) {
140 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
141 new MapRequestBuilder(tlsMapRequest.get().getLeft());
142 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
143 smrib.setTransportAddress(tlsMapRequest.get().getRight());
144 getLispSB().sendMapRequest(smrib.build());
147 return tlsMapReply.get();
151 public MapNotify handleMapRegister(MapRegister mapRegister) {
152 LOG.debug("DAO: Adding mapping for {}",
153 LispAddressStringifier.getString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()));
155 tlsMapNotify.set(null);
156 mapServer.handleMapRegister(mapRegister);
157 // After this invocation we assume that the thread local is filled with the reply
158 return tlsMapNotify.get();
161 public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
162 LOG.debug("DAO: Adding mapping for {}",
163 LispAddressStringifier.getString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()));
165 tlsMapNotify.set(null);
166 mapServer.handleMapRegister(mapRegister);
167 // After this invocation we assume that the thread local is filled with the reply
168 return tlsMapNotify.get();
171 public void setShouldAuthenticate(boolean shouldAuthenticate) {
172 this.shouldAuthenticate = shouldAuthenticate;
173 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
174 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
177 public boolean shouldAuthenticate() {
178 return shouldAuthenticate;
182 public void onAddMapping(AddMapping mapRegisterNotification) {
183 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
184 if (mapNotify != null) {
185 TransportAddressBuilder tab = new TransportAddressBuilder();
186 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
187 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
188 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
189 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
190 smnib.setTransportAddress(tab.build());
191 getLispSB().sendMapNotify(smnib.build());
193 LOG.warn("got null map notify");
198 public void onRequestMapping(RequestMapping mapRequestNotification) {
199 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
200 if (mapReply != null) {
201 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
202 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
203 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
204 getLispSB().sendMapReply(smrib.build());
206 LOG.warn("got null map reply");
211 public void onGotMapReply(GotMapReply notification) {
212 LOG.debug("Received GotMapReply notification, ignoring");
216 public void onGotMapNotify(GotMapNotify notification) {
217 LOG.debug("Received GotMapNotify notification, ignoring");
221 public void onXtrRequestMapping(XtrRequestMapping notification) {
222 LOG.debug("Received XtrRequestMapping notification, ignoring");
226 public void onXtrReplyMapping(XtrReplyMapping notification) {
227 LOG.debug("Received XtrReplyMapping notification, ignoring");
230 private LispSbService getLispSB() {
231 if (lispSB == null) {
232 lispSB = session.getRpcService(LispSbService.class);
238 public void handleMapReply(MapReply reply) {
239 tlsMapReply.set(reply);
243 public void handleMapNotify(MapNotify notify) {
244 tlsMapNotify.set(notify);
248 public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
249 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
250 LispAddressStringifier.getString(subscriber),
251 LispAddressStringifier.getString(smr.getSourceEid().getLispAddressContainer()),
252 LispAddressStringifier.getString(smr.getEidRecord().get(0).getLispAddressContainer()));
253 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
254 smrib.setMapRequest(new MapRequestBuilder(smr).build());
255 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
256 getLispSB().sendMapRequest(smrib.build());
261 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
262 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
265 public void destroy() {
266 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
272 public void close() throws Exception {