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