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