/*
- * Copyright (c) 2013 Contextream, Inc. and others. All rights reserved.
+ * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
package org.opendaylight.lispflowmapping.implementation.lisp;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
import java.util.ArrayList;
-import java.util.List;
+import java.util.Enumeration;
+import java.util.Set;
+import org.apache.commons.lang3.BooleanUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.opendaylight.controller.md.sal.binding.api.NotificationService;
import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
-import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
-import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO.MappingEntry;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKey;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceNoMaskKey;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
-import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServer;
-import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
-import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
-import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
-import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
-import org.opendaylight.lispflowmapping.type.lisp.address.IMaskable;
-import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
+import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
+import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
+import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
+import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
+import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
+import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
+import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.SiteId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class MapServer implements IMapServer {
- private ILispDAO dao;
- protected static final Logger logger = LoggerFactory.getLogger(MapServer.class);
+import com.google.common.base.Preconditions;
- public MapServer(ILispDAO dao) {
- this.dao = dao;
+public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
+
+ protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
+ private IMappingService mapService;
+ private boolean authenticate;
+ private boolean subscriptionService;
+ private IMapNotifyHandler notifyHandler;
+ private NotificationService notificationService;
+
+ public MapServer(IMappingService mapService, boolean authenticate, boolean subscriptionService,
+ IMapNotifyHandler notifyHandler, NotificationService notificationService) {
+ Preconditions.checkNotNull(mapService);
+ this.mapService = mapService;
+ this.authenticate = authenticate;
+ this.subscriptionService = subscriptionService;
+ this.notifyHandler = notifyHandler;
+ this.notificationService = notificationService;
+ if (notificationService != null) {
+ notificationService.registerNotificationListener(this);
+ }
+ }
+
+ @Override
+ public void setSubscriptionService(boolean subscriptionService) {
+ this.subscriptionService = subscriptionService;
+ }
+
+ @Override
+ public boolean shouldAuthenticate() {
+ return authenticate;
+ }
+
+ @Override
+ public void setShouldAuthenticate(boolean shouldAuthenticate) {
+ authenticate = shouldAuthenticate;
}
- public MapNotify handleMapRegister(MapRegister mapRegister) {
- if (dao == null) {
- logger.warn("handleMapRegister called while dao is uninitialized");
- return null;
+ public void handleMapRegister(MapRegister mapRegister) {
+ boolean failed = false;
+ String password = null;
+ for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
+ MappingRecord mapping = record.getMappingRecord();
+ if (authenticate) {
+ MappingAuthkey authkey = mapService.getAuthenticationKey(mapping.getEid());
+ if (authkey != null) {
+ password = authkey.getKeyString();
+ }
+ if (!LispAuthenticationUtil.validate(mapRegister, password)) {
+ LOG.warn("Authentication failed");
+ failed = true;
+ break;
+ }
+ }
+ MappingRecord oldMapping = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound,
+ mapping.getEid());
+ mapService.addMapping(MappingOrigin.Southbound, mapping.getEid(), getSiteId(mapRegister), mapping);
+ if (subscriptionService && !mapping.equals(oldMapping)) {
+ LOG.debug("Sending SMRs for subscribers of {}", mapping.getEid());
+ Set<SubscriberRLOC> subscribers = getSubscribers(mapping.getEid());
+ sendSmrs(mapping, subscribers);
+ }
}
- if (!LispAuthenticationUtil.validate(mapRegister)) {
- return null;
+ if (!failed) {
+ MapNotifyBuilder builder = new MapNotifyBuilder();
+ if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
+ LOG.trace("MapRegister wants MapNotify");
+ MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
+ if (authenticate) {
+ builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(),
+ password));
+ }
+ notifyHandler.handleMapNotify(builder.build());
+ }
}
- EidToLocatorRecord eidRecord = mapRegister.getEidToLocatorRecords().get(0);
- List<MappingEntry<?>> rlocs = new ArrayList<ILispDAO.MappingEntry<?>>();
- rlocs.add(new MappingEntry<Integer>("NumRLOCs", eidRecord.getLocators().size()));
- int i = 0;
- for (LocatorRecord locatorRecord : eidRecord.getLocators()) {
- rlocs.add(new MappingEntry<MappingServiceValue>("RLOC" + (i++), new MappingServiceValue(locatorRecord, eidRecord.getRecordTtl())));
+ }
+
+ private SiteId getSiteId(MapRegister mapRegister) {
+ return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
+ }
+
+ @Override
+ public void onMappingChanged(MappingChanged notification) {
+ if (subscriptionService) {
+ sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
+ if (notification.getChangeType().equals(MappingChange.Removed)) {
+ removeSubscribers(notification.getMappingRecord().getEid());
+ }
}
- if (eidRecord.getPrefix() instanceof IMaskable && eidRecord.getMaskLength() > 0 && eidRecord.getMaskLength() < ((IMaskable)eidRecord.getPrefix()).getMaxMask()) {
- ((IMaskable)eidRecord.getPrefix()).normalize(eidRecord.getMaskLength());
- dao.put(new MappingServiceKey(eidRecord.getPrefix(), (byte)eidRecord.getMaskLength()), rlocs.toArray(new MappingEntry[rlocs.size()]));
- } else {
- dao.put(new MappingServiceNoMaskKey(eidRecord.getPrefix()), rlocs.toArray(new MappingEntry[rlocs.size()]));
-
+ }
+
+ private void sendSmrs(MappingRecord record, Set<SubscriberRLOC> subscribers) {
+ Eid eid = record.getEid();
+ handleSmr(record, subscribers, notifyHandler);
+
+ // For SrcDst LCAF also send SMRs to Dst prefix
+ if (eid.getAddress() instanceof SourceDestKey) {
+ Eid dstAddr = SourceDestKeyHelper.getDst(eid);
+ subscribers = getSubscribers(dstAddr);
+ MappingRecord newRecord = new MappingRecordBuilder().setAction(record.getAction())
+ .setAuthoritative(record.isAuthoritative()).setLocatorRecord(record.getLocatorRecord())
+ .setMapVersion(record.getMapVersion()).setRecordTtl(record.getRecordTtl())
+ .setEid(dstAddr).build();
+ handleSmr(newRecord, subscribers, notifyHandler);
}
- MapNotify mapNotify = null;
- if (mapRegister.isWantMapNotify()) {
- mapNotify = new MapNotify();
- mapNotify.setFromMapRegister(mapRegister);
- mapNotify.setAuthenticationData(LispAuthenticationUtil.getAuthenticationData(mapNotify));
+ }
+
+ private void handleSmr(MappingRecord record, Set<SubscriberRLOC> subscribers, IMapNotifyHandler callback) {
+ if (subscribers == null) {
+ return;
}
- return mapNotify;
+ MapRequestBuilder mrb = MapRequestUtil.prepareSMR(record.getEid(), LispAddressUtil.toRloc(getLocalAddress()));
+ LOG.trace("Built SMR packet: " + mrb.build().toString());
+ for (SubscriberRLOC subscriber : subscribers) {
+ if (subscriber.timedOut()) {
+ LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString());
+ subscribers.remove(subscriber);
+ } else {
+ try {
+ // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked Map-Request.
+ // To ensure consistent behavior it is set to the value used to originally request a given mapping
+ mrb.setEidItem(new ArrayList<EidItem>());
+ mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build());
+ callback.handleSMR(mrb.build(), subscriber.getSrcRloc());
+ } catch (Exception e) {
+ LOG.error("Errors encountered while handling SMR:" + ExceptionUtils.getStackTrace(e));
+ }
+ }
+ }
+ addSubscribers(record.getEid(), subscribers);
}
- public String getAuthenticationKey(LispAddress address, int maskLen) {
- return null;
+ @SuppressWarnings("unchecked")
+ private Set<SubscriberRLOC> getSubscribers(Eid address) {
+ return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
}
- public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
- return false;
+ private void removeSubscribers(Eid address) {
+ mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
}
- public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
- return false;
+ private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
+ mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
}
+ private static InetAddress getLocalAddress() {
+ try {
+ Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
+ while (interfaces.hasMoreElements()) {
+ NetworkInterface current = interfaces.nextElement();
+ LOG.debug("Interface " + current.toString());
+ if (!current.isUp() || current.isLoopback() || current.isVirtual())
+ continue;
+ Enumeration<InetAddress> addresses = current.getInetAddresses();
+ while (addresses.hasMoreElements()) {
+ InetAddress current_addr = addresses.nextElement();
+ // Skip loopback and link local addresses
+ if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress())
+ continue;
+ LOG.debug(current_addr.getHostAddress());
+ return current_addr;
+ }
+ }
+ } catch (SocketException se) {
+ }
+ return null;
+ }
}