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.IMappingServiceKey;
19 import org.opendaylight.lispflowmapping.interfaces.dao.IQueryAll;
20 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
21 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolver;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServer;
24 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
25 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
26 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
27 import org.opendaylight.lispflowmapping.type.lisp.MapRequest;
28 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
29 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
30 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.FrameworkUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class LispMappingService implements CommandProvider, IFlowMapping {
37 protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
39 private ILispDAO lispDao = null;
40 private IMapResolver mapResolver;
41 private IMapServer mapServer;
43 public static void main(String[] args) throws Exception {
44 LispMappingService serv = new LispMappingService();
45 serv.setLispDao(new InMemoryDAO());
49 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<LispIpv4Address, Integer> {
52 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<LispIpv6Address, Integer> {
54 class MappingServiceKeyConvertor implements ILispTypeConverter<IMappingServiceKey, Integer> {
58 void setLispDao(ILispDAO dao) {
59 logger.info("LispDAO set in LispMappingService");
61 mapResolver = new MapResolver(dao);
62 mapServer = new MapServer(dao);
63 logger.debug("Registering LispIpv4Address");
64 lispDao.register(LispIpv4AddressInMemoryConverter.class);
65 logger.debug("Registering LispIpv6Address");
66 lispDao.register(LispIpv6AddressInMemoryConverter.class);
67 logger.debug("Registering MAppingServiceKey");
68 lispDao.register(MappingServiceKeyConvertor.class);
71 void unsetLispDao(ILispDAO dao) {
72 logger.debug("LispDAO was unset in LispMappingService");
79 logger.debug("LISP (RFC6830) Mapping Service is initialized!");
80 registerWithOSGIConsole();
83 private void registerWithOSGIConsole() {
84 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
85 bundleContext.registerService(CommandProvider.class.getName(), this, null);
89 public void destroy() {
90 logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
96 public void _removeEid(final CommandInterpreter ci) {
97 lispDao.remove(new LispIpv4Address(ci.nextArgument()));
100 public void _dumpAll(final CommandInterpreter ci) {
101 ci.println("EID\tRLOCs");
102 if (lispDao instanceof IQueryAll) {
103 ((IQueryAll) lispDao).getAll(new IRowVisitor() {
106 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
107 String key = keyType.getSimpleName() + "#" + keyId;
108 if (!lastKey.equals(key)) {
110 ci.print(key + "\t");
112 ci.print(valueKey + "=" + value + "\t");
118 ci.println("Not implemented by this DAO");
124 public String getHelp() {
125 StringBuffer help = new StringBuffer();
126 help.append("---LISP Mapping Service---\n");
127 help.append("\t dumpAll - Dump all current EID -> RLOC mapping\n");
128 help.append("\t removeEid - Remove a single LispIPv4Address Eid\n");
129 return help.toString();
132 public MapReply handleMapRequest(MapRequest request) {
133 return mapResolver.handleMapRequest(request);
136 public MapNotify handleMapRegister(MapRegister mapRegister) {
137 return mapServer.handleMapRegister(mapRegister);
140 public String getAuthenticationKey(LispAddress address, int maskLen) {
141 return mapServer.getAuthenticationKey(address, maskLen);
144 public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
145 return mapServer.removeAuthenticationKey(address, maskLen);
148 public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
149 return mapServer.addAuthenticationKey(address, maskLen, key);
152 public boolean iterateMask() {
153 return mapResolver.iterateMask();
156 public void setIterateMask(boolean iterateMask) {
157 this.mapResolver.setIterateMask(iterateMask);