support mask eid prefix TELSDN-185: #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.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;
35
36 public class LispMappingService implements CommandProvider, IFlowMapping {
37     protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
38
39     private ILispDAO lispDao = null;
40     private IMapResolver mapResolver;
41     private IMapServer mapServer;
42
43     public static void main(String[] args) throws Exception {
44         LispMappingService serv = new LispMappingService();
45         serv.setLispDao(new InMemoryDAO());
46         serv.init();
47     }
48
49     class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<LispIpv4Address, Integer> {
50     }
51
52     class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<LispIpv6Address, Integer> {
53     }
54     class MappingServiceKeyConvertor implements ILispTypeConverter<IMappingServiceKey, Integer> {
55     }
56     
57
58     void setLispDao(ILispDAO dao) {
59         logger.info("LispDAO set in LispMappingService");
60         lispDao = dao;
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);
69     }
70
71     void unsetLispDao(ILispDAO dao) {
72         logger.debug("LispDAO was unset in LispMappingService");
73         mapServer = null;
74         mapResolver = null;
75         lispDao = null;
76     }
77
78     public void init() {
79         logger.debug("LISP (RFC6830) Mapping Service is initialized!");
80         registerWithOSGIConsole();
81     }
82
83     private void registerWithOSGIConsole() {
84         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
85         bundleContext.registerService(CommandProvider.class.getName(), this, null);
86     }
87
88
89     public void destroy() {
90         logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
91         mapResolver = null;
92         mapServer = null;
93     }
94
95
96     public void _removeEid(final CommandInterpreter ci) {
97         lispDao.remove(new LispIpv4Address(ci.nextArgument()));
98     }
99
100     public void _dumpAll(final CommandInterpreter ci) {
101         ci.println("EID\tRLOCs");
102         if (lispDao instanceof IQueryAll) {
103             ((IQueryAll) lispDao).getAll(new IRowVisitor() {
104                 String lastKey = "";
105
106                 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
107                     String key = keyType.getSimpleName() + "#" + keyId;
108                     if (!lastKey.equals(key)) {
109                         ci.println();
110                         ci.print(key + "\t");
111                     }
112                     ci.print(valueKey + "=" + value + "\t");
113                     lastKey = key;
114                 }
115             });
116             ci.println();
117         } else {
118             ci.println("Not implemented by this DAO");
119         }
120         return;
121     }
122
123
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();
130     }
131
132     public MapReply handleMapRequest(MapRequest request) {
133         return mapResolver.handleMapRequest(request);
134     }
135
136     public MapNotify handleMapRegister(MapRegister mapRegister) {
137         return mapServer.handleMapRegister(mapRegister);
138     }
139
140     public String getAuthenticationKey(LispAddress address, int maskLen) {
141         return mapServer.getAuthenticationKey(address, maskLen);
142     }
143
144     public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
145         return mapServer.removeAuthenticationKey(address, maskLen);
146     }
147
148     public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
149         return mapServer.addAuthenticationKey(address, maskLen, key);
150     }
151
152     public boolean iterateMask() {
153         return mapResolver.iterateMask();
154     }
155
156     public void setIterateMask(boolean iterateMask) {
157         this.mapResolver.setIterateMask(iterateMask);
158     }
159 }