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.AddKeyOutput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingInput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingOutput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyWithRefInput;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyWithRefOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingInput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingOutput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingWithRefInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingWithRefOutput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.LfmMappingDatabaseService;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyInput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyWithRefInput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingInput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingWithRefInput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyInput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyWithRefInput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingInput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingWithRefInput;
78 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
79 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
80 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
81 import org.opendaylight.yangtools.yang.binding.Notification;
82 import org.opendaylight.yangtools.yang.common.RpcResult;
83 import org.osgi.framework.BundleContext;
84 import org.osgi.framework.FrameworkUtil;
85 import org.slf4j.Logger;
86 import org.slf4j.LoggerFactory;
88 public class LispMappingService implements CommandProvider, IFlowMapping, IFlowMappingShell, BindingAwareProvider,
89 IMapRequestResultHandler, IMapNotifyHandler, LfmMappingDatabaseService {
90 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
92 private static final ConfigIni configIni = new ConfigIni();
93 private ILispDAO lispDao = null;
94 private IMapResolverAsync mapResolver;
95 private IMapServerAsync mapServer;
96 private volatile boolean shouldIterateMask;
97 private volatile boolean shouldAuthenticate;
98 private volatile boolean smr = configIni.smrIsSet();
99 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
100 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
101 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
103 private LfmControlPlaneService lispSB = null;
105 private ProviderContext session;
107 private NotificationService notificationService;
109 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
112 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
115 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
118 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
121 void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
122 LOG.trace("BindingAwareBroker set!");
123 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
124 bindingAwareBroker.registerProvider(this, bundleContext);
127 void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
128 LOG.debug("BindingAwareBroker was unset in LispMappingService");
131 public void basicInit(ILispDAO dao) {
133 mapResolver = new MapResolver(dao);
134 mapServer = new MapServer(dao);
137 void setLispDao(ILispDAO dao) {
138 LOG.trace("LispDAO set in LispMappingService");
142 void unsetLispDao(ILispDAO dao) {
143 LOG.trace("LispDAO was unset in LispMappingService");
151 registerWithOSGIConsole();
152 LOG.info("LISP (RFC6830) Mapping Service init finished");
153 } catch (Exception e) {
154 LOG.error(e.getStackTrace().toString());
158 private void registerWithOSGIConsole() {
159 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
160 bundleContext.registerService(CommandProvider.class.getName(), this, null);
163 public void destroy() {
164 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
169 public void _removeEid(final CommandInterpreter ci) {
170 LispAddressContainer eid = LispAFIConvertor.getIPContainer(ci.nextArgument());
171 lispDao.remove(MappingServiceKeyUtil.generateMappingServiceKey(eid));
174 public void _dumpAll(final CommandInterpreter ci) {
175 ci.print(printMappings());
178 public String printMappings() {
179 final StringBuffer sb = new StringBuffer();
180 sb.append("EID\tRLOCs\n");
181 lispDao.getAll(new IRowVisitor() {
184 public void visitRow(Object keyId, String valueKey, Object value) {
185 String key = keyId.getClass().getSimpleName() + "#" + keyId;
186 if (!lastKey.equals(key)) {
187 sb.append("\n" + key + "\t");
189 sb.append(valueKey + "=" + value + "\t");
194 return sb.toString();
197 public void _setShouldOverwriteRlocs(final CommandInterpreter ci) {
199 boolean shouldOverwriteRloc = Boolean.parseBoolean(ci.nextArgument());
200 setOverwrite(shouldOverwriteRloc);
201 } catch (Exception e) {
202 ci.println("Bad Usage!!");
207 public void _addDefaultPassword(final CommandInterpreter ci) {
211 public void addDefaultKeyIPv4() {
212 LispAddressContainer address = LispAFIConvertor.toContainer(
213 new Ipv4AddressBuilder().setIpv4Address(new Ipv4Address("0.0.0.0")).build());
214 addAuthenticationKey(address, 0, "password");
217 public String getHelp() {
218 StringBuffer help = new StringBuffer();
219 help.append("---LISP Mapping Service---\n");
220 help.append("\t dumpAll - Dump all current EID -> RLOC mappings\n");
221 help.append("\t removeEid <EID> - Remove a single EID (/32 or /128)\n");
222 help.append("\t setShouldOverwriteRlocs <true|false> - Set the map server's behavior regarding existing RLOCs\n");
223 help.append("\t addDefaultPassword - Add \"password\" as default password for IPv4 EIDs");
224 return help.toString();
227 public MapReply handleMapRequest(MapRequest request) {
228 return handleMapRequest(request, smr);
231 public MapReply handleMapRequest(MapRequest request, boolean smr) {
232 tlsMapReply.set(null);
233 tlsMapRequest.set(null);
234 mapResolver.handleMapRequest(request, smr, this);
235 // After this invocation we assume that the thread local is filled with
237 if (tlsMapRequest.get() != null) {
238 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
239 new MapRequestBuilder(tlsMapRequest.get().getLeft());
240 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
241 smrib.setTransportAddress(tlsMapRequest.get().getRight());
242 getLispSB().sendMapRequest(smrib.build());
245 return tlsMapReply.get();
250 public MapNotify handleMapRegister(MapRegister mapRegister) {
251 return handleMapRegister(mapRegister, smr);
254 public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
255 tlsMapNotify.set(null);
256 mapServer.handleMapRegister(mapRegister, smr, this);
257 // After this invocation we assume that the thread local is filled with
259 return tlsMapNotify.get();
262 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
263 return mapServer.getAuthenticationKey(address, maskLen);
266 public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
267 mapServer.removeAuthenticationKey(address, maskLen);
270 public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
271 mapServer.addAuthenticationKey(address, maskLen, key);
274 public void removeMapping(LispAddressContainer address, int maskLen) {
275 mapServer.removeMapping(address, maskLen, smr, this);
278 public boolean shouldIterateMask() {
279 return this.shouldIterateMask;
282 public boolean shouldUseSmr() {
286 public void setShouldUseSmr(boolean smr) {
290 public void setShouldIterateMask(boolean shouldIterateMask) {
291 this.shouldIterateMask = shouldIterateMask;
292 this.mapResolver.setShouldIterateMask(shouldIterateMask);
293 this.mapServer.setShouldIterateMask(shouldIterateMask);
296 public void setShouldAuthenticate(boolean shouldAuthenticate) {
297 this.shouldAuthenticate = shouldAuthenticate;
298 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
299 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
302 public boolean shouldAuthenticate() {
303 return shouldAuthenticate;
307 public void onSessionInitiated(ProviderContext session) {
308 LOG.info("Lisp Consumer session initialized!");
309 notificationService = session.getSALService(NotificationService.class);
310 registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
311 registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
312 session.addRpcImplementation(LfmMappingDatabaseService.class, this);
313 this.session = session;
316 public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
317 notificationService.registerNotificationListener(notificationType, listener);
320 private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
323 public void onNotification(AddMapping mapRegisterNotification) {
324 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister(), smr);
325 if (mapNotify != null) {
326 TransportAddressBuilder tab = new TransportAddressBuilder();
327 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
328 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
329 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
330 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
331 smnib.setTransportAddress(tab.build());
332 getLispSB().sendMapNotify(smnib.build());
334 LOG.warn("got null map notify");
340 private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
343 public void onNotification(RequestMapping mapRequestNotification) {
344 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
345 if (mapReply != null) {
346 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
347 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
348 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
349 getLispSB().sendMapReply(smrib.build());
351 LOG.warn("got null map reply");
356 private LfmControlPlaneService getLispSB() {
357 if (lispSB == null) {
358 lispSB = session.getRpcService(LfmControlPlaneService.class);
363 public void handleMapReply(MapReply reply) {
364 tlsMapReply.set(reply);
367 public void handleMapNotify(MapNotify notify) {
368 tlsMapNotify.set(notify);
371 public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
372 LOG.debug("Sending SMR to " + subscriber.toString());
373 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
374 smrib.setMapRequest(new MapRequestBuilder(smr).build());
375 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
376 getLispSB().sendMapRequest(smrib.build());
381 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
382 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
386 public void clean() {
391 public boolean shouldOverwrite() {
392 return mapServer.shouldOverwrite();
396 public void setOverwrite(boolean overwrite) {
397 mapServer.setOverwrite(overwrite);
401 public Future<RpcResult<AddKeyOutput>> addKey(AddKeyInput input) {
402 // TODO Auto-generated method stub
407 public Future<RpcResult<AddMappingOutput>> addMapping(AddMappingInput input) {
408 // TODO Auto-generated method stub
413 public Future<RpcResult<GetKeyOutput>> getKey(GetKeyInput input) {
414 // TODO Auto-generated method stub
419 public Future<RpcResult<GetKeyWithRefOutput>> getKeyWithRef(
420 GetKeyWithRefInput input) {
421 // TODO Auto-generated method stub
426 public Future<RpcResult<GetMappingOutput>> getMapping(GetMappingInput input) {
427 // TODO Auto-generated method stub
432 public Future<RpcResult<GetMappingWithRefOutput>> getMappingWithRef(
433 GetMappingWithRefInput input) {
434 // TODO Auto-generated method stub
439 public Future<RpcResult<Void>> removeKey(RemoveKeyInput input) {
440 // TODO Auto-generated method stub
445 public Future<RpcResult<Void>> removeKeyWithRef(RemoveKeyWithRefInput input) {
446 // TODO Auto-generated method stub
451 public Future<RpcResult<Void>> removeMapping(RemoveMappingInput input) {
452 // TODO Auto-generated method stub
457 public Future<RpcResult<Void>> removeMappingWithRef(
458 RemoveMappingWithRefInput input) {
459 // TODO Auto-generated method stub
464 public Future<RpcResult<Void>> updateKey(UpdateKeyInput input) {
465 // TODO Auto-generated method stub
470 public Future<RpcResult<Void>> updateKeyWithRef(UpdateKeyWithRefInput input) {
471 // TODO Auto-generated method stub
476 public Future<RpcResult<Void>> updateMapping(UpdateMappingInput input) {
477 // TODO Auto-generated method stub
482 public Future<RpcResult<Void>> updateMappingWithRef(
483 UpdateMappingWithRefInput input) {
484 // TODO Auto-generated method stub