Remove unused authentication variable.
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
index e473079dad7d8fdabca81e576c6a1516910450b8..36f0a292a6db2688bdc183f4eeb15e9b1b3ada77 100644 (file)
@@ -13,55 +13,64 @@ import java.net.NetworkInterface;
 import java.net.SocketException;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Objects;
 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.implementation.config.ConfigIni;
 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.LcafSourceDestHelper;
-import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
+import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
+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.MaskUtil;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapRegister;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.eidrecords.EidRecord;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.eidrecords.EidRecordBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.eidtolocatorrecords.EidToLocatorRecord;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.eidtolocatorrecords.EidToLocatorRecordBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.LispAddressContainer;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.lispaddresscontainer.address.LcafSourceDest;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.mapnotifymessage.MapNotifyBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.maprequestnotification.MapRequestBuilder;
+import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
+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.inet.binary.types.rev160303.IpAddressBinary;
+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.SiteId;
+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.mapping.record.list.MappingRecordItemBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
 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.MappingserviceListener;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.SiteId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Preconditions;
 
-public class MapServer implements IMapServerAsync, MappingserviceListener {
+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,
+    public MapServer(IMappingService mapService, boolean subscriptionService,
             IMapNotifyHandler notifyHandler, NotificationService notificationService) {
         Preconditions.checkNotNull(mapService);
         this.mapService = mapService;
-        this.authenticate = authenticate;
         this.subscriptionService = subscriptionService;
         this.notifyHandler = notifyHandler;
         this.notificationService = notificationService;
@@ -75,50 +84,73 @@ public class MapServer implements IMapServerAsync, MappingserviceListener {
         this.subscriptionService = subscriptionService;
     }
 
-    @Override
-    public boolean shouldAuthenticate() {
-        return authenticate;
-    }
+    @SuppressWarnings("unchecked")
+    public void handleMapRegister(MapRegister mapRegister) {
+        boolean mappingUpdated = false;
+        boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled();
+        Set<SubscriberRLOC> subscribers = null;
+        MappingRecord oldMapping;
 
-    @Override
-    public void setShouldAuthenticate(boolean shouldAuthenticate) {
-        authenticate = shouldAuthenticate;
-    }
+        for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
+            MappingRecord mapping = record.getMappingRecord();
 
-    public void handleMapRegister(MapRegister mapRegister) {
-        boolean failed = false;
-        String password = null;
-        for (EidToLocatorRecord record : mapRegister.getEidToLocatorRecord()) {
-            if (authenticate) {
-                password = mapService.getAuthenticationKey(record.getLispAddressContainer());
-                if (!LispAuthenticationUtil.validate(mapRegister, password)) {
-                    LOG.warn("Authentication failed");
-                    failed = true;
-                    break;
+            oldMapping = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, mapping.getEid());
+            mapService.addMapping(MappingOrigin.Southbound, mapping.getEid(), getSiteId(mapRegister), mapping, merge);
+
+            if (subscriptionService) {
+                MappingRecord newMapping = merge ?
+                        (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, mapping.getEid()) : mapping;
+
+                if (mappingChanged(oldMapping, newMapping)) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Mapping update occured for {} SMRs will be sent for its subscribers.",
+                                LispAddressStringifier.getString(mapping.getEid()));
+                    }
+                    subscribers = getSubscribers(mapping.getEid());
+                    sendSmrs(mapping, subscribers);
+                    mappingUpdated = true;
                 }
             }
-            EidToLocatorRecord oldMapping = (EidToLocatorRecord) mapService.getMapping(MappingOrigin.Southbound,
-                    record.getLispAddressContainer());
-            mapService.addMapping(MappingOrigin.Southbound, record.getLispAddressContainer(), getSiteId(mapRegister),
-                    record);
-            if (subscriptionService && !record.equals(oldMapping)) {
-                LOG.debug("Sending SMRs for subscribers of {}", record.getLispAddressContainer());
-                Set<SubscriberRLOC> subscribers = getSubscribers(record.getLispAddressContainer());
-                sendSmrs(record, subscribers);
-            }
         }
-        if (!failed) {
+        if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
+            LOG.trace("MapRegister wants MapNotify");
             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));
+            List<TransportAddress> rlocs = null;
+            if (merge) {
+                Set<IpAddressBinary> notifyRlocs = new HashSet<IpAddressBinary>();
+                List<MappingRecordItem> mergedMappings = new ArrayList<MappingRecordItem>();
+                for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
+                    MappingRecord mapping = record.getMappingRecord();
+                    MappingRecord currentRecord = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound,
+                            mapping.getEid());
+                    mergedMappings.add(new MappingRecordItemBuilder().setMappingRecord(currentRecord).build());
+                    Set<IpAddressBinary> sourceRlocs = (Set<IpAddressBinary>) mapService.getData(
+                            MappingOrigin.Southbound, mapping.getEid(), SubKeys.SRC_RLOCS);
+                    if (sourceRlocs != null) {
+                        notifyRlocs.addAll(sourceRlocs);
+                    }
                 }
