Merge branch 'master' into topic/ietf_yang
[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 org.apache.commons.lang3.tuple.MutablePair;
12 import org.apache.commons.lang3.tuple.Pair;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
17 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
18 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
19 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
20 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
21 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
26 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
27 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
28 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.LispProtoListener;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.lispaddress.LispAddressContainer;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transportaddress.TransportAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transportaddress.TransportAddressBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.LispSbService;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class LispMappingService implements IFlowMapping, BindingAwareProvider, IMapRequestResultHandler,
55         IMapNotifyHandler, LispProtoListener, AutoCloseable {
56     protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
57
58     private volatile boolean shouldAuthenticate = true;
59
60     private static final ConfigIni configIni = new ConfigIni();
61     private volatile boolean smr = configIni.smrIsSet();
62     private volatile String elpPolicy = configIni.getElpPolicy();
63
64     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
65     private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
66     private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
67
68     private LispSbService lispSB = null;
69     private IMapResolverAsync mapResolver;
70     private IMapServerAsync mapServer;
71
72     private IMappingService mapService;
73     private NotificationService notificationService;
74     private BindingAwareBroker broker;
75     private ProviderContext session;
76
77     public LispMappingService() {
78         LOG.debug("LispMappingService Module constructed!");
79     }
80
81     public void setBindingAwareBroker(BindingAwareBroker broker) {
82         this.broker = broker;
83     }
84
85     public void setNotificationService(NotificationService ns) {
86         this.notificationService = ns;
87     }
88
89     public void setMappingService(IMappingService ms) {
90         LOG.trace("MappingService set in LispMappingService");
91         this.mapService = ms;
92     }
93
94     public boolean shouldUseSmr() {
95         return this.smr;
96     }
97
98     public void setShouldUseSmr(boolean smr) {
99         this.smr = smr;
100         if (mapServer != null) {
101             mapServer.setSubscriptionService(smr);
102         }
103         if (mapResolver != null) {
104             mapResolver.setSubscriptionService(smr);
105         }
106     }
107
108     public NotificationService getNotificationService() {
109         return this.notificationService;
110     }
111
112     public void initialize() {
113         broker.registerProvider(this);
114         notificationService.registerNotificationListener(this);
115         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
116         mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
117     }
118
119     public void basicInit() {
120         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
121         mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
122     }
123
124     @Override
125     public void onSessionInitiated(ProviderContext session) {
126         LOG.info("Lisp Consumer session initialized!");
127         this.session = session;
128         LOG.info("LISP (RFC6830) Mapping Service init finished");
129     }
130
131     public MapReply handleMapRequest(MapRequest request) {
132         LOG.debug("DAO: Retrieving mapping for {}",
133                 LispAddressStringifier.getString(request.getEidRecord().get(0).getLispAddressContainer()));
134
135         tlsMapReply.set(null);
136         tlsMapRequest.set(null);
137         mapResolver.handleMapRequest(request);
138         // After this invocation we assume that the thread local is filled with the reply
139         if (tlsMapRequest.get() != null) {
140             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
141             new MapRequestBuilder(tlsMapRequest.get().getLeft());
142             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
143             smrib.setTransportAddress(tlsMapRequest.get().getRight());
144             getLispSB().sendMapRequest(smrib.build());
145             return null;
146         } else {
147             return tlsMapReply.get();
148         }
149     }
150
151     public MapNotify handleMapRegister(MapRegister mapRegister) {
152         LOG.debug("DAO: Adding mapping for {}",
153                 LispAddressStringifier.getString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()));
154
155         tlsMapNotify.set(null);
156         mapServer.handleMapRegister(mapRegister);
157         // After this invocation we assume that the thread local is filled with the reply
158         return tlsMapNotify.get();
159     }
160
161     public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
162         LOG.debug("DAO: Adding mapping for {}",
163                 LispAddressStringifier.getString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()));
164
165         tlsMapNotify.set(null);
166         mapServer.handleMapRegister(mapRegister);
167         // After this invocation we assume that the thread local is filled with the reply
168         return tlsMapNotify.get();
169     }
170
171     public void setShouldAuthenticate(boolean shouldAuthenticate) {
172         this.shouldAuthenticate = shouldAuthenticate;
173         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
174         this.mapServer.setShouldAuthenticate(shouldAuthenticate);
175     }
176
177     public boolean shouldAuthenticate() {
178         return shouldAuthenticate;
179     }
180
181     @Override
182     public void onAddMapping(AddMapping mapRegisterNotification) {
183         MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
184         if (mapNotify != null) {
185             TransportAddressBuilder tab = new TransportAddressBuilder();
186             tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
187             tab.setPort(new PortNumber(LispMessage.PORT_NUM));
188             SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
189             smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
190             smnib.setTransportAddress(tab.build());
191             getLispSB().sendMapNotify(smnib.build());
192         } else {
193             LOG.warn("got null map notify");
194         }
195     }
196
197     @Override
198     public void onRequestMapping(RequestMapping mapRequestNotification) {
199         MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
200         if (mapReply != null) {
201             SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
202             smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
203             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
204             getLispSB().sendMapReply(smrib.build());
205         } else {
206             LOG.warn("got null map reply");
207         }
208     }
209
210     @Override
211     public void onGotMapReply(GotMapReply notification) {
212         LOG.debug("Received GotMapReply notification, ignoring");
213     }
214
215     @Override
216     public void onGotMapNotify(GotMapNotify notification) {
217         LOG.debug("Received GotMapNotify notification, ignoring");
218     }
219
220     @Override
221     public void onXtrRequestMapping(XtrRequestMapping notification) {
222         LOG.debug("Received XtrRequestMapping notification, ignoring");
223     }
224
225     @Override
226     public void onXtrReplyMapping(XtrReplyMapping notification) {
227         LOG.debug("Received XtrReplyMapping notification, ignoring");
228     }
229
230     private LispSbService getLispSB() {
231         if (lispSB == null) {
232             lispSB = session.getRpcService(LispSbService.class);
233         }
234         return lispSB;
235     }
236
237     @Override
238     public void handleMapReply(MapReply reply) {
239         tlsMapReply.set(reply);
240     }
241
242     @Override
243     public void handleMapNotify(MapNotify notify) {
244         tlsMapNotify.set(notify);
245     }
246
247     @Override
248     public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
249         LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
250                 LispAddressStringifier.getString(subscriber),
251                 LispAddressStringifier.getString(smr.getSourceEid().getLispAddressContainer()),
252                 LispAddressStringifier.getString(smr.getEidRecord().get(0).getLispAddressContainer()));
253         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
254         smrib.setMapRequest(new MapRequestBuilder(smr).build());
255         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
256         getLispSB().sendMapRequest(smrib.build());
257
258     }
259
260     @Override
261     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
262         tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
263     }
264
265     public void destroy() {
266         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
267         mapResolver = null;
268         mapServer = null;
269     }
270
271     @Override
272     public void close() throws Exception {
273         destroy();
274     }
275 }