1
2
3
4
5
6
7
8
9 package org.opendaylight.lispflowmapping.implementation;
10
11 import java.util.List;
12
13 import org.apache.commons.lang3.tuple.MutablePair;
14 import org.apache.commons.lang3.tuple.Pair;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
20 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
21 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
22 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
23 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
27 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
28 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
29 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
30 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
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.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public class LispMappingService implements IFlowMapping, BindingAwareProvider, IMapRequestResultHandler,
57 IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
58 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
59
60 private volatile boolean shouldAuthenticate = true;
61
62 private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
63 private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
64
65 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
66 private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify = new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
67 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
68
69 private OdlLispSbService lispSB = null;
70 private IMapResolverAsync mapResolver;
71 private IMapServerAsync mapServer;
72
73 private IMappingService mapService;
74 private NotificationService notificationService;
75 private BindingAwareBroker broker;
76 private ProviderContext session;
77
78 public LispMappingService() {
79 LOG.debug("LispMappingService Module constructed!");
80 }
81
82 public void setBindingAwareBroker(BindingAwareBroker broker) {
83 this.broker = broker;
84 }
85
86 public void setNotificationService(NotificationService ns) {
87 this.notificationService = ns;
88 }
89
90 public void setMappingService(IMappingService ms) {
91 LOG.trace("MappingService set in LispMappingService");
92 this.mapService = ms;
93 }
94
95 public boolean shouldUseSmr() {
96 return this.smr;
97 }
98
99 public void setShouldUseSmr(boolean smr) {
100 this.smr = smr;
101 if (mapServer != null) {
102 mapServer.setSubscriptionService(smr);
103 }
104 if (mapResolver != null) {
105 mapResolver.setSubscriptionService(smr);
106 }
107 }
108
109 public NotificationService getNotificationService() {
110 return this.notificationService;
111 }
112
113 public void initialize() {
114 broker.registerProvider(this);
115 notificationService.registerNotificationListener(this);
116 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
117 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
118 }
119
120 public void basicInit() {
121 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
122 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
123 }
124
125 @Override
126 public void onSessionInitiated(ProviderContext session) {
127 LOG.info("Lisp Consumer session initialized!");
128 this.session = session;
129 LOG.info("LISP (RFC6830) Mapping Service init finished");
130 }
131
132 public MapReply handleMapRequest(MapRequest request) {
133 LOG.debug("DAO: Retrieving mapping for {}",
134 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
135
136 tlsMapReply.set(null);
137 tlsMapRequest.set(null);
138 mapResolver.handleMapRequest(request);
139
140 if (tlsMapRequest.get() != null) {
141 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
142 new MapRequestBuilder(tlsMapRequest.get().getLeft());
143 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
144 smrib.setTransportAddress(tlsMapRequest.get().getRight());
145 getLispSB().sendMapRequest(smrib.build());
146 return null;
147 } else {
148 return tlsMapReply.get();
149 }
150 }
151
152 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
153 LOG.debug("DAO: Adding mapping for {}",
154 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
155 .getMappingRecord().getEid()));
156
157 tlsMapNotify.set(null);
158 mapServer.handleMapRegister(mapRegister);
159
160 return tlsMapNotify.get();
161 }
162
163 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister, boolean smr) {
164 LOG.debug("DAO: Adding mapping for {}",
165 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
166 .getMappingRecord().getEid()));
167
168 tlsMapNotify.set(null);
169 mapServer.handleMapRegister(mapRegister);
170
171 return tlsMapNotify.get();
172 }
173
174 public void setShouldAuthenticate(boolean shouldAuthenticate) {
175 this.shouldAuthenticate = shouldAuthenticate;
176 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
177 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
178 }
179
180 public boolean shouldAuthenticate() {
181 return shouldAuthenticate;
182 }
183
184 private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
185 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
186 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
187 smnib.setTransportAddress(address);
188 getLispSB().sendMapNotify(smnib.build());
189 }
190
191 @Override
192 public void onAddMapping(AddMapping mapRegisterNotification) {
193 Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
194 if (result != null && result.getLeft() != null) {
195 MapNotify mapNotify = result.getLeft();
196 List <TransportAddress> rlocs = result.getRight();
197 if (rlocs == null) {
198 TransportAddressBuilder tab = new TransportAddressBuilder();
199 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
200 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
201 sendMapNotify(mapNotify, tab.build());
202 } else {
203 for (TransportAddress ta : rlocs) {
204 sendMapNotify(mapNotify, ta);
205 }
206 }
207 } else {
208 LOG.debug("Not sending Map-Notify");
209 }
210 }
211
212 @Override
213 public void onRequestMapping(RequestMapping mapRequestNotification) {
214 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
215 if (mapReply != null) {
216 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
217 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
218 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
219 getLispSB().sendMapReply(smrib.build());
220 } else {
221 LOG.warn("got null map reply");
222 }
223 }
224
225 @Override
226 public void onGotMapReply(GotMapReply notification) {
227 LOG.debug("Received GotMapReply notification, ignoring");
228 }
229
230 @Override
231 public void onGotMapNotify(GotMapNotify notification) {
232 LOG.debug("Received GotMapNotify notification, ignoring");
233 }
234
235 @Override
236 public void onXtrRequestMapping(XtrRequestMapping notification) {
237 LOG.debug("Received XtrRequestMapping notification, ignoring");
238 }
239
240 @Override
241 public void onXtrReplyMapping(XtrReplyMapping notification) {
242 LOG.debug("Received XtrReplyMapping notification, ignoring");
243 }
244
245 private OdlLispSbService getLispSB() {
246 if (lispSB == null) {
247 lispSB = session.getRpcService(OdlLispSbService.class);
248 }
249 return lispSB;
250 }
251
252 @Override
253 public void handleMapReply(MapReply reply) {
254 tlsMapReply.set(reply);
255 }
256
257 @Override
258 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
259 tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
260 }
261
262 @Override
263 public void handleSMR(MapRequest smr, Rloc subscriber) {
264 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
265 LispAddressStringifier.getString(subscriber),
266 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
267 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
268 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
269 smrib.setMapRequest(new MapRequestBuilder(smr).build());
270 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
271 getLispSB().sendMapRequest(smrib.build());
272
273 }
274
275 @Override
276 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
277 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
278 }
279
280 public void destroy() {
281 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
282 mapResolver = null;
283 mapServer = null;
284 }
285
286 @Override
287 public void close() throws Exception {
288 destroy();
289 }
290 }