added basic smr configuration
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.java
1 /*
2  * Copyright (c) 2014 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.apache.commons.lang3.tuple.MutablePair;
12 import org.apache.commons.lang3.tuple.Pair;
13 import org.eclipse.osgi.framework.console.CommandInterpreter;
14 import org.eclipse.osgi.framework.console.CommandProvider;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
18 import org.opendaylight.controller.sal.binding.api.NotificationListener;
19 import org.opendaylight.controller.sal.binding.api.NotificationService;
20 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
21 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceNoMaskKey;
22 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
23 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
24 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
25 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
26 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
27 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
28 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
29 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
30 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
31 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
32 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
33 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
34 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.AddMapping;
35 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispflowmappingService;
36 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
37 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
38 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
39 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
40 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.RequestMapping;
41 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapNotifyInputBuilder;
42 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapReplyInputBuilder;
43 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapRequestInputBuilder;
44 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SetXtrPortInputBuilder;
45 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.ShouldListenOnXtrPortInputBuilder;
46 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
47 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
48 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.Address;
49 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
50 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
51 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapreplymessage.MapReplyBuilder;
52 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestmessage.MapRequestBuilder;
53 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.transportaddress.TransportAddress;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
57 import org.opendaylight.yangtools.yang.binding.Notification;
58 import org.osgi.framework.BundleContext;
59 import org.osgi.framework.FrameworkUtil;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 public class LispMappingService implements CommandProvider, IFlowMapping, BindingAwareConsumer, //
64         IMapRequestResultHandler, IMapNotifyHandler {
65     protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
66
67     private ILispDAO lispDao = null;
68     private IMapResolverAsync mapResolver;
69     private IMapServerAsync mapServer;
70     private volatile boolean shouldIterateMask;
71     private volatile boolean shouldAuthenticate;
72     private volatile boolean smr;
73     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
74     private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
75     private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
76
77     private LispflowmappingService lispSB = null;
78
79     private ConsumerContext session;
80
81     private NotificationService notificationService;
82
83     class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
84     }
85
86     class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
87     }
88
89     class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
90     }
91
92     class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
93     }
94
95     void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
96         logger.trace("BindingAwareBroker set!");
97         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
98         bindingAwareBroker.registerConsumer(this, bundleContext);
99     }
100
101     void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
102         logger.debug("BindingAwareBroker was unset in LispMappingService");
103     }
104
105     public void basicInit(ILispDAO dao) {
106         lispDao = dao;
107         mapResolver = new MapResolver(dao);
108         mapServer = new MapServer(dao);
109     }
110
111     void setLispDao(ILispDAO dao) {
112         logger.trace("LispDAO set in LispMappingService");
113         basicInit(dao);
114     }
115
116     void unsetLispDao(ILispDAO dao) {
117         logger.trace("LispDAO was unset in LispMappingService");
118         mapServer = null;
119         mapResolver = null;
120         lispDao = null;
121     }
122
123     public void init() {
124         try {
125             registerWithOSGIConsole();
126             logger.info("LISP (RFC6830) Mapping Service init finished");
127         } catch (Throwable t) {
128             logger.error(t.getStackTrace().toString());
129         }
130     }
131
132     private void registerWithOSGIConsole() {
133         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
134         bundleContext.registerService(CommandProvider.class.getName(), this, null);
135     }
136
137     public void destroy() {
138         logger.info("LISP (RFC6830) Mapping Service is destroyed!");
139         mapResolver = null;
140         mapServer = null;
141     }
142
143     public void _removeEid(final CommandInterpreter ci) {
144         lispDao.remove(LispAFIConvertor.asIPAfiAddress(ci.nextArgument()));
145     }
146
147     public void _dumpAll(final CommandInterpreter ci) {
148         ci.println("EID\tRLOCs");
149         lispDao.getAll(new IRowVisitor() {
150             String lastKey = "";
151
152             public void visitRow(Object keyId, String valueKey, Object value) {
153                 String key = keyId.getClass().getSimpleName() + "#" + keyId;
154                 if (!lastKey.equals(key)) {
155                     ci.println();
156                     ci.print(key + "\t");
157                 }
158                 ci.print(valueKey + "=" + value + "\t");
159                 lastKey = key;
160             }
161         });
162         ci.println();
163         return;
164     }
165
166     public void _setShouldOverwriteRlocs(final CommandInterpreter ci) {
167         try {
168             boolean shouldOverwriteRloc = Boolean.parseBoolean(ci.nextArgument());
169             setOverwrite(shouldOverwriteRloc);
170         } catch (Exception e) {
171             ci.println("Bad Usage!!");
172         }
173
174     }
175
176     public void _addDefaultPassword(final CommandInterpreter ci) {
177         LispAddressContainerBuilder builder = new LispAddressContainerBuilder();
178         builder.setAddress((Address) (new Ipv4Builder().setIpv4Address(new Ipv4Address("0.0.0.0")).build()));
179         addAuthenticationKey(builder.build(), 0, "password");
180     }
181
182     public String getHelp() {
183         StringBuffer help = new StringBuffer();
184         help.append("---LISP Mapping Service---\n");
185         help.append("\t dumpAll        - Dump all current EID -> RLOC mapping\n");
186         help.append("\t removeEid      - Remove a single LispIPv4Address Eid\n");
187         help.append("\t setShouldOverwritingRloc(true/false)      - set the map server's behaivior regarding existing RLOCs\n");
188         return help.toString();
189     }
190
191     public MapReply handleMapRequest(MapRequest request) {
192         return handleMapRequest(request, smr);
193     }
194
195     public MapReply handleMapRequest(MapRequest request, boolean smr) {
196         tlsMapReply.set(null);
197         tlsMapRequest.set(null);
198         mapResolver.handleMapRequest(request, smr, this);
199         // After this invocation we assume that the thread local is filled with
200         // the reply
201         if (tlsMapRequest.get() != null) {
202             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
203             new MapRequestBuilder(tlsMapRequest.get().getLeft());
204             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
205             smrib.setTransportAddress(tlsMapRequest.get().getRight());
206             getLispSB().sendMapRequest(smrib.build());
207             return null;
208         } else {
209             return tlsMapReply.get();
210         }
211
212     }
213
214     public MapNotify handleMapRegister(MapRegister mapRegister) {
215         return handleMapRegister(mapRegister, smr);
216     }
217
218     public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
219         tlsMapNotify.set(null);
220         mapServer.handleMapRegister(mapRegister, smr, this);
221         // After this invocation we assume that the thread local is filled with
222         // the reply
223         return tlsMapNotify.get();
224     }
225
226     public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
227         return mapServer.getAuthenticationKey(address, maskLen);
228     }
229
230     public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
231         mapServer.removeAuthenticationKey(address, maskLen);
232     }
233
234     public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
235         mapServer.addAuthenticationKey(address, maskLen, key);
236     }
237
238     public boolean shouldIterateMask() {
239         return this.shouldIterateMask;
240     }
241
242     public boolean shouldUseSmr() {
243         return this.smr;
244     }
245
246     public void setShouldUseSmr(boolean smr) {
247         this.smr = smr;
248     }
249
250     public void setShouldIterateMask(boolean shouldIterateMask) {
251         this.shouldIterateMask = shouldIterateMask;
252         this.mapResolver.setShouldIterateMask(shouldIterateMask);
253         this.mapServer.setShouldIterateMask(shouldIterateMask);
254     }
255
256     public void setShouldAuthenticate(boolean shouldAuthenticate) {
257         this.shouldAuthenticate = shouldAuthenticate;
258         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
259         this.mapServer.setShouldAuthenticate(shouldAuthenticate);
260     }
261
262     public boolean shouldAuthenticate() {
263         return shouldAuthenticate;
264     }
265
266     public void onSessionInitialized(ConsumerContext session) {
267         logger.info("Lisp Consumer session initialized!");
268         notificationService = session.getSALService(NotificationService.class);
269         registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
270         registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
271         this.session = session;
272     }
273
274     public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
275         notificationService.registerNotificationListener(notificationType, listener);
276     }
277
278     private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
279
280         @Override
281         public void onNotification(AddMapping mapRegisterNotification) {
282             MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister(), smr);
283             if (mapNotify != null) {
284                 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
285                 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
286                 smnib.setTransportAddress(mapRegisterNotification.getTransportAddress());
287                 getLispSB().sendMapNotify(smnib.build());
288             } else {
289                 logger.warn("got null map notify");
290             }
291
292         }
293     }
294
295     private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
296
297         @Override
298         public void onNotification(RequestMapping mapRequestNotification) {
299             MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
300             if (mapReply != null) {
301                 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
302                 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
303                 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
304                 getLispSB().sendMapReply(smrib.build());
305             } else {
306                 logger.warn("got null map reply");
307             }
308         }
309     }
310
311     private LispflowmappingService getLispSB() {
312         if (lispSB == null) {
313             lispSB = session.getRpcService(LispflowmappingService.class);
314         }
315         return lispSB;
316     }
317
318     public void handleMapReply(MapReply reply) {
319         tlsMapReply.set(reply);
320     }
321
322     public void handleMapNotify(MapNotify notify) {
323         tlsMapNotify.set(notify);
324     }
325
326     public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
327         logger.debug("Sending SMR to " + subscriber.toString());
328         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
329         smrib.setMapRequest(new MapRequestBuilder(smr).build());
330         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
331         getLispSB().sendMapRequest(smrib.build());
332
333     }
334
335     @Override
336     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
337         tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
338     }
339
340     @Override
341     public void clean() {
342         lispDao.removeAll();
343     }
344
345     @Override
346     public boolean shouldOverwrite() {
347         return mapServer.shouldOverwrite();
348     }
349
350     @Override
351     public void setOverwrite(boolean overwrite) {
352         mapServer.setOverwrite(overwrite);
353     }
354
355     @Override
356     public void shouldListenOnXtrPort(boolean listenOnXtrPort) {
357         getLispSB().shouldListenOnXtrPort(new ShouldListenOnXtrPortInputBuilder().setShouldListenOnXtrPort(listenOnXtrPort).build());
358     }
359
360     @Override
361     public void setXtrPort(int port) {
362         getLispSB().setXtrPort(new SetXtrPortInputBuilder().setXtrPort(new PortNumber(port)).build());
363     }
364
365 }