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.config.ConfigIni;
21 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
22 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
23 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceNoMaskKey;
24 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
25 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
26 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
27 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
28 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
29 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
30 import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
31 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
32 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
33 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMappingShell;
34 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
35 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
36 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
37 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
38 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.AddMapping;
39 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispflowmappingService;
40 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
41 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
42 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
43 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
44 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.RequestMapping;
45 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapNotifyInputBuilder;
46 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapReplyInputBuilder;
47 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.SendMapRequestInputBuilder;
48 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
49 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
50 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.Address;
51 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
52 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
53 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapreplymessage.MapReplyBuilder;
54 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestmessage.MapRequestBuilder;
55 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.transportaddress.TransportAddress;
56 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.transportaddress.TransportAddressBuilder;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
60 import org.opendaylight.yangtools.yang.binding.Notification;
61 import org.osgi.framework.BundleContext;
62 import org.osgi.framework.FrameworkUtil;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 public class LispMappingService implements CommandProvider, IFlowMapping, IFlowMappingShell, BindingAwareConsumer,
67 IMapRequestResultHandler, IMapNotifyHandler {
68 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
70 private static final ConfigIni configIni = new ConfigIni();
71 private ILispDAO lispDao = null;
72 private IMapResolverAsync mapResolver;
73 private IMapServerAsync mapServer;
74 private volatile boolean shouldIterateMask;
75 private volatile boolean shouldAuthenticate;
76 private volatile boolean smr = configIni.smrIsSet();
77 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
78 private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
79 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
81 private LispflowmappingService lispSB = null;
83 private ConsumerContext session;
85 private NotificationService notificationService;
87 class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
90 class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
93 class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
96 class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
99 void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
100 LOG.trace("BindingAwareBroker set!");
101 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
102 bindingAwareBroker.registerConsumer(this, bundleContext);
105 void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
106 LOG.debug("BindingAwareBroker was unset in LispMappingService");
109 public void basicInit(ILispDAO dao) {
111 mapResolver = new MapResolver(dao);
112 mapServer = new MapServer(dao);
115 void setLispDao(ILispDAO dao) {
116 LOG.trace("LispDAO set in LispMappingService");
120 void unsetLispDao(ILispDAO dao) {
121 LOG.trace("LispDAO was unset in LispMappingService");
129 registerWithOSGIConsole();
130 LOG.info("LISP (RFC6830) Mapping Service init finished");
131 } catch (Exception e) {
132 LOG.error(e.getStackTrace().toString());
136 private void registerWithOSGIConsole() {
137 BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
138 bundleContext.registerService(CommandProvider.class.getName(), this, null);
141 public void destroy() {
142 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
147 public void _removeEid(final CommandInterpreter ci) {
148 LispAddressContainer eid = LispAFIConvertor.getIPContainer(ci.nextArgument());
149 lispDao.remove(MappingServiceKeyUtil.generateMappingServiceKey(eid));
152 public void _dumpAll(final CommandInterpreter ci) {
153 ci.print(printMappings());
156 public String printMappings() {
157 final StringBuffer sb = new StringBuffer();
158 sb.append("EID\tRLOCs\n");
159 lispDao.getAll(new IRowVisitor() {
162 public void visitRow(Object keyId, String valueKey, Object value) {
163 String key = keyId.getClass().getSimpleName() + "#" + keyId;
164 if (!lastKey.equals(key)) {
165 sb.append("\n" + key + "\t");
167 sb.append(valueKey + "=" + value + "\t");
172 return sb.toString();
175 public void _setShouldOverwriteRlocs(final CommandInterpreter ci) {
177 boolean shouldOverwriteRloc = Boolean.parseBoolean(ci.nextArgument());
178 setOverwrite(shouldOverwriteRloc);
179 } catch (Exception e) {
180 ci.println("Bad Usage!!");
185 public void _addDefaultPassword(final CommandInterpreter ci) {
186 LispAddressContainerBuilder builder = new LispAddressContainerBuilder();
187 builder.setAddress((Address) (new Ipv4Builder().setIpv4Address(new Ipv4Address("0.0.0.0")).build()));
188 addAuthenticationKey(builder.build(), 0, "password");
191 public String getHelp() {
192 StringBuffer help = new StringBuffer();
193 help.append("---LISP Mapping Service---\n");
194 help.append("\t dumpAll - Dump all current EID -> RLOC mappings\n");
195 help.append("\t removeEid <EID> - Remove a single EID (/32 or /128)\n");
196 help.append("\t setShouldOverwriteRlocs <true|false> - Set the map server's behavior regarding existing RLOCs\n");
197 help.append("\t addDefaultPassword - Add \"password\" as default password for IPv4 EIDs");
198 return help.toString();
201 public MapReply handleMapRequest(MapRequest request) {
202 return handleMapRequest(request, smr);
205 public MapReply handleMapRequest(MapRequest request, boolean smr) {
206 tlsMapReply.set(null);
207 tlsMapRequest.set(null);
208 mapResolver.handleMapRequest(request, smr, this);
209 // After this invocation we assume that the thread local is filled with
211 if (tlsMapRequest.get() != null) {
212 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
213 new MapRequestBuilder(tlsMapRequest.get().getLeft());
214 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
215 smrib.setTransportAddress(tlsMapRequest.get().getRight());
216 getLispSB().sendMapRequest(smrib.build());
219 return tlsMapReply.get();
224 public MapNotify handleMapRegister(MapRegister mapRegister) {
225 return handleMapRegister(mapRegister, smr);
228 public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
229 tlsMapNotify.set(null);
230 mapServer.handleMapRegister(mapRegister, smr, this);
231 // After this invocation we assume that the thread local is filled with
233 return tlsMapNotify.get();
236 public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
237 return mapServer.getAuthenticationKey(address, maskLen);
240 public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
241 mapServer.removeAuthenticationKey(address, maskLen);
244 public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
245 mapServer.addAuthenticationKey(address, maskLen, key);
248 public void removeMapping(LispAddressContainer address, int maskLen) {
249 mapServer.removeMapping(address, maskLen, smr, this);
252 public boolean shouldIterateMask() {
253 return this.shouldIterateMask;
256 public boolean shouldUseSmr() {
260 public void setShouldUseSmr(boolean smr) {
264 public void setShouldIterateMask(boolean shouldIterateMask) {
265 this.shouldIterateMask = shouldIterateMask;
266 this.mapResolver.setShouldIterateMask(shouldIterateMask);
267 this.mapServer.setShouldIterateMask(shouldIterateMask);
270 public void setShouldAuthenticate(boolean shouldAuthenticate) {
271 this.shouldAuthenticate = shouldAuthenticate;
272 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
273 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
276 public boolean shouldAuthenticate() {
277 return shouldAuthenticate;
280 public void onSessionInitialized(ConsumerContext session) {
281 LOG.info("Lisp Consumer session initialized!");
282 notificationService = session.getSALService(NotificationService.class);
283 registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
284 registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
285 this.session = session;
288 public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
289 notificationService.registerNotificationListener(notificationType, listener);
292 private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
295 public void onNotification(AddMapping mapRegisterNotification) {
296 MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister(), smr);
297 if (mapNotify != null) {
298 TransportAddressBuilder tab = new TransportAddressBuilder();
299 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
300 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
301 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
302 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
303 smnib.setTransportAddress(tab.build());
304 getLispSB().sendMapNotify(smnib.build());
306 LOG.warn("got null map notify");
312 private class MapRequestNotificationHandler implements NotificationListener<RequestMapping> {
315 public void onNotification(RequestMapping mapRequestNotification) {
316 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
317 if (mapReply != null) {
318 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
319 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
320 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
321 getLispSB().sendMapReply(smrib.build());
323 LOG.warn("got null map reply");
328 private LispflowmappingService getLispSB() {
329 if (lispSB == null) {
330 lispSB = session.getRpcService(LispflowmappingService.class);
335 public void handleMapReply(MapReply reply) {
336 tlsMapReply.set(reply);
339 public void handleMapNotify(MapNotify notify) {
340 tlsMapNotify.set(notify);
343 public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
344 LOG.debug("Sending SMR to " + subscriber.toString());
345 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
346 smrib.setMapRequest(new MapRequestBuilder(smr).build());
347 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
348 getLispSB().sendMapRequest(smrib.build());
353 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
354 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
358 public void clean() {
363 public boolean shouldOverwrite() {
364 return mapServer.shouldOverwrite();
368 public void setOverwrite(boolean overwrite) {
369 mapServer.setOverwrite(overwrite);