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 java.util.concurrent.Future;
13 import org.apache.commons.lang3.tuple.MutablePair;
14 import org.apache.commons.lang3.tuple.Pair;
15 import org.eclipse.osgi.framework.console.CommandInterpreter;
16 import org.eclipse.osgi.framework.console.CommandProvider;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
21 import org.opendaylight.controller.sal.binding.api.NotificationListener;
22 import org.opendaylight.controller.sal.binding.api.NotificationService;
23 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
24 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
25 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
26 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceNoMaskKey;
27 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
28 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
29 import org.opendaylight.lispflowmapping.implementation.mdsal.AuthenticationKeyDataListener;
30 import org.opendaylight.lispflowmapping.implementation.mdsal.MappingDataListener;
31 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
32 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
33 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
34 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
35 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
36 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
38 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMappingShell;
39 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
40 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
41 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
42 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.AddMapping;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LfmControlPlaneService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapNotify;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRegister;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapReply;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRequest;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.RequestMapping;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapNotifyInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapReplyInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapRequestInputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4AddressBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapnotifymessage.MapNotifyBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapreplymessage.MapReplyBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequestmessage.MapRequestBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddress;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddressBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddKeyInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyOutput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.LfmMappingDatabaseService;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingInput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyInput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingInput;
71 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
72 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
73 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
74 import org.opendaylight.yangtools.yang.binding.Notification;
75 import org.opendaylight.yangtools.yang.common.RpcResult;
76 import org.osgi.framework.BundleContext;
77 import org.osgi.framework.FrameworkUtil;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
81 public class LispMappingService implements CommandProvider, IFlowMapping, IFlowMappingShell, BindingAwareProvider,
82 IMapRequestResultHandler, IMapNotifyHandler, LfmMappingDatabaseService {
83 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
85 private static final ConfigIni configIni = new ConfigIni();
86 private LfmMappingDatabaseRPCs rpc;
87 private AuthenticationKeyDataListener keyListener;
88 private MappingDataListener mappingListener;
89 private ILispDAO lispDao = null;
90 private IMapResolverAsync mapResolver;
91 private IMapServerAsync mapServer;
92 private volatile boolean shouldIterateMask;
93 private volatile boolean shouldAuthenticate;
94 private volatile boolean smr = configIni.smrIsSet();
95 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
96 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
97 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
99 private LfmControlPlaneService lispSB = null;
101 private ProviderContext session;
103 private NotificationService notificationService;
105 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
108 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
111 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
114 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
117 void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
118 LOG.trace("BindingAwareBroker set!");
119 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
120 bindingAwareBroker.registerProvider(this, bundleContext);
123 void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
124 LOG.debug("BindingAwareBroker was unset in LispMappingService");
127 public void basicInit(ILispDAO dao) {
129 mapResolver = new MapResolver(dao);
130 mapServer = new MapServer(dao);
133 void setLispDao(ILispDAO dao) {
134 LOG.trace("LispDAO set in LispMappingService");
138 void unsetLispDao(ILispDAO dao) {
139 LOG.trace("LispDAO was unset in LispMappingService");
147 registerWithOSGIConsole();
148 LOG.info("LISP (RFC6830) Mapping Service init finished");
149 } catch (Exception e) {
150 LOG.error(e.getStackTrace().toString());
154 private void registerWithOSGIConsole() {
155 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
156 bundleContext.registerService(CommandProvider.class.getName(), this, null);
159 public void destroy() {
160 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
163 closeDataListeners();
166 public void _removeEid(final CommandInterpreter ci) {
167 LispAddressContainer eid = LispAFIConvertor.getIPContainer(ci.nextArgument());
168 lispDao.remove(MappingServiceKeyUtil.generateMappingServiceKey(eid));
171 public void _dumpAll(final CommandInterpreter ci) {
172 ci.print(printMappings());
175 public String printMappings() {
176 final StringBuffer sb = new StringBuffer();
177 sb.append("EID\tRLOCs\n");
178 lispDao.getAll(new IRowVisitor() {
181 public void visitRow(Object keyId, String valueKey, Object value) {
182 String key = keyId.getClass().getSimpleName() + "#" + keyId;
183 if (!lastKey.equals(key)) {
184 sb.append("\n" + key + "\t");
186 sb.append(valueKey + "=" + value + "\t");
191 return sb.toString();
194 public void _setShouldOverwriteRlocs(final CommandInterpreter ci) {
196 boolean shouldOverwriteRloc = Boolean.parseBoolean(ci.nextArgument());
197 setOverwrite(shouldOverwriteRloc);
198 } catch (Exception e) {
199 ci.println("Bad Usage!!");
204 public void _addDefaultPassword(final CommandInterpreter ci) {
208 public void addDefaultKeyIPv4() {
209 LispAddressContainer address = LispAFIConvertor.toContainer(
210 new Ipv4AddressBuilder().setIpv4Address(new Ipv4Address("0.0.0.0")).build());
211 addAuthenticationKey(address, 0, "password");
214 public String getHelp() {
215 StringBuffer help = new StringBuffer();
216 help.append("---LISP Mapping Service---\n");
217 help.append("\t dumpAll - Dump all current EID -> RLOC mappings\n");
218 help.append("\t removeEid <EID> - Remove a single EID (/32 or /128)\n");
219 help.append("\t setShouldOverwriteRlocs <true|false> - Set the map server's behavior regarding existing RLOCs\n");
220 help.append("\t addDefaultPassword - Add \"password\" as default password for IPv4 EIDs");
221 return help.toString();
224 public MapReply handleMapRequest(MapRequest request) {
225 return handleMapRequest(request, smr);
228 public MapReply handleMapRequest(MapRequest request, boolean smr) {
229 LOG.debug("DAO: Retrieving mapping for {}/{}",
230 LispAFIConvertor.toString(request.getEidRecord().get(0).getLispAddressContainer()),
231 request.getEidRecord().get(0).getMask());
233 tlsMapReply.set(null);
234 tlsMapRequest.set(null);
235 mapResolver.handleMapRequest(request, smr, this);
236 // After this invocation we assume that the thread local is filled with
238 if (tlsMapRequest.get() != null) {
239 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
240 new MapRequestBuilder(tlsMapRequest.get().getLeft());
241 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
242 smrib.setTransportAddress(tlsMapRequest.get().getRight());
243 getLispSB().sendMapRequest(smrib.build());
246 return tlsMapReply.get();
251 public MapNotify handleMapRegister(MapRegister mapRegister) {
252 return handleMapRegister(mapRegister, smr);
255 public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
256 LOG.debug("DAO: Adding mapping for {}/{}",
257 LispAFIConvertor.toString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()),
258 mapRegister.getEidToLocatorRecord().get(0).getMaskLength());
260 tlsMapNotify.set(null);
261 mapServer.handleMapRegister(mapRegister, smr, this);
262 // After this invocation we assume that the thread local is filled with
264 return tlsMapNotify.get();
267 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
268 LOG.debug("DAO: Retrieving authentication key for {}/{}", LispAFIConvertor.toString(address), maskLen);
269 return mapServer.getAuthenticationKey(address, maskLen);
272 public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
273 LOG.debug("DAO: Removing authentication key for {}/{}", LispAFIConvertor.toString(address), maskLen);
274 mapServer.removeAuthenticationKey(address, maskLen);
277 public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
278 LOG.debug("DAO: Adding authentication key '{}' for {}/{}", key, LispAFIConvertor.toString(address), maskLen);
279 mapServer.addAuthenticationKey(address, maskLen, key);
282 public void removeMapping(LispAddressContainer address, int maskLen) {
283 LOG.debug("DAO: Removing mapping for {}/{}", LispAFIConvertor.toString(address), maskLen);
284 mapServer.removeMapping(address, maskLen, smr, this);
287 public boolean shouldIterateMask() {
288 return this.shouldIterateMask;
291 public boolean shouldUseSmr() {
295 public void setShouldUseSmr(boolean smr) {
299 public void setShouldIterateMask(boolean shouldIterateMask) {
300 this.shouldIterateMask = shouldIterateMask;
301 this.mapResolver.setShouldIterateMask(shouldIterateMask);
302 this.mapServer.setShouldIterateMask(shouldIterateMask);
305 public void setShouldAuthenticate(boolean shouldAuthenticate) {
306 this.shouldAuthenticate = shouldAuthenticate;
307 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
308 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
311 public boolean shouldAuthenticate() {
312 return shouldAuthenticate;
316 public void onSessionInitiated(ProviderContext session) {
317 LOG.info("Lisp Consumer session initialized!");
318 notificationService = session.getSALService(NotificationService.class);
319 registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
320 registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
321 registerDataListeners(session.getSALService(DataBroker.class));
322 session.addRpcImplementation(LfmMappingDatabaseService.class, this);
323 this.rpc = new LfmMappingDatabaseRPCs(this, session.getSALService(DataBroker.class));
324 this.session = session;
327 private void registerDataListeners(DataBroker broker) {
328 keyListener = new AuthenticationKeyDataListener(broker, this);
329 mappingListener = new MappingDataListener(broker, this);
332 private void closeDataListeners() {
333 keyListener.closeDataChangeListener();
334 mappingListener.closeDataChangeListener();
337 public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
338 notificationService.registerNotificationListener(notificationType, listener);
341 private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
344 public void onNotification(AddMapping mapRegisterNotification) {
345 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister(), smr);
346 if (mapNotify != null) {
347 TransportAddressBuilder tab = new TransportAddressBuilder();
348 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
349 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
350 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
351 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
352 smnib.setTransportAddress(tab.build());
353 getLispSB().sendMapNotify(smnib.build());
355 LOG.warn("got null map notify");
361 private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
364 public void onNotification(RequestMapping mapRequestNotification) {
365 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
366 if (mapReply != null) {
367 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
368 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
369 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
370 getLispSB().sendMapReply(smrib.build());
372 LOG.warn("got null map reply");
377 private LfmControlPlaneService getLispSB() {
378 if (lispSB == null) {
379 lispSB = session.getRpcService(LfmControlPlaneService.class);
384 public void handleMapReply(MapReply reply) {
385 tlsMapReply.set(reply);
388 public void handleMapNotify(MapNotify notify) {
389 tlsMapNotify.set(notify);
392 public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
393 LOG.debug("Sending SMR to " + subscriber.toString());
394 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
395 smrib.setMapRequest(new MapRequestBuilder(smr).build());
396 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
397 getLispSB().sendMapRequest(smrib.build());
402 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
403 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
407 public void clean() {
412 public boolean shouldOverwrite() {
413 return mapServer.shouldOverwrite();
417 public void setOverwrite(boolean overwrite) {
418 mapServer.setOverwrite(overwrite);
422 public Future<RpcResult<Void>> addKey(AddKeyInput input) {
423 return rpc.addKey(input);
427 public Future<RpcResult<Void>> addMapping(AddMappingInput input) {
428 return rpc.addMapping(input);
432 public Future<RpcResult<GetKeyOutput>> getKey(GetKeyInput input) {
433 return rpc.getKey(input);
437 public Future<RpcResult<GetMappingOutput>> getMapping(GetMappingInput input) {
438 return rpc.getMapping(input);
442 public Future<RpcResult<Void>> removeKey(RemoveKeyInput input) {
443 return rpc.removeKey(input);
447 public Future<RpcResult<Void>> removeMapping(RemoveMappingInput input) {
448 return rpc.removeMapping(input);
452 public Future<RpcResult<Void>> updateKey(UpdateKeyInput input) {
453 return rpc.updateKey(input);
457 public Future<RpcResult<Void>> updateMapping(UpdateMappingInput input) {
458 return rpc.updateMapping(input);