e13506ca727fc0d34d1e076378102be24537baf6
[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.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;
44
45 public class LispMappingService implements CommandProvider, IFlowMapping, BindingAwareConsumer, NotificationListener<LispNotification> {
46     protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
47
48     private ILispDAO lispDao = null;
49     private volatile boolean shouldIterateMask;
50     private volatile boolean shouldAuthenticate;
51     private IMapResolver mapResolver;
52     private IMapServer mapServer;
53
54     private ILispSouthboundPlugin lispSB = null;
55
56     private ConsumerContext session;
57
58     public static void main(String[] args) throws Exception {
59         LispMappingService serv = new LispMappingService();
60         serv.setLispDao(new InMemoryDAO());
61         serv.init();
62     }
63
64     class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<LispIpv4Address, Integer> {
65     }
66
67     class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<LispIpv6Address, Integer> {
68     }
69
70     class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
71     }
72
73     class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
74     }
75
76     void setLispDao(ILispDAO dao) {
77         logger.info("LispDAO set in LispMappingService");
78         lispDao = dao;
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);
89     }
90
91     void unsetLispDao(ILispDAO dao) {
92         logger.info("LispDAO was unset in LispMappingService");
93         mapServer = null;
94         mapResolver = null;
95         lispDao = null;
96     }
97
98     public void init() {
99         logger.info("LISP (RFC6830) Mapping Service is initialized!");
100         registerWithOSGIConsole();
101     }
102
103     private void registerWithOSGIConsole() {
104         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
105         bundleContext.registerService(CommandProvider.class.getName(), this, null);
106     }
107
108     public void destroy() {
109         logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
110         mapResolver = null;
111         mapServer = null;
112     }
113
114     public void _removeEid(final CommandInterpreter ci) {
115         lispDao.remove(new LispIpv4Address(ci.nextArgument()));
116     }
117
118     public void _dumpAll(final CommandInterpreter ci) {
119         ci.println("EID\tRLOCs");
120         if (lispDao instanceof IQueryAll) {
121             ((IQueryAll) lispDao).getAll(new IRowVisitor() {
122                 String lastKey = "";
123
124                 public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
125                     String key = keyType.getSimpleName() + "#" + keyId;
126                     if (!lastKey.equals(key)) {
127                         ci.println();
128                         ci.print(key + "\t");
129                     }
130                     ci.print(valueKey + "=" + value + "\t");
131                     lastKey = key;
132                 }
133             });
134             ci.println();
135         } else {
136             ci.println("Not implemented by this DAO");
137         }
138         return;
139     }
140
141     public void _addDefaultPassword(final CommandInterpreter ci) {
142         addAuthenticationKey(new LispIpv4Address("0.0.0.0"), 0, "password");
143     }
144
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();
151     }
152
153     public MapReply handleMapRequest(MapRequest request) {
154         return mapResolver.handleMapRequest(request);
155     }
156
157     public MapNotify handleMapRegister(MapRegister mapRegister) {
158         return mapServer.handleMapRegister(mapRegister);
159     }
160
161     public String getAuthenticationKey(LispAddress address, int maskLen) {
162         return mapServer.getAuthenticationKey(address, maskLen);
163     }
164
165     public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
166         return mapServer.removeAuthenticationKey(address, maskLen);
167     }
168
169     public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
170         return mapServer.addAuthenticationKey(address, maskLen, key);
171     }
172
173     public boolean shouldIterateMask() {
174         return this.shouldIterateMask;
175     }
176
177     public void setShouldIterateMask(boolean shouldIterateMask) {
178         this.shouldIterateMask = shouldIterateMask;
179         this.mapResolver.setShouldIterateMask(shouldIterateMask);
180         this.mapServer.setShouldIterateMask(shouldIterateMask);
181     }
182
183     public void setShouldAuthenticate(boolean shouldAuthenticate) {
184         this.shouldAuthenticate = shouldAuthenticate;
185         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
186         this.mapServer.setShouldAuthenticate(shouldAuthenticate);
187     }
188
189     public boolean shouldAuthenticate() {
190         return shouldAuthenticate;
191     }
192
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;
198     }
199
200     public void onNotification(LispNotification notification) {
201         try {
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());
212             } else {
213                 logger.error("Unknown notification: " + notification);
214             }
215         } catch (Throwable t) {
216             t.printStackTrace();
217         }
218     }
219
220     private ILispSouthboundPlugin getLispSB() {
221         if (lispSB == null) {
222             lispSB = session.getRpcService(ILispSouthboundPlugin.class);
223         }
224         return lispSB;
225     }
226 }