X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=mappingservice%2Fimplementation%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Flispflowmapping%2Fimplementation%2Fmdsal%2FMappingDataListener.java;h=d094897b835d703d9f9bfb98c543eed41f3c4c49;hb=a5458da5eb1fa27c052ff93dee8b39e69aa0ca4d;hp=d3c070063b71c41915c4aebec888a22840a92dbe;hpb=927cbc753dfc85f6ecd72f56f1640ff05e2a905c;p=lispflowmapping.git diff --git a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/MappingDataListener.java b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/MappingDataListener.java index d3c070063..d094897b8 100644 --- a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/MappingDataListener.java +++ b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/MappingDataListener.java @@ -7,19 +7,28 @@ */ package org.opendaylight.lispflowmapping.implementation.mdsal; -import java.util.Map; -import java.util.Set; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; +import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType; +import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; -import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil; import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem; +import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder; +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.mappingservice.rev150906.MappingChange; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier; -import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,7 +40,7 @@ import org.slf4j.LoggerFactory; * @author Florin Coras * */ -public class MappingDataListener extends AbstractDataListener { +public class MappingDataListener extends AbstractDataListener { private static final Logger LOG = LoggerFactory.getLogger(MappingDataListener.class); private IMappingSystem mapSystem; private NotificationPublishService notificationPublishService; @@ -50,67 +59,118 @@ public class MappingDataListener extends AbstractDataListener { this.notificationPublishService = nps; } + void setMappingSystem(IMappingSystem msmr) { + this.mapSystem = msmr; + } + @Override - public void onDataChanged( - AsyncDataChangeEvent, DataObject> change) { + public void onDataTreeChanged(Collection> changes) { + for (DataTreeModification change : changes) { + final DataObjectModification mod = change.getRootNode(); - // Process newly created mappings - Map, DataObject> createdData = change.getCreatedData(); - for (Map.Entry, DataObject> entry : createdData.entrySet()) { - if (entry.getValue() instanceof Mapping) { - Mapping mapping = (Mapping)entry.getValue(); + if (ModificationType.DELETE == mod.getModificationType()) { + // Process deleted mappings - LOG.trace("Received created data"); - LOG.trace("Key: {}", entry.getKey()); - LOG.trace("Value: {}", mapping); + final Mapping mapping = mod.getDataBefore(); - mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(), - mapping.getMappingRecord()); - } - } + // Only treat mapping changes caused by Northbound, since Southbound changes are already handled + // before being persisted. + if (mapping.getOrigin() == MappingOrigin.Southbound) { + continue; + } - // Process updated mappings - Map, DataObject> updatedData = change.getUpdatedData(); - for (Map.Entry, DataObject> entry : updatedData.entrySet()) { - if (entry.getValue() instanceof Mapping) { - Mapping mapping = (Mapping)entry.getValue(); + LOG.trace("Received deleted data"); + LOG.trace("Key: {}", change.getRootPath().getRootIdentifier()); + LOG.trace("Value: {}", mapping); - LOG.trace("Received changed data"); - LOG.trace("Key: {}", entry.getKey()); - LOG.trace("Value: {}", entry.getValue()); + final Mapping convertedMapping = convertToBinaryIfNecessary(mapping); - mapSystem.addMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid(), - mapping.getMappingRecord()); + mapSystem.removeMapping(convertedMapping.getOrigin(), convertedMapping.getMappingRecord().getEid()); try { - notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Updated)); + notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged( + convertedMapping, MappingChange.Removed)); } catch (InterruptedException e) { LOG.warn("Notification publication interrupted!"); } - } - } - // Process deleted mappings - Set> removedData = change.getRemovedPaths(); - for (InstanceIdentifier entry : removedData) { - DataObject dataObject = change.getOriginalData().get(entry); - if (dataObject instanceof Mapping) { - Mapping mapping = (Mapping)dataObject; + } else if (ModificationType.SUBTREE_MODIFIED == mod.getModificationType() || ModificationType.WRITE == mod + .getModificationType()) { + final Mapping mapping = mod.getDataAfter(); - LOG.trace("Received deleted data"); - LOG.trace("Key: {}", entry); - LOG.trace("Value: {}", dataObject); + // Only treat mapping changes caused by Northbound, since Southbound changes are already handled + // before being persisted. XXX separate NB and SB to avoid ignoring SB notifications + if (mapping.getOrigin() == MappingOrigin.Southbound) { + continue; + } + + MappingChange mappingChange; + + if (ModificationType.SUBTREE_MODIFIED == mod.getModificationType()) { + LOG.trace("Received update data"); + mappingChange = MappingChange.Updated; + } else { + LOG.trace("Received write data"); + mappingChange = MappingChange.Created; + } + LOG.trace("Key: {}", change.getRootPath().getRootIdentifier()); + LOG.trace("Value: {}", mapping); + + final Mapping convertedMapping = convertToBinaryIfNecessary(mapping); + + mapSystem.addMapping(convertedMapping.getOrigin(), convertedMapping.getMappingRecord().getEid(), + convertedMapping.getMappingRecord(), false); - mapSystem.removeMapping(mapping.getOrigin(), mapping.getMappingRecord().getEid()); try { - notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged(mapping, MappingChange.Removed)); + // The notifications are used for sending SMR. + notificationPublishService.putNotification(MSNotificationInputUtil.toMappingChanged( + convertedMapping, mappingChange)); } catch (InterruptedException e) { LOG.warn("Notification publication interrupted!"); } + + } else { + LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType()); } } } - void setMappingSystem(IMappingSystem msmr) { - this.mapSystem = msmr; + private static Mapping convertToBinaryIfNecessary(Mapping mapping) { + MappingRecord originalRecord = mapping.getMappingRecord(); + List originalLocators = originalRecord.getLocatorRecord(); + + List convertedLocators = null; + if (originalLocators != null) { + // If convertedLocators is non-null, while originalLocators is also non-null, conversion has been made + convertedLocators = convertToBinaryIfNecessary(originalLocators); + } + + if (LispAddressUtil.addressNeedsConversionToBinary(originalRecord.getEid().getAddress()) || + (originalLocators != null && convertedLocators != null)) { + MappingRecordBuilder mrb = new MappingRecordBuilder(originalRecord); + mrb.setEid(LispAddressUtil.convertToBinary(originalRecord.getEid())); + if (convertedLocators != null) { + mrb.setLocatorRecord(convertedLocators); + } + return new MappingBuilder(mapping).setMappingRecord(mrb.build()).build(); + } + return mapping; + } + + private static List convertToBinaryIfNecessary(List originalLocators) { + List convertedLocators = null; + for (LocatorRecord record : originalLocators) { + if (LispAddressUtil.addressNeedsConversionToBinary(record.getRloc().getAddress())) { + LocatorRecordBuilder lrb = new LocatorRecordBuilder(record); + lrb.setRloc(LispAddressUtil.convertToBinary(record.getRloc())); + if (convertedLocators == null) { + convertedLocators = new ArrayList(); + } + convertedLocators.add(lrb.build()); + } + } + if (convertedLocators != null) { + return convertedLocators; + } + return originalLocators; } }