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 java.util.List;
13 import org.apache.commons.lang3.tuple.MutablePair;
14 import org.apache.commons.lang3.tuple.Pair;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
20 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
21 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
22 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
23 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
27 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
28 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
29 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
30 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
56 public class LispMappingService implements IFlowMapping, BindingAwareProvider, IMapRequestResultHandler,
57 IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
58 protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
60 private volatile boolean shouldAuthenticate = true;
62 private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
63 private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
65 private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
66 private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify =
67 new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
68 private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest =
69 new ThreadLocal<Pair<MapRequest, TransportAddress>>();
71 private OdlLispSbService lispSB = null;
72 private IMapResolverAsync mapResolver;
73 private IMapServerAsync mapServer;
75 private IMappingService mapService;
76 private NotificationService notificationService;
77 private BindingAwareBroker broker;
78 private ProviderContext session;
80 public LispMappingService() {
81 LOG.debug("LispMappingService Module constructed!");
84 public void setBindingAwareBroker(BindingAwareBroker broker) {
88 public void setNotificationService(NotificationService ns) {
89 this.notificationService = ns;
92 public void setMappingService(IMappingService ms) {
93 LOG.trace("MappingService set in LispMappingService");
97 public boolean shouldUseSmr() {
101 public void setShouldUseSmr(boolean smr) {
103 if (mapServer != null) {
104 mapServer.setSubscriptionService(smr);
106 if (mapResolver != null) {
107 mapResolver.setSubscriptionService(smr);
109 ConfigIni.getInstance().setSmr(smr);
112 public NotificationService getNotificationService() {
113 return this.notificationService;
116 public void initialize() {
117 broker.registerProvider(this);
118 notificationService.registerNotificationListener(this);
119 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
120 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
123 public void basicInit() {
124 mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
125 mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
129 public void onSessionInitiated(ProviderContext session) {
130 LOG.info("Lisp Consumer session initialized!");
131 this.session = session;
132 LOG.info("LISP (RFC6830) Mapping Service init finished");
135 public MapReply handleMapRequest(MapRequest request) {
136 LOG.debug("DAO: Retrieving mapping for {}",
137 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
139 tlsMapReply.set(null);
140 tlsMapRequest.set(null);
141 mapResolver.handleMapRequest(request);
142 // After this invocation we assume that the thread local is filled with the reply
143 if (tlsMapRequest.get() != null) {
144 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
145 new MapRequestBuilder(tlsMapRequest.get().getLeft());
146 smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
147 smrib.setTransportAddress(tlsMapRequest.get().getRight());
148 getLispSB().sendMapRequest(smrib.build());
151 return tlsMapReply.get();
155 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
156 LOG.debug("DAO: Adding mapping for {}",
157 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
158 .getMappingRecord().getEid()));
160 tlsMapNotify.set(null);
161 mapServer.handleMapRegister(mapRegister);
162 // After this invocation we assume that the thread local is filled with the reply
163 return tlsMapNotify.get();
166 public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister, boolean smr) {
167 LOG.debug("DAO: Adding mapping for {}",
168 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
169 .getMappingRecord().getEid()));
171 tlsMapNotify.set(null);
172 mapServer.handleMapRegister(mapRegister);
173 // After this invocation we assume that the thread local is filled with the reply
174 return tlsMapNotify.get();
177 public void setShouldAuthenticate(boolean shouldAuthenticate) {
178 this.shouldAuthenticate = shouldAuthenticate;
179 this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
180 this.mapServer.setShouldAuthenticate(shouldAuthenticate);
183 public boolean shouldAuthenticate() {
184 return shouldAuthenticate;
187 private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
188 SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
189 smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
190 smnib.setTransportAddress(address);
191 getLispSB().sendMapNotify(smnib.build());
195 public void onAddMapping(AddMapping mapRegisterNotification) {
196 Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
197 if (result != null && result.getLeft() != null) {
198 MapNotify mapNotify = result.getLeft();
199 List <TransportAddress> rlocs = result.getRight();
201 TransportAddressBuilder tab = new TransportAddressBuilder();
202 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
203 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
204 sendMapNotify(mapNotify, tab.build());
206 for (TransportAddress ta : rlocs) {
207 sendMapNotify(mapNotify, ta);
211 LOG.debug("Not sending Map-Notify");
216 public void onRequestMapping(RequestMapping mapRequestNotification) {
217 MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
218 if (mapReply != null) {
219 SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
220 smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
221 smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
222 getLispSB().sendMapReply(smrib.build());
224 LOG.warn("got null map reply");
229 public void onGotMapReply(GotMapReply notification) {
230 LOG.debug("Received GotMapReply notification, ignoring");
234 public void onGotMapNotify(GotMapNotify notification) {
235 LOG.debug("Received GotMapNotify notification, ignoring");
239 public void onXtrRequestMapping(XtrRequestMapping notification) {
240 LOG.debug("Received XtrRequestMapping notification, ignoring");
244 public void onXtrReplyMapping(XtrReplyMapping notification) {
245 LOG.debug("Received XtrReplyMapping notification, ignoring");
248 private OdlLispSbService getLispSB() {
249 if (lispSB == null) {
250 lispSB = session.getRpcService(OdlLispSbService.class);
256 public void handleMapReply(MapReply reply) {
257 tlsMapReply.set(reply);
261 public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
262 tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
266 public void handleSMR(MapRequest smr, Rloc subscriber) {
267 LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
268 LispAddressStringifier.getString(subscriber),
269 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
270 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
271 SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
272 smrib.setMapRequest(new MapRequestBuilder(smr).build());
273 smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
274 getLispSB().sendMapRequest(smrib.build());
279 public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
280 tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
283 public void destroy() {
284 LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
290 public void close() throws Exception {