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 volatile boolean smr = ConfigIni.getInstance().smrIsSet();
61 private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
63 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
64 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
65 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
67 private OdlLispSbService lispSB = null;
68 private IMapResolverAsync mapResolver;
69 private IMapServerAsync mapServer;
71 private IMappingService mapService;
72 private NotificationService notificationService;
73 private BindingAwareBroker broker;
74 private ProviderContext session;
76 public LispMappingService() {
77 LOG.debug("LispMappingService Module constructed!");
80 public void setBindingAwareBroker(BindingAwareBroker broker) {
84 public void setNotificationService(NotificationService ns) {
85 this.notificationService = ns;
88 public void setMappingService(IMappingService ms) {
89 LOG.trace("MappingService set in LispMappingService");
93 public boolean shouldUseSmr() {
97 public void setShouldUseSmr(boolean smr) {
99 if (mapServer != null) {
100 mapServer.setSubscriptionService(smr);
102 if (mapResolver != null) {
103 mapResolver.setSubscriptionService(smr);
107 public NotificationService getNotificationService() {
108 return this.notificationService;
111 public void initialize() {
112 broker.registerProvider(this);
113 notificationService.registerNotificationListener(this);
114 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
115 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
118 public void basicInit() {
119 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
120 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
124 public void onSessionInitiated(ProviderContext session) {
125 LOG.info("Lisp Consumer session initialized!");
126 this.session = session;
127 LOG.info("LISP (RFC6830) Mapping Service init finished");
130 public MapReply handleMapRequest(MapRequest request) {
131 LOG.debug("DAO: Retrieving mapping for {}",
132 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
134 tlsMapReply.set(null);
135 tlsMapRequest.set(null);
136 mapResolver.handleMapRequest(request);
137 // After this invocation we assume that the thread local is filled with the reply
138 if (tlsMapRequest.get() != null) {
139 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
140 new MapRequestBuilder(tlsMapRequest.get().getLeft());
141 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
142 smrib.setTransportAddress(tlsMapRequest.get().getRight());
143 getLispSB().sendMapRequest(smrib.build());
146 return tlsMapReply.get();
150 public MapNotify handleMapRegister(MapRegister mapRegister) {
151 LOG.debug("DAO: Adding mapping for {}",
152 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
153 .getMappingRecord().getEid()));
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.getMappingRecordItem().get(0)
164 .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.shouldAuthenticate = shouldAuthenticate;
174 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
175 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
178 public boolean shouldAuthenticate() {
179 return shouldAuthenticate;
183 public void onAddMapping(AddMapping mapRegisterNotification) {
184 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
185 if (mapNotify != null) {
186 TransportAddressBuilder tab = new TransportAddressBuilder();
187 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
188 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
189 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
190 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
191 smnib.setTransportAddress(tab.build());
192 getLispSB().sendMapNotify(smnib.build());
194 LOG.warn("got null map notify");
199 public void onRequestMapping(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 public void onGotMapReply(GotMapReply notification) {
213 LOG.debug("Received GotMapReply notification, ignoring");
217 public void onGotMapNotify(GotMapNotify notification) {
218 LOG.debug("Received GotMapNotify notification, ignoring");
222 public void onXtrRequestMapping(XtrRequestMapping notification) {
223 LOG.debug("Received XtrRequestMapping notification, ignoring");
227 public void onXtrReplyMapping(XtrReplyMapping notification) {
228 LOG.debug("Received XtrReplyMapping notification, ignoring");
231 private OdlLispSbService getLispSB() {
232 if (lispSB == null) {
233 lispSB = session.getRpcService(OdlLispSbService.class);
239 public void handleMapReply(MapReply reply) {
240 tlsMapReply.set(reply);
244 public void handleMapNotify(MapNotify notify) {
245 tlsMapNotify.set(notify);
249 public void handleSMR(MapRequest smr, Rloc subscriber) {
250 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
251 LispAddressStringifier.getString(subscriber),
252 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
253 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
254 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
255 smrib.setMapRequest(new MapRequestBuilder(smr).build());
256 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
257 getLispSB().sendMapRequest(smrib.build());
262 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
263 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
266 public void destroy() {
267 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
273 public void close() throws Exception {