2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
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
9 package org.opendaylight.lispflowmapping.implementation.lisp;
11 import java.net.InetAddress;
12 import java.net.NetworkInterface;
13 import java.net.SocketException;
14 import java.util.ArrayList;
15 import java.util.Enumeration;
18 import org.apache.commons.lang3.BooleanUtils;
19 import org.apache.commons.lang3.exception.ExceptionUtils;
20 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
21 import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
22 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
23 import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
24 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
25 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
26 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
27 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
28 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
29 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
30 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
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.eid.container.Eid;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.SiteId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 import com.google.common.base.Preconditions;
52 public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
54 protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
55 private IMappingService mapService;
56 private boolean authenticate;
57 private boolean subscriptionService;
58 private IMapNotifyHandler notifyHandler;
59 private NotificationService notificationService;
61 public MapServer(IMappingService mapService, boolean authenticate, boolean subscriptionService,
62 IMapNotifyHandler notifyHandler, NotificationService notificationService) {
63 Preconditions.checkNotNull(mapService);
64 this.mapService = mapService;
65 this.authenticate = authenticate;
66 this.subscriptionService = subscriptionService;
67 this.notifyHandler = notifyHandler;
68 this.notificationService = notificationService;
69 if (notificationService != null) {
70 notificationService.registerNotificationListener(this);
75 public void setSubscriptionService(boolean subscriptionService) {
76 this.subscriptionService = subscriptionService;
80 public boolean shouldAuthenticate() {
85 public void setShouldAuthenticate(boolean shouldAuthenticate) {
86 authenticate = shouldAuthenticate;
89 public void handleMapRegister(MapRegister mapRegister) {
90 boolean failed = false;
91 String password = null;
92 for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
93 MappingRecord mapping = record.getMappingRecord();
95 MappingAuthkey authkey = mapService.getAuthenticationKey(mapping.getEid());
96 if (authkey != null) {
97 password = authkey.getKeyString();
99 if (!LispAuthenticationUtil.validate(mapRegister, password)) {
100 LOG.warn("Authentication failed");
105 MappingRecord oldMapping = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound,
107 mapService.addMapping(MappingOrigin.Southbound, mapping.getEid(), getSiteId(mapRegister), mapping);
108 if (subscriptionService && !mapping.equals(oldMapping)) {
109 LOG.debug("Sending SMRs for subscribers of {}", mapping.getEid());
110 Set<SubscriberRLOC> subscribers = getSubscribers(mapping.getEid());
111 sendSmrs(mapping, subscribers);
115 MapNotifyBuilder builder = new MapNotifyBuilder();
116 if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
117 LOG.trace("MapRegister wants MapNotify");
118 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
120 builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(),
123 notifyHandler.handleMapNotify(builder.build());
128 private SiteId getSiteId(MapRegister mapRegister) {
129 return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
133 public void onMappingChanged(MappingChanged notification) {
134 if (subscriptionService) {
135 sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
136 if (notification.getChangeType().equals(MappingChange.Removed)) {
137 removeSubscribers(notification.getMappingRecord().getEid());
142 private void sendSmrs(MappingRecord record, Set<SubscriberRLOC> subscribers) {
143 Eid eid = record.getEid();
144 handleSmr(record, subscribers, notifyHandler);
146 // For SrcDst LCAF also send SMRs to Dst prefix
147 if (eid.getAddress() instanceof SourceDestKey) {
148 Eid dstAddr = SourceDestKeyHelper.getDst(eid);
149 subscribers = getSubscribers(dstAddr);
150 MappingRecord newRecord = new MappingRecordBuilder().setAction(record.getAction())
151 .setAuthoritative(record.isAuthoritative()).setLocatorRecord(record.getLocatorRecord())
152 .setMapVersion(record.getMapVersion()).setRecordTtl(record.getRecordTtl())
153 .setEid(dstAddr).build();
154 handleSmr(newRecord, subscribers, notifyHandler);
158 private void handleSmr(MappingRecord record, Set<SubscriberRLOC> subscribers, IMapNotifyHandler callback) {
159 if (subscribers == null) {
162 MapRequestBuilder mrb = MapRequestUtil.prepareSMR(record.getEid(), LispAddressUtil.toRloc(getLocalAddress()));
163 LOG.trace("Built SMR packet: " + mrb.build().toString());
164 for (SubscriberRLOC subscriber : subscribers) {
165 if (subscriber.timedOut()) {
166 LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString());
167 subscribers.remove(subscriber);
170 // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked Map-Request.
171 // To ensure consistent behavior it is set to the value used to originally request a given mapping
172 mrb.setEidItem(new ArrayList<EidItem>());
173 mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build());
174 callback.handleSMR(mrb.build(), subscriber.getSrcRloc());
175 } catch (Exception e) {
176 LOG.error("Errors encountered while handling SMR:" + ExceptionUtils.getStackTrace(e));
180 addSubscribers(record.getEid(), subscribers);
183 @SuppressWarnings("unchecked")
184 private Set<SubscriberRLOC> getSubscribers(Eid address) {
185 return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
188 private void removeSubscribers(Eid address) {
189 mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
192 private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
193 mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
196 private static InetAddress getLocalAddress() {
198 Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
199 while (interfaces.hasMoreElements()) {
200 NetworkInterface current = interfaces.nextElement();
201 LOG.debug("Interface " + current.toString());
202 if (!current.isUp() || current.isLoopback() || current.isVirtual())
204 Enumeration<InetAddress> addresses = current.getInetAddresses();
205 while (addresses.hasMoreElements()) {
206 InetAddress current_addr = addresses.nextElement();
207 // Skip loopback and link local addresses
208 if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress())
210 LOG.debug(current_addr.getHostAddress());
214 } catch (SocketException se) {