Authentication to southbound
[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 java.util.List;
12
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.MappingKeepAlive;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 public class LispMappingService implements IFlowMapping, BindingAwareProvider, IMapRequestResultHandler,
62         IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
63     protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
64
65     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
66     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
67
68     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
69     private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify =
70             new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
71     private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest =
72             new ThreadLocal<Pair<MapRequest, TransportAddress>>();
73
74     private OdlLispSbService lispSB = null;
75     private IMapResolverAsync mapResolver;
76     private IMapServerAsync mapServer;
77
78     private IMappingService mapService;
79     private NotificationService notificationService;
80     private BindingAwareBroker broker;
81     private ProviderContext session;
82
83     public LispMappingService() {
84         LOG.debug("LispMappingService Module constructed!");
85     }
86
87     public void setBindingAwareBroker(BindingAwareBroker broker) {
88         this.broker = broker;
89     }
90
91     public void setNotificationService(NotificationService ns) {
92         this.notificationService = ns;
93     }
94
95     public void setMappingService(IMappingService ms) {
96         LOG.trace("MappingService set in LispMappingService");
97         this.mapService = ms;
98     }
99
100     public boolean shouldUseSmr() {
101         return this.smr;
102     }
103
104     public void setShouldUseSmr(boolean smr) {
105         this.smr = smr;
106         if (mapServer != null) {
107             mapServer.setSubscriptionService(smr);
108         }
109         if (mapResolver != null) {
110             mapResolver.setSubscriptionService(smr);
111         }
112         ConfigIni.getInstance().setSmr(smr);
113     }
114
115     public NotificationService getNotificationService() {
116         return this.notificationService;
117     }
118
119     public void initialize() {
120         broker.registerProvider(this);
121         notificationService.registerNotificationListener(this);
122         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
123         mapServer = new MapServer(mapService, smr, this, notificationService);
124     }
125
126     public void basicInit() {
127         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
128         mapServer = new MapServer(mapService, smr, this, notificationService);
129     }
130
131     @Override
132     public void onSessionInitiated(ProviderContext session) {
133         LOG.info("Lisp Consumer session initialized!");
134         this.session = session;
135         LOG.info("LISP (RFC6830) Mapping Service init finished");
136     }
137
138     public MapReply handleMapRequest(MapRequest request) {
139         if (LOG.isDebugEnabled()) {
140             LOG.debug("DAO: Retrieving mapping for {}",
141                     LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
142         }
143
144         tlsMapReply.set(null);
145         tlsMapRequest.set(null);
146         mapResolver.handleMapRequest(request);
147         // After this invocation we assume that the thread local is filled with the reply
148         if (tlsMapRequest.get() != null) {
149             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
150             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
151             smrib.setTransportAddress(tlsMapRequest.get().getRight());
152             getLispSB().sendMapRequest(smrib.build());
153             return null;
154         } else {
155             return tlsMapReply.get();
156         }
157     }
158
159     public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
160         if (LOG.isDebugEnabled()) {
161             LOG.debug("DAO: Adding mapping for {}",
162                     LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
163                             .getMappingRecord().getEid()));
164         }
165
166         tlsMapNotify.set(null);
167         mapServer.handleMapRegister(mapRegister);
168         // After this invocation we assume that the thread local is filled with the reply
169         return tlsMapNotify.get();
170     }
171
172     public void setShouldAuthenticate(boolean shouldAuthenticate) {
173         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
174     }
175
176     private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
177         SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
178         smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
179         smnib.setTransportAddress(address);
180         getLispSB().sendMapNotify(smnib.build());
181     }
182
183     @Override
184     public void onAddMapping(AddMapping mapRegisterNotification) {
185         Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
186         if (result != null && result.getLeft() != null) {
187             MapNotify mapNotify = result.getLeft();
188             List <TransportAddress> rlocs = result.getRight();
189             if (rlocs == null) {
190                 TransportAddressBuilder tab = new TransportAddressBuilder();
191                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
192                 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
193                 sendMapNotify(mapNotify, tab.build());
194             } else {
195                 for (TransportAddress ta : rlocs) {
196                     sendMapNotify(mapNotify, ta);
197                 }
198             }
199         } else {
200             LOG.debug("Not sending Map-Notify");
201         }
202     }
203
204     @Override
205     public void onRequestMapping(RequestMapping mapRequestNotification) {
206         MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
207         if (mapReply != null) {
208             SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
209             smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
210             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
211             getLispSB().sendMapReply(smrib.build());
212         } else {
213             LOG.warn("got null map reply");
214         }
215     }
216
217     @Override
218     public void onGotMapReply(GotMapReply notification) {
219         LOG.debug("Received GotMapReply notification, ignoring");
220     }
221
222     @Override
223     public void onGotMapNotify(GotMapNotify notification) {
224         LOG.debug("Received GotMapNotify notification, ignoring");
225     }
226
227     @Override
228     public void onXtrRequestMapping(XtrRequestMapping notification) {
229         LOG.debug("Received XtrRequestMapping notification, ignoring");
230     }
231
232     @Override
233     public void onXtrReplyMapping(XtrReplyMapping notification) {
234         LOG.debug("Received XtrReplyMapping notification, ignoring");
235     }
236
237     @Override
238     public void onMappingKeepAlive(MappingKeepAlive notification) {
239         final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
240         for (EidLispAddress eidLispAddress : cacheMetadata.getEidLispAddress()) {
241             final Eid eid = eidLispAddress.getEid();
242             final Long timestamp = cacheMetadata.getTimestamp();
243             LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
244                     timestamp);
245             mapService.updateMappingRegistration(MappingOrigin.Southbound, eid, timestamp);
246         }
247     }
248
249     private OdlLispSbService getLispSB() {
250         if (lispSB == null) {
251             lispSB = session.getRpcService(OdlLispSbService.class);
252         }
253         return lispSB;
254     }
255
256     @Override
257     public void handleMapReply(MapReply reply) {
258         tlsMapReply.set(reply);
259     }
260
261     @Override
262     public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
263         tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
264     }
265
266     @Override
267     public void handleSMR(MapRequest smr, Rloc subscriber) {
268         if (LOG.isDebugEnabled()) {
269             LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
270                     LispAddressStringifier.getString(subscriber),
271                     LispAddressStringifier.getString(smr.getSourceEid().getEid()),
272                     LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
273         }
274         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
275         smrib.setMapRequest(new MapRequestBuilder(smr).build());
276         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
277         getLispSB().sendMapRequest(smrib.build());
278
279     }
280
281     @Override
282     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
283         tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
284     }
285
286     public void destroy() {
287         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
288         mapResolver = null;
289         mapServer = null;
290     }
291
292     @Override
293     public void close() throws Exception {
294         destroy();
295     }
296 }