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.EidToLocatorRecord;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LfmControlPlaneService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapNotify;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRegister;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapReply;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRequest;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.RequestMapping;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapNotifyInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapReplyInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapRequestInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4AddressBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapnotifymessage.MapNotifyBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapreplymessage.MapReplyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequestmessage.MapRequestBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddress;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddressBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddKeyInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddKeyOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingOutput;
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.GetKeyWithRefInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyWithRefOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingInput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingOutput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingWithRefInput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingWithRefOutput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.LfmMappingDatabaseService;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyInput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyWithRefInput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingInput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingWithRefInput;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyInput;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyWithRefInput;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingInput;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingWithRefInput;
79 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
80 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
81 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
82 import org.opendaylight.yangtools.yang.binding.Notification;
83 import org.opendaylight.yangtools.yang.common.RpcResult;
84 import org.osgi.framework.BundleContext;
85 import org.osgi.framework.FrameworkUtil;
86 import org.slf4j.Logger;
87 import org.slf4j.LoggerFactory;
89 public class LispMappingService implements CommandProvider, IFlowMapping, IFlowMappingShell, BindingAwareProvider,
90 IMapRequestResultHandler, IMapNotifyHandler, LfmMappingDatabaseService {
91 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
93 private static final ConfigIni configIni = new ConfigIni();
94 private ILispDAO lispDao = null;
95 private IMapResolverAsync mapResolver;
96 private IMapServerAsync mapServer;
97 private volatile boolean shouldIterateMask;
98 private volatile boolean shouldAuthenticate;
99 private volatile boolean smr = configIni.smrIsSet();
100 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
101 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
102 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
104 private LfmControlPlaneService lispSB = null;
106 private ProviderContext session;
108 private NotificationService notificationService;
110 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
113 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
116 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
119 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
122 void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
123 LOG.trace("BindingAwareBroker set!");
124 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
125 bindingAwareBroker.registerProvider(this, bundleContext);
128 void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
129 LOG.debug("BindingAwareBroker was unset in LispMappingService");
132 public void basicInit(ILispDAO dao) {
134 mapResolver = new MapResolver(dao);
135 mapServer = new MapServer(dao);
138 void setLispDao(ILispDAO dao) {
139 LOG.trace("LispDAO set in LispMappingService");
143 void unsetLispDao(ILispDAO dao) {
144 LOG.trace("LispDAO was unset in LispMappingService");
152 registerWithOSGIConsole();
153 LOG.info("LISP (RFC6830) Mapping Service init finished");
154 } catch (Exception e) {
155 LOG.error(e.getStackTrace().toString());
159 private void registerWithOSGIConsole() {
160 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
161 bundleContext.registerService(CommandProvider.class.getName(), this, null);
164 public void destroy() {
165 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
170 public void _removeEid(final CommandInterpreter ci) {
171 LispAddressContainer eid = LispAFIConvertor.getIPContainer(ci.nextArgument());
172 lispDao.remove(MappingServiceKeyUtil.generateMappingServiceKey(eid));
175 public void _dumpAll(final CommandInterpreter ci) {
176 ci.print(printMappings());
179 public String printMappings() {
180 final StringBuffer sb = new StringBuffer();
181 sb.append("EID\tRLOCs\n");
182 lispDao.getAll(new IRowVisitor() {
185 public void visitRow(Object keyId, String valueKey, Object value) {
186 String key = keyId.getClass().getSimpleName() + "#" + keyId;
187 if (!lastKey.equals(key)) {
188 sb.append("\n" + key + "\t");
190 sb.append(valueKey + "=" + value + "\t");
195 return sb.toString();
198 public void _setShouldOverwriteRlocs(final CommandInterpreter ci) {
200 boolean shouldOverwriteRloc = Boolean.parseBoolean(ci.nextArgument());
201 setOverwrite(shouldOverwriteRloc);
202 } catch (Exception e) {
203 ci.println("Bad Usage!!");
208 public void _addDefaultPassword(final CommandInterpreter ci) {
212 public void addDefaultKeyIPv4() {
213 LispAddressContainer address = LispAFIConvertor.toContainer(
214 new Ipv4AddressBuilder().setIpv4Address(new Ipv4Address("0.0.0.0")).build());
215 addAuthenticationKey(address, 0, "password");
218 public String getHelp() {
219 StringBuffer help = new StringBuffer();
220 help.append("---LISP Mapping Service---\n");
221 help.append("\t dumpAll - Dump all current EID -> RLOC mappings\n");
222 help.append("\t removeEid <EID> - Remove a single EID (/32 or /128)\n");
223 help.append("\t setShouldOverwriteRlocs <true|false> - Set the map server's behavior regarding existing RLOCs\n");
224 help.append("\t addDefaultPassword - Add \"password\" as default password for IPv4 EIDs");
225 return help.toString();
228 public MapReply handleMapRequest(MapRequest request) {
229 return handleMapRequest(request, smr);
232 public MapReply handleMapRequest(MapRequest request, boolean smr) {
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 tlsMapNotify.set(null);
257 mapServer.handleMapRegister(mapRegister, smr, this);
258 // After this invocation we assume that the thread local is filled with
260 return tlsMapNotify.get();
263 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
264 return mapServer.getAuthenticationKey(address, maskLen);
267 public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
268 mapServer.removeAuthenticationKey(address, maskLen);
271 public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
272 mapServer.addAuthenticationKey(address, maskLen, key);
275 public void removeMapping(LispAddressContainer address, int maskLen) {
276 mapServer.removeMapping(address, maskLen, smr, this);
279 public boolean shouldIterateMask() {
280 return this.shouldIterateMask;
283 public boolean shouldUseSmr() {
287 public void setShouldUseSmr(boolean smr) {
291 public void setShouldIterateMask(boolean shouldIterateMask) {
292 this.shouldIterateMask = shouldIterateMask;
293 this.mapResolver.setShouldIterateMask(shouldIterateMask);
294 this.mapServer.setShouldIterateMask(shouldIterateMask);
297 public void setShouldAuthenticate(boolean shouldAuthenticate) {
298 this.shouldAuthenticate = shouldAuthenticate;
299 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
300 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
303 public boolean shouldAuthenticate() {
304 return shouldAuthenticate;
308 public void onSessionInitiated(ProviderContext session) {
309 LOG.info("Lisp Consumer session initialized!");
310 notificationService = session.getSALService(NotificationService.class);
311 registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
312 registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
313 session.addRpcImplementation(LfmMappingDatabaseService.class, this);
314 this.session = session;
317 public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
318 notificationService.registerNotificationListener(notificationType, listener);
321 private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
324 public void onNotification(AddMapping mapRegisterNotification) {
325 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister(), smr);
326 if (mapNotify != null) {
327 TransportAddressBuilder tab = new TransportAddressBuilder();
328 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
329 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
330 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
331 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
332 smnib.setTransportAddress(tab.build());
333 getLispSB().sendMapNotify(smnib.build());
335 LOG.warn("got null map notify");
341 private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
344 public void onNotification(RequestMapping mapRequestNotification) {
345 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
346 if (mapReply != null) {
347 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
348 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
349 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
350 getLispSB().sendMapReply(smrib.build());
352 LOG.warn("got null map reply");
357 private LfmControlPlaneService getLispSB() {
358 if (lispSB == null) {
359 lispSB = session.getRpcService(LfmControlPlaneService.class);
364 public void handleMapReply(MapReply reply) {
365 tlsMapReply.set(reply);
368 public void handleMapNotify(MapNotify notify) {
369 tlsMapNotify.set(notify);
372 public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
373 LOG.debug("Sending SMR to " + subscriber.toString());
374 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
375 smrib.setMapRequest(new MapRequestBuilder(smr).build());
376 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
377 getLispSB().sendMapRequest(smrib.build());
382 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
383 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
387 public void clean() {
392 public boolean shouldOverwrite() {
393 return mapServer.shouldOverwrite();
397 public void setOverwrite(boolean overwrite) {
398 mapServer.setOverwrite(overwrite);
402 public Future<RpcResult<AddKeyOutput>> addKey(AddKeyInput input) {
403 // TODO Auto-generated method stub
408 public Future<RpcResult<AddMappingOutput>> addMapping(AddMappingInput input) {
409 // TODO Auto-generated method stub
414 public Future<RpcResult<GetKeyOutput>> getKey(GetKeyInput input) {
415 // TODO Auto-generated method stub
420 public Future<RpcResult<GetKeyWithRefOutput>> getKeyWithRef(
421 GetKeyWithRefInput input) {
422 // TODO Auto-generated method stub
427 public Future<RpcResult<GetMappingOutput>> getMapping(GetMappingInput input) {
428 // TODO Auto-generated method stub
433 public Future<RpcResult<GetMappingWithRefOutput>> getMappingWithRef(
434 GetMappingWithRefInput input) {
435 // TODO Auto-generated method stub
440 public Future<RpcResult<Void>> removeKey(RemoveKeyInput input) {
441 // TODO Auto-generated method stub
446 public Future<RpcResult<Void>> removeKeyWithRef(RemoveKeyWithRefInput input) {
447 // TODO Auto-generated method stub
452 public Future<RpcResult<Void>> removeMapping(RemoveMappingInput input) {
453 // TODO Auto-generated method stub
458 public Future<RpcResult<Void>> removeMappingWithRef(
459 RemoveMappingWithRefInput input) {
460 // TODO Auto-generated method stub
465 public Future<RpcResult<Void>> updateKey(UpdateKeyInput input) {
466 // TODO Auto-generated method stub
471 public Future<RpcResult<Void>> updateKeyWithRef(UpdateKeyWithRefInput input) {
472 // TODO Auto-generated method stub
477 public Future<RpcResult<Void>> updateMapping(UpdateMappingInput input) {
478 // TODO Auto-generated method stub
483 public Future<RpcResult<Void>> updateMappingWithRef(
484 UpdateMappingWithRefInput input) {
485 // TODO Auto-generated method stub