2 * Copyright (c) 2014 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.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;
63 public class LispMappingService implements CommandProvider, IFlowMapping, BindingAwareConsumer, //
64 IMapRequestResultHandler, IMapNotifyHandler {
65 protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
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>>();
77 private LispflowmappingService lispSB = null;
79 private ConsumerContext session;
81 private NotificationService notificationService;
83 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
86 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
89 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
92 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
95 void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
96 logger.trace("BindingAwareBroker set!");
97 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
98 bindingAwareBroker.registerConsumer(this, bundleContext);
101 void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
102 logger.debug("BindingAwareBroker was unset in LispMappingService");
105 public void basicInit(ILispDAO dao) {
107 mapResolver = new MapResolver(dao);
108 mapServer = new MapServer(dao);
111 void setLispDao(ILispDAO dao) {
112 logger.trace("LispDAO set in LispMappingService");
116 void unsetLispDao(ILispDAO dao) {
117 logger.trace("LispDAO was unset in LispMappingService");
125 registerWithOSGIConsole();
126 logger.info("LISP (RFC6830) Mapping Service init finished");
127 } catch (Throwable t) {
128 logger.error(t.getStackTrace().toString());
132 private void registerWithOSGIConsole() {
133 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
134 bundleContext.registerService(CommandProvider.class.getName(), this, null);
137 public void destroy() {
138 logger.info("LISP (RFC6830) Mapping Service is destroyed!");
143 public void _removeEid(final CommandInterpreter ci) {
144 lispDao.remove(LispAFIConvertor.asIPAfiAddress(ci.nextArgument()));
147 public void _dumpAll(final CommandInterpreter ci) {
148 ci.println("EID\tRLOCs");
149 lispDao.getAll(new IRowVisitor() {
152 public void visitRow(Object keyId, String valueKey, Object value) {
153 String key = keyId.getClass().getSimpleName() + "#" + keyId;
154 if (!lastKey.equals(key)) {
156 ci.print(key + "\t");
158 ci.print(valueKey + "=" + value + "\t");
166 public void _setShouldOverwriteRlocs(final CommandInterpreter ci) {
168 boolean shouldOverwriteRloc = Boolean.parseBoolean(ci.nextArgument());
169 setOverwrite(shouldOverwriteRloc);
170 } catch (Exception e) {
171 ci.println("Bad Usage!!");
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");
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();
191 public MapReply handleMapRequest(MapRequest request) {
192 return handleMapRequest(request, smr);
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
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());
209 return tlsMapReply.get();
214 public MapNotify handleMapRegister(MapRegister mapRegister) {
215 return handleMapRegister(mapRegister, smr);
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
223 return tlsMapNotify.get();
226 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
227 return mapServer.getAuthenticationKey(address, maskLen);
230 public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
231 mapServer.removeAuthenticationKey(address, maskLen);
234 public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
235 mapServer.addAuthenticationKey(address, maskLen, key);
238 public boolean shouldIterateMask() {
239 return this.shouldIterateMask;
242 public boolean shouldUseSmr() {
246 public void setShouldUseSmr(boolean smr) {
250 public void setShouldIterateMask(boolean shouldIterateMask) {
251 this.shouldIterateMask = shouldIterateMask;
252 this.mapResolver.setShouldIterateMask(shouldIterateMask);
253 this.mapServer.setShouldIterateMask(shouldIterateMask);
256 public void setShouldAuthenticate(boolean shouldAuthenticate) {
257 this.shouldAuthenticate = shouldAuthenticate;
258 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
259 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
262 public boolean shouldAuthenticate() {
263 return shouldAuthenticate;
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;
274 public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
275 notificationService.registerNotificationListener(notificationType, listener);
278 private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
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());
289 logger.warn("got null map notify");
295 private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
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());
306 logger.warn("got null map reply");
311 private LispflowmappingService getLispSB() {
312 if (lispSB == null) {
313 lispSB = session.getRpcService(LispflowmappingService.class);
318 public void handleMapReply(MapReply reply) {
319 tlsMapReply.set(reply);
322 public void handleMapNotify(MapNotify notify) {
323 tlsMapNotify.set(notify);
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());
336 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
337 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
341 public void clean() {
346 public boolean shouldOverwrite() {
347 return mapServer.shouldOverwrite();
351 public void setOverwrite(boolean overwrite) {
352 mapServer.setOverwrite(overwrite);
356 public void shouldListenOnXtrPort(boolean listenOnXtrPort) {
357 getLispSB().shouldListenOnXtrPort(new ShouldListenOnXtrPortInputBuilder().setShouldListenOnXtrPort(listenOnXtrPort).build());
361 public void setXtrPort(int port) {
362 getLispSB().setXtrPort(new SetXtrPortInputBuilder().setXtrPort(new PortNumber(port)).build());