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.net.InetAddress;
12 import java.net.UnknownHostException;
14 import org.apache.commons.lang3.tuple.MutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.eclipse.osgi.framework.console.CommandInterpreter;
17 import org.eclipse.osgi.framework.console.CommandProvider;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
21 import org.opendaylight.controller.sal.binding.api.NotificationListener;
22 import org.opendaylight.controller.sal.binding.api.NotificationService;
23 import org.opendaylight.lispflowmapping.implementation.dao.InMemoryDAO;
24 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
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.util.LispAFIConvertor;
29 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
30 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
31 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
32 import org.opendaylight.lispflowmapping.interfaces.dao.IQueryAll;
33 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
34 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
35 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
36 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
38 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
39 import org.opendaylight.lispflowmapping.type.sbplugin.ILispSouthboundPlugin;
40 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.AddMapping;
41 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
42 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
43 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
44 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
45 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.RequestMapping;
46 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
47 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
48 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.Address;
49 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
52 import org.osgi.framework.BundleContext;
53 import org.osgi.framework.FrameworkUtil;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 import com.google.common.net.InetAddresses;
59 public class LispMappingService implements CommandProvider, IFlowMapping, BindingAwareConsumer, //
60 IMapRequestResultHandler, IMapNotifyHandler {
61 protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
63 private ILispDAO lispDao = null;
64 private IMapResolverAsync mapResolver;
65 private IMapServerAsync mapServer;
66 private volatile boolean shouldIterateMask;
67 private volatile boolean shouldAuthenticate;
68 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
69 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
70 private ThreadLocal<Pair<MapRequest, InetAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, InetAddress>>();
72 private ILispSouthboundPlugin lispSB = null;
74 private ConsumerContext session;
76 public static void main(String[] args) throws Exception {
77 LispMappingService serv = new LispMappingService();
78 serv.setLispDao(new InMemoryDAO());
82 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
85 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
88 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
91 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
94 void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
95 logger.trace("BindingAwareBroker set!");
96 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
97 bindingAwareBroker.registerConsumer(this, bundleContext);
100 void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
101 logger.debug("BindingAwareBroker was unset in LispMappingService");
104 public void basicInit(ILispDAO dao) {
106 mapResolver = new MapResolver(dao);
107 mapServer = new MapServer(dao);
110 void setLispDao(ILispDAO dao) {
111 logger.trace("LispDAO set in LispMappingService");
113 logger.trace("Registering LispIpv4Address");
114 lispDao.register(LispIpv4AddressInMemoryConverter.class);
115 logger.trace("Registering LispIpv6Address");
116 lispDao.register(LispIpv6AddressInMemoryConverter.class);
117 logger.trace("Registering MappingServiceKey");
118 lispDao.register(MappingServiceKeyConvertor.class);
119 logger.trace("Registering MappingServiceNoMaskKey");
120 lispDao.register(MappingServiceNoMaskKeyConvertor.class);
123 void unsetLispDao(ILispDAO dao) {
124 logger.trace("LispDAO was unset in LispMappingService");
132 registerWithOSGIConsole();
133 logger.info("LISP (RFC6830) Mapping Service init finished");
134 } catch (Throwable t) {
135 logger.error(t.getStackTrace().toString());
139 private void registerWithOSGIConsole() {
140 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
141 bundleContext.registerService(CommandProvider.class.getName(), this, null);
144 public void destroy() {
145 logger.info("LISP (RFC6830) Mapping Service is destroyed!");
150 public void _removeEid(final CommandInterpreter ci) {
151 lispDao.remove(LispAFIConvertor.asIPAfiAddress(ci.nextArgument()));
154 public void _dumpAll(final CommandInterpreter ci) {
155 ci.println("EID\tRLOCs");
156 if (lispDao instanceof IQueryAll) {
157 ((IQueryAll) lispDao).getAll(new IRowVisitor() {
160 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
161 String key = keyType.getSimpleName() + "#" + keyId;
162 if (!lastKey.equals(key)) {
164 ci.print(key + "\t");
166 ci.print(valueKey + "=" + value + "\t");
172 ci.println("Not implemented by this DAO");
177 public void _addDefaultPassword(final CommandInterpreter ci) {
178 LispAddressContainerBuilder builder = new LispAddressContainerBuilder();
179 builder.setAddress((Address) (new Ipv4Builder().setIpv4Address(new Ipv4Address("0.0.0.0")).build()));
180 addAuthenticationKey(builder.build(), 0, "password");
183 public String getHelp() {
184 StringBuffer help = new StringBuffer();
185 help.append("---LISP Mapping Service---\n");
186 help.append("\t dumpAll - Dump all current EID -> RLOC mapping\n");
187 help.append("\t removeEid - Remove a single LispIPv4Address Eid\n");
188 return help.toString();
191 public MapReply handleMapRequest(MapRequest request) {
192 tlsMapReply.set(null);
193 tlsMapRequest.set(null);
194 mapResolver.handleMapRequest(request, this);
195 // After this invocation we assume that the thread local is filled with
197 if (tlsMapRequest.get() != null) {
198 getLispSB().handleMapRequest(tlsMapRequest.get().getLeft(), tlsMapRequest.get().getRight());
201 return tlsMapReply.get();
206 public MapNotify handleMapRegister(MapRegister mapRegister) {
207 tlsMapNotify.set(null);
208 mapServer.handleMapRegister(mapRegister, this);
209 // After this invocation we assume that the thread local is filled with
211 return tlsMapNotify.get();
214 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
215 return mapServer.getAuthenticationKey(address, maskLen);
218 public boolean removeAuthenticationKey(LispAddressContainer address, int maskLen) {
219 return mapServer.removeAuthenticationKey(address, maskLen);
222 public boolean addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
223 return mapServer.addAuthenticationKey(address, maskLen, key);
226 public boolean shouldIterateMask() {
227 return this.shouldIterateMask;
230 public void setShouldIterateMask(boolean shouldIterateMask) {
231 this.shouldIterateMask = shouldIterateMask;
232 this.mapResolver.setShouldIterateMask(shouldIterateMask);
233 this.mapServer.setShouldIterateMask(shouldIterateMask);
236 public void setShouldAuthenticate(boolean shouldAuthenticate) {
237 this.shouldAuthenticate = shouldAuthenticate;
238 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
239 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
242 public boolean shouldAuthenticate() {
243 return shouldAuthenticate;
246 public void onSessionInitialized(ConsumerContext session) {
247 logger.info("Lisp Consumer session initialized!");
248 NotificationService notificationService = session.getSALService(NotificationService.class);
249 // notificationService.registerNotificationListener(LispNotification.class,
251 notificationService.registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
252 notificationService.registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
253 this.session = session;
256 private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
259 public void onNotification(AddMapping mapRegisterNotification) {
260 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
261 getLispSB().handleMapNotify(mapNotify,
262 LispNotificationHelper.getInetAddressFromIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress()));
267 private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
270 public void onNotification(RequestMapping mapRequestNotification) {
271 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
272 getLispSB().handleMapReply(mapReply,
273 LispNotificationHelper.getInetAddressFromIpAddress(mapRequestNotification.getTransportAddress().getIpAddress()));
278 private ILispSouthboundPlugin getLispSB() {
279 if (lispSB == null) {
280 lispSB = session.getRpcService(ILispSouthboundPlugin.class);
285 public void handleMapReply(MapReply reply) {
286 tlsMapReply.set(reply);
289 public void handleMapNotify(MapNotify notify) {
290 tlsMapNotify.set(notify);
294 public void handleNonProxyMapRequest(MapRequest mapRequest, InetAddress targetAddress) {
295 tlsMapRequest.set(new MutablePair<MapRequest, InetAddress>(mapRequest, targetAddress));