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.sal.binding.api.BindingAwareBroker.ProviderContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
16 import org.opendaylight.controller.sal.binding.api.NotificationListener;
17 import org.opendaylight.controller.sal.binding.api.NotificationService;
18 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
19 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
20 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
21 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
22 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
27 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
28 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
29 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.AddMapping;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapNotify;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapRegister;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapRequest;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.RequestMapping;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.LispAddressContainer;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.mapnotifymessage.MapNotifyBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.mapreplymessage.MapReplyBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.maprequestmessage.MapRequestBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.transportaddress.TransportAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.transportaddress.TransportAddressBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.LispSbService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
47 import org.opendaylight.yangtools.yang.binding.Notification;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class LispMappingService implements IFlowMapping, BindingAwareProvider, IMapRequestResultHandler,
52 IMapNotifyHandler, AutoCloseable {
53 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
55 private volatile boolean shouldAuthenticate = true;
57 private static final ConfigIni configIni = new ConfigIni();
58 private volatile boolean smr = configIni.smrIsSet();
59 private volatile String elpPolicy = configIni.getElpPolicy();
61 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
62 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
63 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
65 private LispSbService lispSB = null;
66 private IMapResolverAsync mapResolver;
67 private IMapServerAsync mapServer;
69 private IMappingService mapService;
70 private NotificationService notificationService;
71 private BindingAwareBroker broker;
72 private ProviderContext session;
74 public LispMappingService() {
75 LOG.debug("LispMappingService Module constructed!");
78 public void setBindingAwareBroker(BindingAwareBroker broker) {
82 public void setMappingService(IMappingService ms) {
83 LOG.trace("MappingService set in LispMappingService");
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);
101 public void initialize() {
102 broker.registerProvider(this);
103 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
104 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
107 public void basicInit() {
108 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
109 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
113 public void onSessionInitiated(ProviderContext session) {
114 LOG.info("Lisp Consumer session initialized!");
115 notificationService = session.getSALService(NotificationService.class);
116 registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
117 registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
118 this.session = session;
119 LOG.info("LISP (RFC6830) Mapping Service init finished");
122 public MapReply handleMapRequest(MapRequest request) {
123 LOG.debug("DAO: Retrieving mapping for {}",
124 LispAddressStringifier.getString(request.getEidRecord().get(0).getLispAddressContainer()));
126 tlsMapReply.set(null);
127 tlsMapRequest.set(null);
128 mapResolver.handleMapRequest(request);
129 // After this invocation we assume that the thread local is filled with the reply
130 if (tlsMapRequest.get() != null) {
131 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
132 new MapRequestBuilder(tlsMapRequest.get().getLeft());
133 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
134 smrib.setTransportAddress(tlsMapRequest.get().getRight());
135 getLispSB().sendMapRequest(smrib.build());
138 return tlsMapReply.get();
142 public MapNotify handleMapRegister(MapRegister mapRegister) {
143 LOG.debug("DAO: Adding mapping for {}",
144 LispAddressStringifier.getString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()));
146 tlsMapNotify.set(null);
147 mapServer.handleMapRegister(mapRegister);
148 // After this invocation we assume that the thread local is filled with the reply
149 return tlsMapNotify.get();
152 public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
153 LOG.debug("DAO: Adding mapping for {}",
154 LispAddressStringifier.getString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()));
156 tlsMapNotify.set(null);
157 mapServer.handleMapRegister(mapRegister);
158 // After this invocation we assume that the thread local is filled with the reply
159 return tlsMapNotify.get();
162 public void setShouldAuthenticate(boolean shouldAuthenticate) {
163 this.shouldAuthenticate = shouldAuthenticate;
164 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
165 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
168 public boolean shouldAuthenticate() {
169 return shouldAuthenticate;
172 public <T extends Notification> void registerNotificationListener(Class<T> notificationType,
173 NotificationListener<T> listener) {
174 notificationService.registerNotificationListener(notificationType, listener);
177 private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
180 public void onNotification(AddMapping mapRegisterNotification) {
181 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
182 if (mapNotify != null) {
183 TransportAddressBuilder tab = new TransportAddressBuilder();
184 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
185 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
186 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
187 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
188 smnib.setTransportAddress(tab.build());
189 getLispSB().sendMapNotify(smnib.build());
191 LOG.warn("got null map notify");
196 private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
199 public void onNotification(RequestMapping mapRequestNotification) {
200 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
201 if (mapReply != null) {
202 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
203 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
204 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
205 getLispSB().sendMapReply(smrib.build());
207 LOG.warn("got null map reply");
212 private LispSbService getLispSB() {
213 if (lispSB == null) {
214 lispSB = session.getRpcService(LispSbService.class);
220 public void handleMapReply(MapReply reply) {
221 tlsMapReply.set(reply);
225 public void handleMapNotify(MapNotify notify) {
226 tlsMapNotify.set(notify);
230 public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
231 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
232 LispAddressStringifier.getString(subscriber),
233 LispAddressStringifier.getString(smr.getSourceEid().getLispAddressContainer()),
234 LispAddressStringifier.getString(smr.getEidRecord().get(0).getLispAddressContainer()));
235 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
236 smrib.setMapRequest(new MapRequestBuilder(smr).build());
237 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
238 getLispSB().sendMapRequest(smrib.build());
243 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
244 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
247 public void destroy() {
248 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
254 public void close() throws Exception {