Clustering - adding to 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 java.util.List;
12 import org.apache.commons.lang3.tuple.MutablePair;
13 import org.apache.commons.lang3.tuple.Pair;
14 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
16 import org.opendaylight.lispflowmapping.implementation.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.lisp.type.LispMessage;
21 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
22 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
23 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
26 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
27 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
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.eid.container.Eid;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.MapRegisterCacheMetadata;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestmessage.MapRequestBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
55 import org.opendaylight.lispflowmapping.clustering.ClusterNodeModulSwitcherImpl;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
60         IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
61     protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
62     private final ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher;
63
64     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
65     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
66
67     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
68     private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify =
69             new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
70     private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest =
71             new ThreadLocal<Pair<MapRequest, TransportAddress>>();
72
73     private final OdlLispSbService lispSB;
74     private IMapResolverAsync mapResolver;
75     private IMapServerAsync mapServer;
76
77     private final IMappingService mapService;
78     private final NotificationService notificationService;
79
80     public LispMappingService(final NotificationService notificationService,
81             final IMappingService mappingService,
82             final OdlLispSbService odlLispService, final EntityOwnershipService entityOnwershipService) {
83
84         this.notificationService = notificationService;
85         this.mapService = mappingService;
86         this.lispSB = odlLispService;
87         clusterNodeModulSwitcher = new ClusterNodeModulSwitcherImpl(entityOnwershipService);
88         LOG.debug("LispMappingService Module constructed!");
89     }
90
91     public boolean shouldUseSmr() {
92         return this.smr;
93     }
94
95     public void setShouldUseSmr(boolean smr) {
96         this.smr = smr;
97         if (mapServer != null) {
98             mapServer.setSubscriptionService(smr);
99         }
100         if (mapResolver != null) {
101             mapResolver.setSubscriptionService(smr);
102         }
103         ConfigIni.getInstance().setSmr(smr);
104     }
105
106     public NotificationService getNotificationService() {
107         return this.notificationService;
108     }
109
110     public void initialize() {
111         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
112         mapServer = new MapServer(mapService, smr, this, notificationService, clusterNodeModulSwitcher);
113         LOG.info("LISP (RFC6830) Mapping Service init finished");
114     }
115
116     public void basicInit() {
117         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
118         mapServer = new MapServer(mapService, smr, this, notificationService, clusterNodeModulSwitcher);
119     }
120
121     public MapReply handleMapRequest(MapRequest request) {
122         if (LOG.isDebugEnabled()) {
123             LOG.debug("DAO: Retrieving mapping for {}",
124                     LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
125         }
126
127         tlsMapReply.set(null);
128         tlsMapRequest.set(null);
129         mapResolver.handleMapRequest(request);
130         // After this invocation we assume that the thread local is filled with the reply
131         if (tlsMapRequest.get() != null) {
132             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
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 Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
143         if (LOG.isDebugEnabled()) {
144             LOG.debug("DAO: Adding mapping for {}",
145                     LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
146                             .getMappingRecord().getEid()));
147         }
148
149         tlsMapNotify.set(null);
150         mapServer.handleMapRegister(mapRegister);
151         // After this invocation we assume that the thread local is filled with the reply
152         return tlsMapNotify.get();
153     }
154
155     public void setShouldAuthenticate(boolean shouldAuthenticate) {
156         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
157     }
158
159     private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
160         SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
161         smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
162         smnib.setTransportAddress(address);
163         getLispSB().sendMapNotify(smnib.build());
164     }
165
166     @Override
167     public void onAddMapping(AddMapping mapRegisterNotification) {
168         Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
169         if (result != null && result.getLeft() != null) {
170             MapNotify mapNotify = result.getLeft();
171             List <TransportAddress> rlocs = result.getRight();
172             if (rlocs == null) {
173                 TransportAddressBuilder tab = new TransportAddressBuilder();
174                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
175                 tab.setPort(new PortNumber(LispMessage.PORT_NUM));
176                 sendMapNotify(mapNotify, tab.build());
177             } else {
178                 for (TransportAddress ta : rlocs) {
179                     sendMapNotify(mapNotify, ta);
180                 }
181             }
182         } else {
183             LOG.debug("Not sending Map-Notify");
184         }
185     }
186
187     @Override
188     public void onRequestMapping(RequestMapping mapRequestNotification) {
189         MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
190         if (mapReply != null) {
191             SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
192             smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
193             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
194             getLispSB().sendMapReply(smrib.build());
195         } else {
196             LOG.debug("handleMapRequest: Got null MapReply");
197         }
198     }
199
200     @Override
201     public void onGotMapReply(GotMapReply notification) {
202         LOG.debug("Received GotMapReply notification, ignoring");
203     }
204
205     @Override
206     public void onGotMapNotify(GotMapNotify notification) {
207         LOG.debug("Received GotMapNotify notification, ignoring");
208     }
209
210     @Override
211     public void onXtrRequestMapping(XtrRequestMapping notification) {
212         LOG.debug("Received XtrRequestMapping notification, ignoring");
213     }
214
215     @Override
216     public void onXtrReplyMapping(XtrReplyMapping notification) {
217         LOG.debug("Received XtrReplyMapping notification, ignoring");
218     }
219
220     @Override
221     public void onMappingKeepAlive(MappingKeepAlive notification) {
222         final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
223         for (EidLispAddress eidLispAddress : cacheMetadata.getEidLispAddress()) {
224             final Eid eid = eidLispAddress.getEid();
225             final Long timestamp = cacheMetadata.getTimestamp();
226             LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
227                     timestamp);
228             mapService.updateMappingRegistration(MappingOrigin.Southbound, eid, timestamp);
229         }
230     }
231
232     private OdlLispSbService getLispSB() {
233         return lispSB;
234     }
235
236     @Override
237     public void handleMapReply(MapReply reply) {
238         tlsMapReply.set(reply);
239     }
240
241     @Override
242     public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
243         tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
244     }
245
246     @Override
247     public void handleSMR(MapRequest smr, Rloc subscriber) {
248         if (LOG.isDebugEnabled()) {
249             LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
250                     LispAddressStringifier.getString(subscriber),
251                     LispAddressStringifier.getString(smr.getSourceEid().getEid()),
252                     LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
253         }
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 }