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