2 * Copyright (c) 2013 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 org.eclipse.osgi.framework.console.CommandInterpreter;
12 import org.eclipse.osgi.framework.console.CommandProvider;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
15 import org.opendaylight.controller.sal.binding.api.NotificationListener;
16 import org.opendaylight.controller.sal.binding.api.NotificationService;
17 import org.opendaylight.lispflowmapping.implementation.dao.InMemoryDAO;
18 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
19 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
20 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
21 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
22 import org.opendaylight.lispflowmapping.interfaces.dao.IQueryAll;
23 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
24 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKey;
25 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceNoMaskKey;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
27 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolver;
28 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServer;
29 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
30 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
31 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
32 import org.opendaylight.lispflowmapping.type.lisp.MapRequest;
33 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
34 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
35 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
36 import org.opendaylight.lispflowmapping.type.sbplugin.ILispSouthboundPlugin;
37 import org.opendaylight.lispflowmapping.type.sbplugin.LispNotification;
38 import org.opendaylight.lispflowmapping.type.sbplugin.MapRegisterNotification;
39 import org.opendaylight.lispflowmapping.type.sbplugin.MapRequestNotification;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.FrameworkUtil;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class LispMappingService implements CommandProvider, IFlowMapping, BindingAwareConsumer, NotificationListener<LispNotification> {
46 protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
48 private ILispDAO lispDao = null;
49 private volatile boolean shouldIterateMask;
50 private volatile boolean shouldAuthenticate;
51 private IMapResolver mapResolver;
52 private IMapServer mapServer;
54 private ILispSouthboundPlugin lispSB = null;
56 private ConsumerContext session;
58 public static void main(String[] args) throws Exception {
59 LispMappingService serv = new LispMappingService();
60 serv.setLispDao(new InMemoryDAO());
64 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<LispIpv4Address, Integer> {
67 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<LispIpv6Address, Integer> {
70 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
73 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
76 void setLispDao(ILispDAO dao) {
77 logger.info("LispDAO set in LispMappingService");
79 mapResolver = new MapResolver(dao);
80 mapServer = new MapServer(dao);
81 logger.debug("Registering LispIpv4Address");
82 lispDao.register(LispIpv4AddressInMemoryConverter.class);
83 logger.debug("Registering LispIpv6Address");
84 lispDao.register(LispIpv6AddressInMemoryConverter.class);
85 logger.debug("Registering MappingServiceKey");
86 lispDao.register(MappingServiceKeyConvertor.class);
87 logger.debug("Registering MappingServiceNoMaskKey");
88 lispDao.register(MappingServiceNoMaskKeyConvertor.class);
91 void unsetLispDao(ILispDAO dao) {
92 logger.info("LispDAO was unset in LispMappingService");
99 logger.info("LISP (RFC6830) Mapping Service is initialized!");
100 registerWithOSGIConsole();
103 private void registerWithOSGIConsole() {
104 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
105 bundleContext.registerService(CommandProvider.class.getName(), this, null);
108 public void destroy() {
109 logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
114 public void _removeEid(final CommandInterpreter ci) {
115 lispDao.remove(new LispIpv4Address(ci.nextArgument()));
118 public void _dumpAll(final CommandInterpreter ci) {
119 ci.println("EID\tRLOCs");
120 if (lispDao instanceof IQueryAll) {
121 ((IQueryAll) lispDao).getAll(new IRowVisitor() {
124 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
125 String key = keyType.getSimpleName() + "#" + keyId;
126 if (!lastKey.equals(key)) {
128 ci.print(key + "\t");
130 ci.print(valueKey + "=" + value + "\t");
136 ci.println("Not implemented by this DAO");
141 public void _addDefaultPassword(final CommandInterpreter ci) {
142 addAuthenticationKey(new LispIpv4Address("0.0.0.0"), 0, "password");
145 public String getHelp() {
146 StringBuffer help = new StringBuffer();
147 help.append("---LISP Mapping Service---\n");
148 help.append("\t dumpAll - Dump all current EID -> RLOC mapping\n");
149 help.append("\t removeEid - Remove a single LispIPv4Address Eid\n");
150 return help.toString();
153 public MapReply handleMapRequest(MapRequest request) {
154 return mapResolver.handleMapRequest(request);
157 public MapNotify handleMapRegister(MapRegister mapRegister) {
158 return mapServer.handleMapRegister(mapRegister);
161 public String getAuthenticationKey(LispAddress address, int maskLen) {
162 return mapServer.getAuthenticationKey(address, maskLen);
165 public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
166 return mapServer.removeAuthenticationKey(address, maskLen);
169 public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
170 return mapServer.addAuthenticationKey(address, maskLen, key);
173 public boolean shouldIterateMask() {
174 return this.shouldIterateMask;
177 public void setShouldIterateMask(boolean shouldIterateMask) {
178 this.shouldIterateMask = shouldIterateMask;
179 this.mapResolver.setShouldIterateMask(shouldIterateMask);
180 this.mapServer.setShouldIterateMask(shouldIterateMask);
183 public void setShouldAuthenticate(boolean shouldAuthenticate) {
184 this.shouldAuthenticate = shouldAuthenticate;
185 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
186 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
189 public boolean shouldAuthenticate() {
190 return shouldAuthenticate;
193 public void onSessionInitialized(ConsumerContext session) {
194 logger.info("Lisp Consumer session initialized!");
195 NotificationService notificationService = session.getSALService(NotificationService.class);
196 notificationService.registerNotificationListener(LispNotification.class, this);
197 this.session = session;
200 public void onNotification(LispNotification notification) {
202 if (notification instanceof MapRegisterNotification) {
203 logger.trace("MapRegister notification recieved!");
204 MapRegisterNotification mapRegisterNotification = (MapRegisterNotification) notification;
205 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
206 getLispSB().handleMapNotify(mapNotify, mapRegisterNotification.getSourceAddress());
207 } else if (notification instanceof MapRequestNotification) {
208 logger.trace("MapRequest notification recieved!");
209 MapRequestNotification mapRequestNotification = (MapRequestNotification) notification;
210 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
211 getLispSB().handleMapReply(mapReply, mapRequestNotification.getSourceAddress());
213 logger.error("Unknown notification: " + notification);
215 } catch (Throwable t) {
220 private ILispSouthboundPlugin getLispSB() {
221 if (lispSB == null) {
222 lispSB = session.getRpcService(ILispSouthboundPlugin.class);