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.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
20 import org.opendaylight.controller.sal.binding.api.NotificationListener;
21 import org.opendaylight.controller.sal.binding.api.NotificationService;
22 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
23 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
24 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
25 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceNoMaskKey;
26 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
27 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
28 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
29 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
30 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
31 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
32 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
33 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
34 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
35 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMappingShell;
36 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
38 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
39 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.AddMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LfmControlPlaneService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapNotify;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRegister;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapReply;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRequest;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.RequestMapping;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapNotifyInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapReplyInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapRequestInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4AddressBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapnotifymessage.MapNotifyBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapreplymessage.MapReplyBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequestmessage.MapRequestBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddress;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddressBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddKeyInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyInput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyOutput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.LfmMappingDatabaseService;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingInput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyInput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingInput;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
70 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
71 import org.opendaylight.yangtools.yang.binding.Notification;
72 import org.opendaylight.yangtools.yang.common.RpcResult;
73 import org.osgi.framework.BundleContext;
74 import org.osgi.framework.FrameworkUtil;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
78 public class LispMappingService implements CommandProvider, IFlowMapping, IFlowMappingShell, BindingAwareProvider,
79 IMapRequestResultHandler, IMapNotifyHandler, LfmMappingDatabaseService {
80 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
82 private static final ConfigIni configIni = new ConfigIni();
83 private LfmMappingDatabaseRPCs rpc = new LfmMappingDatabaseRPCs();
84 private ILispDAO lispDao = null;
85 private IMapResolverAsync mapResolver;
86 private IMapServerAsync mapServer;
87 private volatile boolean shouldIterateMask;
88 private volatile boolean shouldAuthenticate;
89 private volatile boolean smr = configIni.smrIsSet();
90 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
91 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
92 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
94 private LfmControlPlaneService lispSB = null;
96 private ProviderContext session;
98 private NotificationService notificationService;
100 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
103 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
106 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
109 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
112 void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
113 LOG.trace("BindingAwareBroker set!");
114 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
115 bindingAwareBroker.registerProvider(this, bundleContext);
118 void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
119 LOG.debug("BindingAwareBroker was unset in LispMappingService");
122 public void basicInit(ILispDAO dao) {
124 mapResolver = new MapResolver(dao);
125 mapServer = new MapServer(dao);
128 void setLispDao(ILispDAO dao) {
129 LOG.trace("LispDAO set in LispMappingService");
133 void unsetLispDao(ILispDAO dao) {
134 LOG.trace("LispDAO was unset in LispMappingService");
142 registerWithOSGIConsole();
143 LOG.info("LISP (RFC6830) Mapping Service init finished");
144 } catch (Exception e) {
145 LOG.error(e.getStackTrace().toString());
149 private void registerWithOSGIConsole() {
150 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
151 bundleContext.registerService(CommandProvider.class.getName(), this, null);
154 public void destroy() {
155 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
160 public void _removeEid(final CommandInterpreter ci) {
161 LispAddressContainer eid = LispAFIConvertor.getIPContainer(ci.nextArgument());
162 lispDao.remove(MappingServiceKeyUtil.generateMappingServiceKey(eid));
165 public void _dumpAll(final CommandInterpreter ci) {
166 ci.print(printMappings());
169 public String printMappings() {
170 final StringBuffer sb = new StringBuffer();
171 sb.append("EID\tRLOCs\n");
172 lispDao.getAll(new IRowVisitor() {
175 public void visitRow(Object keyId, String valueKey, Object value) {
176 String key = keyId.getClass().getSimpleName() + "#" + keyId;
177 if (!lastKey.equals(key)) {
178 sb.append("\n" + key + "\t");
180 sb.append(valueKey + "=" + value + "\t");
185 return sb.toString();
188 public void _setShouldOverwriteRlocs(final CommandInterpreter ci) {
190 boolean shouldOverwriteRloc = Boolean.parseBoolean(ci.nextArgument());
191 setOverwrite(shouldOverwriteRloc);
192 } catch (Exception e) {
193 ci.println("Bad Usage!!");
198 public void _addDefaultPassword(final CommandInterpreter ci) {
202 public void addDefaultKeyIPv4() {
203 LispAddressContainer address = LispAFIConvertor.toContainer(
204 new Ipv4AddressBuilder().setIpv4Address(new Ipv4Address("0.0.0.0")).build());
205 addAuthenticationKey(address, 0, "password");
208 public String getHelp() {
209 StringBuffer help = new StringBuffer();
210 help.append("---LISP Mapping Service---\n");
211 help.append("\t dumpAll - Dump all current EID -> RLOC mappings\n");
212 help.append("\t removeEid <EID> - Remove a single EID (/32 or /128)\n");
213 help.append("\t setShouldOverwriteRlocs <true|false> - Set the map server's behavior regarding existing RLOCs\n");
214 help.append("\t addDefaultPassword - Add \"password\" as default password for IPv4 EIDs");
215 return help.toString();
218 public MapReply handleMapRequest(MapRequest request) {
219 return handleMapRequest(request, smr);
222 public MapReply handleMapRequest(MapRequest request, boolean smr) {
223 tlsMapReply.set(null);
224 tlsMapRequest.set(null);
225 mapResolver.handleMapRequest(request, smr, this);
226 // After this invocation we assume that the thread local is filled with
228 if (tlsMapRequest.get() != null) {
229 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
230 new MapRequestBuilder(tlsMapRequest.get().getLeft());
231 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
232 smrib.setTransportAddress(tlsMapRequest.get().getRight());
233 getLispSB().sendMapRequest(smrib.build());
236 return tlsMapReply.get();
241 public MapNotify handleMapRegister(MapRegister mapRegister) {
242 return handleMapRegister(mapRegister, smr);
245 public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
246 tlsMapNotify.set(null);
247 mapServer.handleMapRegister(mapRegister, smr, this);
248 // After this invocation we assume that the thread local is filled with
250 return tlsMapNotify.get();
253 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
254 return mapServer.getAuthenticationKey(address, maskLen);
257 public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
258 mapServer.removeAuthenticationKey(address, maskLen);
261 public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
262 mapServer.addAuthenticationKey(address, maskLen, key);
265 public void removeMapping(LispAddressContainer address, int maskLen) {
266 mapServer.removeMapping(address, maskLen, smr, this);
269 public boolean shouldIterateMask() {
270 return this.shouldIterateMask;
273 public boolean shouldUseSmr() {
277 public void setShouldUseSmr(boolean smr) {
281 public void setShouldIterateMask(boolean shouldIterateMask) {
282 this.shouldIterateMask = shouldIterateMask;
283 this.mapResolver.setShouldIterateMask(shouldIterateMask);
284 this.mapServer.setShouldIterateMask(shouldIterateMask);
287 public void setShouldAuthenticate(boolean shouldAuthenticate) {
288 this.shouldAuthenticate = shouldAuthenticate;
289 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
290 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
293 public boolean shouldAuthenticate() {
294 return shouldAuthenticate;
298 public void onSessionInitiated(ProviderContext session) {
299 LOG.info("Lisp Consumer session initialized!");
300 notificationService = session.getSALService(NotificationService.class);
301 registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
302 registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
303 session.addRpcImplementation(LfmMappingDatabaseService.class, this);
304 rpc.setLispMappingService(this);
305 this.session = session;
308 public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
309 notificationService.registerNotificationListener(notificationType, listener);
312 private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
315 public void onNotification(AddMapping mapRegisterNotification) {
316 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister(), smr);
317 if (mapNotify != null) {
318 TransportAddressBuilder tab = new TransportAddressBuilder();
319 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
320 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
321 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
322 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
323 smnib.setTransportAddress(tab.build());
324 getLispSB().sendMapNotify(smnib.build());
326 LOG.warn("got null map notify");
332 private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
335 public void onNotification(RequestMapping mapRequestNotification) {
336 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
337 if (mapReply != null) {
338 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
339 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
340 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
341 getLispSB().sendMapReply(smrib.build());
343 LOG.warn("got null map reply");
348 private LfmControlPlaneService getLispSB() {
349 if (lispSB == null) {
350 lispSB = session.getRpcService(LfmControlPlaneService.class);
355 public void handleMapReply(MapReply reply) {
356 tlsMapReply.set(reply);
359 public void handleMapNotify(MapNotify notify) {
360 tlsMapNotify.set(notify);
363 public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
364 LOG.debug("Sending SMR to " + subscriber.toString());
365 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
366 smrib.setMapRequest(new MapRequestBuilder(smr).build());
367 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
368 getLispSB().sendMapRequest(smrib.build());
373 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
374 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
378 public void clean() {
383 public boolean shouldOverwrite() {
384 return mapServer.shouldOverwrite();
388 public void setOverwrite(boolean overwrite) {
389 mapServer.setOverwrite(overwrite);
393 public Future<RpcResult<Void>> addKey(AddKeyInput input) {
394 return rpc.addKey(input);
398 public Future<RpcResult<Void>> addMapping(AddMappingInput input) {
399 return rpc.addMapping(input);
403 public Future<RpcResult<GetKeyOutput>> getKey(GetKeyInput input) {
404 return rpc.getKey(input);
408 public Future<RpcResult<GetMappingOutput>> getMapping(GetMappingInput input) {
409 return rpc.getMapping(input);
413 public Future<RpcResult<Void>> removeKey(RemoveKeyInput input) {
414 return rpc.removeKey(input);
418 public Future<RpcResult<Void>> removeMapping(RemoveMappingInput input) {
419 return rpc.removeMapping(input);
423 public Future<RpcResult<Void>> updateKey(UpdateKeyInput input) {
424 return rpc.updateKey(input);
428 public Future<RpcResult<Void>> updateMapping(UpdateMappingInput input) {
429 return rpc.updateMapping(input);