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.lispflowmapping.implementation.dao.InMemoryDAO;
14 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
15 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
18 import org.opendaylight.lispflowmapping.interfaces.dao.IQueryAll;
19 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
20 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKey;
21 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceNoMaskKey;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolver;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServer;
25 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
26 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
27 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
28 import org.opendaylight.lispflowmapping.type.lisp.MapRequest;
29 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
30 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
31 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.FrameworkUtil;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class LispMappingService implements CommandProvider, IFlowMapping {
38 protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
40 private ILispDAO lispDao = null;
41 private volatile boolean shouldIterateMask;
42 private volatile boolean shouldAuthenticate;
43 private IMapResolver mapResolver;
44 private IMapServer mapServer;
46 public static void main(String[] args) throws Exception {
47 LispMappingService serv = new LispMappingService();
48 serv.setLispDao(new InMemoryDAO());
52 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<LispIpv4Address, Integer> {
55 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<LispIpv6Address, Integer> {
58 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
61 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
64 void setLispDao(ILispDAO dao) {
65 logger.info("LispDAO set in LispMappingService");
67 mapResolver = new MapResolver(dao);
68 mapServer = new MapServer(dao);
69 logger.debug("Registering LispIpv4Address");
70 lispDao.register(LispIpv4AddressInMemoryConverter.class);
71 logger.debug("Registering LispIpv6Address");
72 lispDao.register(LispIpv6AddressInMemoryConverter.class);
73 logger.debug("Registering MappingServiceKey");
74 lispDao.register(MappingServiceKeyConvertor.class);
75 logger.debug("Registering MappingServiceNoMaskKey");
76 lispDao.register(MappingServiceNoMaskKeyConvertor.class);
79 void unsetLispDao(ILispDAO dao) {
80 logger.debug("LispDAO was unset in LispMappingService");
87 logger.debug("LISP (RFC6830) Mapping Service is initialized!");
88 registerWithOSGIConsole();
91 private void registerWithOSGIConsole() {
92 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
93 bundleContext.registerService(CommandProvider.class.getName(), this, null);
96 public void destroy() {
97 logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
102 public void _removeEid(final CommandInterpreter ci) {
103 lispDao.remove(new LispIpv4Address(ci.nextArgument()));
106 public void _dumpAll(final CommandInterpreter ci) {
107 ci.println("EID\tRLOCs");
108 if (lispDao instanceof IQueryAll) {
109 ((IQueryAll) lispDao).getAll(new IRowVisitor() {
112 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
113 String key = keyType.getSimpleName() + "#" + keyId;
114 if (!lastKey.equals(key)) {
116 ci.print(key + "\t");
118 ci.print(valueKey + "=" + value + "\t");
124 ci.println("Not implemented by this DAO");
129 public void _addDefaultPassword(final CommandInterpreter ci) {
130 addAuthenticationKey(new LispIpv4Address("0.0.0.0"), 0, "password");
133 public String getHelp() {
134 StringBuffer help = new StringBuffer();
135 help.append("---LISP Mapping Service---\n");
136 help.append("\t dumpAll - Dump all current EID -> RLOC mapping\n");
137 help.append("\t removeEid - Remove a single LispIPv4Address Eid\n");
138 return help.toString();
141 public MapReply handleMapRequest(MapRequest request) {
142 return mapResolver.handleMapRequest(request);
145 public MapNotify handleMapRegister(MapRegister mapRegister) {
146 return mapServer.handleMapRegister(mapRegister);
149 public String getAuthenticationKey(LispAddress address, int maskLen) {
150 return mapServer.getAuthenticationKey(address, maskLen);
153 public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
154 return mapServer.removeAuthenticationKey(address, maskLen);
157 public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
158 return mapServer.addAuthenticationKey(address, maskLen, key);
161 public boolean shouldIterateMask() {
162 return this.shouldIterateMask;
165 public void setShouldIterateMask(boolean shouldIterateMask) {
166 this.shouldIterateMask = shouldIterateMask;
167 this.mapResolver.setShouldIterateMask(shouldIterateMask);
168 this.mapServer.setShouldIterateMask(shouldIterateMask);
171 public void setShouldAuthenticate(boolean shouldAuthenticate) {
172 this.shouldAuthenticate = shouldAuthenticate;
173 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
174 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
177 public boolean shouldAuthenticate() {
178 return shouldAuthenticate;