Bug 4822: Simplify access to configuration variables
[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.MapNotify;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
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.mapnotifymessage.MapNotifyBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
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, OdlLispProtoListener, AutoCloseable {
56     protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
57
58     private volatile boolean shouldAuthenticate = true;
59
60     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
61     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
62
63     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
64     private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
65     private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
66
67     private OdlLispSbService lispSB = null;
68     private IMapResolverAsync mapResolver;
69     private IMapServerAsync mapServer;
70
71     private IMappingService mapService;
72     private NotificationService notificationService;
73     private BindingAwareBroker broker;
74     private ProviderContext session;
75
76     public LispMappingService() {
77         LOG.debug("LispMappingService Module constructed!");
78     }
79
80     public void setBindingAwareBroker(BindingAwareBroker broker) {
81         this.broker = broker;
82     }
83
84     public void setNotificationService(NotificationService ns) {
85         this.notificationService = ns;
86     }
87
88     public void setMappingService(IMappingService ms) {
89         LOG.trace("MappingService set in LispMappingService");
90         this.mapService = ms;
91     }
92
93     public boolean shouldUseSmr() {
94         return this.smr;
95     }
96
97     public void setShouldUseSmr(boolean smr) {
98         this.smr = smr;
99         if (mapServer != null) {
100             mapServer.setSubscriptionService(smr);
101         }
102         if (mapResolver != null) {
103             mapResolver.setSubscriptionService(smr);
104         }
105     }
106
107     public NotificationService getNotificationService() {
108         return this.notificationService;
109     }
110
111     public void initialize() {
112         broker.registerProvider(this);
113         notificationService.registerNotificationListener(this);
114         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
115         mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
116     }
117
118     public void basicInit() {
119         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
120         mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
121     }
122
123     @Override
124     public void onSessionInitiated(ProviderContext session) {
125         LOG.info("Lisp Consumer session initialized!");
126         this.session = session;
127         LOG.info("LISP (RFC6830) Mapping Service init finished");
128     }
129
130     public MapReply handleMapRequest(MapRequest request) {
131         LOG.debug("DAO: Retrieving mapping for {}",
132                 LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
133
134         tlsMapReply.set(null);
135         tlsMapRequest.set(null);
136         mapResolver.handleMapRequest(request);
137         // After this invocation we assume that the thread local is filled with the reply
138         if (tlsMapRequest.get() != null) {
139             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
140             new MapRequestBuilder(tlsMapRequest.get().getLeft());
141             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
142             smrib.setTransportAddress(tlsMapRequest.get().getRight());
143             getLispSB().sendMapRequest(smrib.build());
144             return null;
145         } else {
146             return tlsMapReply.get();
147         }
148     }
149
150     public MapNotify handleMapRegister(MapRegister mapRegister) {
151         LOG.debug("DAO: Adding mapping for {}",
152                 LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
153                         .getMappingRecord().getEid()));
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.getMappingRecordItem().get(0)
164                         .getMappingRecord().getEid()));
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.shouldAuthenticate = shouldAuthenticate;
174         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
175         this.mapServer.setShouldAuthenticate(shouldAuthenticate);
176     }
177
178     public boolean shouldAuthenticate() {
179         return shouldAuthenticate;
180     }
181
182     @Override
183     public void onAddMapping(AddMapping mapRegisterNotification) {
184         MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
185         if (mapNotify != null) {
186             TransportAddressBuilder tab = new TransportAddressBuilder();
187             tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
188             tab.setPort(new PortNumber(LispMessage.PORT_NUM));
189             SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
190             smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
191             smnib.setTransportAddress(tab.build());
192             getLispSB().sendMapNotify(smnib.build());
193         } else {
194             LOG.warn("got null map notify");
195         }
196     }
197
198     @Override
199     public void onRequestMapping(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     @Override
212     public void onGotMapReply(GotMapReply notification) {
213         LOG.debug("Received GotMapReply notification, ignoring");
214     }
215
216     @Override
217     public void onGotMapNotify(GotMapNotify notification) {
218         LOG.debug("Received GotMapNotify notification, ignoring");
219     }
220
221     @Override
222     public void onXtrRequestMapping(XtrRequestMapping notification) {
223         LOG.debug("Received XtrRequestMapping notification, ignoring");
224     }
225
226     @Override
227     public void onXtrReplyMapping(XtrReplyMapping notification) {
228         LOG.debug("Received XtrReplyMapping notification, ignoring");
229     }
230
231     private OdlLispSbService getLispSB() {
232         if (lispSB == null) {
233             lispSB = session.getRpcService(OdlLispSbService.class);
234         }
235         return lispSB;
236     }
237
238     @Override
239     public void handleMapReply(MapReply reply) {
240         tlsMapReply.set(reply);
241     }
242
243     @Override
244     public void handleMapNotify(MapNotify notify) {
245         tlsMapNotify.set(notify);
246     }
247
248     @Override
249     public void handleSMR(MapRequest smr, Rloc subscriber) {
250         LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
251                 LispAddressStringifier.getString(subscriber),
252                 LispAddressStringifier.getString(smr.getSourceEid().getEid()),
253                 LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
254         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
255         smrib.setMapRequest(new MapRequestBuilder(smr).build());
256         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
257         getLispSB().sendMapRequest(smrib.build());
258
259     }
260
261     @Override
262     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
263         tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
264     }
265
266     public void destroy() {
267         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
268         mapResolver = null;
269         mapServer = null;
270     }
271
272     @Override
273     public void close() throws Exception {
274         destroy();
275     }
276 }