+ private ILispDAO getVniTable(Eid eid) {
+ long vni = 0;
+ if (eid.getVirtualNetworkId() == null) {
+ vni = 0;
+ } else {
+ vni = eid.getVirtualNetworkId().getValue();
+ }
+ return (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
+ }
+
+ private ILispDAO getOrInstantiateVniTable(Eid eid) {
+ long vni = 0;
+ if (eid.getVirtualNetworkId() == null) {
+ vni = 0;
+ } else {
+ vni = eid.getVirtualNetworkId().getValue();
+ }
+ ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
+ if (table == null) {
+ table = dao.putNestedTable(vni, SubKeys.VNI);
+ }
+ return table;
+ }
+
+ private ILispDAO getXtrIdTable(Eid eid, ILispDAO dao) {
+ return (ILispDAO) dao.getSpecific(eid, SubKeys.XTRID_RECORDS);
+ }
+
+ private ILispDAO getOrInstantiateXtrIdTable(Eid eid, ILispDAO dao) {
+ ILispDAO table = (ILispDAO) dao.getSpecific(eid, SubKeys.XTRID_RECORDS);
+ if (table == null) {
+ table = dao.putNestedTable(eid, SubKeys.XTRID_RECORDS);
+ }
+ return table;
+ }
+
+ private void removeExpiredXtrIdTableEntries(ILispDAO xtrIdDao, List<byte[]> expiredMappings) {
+ for (byte[] xtrId : expiredMappings) {
+ xtrIdDao.removeSpecific(xtrId, SubKeys.RECORD);
+ }
+ }
+
+ public void addMapping(Eid key, Object value, boolean shouldOverwrite) {
+ if (value == null) {
+ LOG.warn("addMapping() called with null 'value', ignoring");
+ return;
+ }
+
+ if (!(value instanceof MappingRecord)) {
+ LOG.warn("addMapping() called with a 'value' that is not a 'MappingRecord', ignoring");
+ return;
+ }
+
+ MappingRecord record = (MappingRecord) value;
+ if (record.getXtrId() == null && !shouldOverwrite && ConfigIni.getInstance().mappingMergeIsSet()) {
+ LOG.warn("addMapping() called will null xTR-ID in MappingRecord, while merge is set, ignoring");
+ return;
+ }
+
+ Date regdate = new Date(record.getTimestamp()); // The serializer always sets it
+ Eid eid = MaskUtil.normalize(key);
+ ILispDAO table = getOrInstantiateVniTable(key);
+
+ ILispDAO xtrIdDao = null;
+ if (!shouldOverwrite) {
+ xtrIdDao = getOrInstantiateXtrIdTable(eid, table);
+ xtrIdDao.put(record.getXtrId(), new MappingEntry<>(SubKeys.RECORD, value));
+ }
+
+ if (ConfigIni.getInstance().mappingMergeIsSet()) {
+ List<byte[]> expiredMappings = new ArrayList<byte[]>();
+ Set<IpAddress> sourceRlocs = new HashSet<IpAddress>();
+ MappingRecord mergedEntry = MappingMergeUtil.mergeXtrIdMappings(getXtrIdMappingList(xtrIdDao),
+ expiredMappings, sourceRlocs);
+ removeExpiredXtrIdTableEntries(xtrIdDao, expiredMappings);
+ regdate = new Date(mergedEntry.getTimestamp());
+ table.put(eid, new MappingEntry<>(SubKeys.REGDATE, regdate));
+ table.put(eid, new MappingEntry<>(SubKeys.RECORD, mergedEntry));
+ table.put(eid, new MappingEntry<>(SubKeys.SRC_RLOCS, sourceRlocs));
+ } else {
+ table.put(eid, new MappingEntry<>(SubKeys.REGDATE, regdate));
+ table.put(eid, new MappingEntry<>(SubKeys.RECORD, value));
+ }