support map request serialization TELSDN-378: #close
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.java
1 /*
2  * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.lispflowmapping.implementation;
10
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;
36
37 public class LispMappingService implements CommandProvider, IFlowMapping {
38     protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
39
40     private ILispDAO lispDao = null;
41     private volatile boolean shouldIterateMask;
42     private volatile boolean shouldAuthenticate;
43     private IMapResolver mapResolver;
44     private IMapServer mapServer;
45
46     public static void main(String[] args) throws Exception {
47         LispMappingService serv = new LispMappingService();
48         serv.setLispDao(new InMemoryDAO());
49         serv.init();
50     }
51
52     class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<LispIpv4Address, Integer> {
53     }
54
55     class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<LispIpv6Address, Integer> {
56     }
57
58     class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
59     }
60
61     class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
62     }
63
64     void setLispDao(ILispDAO dao) {
65         logger.info("LispDAO set in LispMappingService");
66         lispDao = dao;
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);
77     }
78
79     void unsetLispDao(ILispDAO dao) {
80         logger.debug("LispDAO was unset in LispMappingService");
81         mapServer = null;
82         mapResolver = null;
83         lispDao = null;
84     }
85
86     public void init() {
87         logger.debug("LISP (RFC6830) Mapping Service is initialized!");
88         registerWithOSGIConsole();
89     }
90
91     private void registerWithOSGIConsole() {
92         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
93         bundleContext.registerService(CommandProvider.class.getName(), this, null);
94     }
95
96     public void destroy() {
97         logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
98         mapResolver = null;
99         mapServer = null;
100     }
101
102     public void _removeEid(final CommandInterpreter ci) {
103         lispDao.remove(new LispIpv4Address(ci.nextArgument()));
104     }
105
106     public void _dumpAll(final CommandInterpreter ci) {
107         ci.println("EID\tRLOCs");
108         if (lispDao instanceof IQueryAll) {
109             ((IQueryAll) lispDao).getAll(new IRowVisitor() {
110                 String lastKey = "";
111
112                 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
113                     String key = keyType.getSimpleName() + "#" + keyId;
114                     if (!lastKey.equals(key)) {
115                         ci.println();
116                         ci.print(key + "\t");
117                     }
118                     ci.print(valueKey + "=" + value + "\t");
119                     lastKey = key;
120                 }
121             });
122             ci.println();
123         } else {
124             ci.println("Not implemented by this DAO");
125         }
126         return;
127     }
128
129     public void _addDefaultPassword(final CommandInterpreter ci) {
130         addAuthenticationKey(new LispIpv4Address("0.0.0.0"), 0, "password");
131     }
132
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();
139     }
140
141     public MapReply handleMapRequest(MapRequest request) {
142         return mapResolver.handleMapRequest(request);
143     }
144
145     public MapNotify handleMapRegister(MapRegister mapRegister) {
146         return mapServer.handleMapRegister(mapRegister);
147     }
148
149     public String getAuthenticationKey(LispAddress address, int maskLen) {
150         return mapServer.getAuthenticationKey(address, maskLen);
151     }
152
153     public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
154         return mapServer.removeAuthenticationKey(address, maskLen);
155     }
156
157     public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
158         return mapServer.addAuthenticationKey(address, maskLen, key);
159     }
160
161     public boolean shouldIterateMask() {
162         return this.shouldIterateMask;
163     }
164
165     public void setShouldIterateMask(boolean shouldIterateMask) {
166         this.shouldIterateMask = shouldIterateMask;
167         this.mapResolver.setShouldIterateMask(shouldIterateMask);
168         this.mapServer.setShouldIterateMask(shouldIterateMask);
169     }
170
171     public void setShouldAuthenticate(boolean shouldAuthenticate) {
172         this.shouldAuthenticate = shouldAuthenticate;
173         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
174         this.mapServer.setShouldAuthenticate(shouldAuthenticate);
175     }
176
177     public boolean shouldAuthenticate() {
178         return shouldAuthenticate;
179     }
180
181 }