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