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