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