-                notifyHandler.handleMapNotify(builder.build());
+                MapNotifyBuilderHelper.setFromMapRegisterAndMappingRecordItems(builder, mapRegister, mergedMappings);
+                // send map-notify to merge group only when mapping record is changed
+                if (mappingUpdated) {
+                    rlocs = getTransportAddresses(notifyRlocs);
+                }
+            } else {
+                MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
             }
+            notifyHandler.handleMapNotify(builder.build(), rlocs);
+        }
+    }
+
+    private static List<TransportAddress> getTransportAddresses(Set<IpAddressBinary> addresses) {
+        List<TransportAddress> rlocs = new ArrayList<TransportAddress>();
+        for (IpAddressBinary address : addresses) {
+            TransportAddressBuilder tab = new TransportAddressBuilder();
+            tab.setIpAddress(address);
+            tab.setPort(new PortNumber(LispMessage.PORT_NUM));
+            rlocs.add(tab.build());
         }
+        return rlocs;
     }
 
     private SiteId getSiteId(MapRegister mapRegister) {
@@ -128,71 +160,91 @@ public class MapServer implements IMapServerAsync, MappingserviceListener {
     @Override
     public void onMappingChanged(MappingChanged notification) {
         if (subscriptionService) {
-            sendSmrs(new EidToLocatorRecordBuilder(notification.getMapping()).build(), getSubscribers(notification
-                    .getMapping().getLispAddressContainer()));
-            if (notification.getChange().equals(MappingChange.Removed)) {
-                removeSubscribers(notification.getMapping().getLispAddressContainer());
+            sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
+            if (notification.getChangeType().equals(MappingChange.Removed)) {
+                removeSubscribers(notification.getMappingRecord().getEid());
             }
         }
     }
 
-    private void sendSmrs(EidToLocatorRecord record, Set<SubscriberRLOC> subscribers) {
-        LispAddressContainer eid = record.getLispAddressContainer();
-        handleSmr(record, subscribers, notifyHandler);
+    private static boolean mappingChanged(MappingRecord oldMapping, MappingRecord newMapping) {
+        // We only check for fields we care about
+        // XXX: This code needs to be checked and updated when the YANG model is modified
+        Preconditions.checkNotNull(newMapping, "The new mapping should never be null");
+        if (oldMapping == null) {
+            LOG.trace("mappingChanged(): old mapping is null");
+            return true;
+        } else if (!Objects.equals(oldMapping.getEid(), newMapping.getEid())) {
+            LOG.trace("mappingChanged(): EID");
+            return true;
+        } else if (!Objects.equals(oldMapping.getLocatorRecord(), newMapping.getLocatorRecord())) {
+            LOG.trace("mappingChanged(): RLOC");
+            return true;
+        } else if (!Objects.equals(oldMapping.getAction(), newMapping.getAction())) {
+            LOG.trace("mappingChanged(): action");
+            return true;
+        } else if (!Objects.equals(oldMapping.getRecordTtl(), newMapping.getRecordTtl())) {
+            LOG.trace("mappingChanged(): TTL");
+            return true;
+        } else if (!Objects.equals(oldMapping.getMapVersion(), newMapping.getMapVersion())) {
+            LOG.trace("mappingChanged(): mapping version");
+            return true;
+        }
+        return false;
+    }
+
+    private void sendSmrs(MappingRecord record, Set<SubscriberRLOC> subscribers) {
+        Eid eid = record.getEid();
+        handleSmr(eid, subscribers, notifyHandler);
 
         // For SrcDst LCAF also send SMRs to Dst prefix
-        if (eid.getAddress() instanceof LcafSourceDest) {
-            LispAddressContainer dstAddr = LispAFIConvertor.toContainer(LcafSourceDestHelper.getDstAfi(eid));
-            short dstMask = LcafSourceDestHelper.getDstMask(eid);
-            subscribers = getSubscribers(dstAddr);
-            EidToLocatorRecord newRecord = new EidToLocatorRecordBuilder().setAction(record.getAction())
-                    .setAuthoritative(record.isAuthoritative()).setLocatorRecord(record.getLocatorRecord())
-                    .setMapVersion(record.getMapVersion()).setRecordTtl(record.getRecordTtl())
-                    .setLispAddressContainer(dstAddr).setMaskLength(dstMask).build();
-            handleSmr(newRecord, subscribers, notifyHandler);
+        if (eid.getAddress() instanceof SourceDestKey) {
+            Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid);
+            Set<SubscriberRLOC> dstSubs = getSubscribers(dstAddr);
+            MappingRecord newRecord = new MappingRecordBuilder(record).setEid(dstAddr).build();
+            handleSmr(newRecord.getEid(), dstSubs, notifyHandler);
         }
     }
 
-    private void handleSmr(EidToLocatorRecord record, Set<SubscriberRLOC> subscribers, IMapNotifyHandler callback) {
+    private void handleSmr(Eid eid, Set<SubscriberRLOC> subscribers, IMapNotifyHandler callback) {
         if (subscribers == null) {
             return;
         }
-        MapRequestBuilder mrb = MapRequestUtil.prepareSMR(record.getLispAddressContainer(),
-                LispAFIConvertor.toContainer(getLocalAddress()));
+        MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress()));
         LOG.trace("Built SMR packet: " + mrb.build().toString());
-        for (SubscriberRLOC subscriber : subscribers) {
+        // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a SubscriberRLOC
+        // from a Set.
+        Iterator<SubscriberRLOC> iterator = subscribers.iterator();
+        while (iterator.hasNext()) {
+            SubscriberRLOC subscriber = iterator.next();
             if (subscriber.timedOut()) {
                 LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString());
-                subscribers.remove(subscriber);
+                iterator.remove();
             } 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.setEidRecord(new ArrayList<EidRecord>());
-                    mrb.getEidRecord()
-                            .add(new EidRecordBuilder()
-                                    .setMask(
-                                            (short) MaskUtil.getMaxMask(LispAFIConvertor.toAFI(subscriber.getSrcEid())))
-                                    .setLispAddressContainer(subscriber.getSrcEid()).build());
+                    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.getLispAddressContainer(), subscribers);
+        addSubscribers(eid, subscribers);
     }
 
     @SuppressWarnings("unchecked")
-    private Set<SubscriberRLOC> getSubscribers(LispAddressContainer address) {
+    private Set<SubscriberRLOC> getSubscribers(Eid address) {
         return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
     }
 
-    private void removeSubscribers(LispAddressContainer address) {
+    private void removeSubscribers(Eid address) {
         mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
     }
 
-    private void addSubscribers(LispAddressContainer address, Set<SubscriberRLOC> subscribers) {
+    private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
         mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
     }
 
@@ -202,19 +254,22 @@ public class MapServer implements IMapServerAsync, MappingserviceListener {
             while (interfaces.hasMoreElements()) {
                 NetworkInterface current = interfaces.nextElement();
                 LOG.debug("Interface " + current.toString());
-                if (!current.isUp() || current.isLoopback() || current.isVirtual())
+                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())
+                    if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress()) {
                         continue;
+                    }
                     LOG.debug(current_addr.getHostAddress());
                     return current_addr;
                 }
             }
         } catch (SocketException se) {
+            LOG.debug("Caught socket exceptio", se);
         }
         return null;
     }