Migrate Uint users
[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 com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import org.apache.commons.lang3.tuple.MutablePair;
15 import org.apache.commons.lang3.tuple.Pair;
16 import org.opendaylight.lispflowmapping.config.ConfigIni;
17 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
18 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
19 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
20 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
21 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
26 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
27 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
28 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
29 import org.opendaylight.mdsal.binding.api.NotificationService;
30 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
31 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
32 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
64         IMapNotifyHandler, OdlLispProtoListener, AutoCloseable, ClusterSingletonService {
65     private static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
66     private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
67             LISPFLOWMAPPING_ENTITY_NAME);
68
69
70     private static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
71     private final ClusterSingletonServiceProvider clusterSingletonService;
72
73     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
74     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
75
76     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<>();
77     private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify = new ThreadLocal<>();
78     private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<>();
79
80     private final OdlLispSbService lispSB;
81     private IMapResolverAsync mapResolver;
82     private IMapServerAsync mapServer;
83
84     private final IMappingService mapService;
85     private final NotificationService notificationService;
86
87     public LispMappingService(final NotificationService notificationService,
88             final IMappingService mappingService,
89             final OdlLispSbService odlLispService, final ClusterSingletonServiceProvider clusterSingletonService) {
90
91         this.notificationService = notificationService;
92         this.mapService = mappingService;
93         this.lispSB = odlLispService;
94         this.clusterSingletonService = clusterSingletonService;
95         LOG.debug("LispMappingService Module constructed!");
96     }
97
98     public boolean shouldUseSmr() {
99         return this.smr;
100     }
101
102     @Override
103     public void setShouldUseSmr(boolean shouldUseSmr) {
104         this.smr = shouldUseSmr;
105         if (mapServer != null) {
106             mapServer.setSubscriptionService(shouldUseSmr);
107         }
108         if (mapResolver != null) {
109             mapResolver.setSubscriptionService(shouldUseSmr);
110         }
111         ConfigIni.getInstance().setSmr(shouldUseSmr);
112     }
113
114     public NotificationService getNotificationService() {
115         return this.notificationService;
116     }
117
118     public void initialize() {
119         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
120         mapServer = new MapServer(mapService, smr, this, notificationService);
121         this.clusterSingletonService.registerClusterSingletonService(this);
122         mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
123         LOG.info("LISP (RFC6830) Mapping Service init finished");
124     }
125
126     public void basicInit() {
127         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
128         mapServer = new MapServer(mapService, smr, this, notificationService);
129         mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
130     }
131
132     @Override
133     public MapReply handleMapRequest(MapRequest request) {
134         if (LOG.isDebugEnabled()) {
135             LOG.debug("LISP: Retrieving mapping for {}",
136                     LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
137         }
138
139         tlsMapReply.set(null);
140         tlsMapRequest.set(null);
141         mapResolver.handleMapRequest(request);
142         // After this invocation we assume that the thread local is filled with the reply
143         if (tlsMapRequest.get() != null) {
144             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
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     @Override
155     public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
156         if (LOG.isDebugEnabled()) {
157             LOG.debug("LISP: Adding mapping for {}",
158                     LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
159                             .getMappingRecord().getEid()));
160         }
161
162         tlsMapNotify.set(null);
163         mapServer.handleMapRegister(mapRegister);
164         // After this invocation we assume that the thread local is filled with the reply
165         return tlsMapNotify.get();
166     }
167
168     public void setShouldAuthenticate(boolean shouldAuthenticate) {
169         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
170     }
171
172     private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
173         SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
174         smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
175         smnib.setTransportAddress(address);
176         getLispSB().sendMapNotify(smnib.build());
177     }
178
179     @Override
180     public void onAddMapping(AddMapping mapRegisterNotification) {
181         Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
182         if (result != null && result.getLeft() != null) {
183             MapNotify mapNotify = result.getLeft();
184             List<TransportAddress> rlocs = result.getRight();
185             if (rlocs == null) {
186                 TransportAddressBuilder tab = new TransportAddressBuilder();
187                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
188                 tab.setPort(new PortNumber(LispMessage.PORT_NUMBER));
189                 sendMapNotify(mapNotify, tab.build());
190             } else {
191                 for (TransportAddress ta : rlocs) {
192                     sendMapNotify(mapNotify, ta);
193                 }
194             }
195         } else {
196             LOG.debug("Not sending Map-Notify");
197         }
198     }
199
200     @Override
201     public void onRequestMapping(RequestMapping mapRequestNotification) {
202         MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
203         if (mapReply != null) {
204             SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
205             smrib.setMapReply(new MapReplyBuilder(mapReply).build());
206             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
207             getLispSB().sendMapReply(smrib.build());
208         } else {
209             LOG.debug("handleMapRequest: Got null MapReply");
210         }
211     }
212
213     @Override
214     public void onGotMapReply(GotMapReply notification) {
215         LOG.debug("Received GotMapReply notification, ignoring");
216     }
217
218     @Override
219     public void onGotMapNotify(GotMapNotify notification) {
220         LOG.debug("Received GotMapNotify notification, ignoring");
221     }
222
223     @Override
224     public void onXtrRequestMapping(XtrRequestMapping notification) {
225         LOG.debug("Received XtrRequestMapping notification, ignoring");
226     }
227
228     @Override
229     public void onXtrReplyMapping(XtrReplyMapping notification) {
230         LOG.debug("Received XtrReplyMapping notification, ignoring");
231     }
232
233     @Override
234     public void onMappingKeepAlive(MappingKeepAlive notification) {
235         final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
236         for (EidLispAddress eidLispAddress : cacheMetadata.nonnullEidLispAddress().values()) {
237             final Eid eid = eidLispAddress.getEid();
238             final XtrId xtrId = cacheMetadata.getXtrId();
239             final Long timestamp = cacheMetadata.getTimestamp();
240             LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
241                     timestamp);
242             mapService.refreshMappingRegistration(eid, xtrId, timestamp);
243         }
244     }
245
246     private OdlLispSbService getLispSB() {
247         return lispSB;
248     }
249
250     @Override
251     public void handleMapReply(MapReply reply) {
252         tlsMapReply.set(reply);
253     }
254
255     @Override
256     public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
257         tlsMapNotify.set(new MutablePair<>(notify, rlocs));
258     }
259
260     @Override
261     public void handleSMR(MapRequest smrMapRequest, Rloc subscriber) {
262         if (LOG.isDebugEnabled()) {
263             LOG.debug("Sending SMR Map-Request to {} with Source-EID {} and EID Record {} (reversed)",
264                     LispAddressStringifier.getString(subscriber),
265                     LispAddressStringifier.getString(smrMapRequest.getSourceEid().getEid()),
266                     LispAddressStringifier.getString(smrMapRequest.getEidItem().get(0).getEid()));
267         }
268         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
269         smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
270         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
271         getLispSB().sendMapRequest(smrib.build());
272
273     }
274
275     @Override
276     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
277         tlsMapRequest.set(new MutablePair<>(mapRequest, transportAddress));
278     }
279
280     private void destroy() {
281         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
282         mapResolver = null;
283         mapServer = null;
284     }
285
286     @Override
287     public void close() throws Exception {
288         destroy();
289         clusterSingletonService.close();
290     }
291
292     @Override
293     public void instantiateServiceInstance() {
294         mapService.setIsMaster(true);
295     }
296
297     @Override
298     public ListenableFuture<Void> closeServiceInstance() {
299         if (mapService != null) {
300             mapService.setIsMaster(false);
301         }
302         return Futures.<Void>immediateFuture(null);
303     }
304
305     @Override
306     public ServiceGroupIdentifier getIdentifier() {
307         return SERVICE_GROUP_IDENTIFIER;
308     }
309 }