Merge "Use odlparent-lite as artifacts parent"
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
index d575d523295aad5e0481a5755c81b2b669122363..06a16fa236fa39cee6fb8f1c15a7997c16ae7c64 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.Map;
+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.IMappingServiceKey;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKeyUtil;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOC;
-import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
+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.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.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 IMapServerAsync {
-    private ILispDAO dao;
-    private volatile boolean shouldAuthenticate;
-    private volatile boolean shouldIterateMask;
-    protected static final Logger logger = LoggerFactory.getLogger(MapServer.class);
+import com.google.common.base.Preconditions;
+
+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);
+        }
+    }
 
-    public MapServer(ILispDAO dao) {
-        this(dao, true);
+    @Override
+    public void setSubscriptionService(boolean subscriptionService) {
+        this.subscriptionService = subscriptionService;
     }
 
-    public MapServer(ILispDAO dao, boolean authenticate) {
-        this(dao, authenticate, true);
+    @Override
+    public boolean shouldAuthenticate() {
+        return authenticate;
     }
 
-    public MapServer(ILispDAO dao, boolean authenticate, boolean iterateAuthenticationMask) {
-        this.dao = dao;
-        this.shouldAuthenticate = authenticate;
-        this.shouldIterateMask = iterateAuthenticationMask;
+    @Override
+    public void setShouldAuthenticate(boolean shouldAuthenticate) {
+        authenticate = shouldAuthenticate;
     }
 
-    public void handleMapRegister(MapRegister mapRegister, IMapNotifyHandler callback) {
-        if (dao == null) {
-            logger.warn("handleMapRegister called while dao is uninitialized");
-        } else {
-            boolean failed = false;
-            String password = null;
-            for (EidToLocatorRecord eidRecord : mapRegister.getEidToLocatorRecords()) {
-                if (shouldAuthenticate) {
-                    password = getPassword(eidRecord.getPrefix(), eidRecord.getMaskLength());
-                    if (!LispAuthenticationUtil.validate(mapRegister, password)) {
-                        logger.debug("Authentication failed");
-                        failed = true;
-                        break;
-                    }
+    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();
                 }
-                MappingServiceValue value = new MappingServiceValue();
-                MappingEntry<MappingServiceValue> entry = new MappingEntry<MappingServiceValue>("value", value);
-                List<MappingServiceRLOC> rlocs = new ArrayList<MappingServiceRLOC>();
-                for (LocatorRecord locatorRecord : eidRecord.getLocators()) {
-                    rlocs.add(new MappingServiceRLOC(locatorRecord, eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord.isAuthoritative()));
+                if (!LispAuthenticationUtil.validate(mapRegister, password)) {
+                    LOG.warn("Authentication failed");
+                    failed = true;
+                    break;
                 }
-                value.setRlocs(rlocs);
-                IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eidRecord.getPrefix(), eidRecord.getMaskLength());
-                dao.put(key, entry);
-
             }
-            if (!failed) {
-                MapNotify mapNotify = null;
-                if (mapRegister.isWantMapNotify()) {
-                    logger.trace("MapRegister wants MapNotify");
-                    mapNotify = new MapNotify();
-                    mapNotify.setFromMapRegister(mapRegister);
-                    if (shouldAuthenticate) {
-                        mapNotify.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(mapNotify, password));
-                    }
-                    callback.handleMapNotify(mapNotify);
-                }
+            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);
             }
         }
-    }
-
-    private String getPassword(LispAddress prefix, int maskLength) {
-        if (prefix instanceof IMaskable) {
-            prefix = ((IMaskable) prefix).clone();
-        }
-        while (maskLength >= 0) {
-            IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(prefix, maskLength);
-            Map<String, ?> daoMap = dao.get(key);
-            if (daoMap != null) {
-                MappingServiceValue value = (MappingServiceValue) daoMap.get("value");
-                if (value != null && value.getKey() != null) {
-                    return value.getKey();
-                } else if (shouldIterateMask()) {
-                    maskLength -= 1;
-                } else {
-                    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));
                 }
-            } else {
-                maskLength -= 1;
-
+                notifyHandler.handleMapNotify(builder.build());
             }
         }
-        return null;
     }
 
-    public String getAuthenticationKey(LispAddress address, int maskLen) {
-        return getPassword(address, maskLen);
+    private SiteId getSiteId(MapRegister mapRegister) {
+        return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
     }
 
-    public boolean removeAuthenticationKey(LispAddress address, int maskLen) {
-        IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
-        Map<String, ?> daoMap = dao.get(key);
-        if (daoMap != null) {
-            MappingServiceValue value = (MappingServiceValue) daoMap.get("value");
-            if (value != null) {
-                value.setKey(null);
-                if (value.isEmpty()) {
-                    dao.remove(key);
-                } else {
-                    dao.put(key, new MappingEntry<MappingServiceValue>("value", value));
-                }
-                return true;
-            } else {
-                return false;
+    @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());
             }
-        } else {
-            return false;
         }
     }
 
-    public boolean addAuthenticationKey(LispAddress address, int maskLen, String key) {
-        IMappingServiceKey mappingServiceKey = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
-        Map<String, ?> daoMap = dao.get(mappingServiceKey);
-        MappingServiceValue value = null;
-        if (daoMap != null) {
-            value = (MappingServiceValue) daoMap.get("value");
-            if (value == null) {
-                value = new MappingServiceValue();
-            }
-        } else {
-            value = new MappingServiceValue();
+    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);
         }
-        value.setKey(key);
-        MappingEntry<MappingServiceValue> entry = new MappingEntry<MappingServiceValue>("value", value);
-        dao.put(mappingServiceKey, entry);
-        return true;
     }
 
-    public boolean shouldAuthenticate() {
-        return shouldAuthenticate;
+    private void handleSmr(MappingRecord record, Set<SubscriberRLOC> subscribers, IMapNotifyHandler callback) {
+        if (subscribers == null) {
+            return;
+        }
+        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 boolean shouldIterateMask() {
-        return shouldIterateMask;
+    @SuppressWarnings("unchecked")
+    private Set<SubscriberRLOC> getSubscribers(Eid address) {
+        return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
     }
 
-    public void setShouldIterateMask(boolean shouldIterateMask) {
-        this.shouldIterateMask = shouldIterateMask;
+    private void removeSubscribers(Eid address) {
+        mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
     }
 
-    public void setShouldAuthenticate(boolean shouldAuthenticate) {
-        this.shouldAuthenticate = shouldAuthenticate;
+    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;
+    }
 }