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.MapNotify;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
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.mapnotifymessage.MapNotifyBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
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, OdlLispProtoListener, 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 OdlLispSbService 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.getEidItem().get(0).getEid()));
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.getMappingRecordItem().get(0)
154 .getMappingRecord().getEid()));
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 MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
163 LOG.debug("DAO: Adding mapping for {}",
164 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
165 .getMappingRecord().getEid()));
167 tlsMapNotify.set(null);
168 mapServer.handleMapRegister(mapRegister);
169 // After this invocation we assume that the thread local is filled with the reply
170 return tlsMapNotify.get();
173 public void setShouldAuthenticate(boolean shouldAuthenticate) {
174 this.shouldAuthenticate = shouldAuthenticate;
175 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
176 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
179 public boolean shouldAuthenticate() {
180 return shouldAuthenticate;
184 public void onAddMapping(AddMapping mapRegisterNotification) {
185 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
186 if (mapNotify != null) {
187 TransportAddressBuilder tab = new TransportAddressBuilder();
188 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
189 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
190 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
191 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
192 smnib.setTransportAddress(tab.build());
193 getLispSB().sendMapNotify(smnib.build());
195 LOG.warn("got null map notify");
200 public void onRequestMapping(RequestMapping mapRequestNotification) {
201 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
202 if (mapReply != null) {
203 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
204 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
205 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
206 getLispSB().sendMapReply(smrib.build());
208 LOG.warn("got null map reply");
213 public void onGotMapReply(GotMapReply notification) {
214 LOG.debug("Received GotMapReply notification, ignoring");
218 public void onGotMapNotify(GotMapNotify notification) {
219 LOG.debug("Received GotMapNotify notification, ignoring");
223 public void onXtrRequestMapping(XtrRequestMapping notification) {
224 LOG.debug("Received XtrRequestMapping notification, ignoring");
228 public void onXtrReplyMapping(XtrReplyMapping notification) {
229 LOG.debug("Received XtrReplyMapping notification, ignoring");
232 private OdlLispSbService getLispSB() {
233 if (lispSB == null) {
234 lispSB = session.getRpcService(OdlLispSbService.class);
240 public void handleMapReply(MapReply reply) {
241 tlsMapReply.set(reply);
245 public void handleMapNotify(MapNotify notify) {
246 tlsMapNotify.set(notify);
250 public void handleSMR(MapRequest smr, Rloc subscriber) {
251 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
252 LispAddressStringifier.getString(subscriber),
253 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
254 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
255 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
256 smrib.setMapRequest(new MapRequestBuilder(smr).build());
257 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
258 getLispSB().sendMapRequest(smrib.build());
263 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
264 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
267 public void destroy() {
268 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
274 public void close() throws Exception {