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