+
+ public static void addSubscribers(LispAddressContainer eid, int mask, Set<SubscriberRLOC> subscribers,
+ ILispDAO dao) {
+ IMappingKey key = MappingKeyUtil.generateMappingKey(eid, mask);
+ dao.put(key, new MappingEntry<Set<SubscriberRLOC>>(DAOSubKeys.SUBSCRIBERS_SUBKEY.toString(), subscribers));
+ }
+
+ public static void addAuthenticationKey(LispAddressContainer address, int maskLen, String key, ILispDAO dao) {
+ IMappingKey mappingServiceKey = MappingKeyUtil.generateMappingKey(address, maskLen);
+ if (address.getAddress() instanceof LcafSourceDest) {
+ IMappingKey srcKey = MappingKeyUtil.generateMappingKey(getSrcForLcafSrcDst(address),
+ getSrcMaskForLcafSrcDst(address));
+ ILispDAO srcDstDao = getOrInstantiateSrcDstInnerDao(address, maskLen, dao);
+ srcDstDao.put(srcKey, new MappingEntry<String>(DAOSubKeys.PASSWORD_SUBKEY.toString(), key));
+ } else {
+ dao.put(mappingServiceKey, new MappingEntry<String>(DAOSubKeys.PASSWORD_SUBKEY.toString(), key));
+ }
+ }
+
+ public static String getPasswordForMaskable(LispAddressContainer prefix, int maskLength, ILispDAO db,
+ boolean shouldIterate) {
+ while (maskLength >= 0) {
+ IMappingKey key = MappingKeyUtil.generateMappingKey(prefix, maskLength);
+ Object password = db.getSpecific(key, DAOSubKeys.PASSWORD_SUBKEY.toString());
+ if (password != null && password instanceof String) {
+ return (String) password;
+ } else if (shouldIterate) {
+ maskLength -= 1;
+ } else {
+ LOG.warn("Failed to find password!");
+ return null;
+ }
+ }
+ return null;
+ }
+
+ public static String getPassword(LispAddressContainer prefix, int maskLength, ILispDAO dao, boolean iterate) {
+ if (MaskUtil.isMaskable(LispAFIConvertor.toAFI(prefix))) {
+ return getPasswordForMaskable(prefix, maskLength, dao, iterate);
+ } else if (prefix.getAddress() instanceof LcafSourceDest) {
+ ILispDAO srcDstDao = getSrcDstInnerDao(prefix, maskLength, dao);
+ if (srcDstDao != null) {
+ return getPasswordForMaskable(LispAFIConvertor.toContainer(getSrcForLcafSrcDst(prefix)),
+ getSrcMaskForLcafSrcDst(prefix), srcDstDao, iterate);
+ }
+ return null;
+ } else {
+ IMappingKey key = MappingKeyUtil.generateMappingKey(prefix, maskLength);
+ Object password = dao.getSpecific(key, DAOSubKeys.PASSWORD_SUBKEY.toString());
+ if (password != null && password instanceof String) {
+ return (String) password;
+ } else {
+ LOG.warn("Failed to find password!");
+ return null;
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public static Set<SubscriberRLOC> getSubscribers(LispAddressContainer prefix, int maskLength, ILispDAO dao) {
+ Object subscribers;
+ if (prefix.getAddress() instanceof LcafSourceDest) {
+ IMappingKey srcKey = MappingKeyUtil.generateMappingKey(getSrcForLcafSrcDst(prefix),
+ getSrcMaskForLcafSrcDst(prefix));
+ ILispDAO srcDstDao = getSrcDstInnerDao(prefix, maskLength, dao);
+ subscribers = srcDstDao.getSpecific(srcKey, DAOSubKeys.SUBSCRIBERS_SUBKEY.toString());
+ } else {
+ IMappingKey key = MappingKeyUtil.generateMappingKey(prefix, maskLength);
+ subscribers = dao.getSpecific(key, DAOSubKeys.SUBSCRIBERS_SUBKEY.toString());
+ }
+
+ if (subscribers != null && subscribers instanceof Set<?>) {
+ return (Set<SubscriberRLOC>) subscribers;
+ }
+ return null;
+ }
+
+ // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over
+ // src. This method returns the DAO
+ // associated to a dst or creates it if it doesn't exist.
+ public static ILispDAO getOrInstantiateSrcDstInnerDao(LispAddressContainer address, int maskLen, ILispDAO dao) {
+ IMappingKey dstKey = MappingKeyUtil.generateMappingKey(getDstForLcafSrcDst(address),
+ getDstMaskForLcafSrcDst(address));
+ ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, DAOSubKeys.LCAF_SRCDST_SUBKEY.toString());
+ if (srcDstDao == null) {
+ srcDstDao = new HashMapDb();
+ dao.put(dstKey, new MappingEntry<>(DAOSubKeys.LCAF_SRCDST_SUBKEY.toString(), srcDstDao));
+ }
+ return srcDstDao;
+ }
+
+ // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over
+ // src. This method returns the DAO
+ // associated to a dst or null if it doesn't exist.
+ public static ILispDAO getSrcDstInnerDao(LispAddressContainer address, int maskLen, ILispDAO dao) {
+ IMappingKey dstKey = MappingKeyUtil.generateMappingKey(getDstForLcafSrcDst(address),
+ getDstMaskForLcafSrcDst(address));
+ return (ILispDAO) dao.getSpecific(dstKey, DAOSubKeys.LCAF_SRCDST_SUBKEY.toString());
+ }
+
+ public static boolean saveRlocs(EidToLocatorRecord eidRecord, boolean checkForChanges, ILispDAO dao,
+ boolean shouldIterate, boolean shouldOverwrite) {
+ Map<String, RLOCGroup> rlocGroups = new HashMap<String, RLOCGroup>();
+ if (eidRecord.getLocatorRecord() != null) {
+ for (LocatorRecord locatorRecord : eidRecord.getLocatorRecord()) {
+ String subkey = getAddressKey(locatorRecord.getLispAddressContainer().getAddress(), shouldOverwrite);
+ if (!rlocGroups.containsKey(subkey)) {
+ rlocGroups
+ .put(subkey,
+ new RLOCGroup(eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord
+ .isAuthoritative()));
+ }
+ rlocGroups.get(subkey).addRecord(locatorRecord);
+ }
+ } else {
+ rlocGroups.put(DAOSubKeys.ADDRESS_SUBKEY.toString(),
+ new RLOCGroup(eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord.isAuthoritative()));
+ }
+ List<MappingEntry<RLOCGroup>> entries = new ArrayList<>();
+ for (String subkey : rlocGroups.keySet()) {
+ entries.add(new MappingEntry<>(subkey, rlocGroups.get(subkey)));
+ }
+
+ if (eidRecord.getLispAddressContainer().getAddress() instanceof LcafSourceDest) {
+ Entry<IMappingKey, List<RLOCGroup>> oldMapping = null, newMapping = null;
+ LispAFIAddress srcAddr = getSrcForLcafSrcDst(eidRecord.getLispAddressContainer());
+ LispAFIAddress dstAddr = getDstForLcafSrcDst(eidRecord.getLispAddressContainer());
+ short srcMask = getSrcMaskForLcafSrcDst(eidRecord.getLispAddressContainer());
+ short dstMask = getDstMaskForLcafSrcDst(eidRecord.getLispAddressContainer());
+
+ if (checkForChanges) {
+ oldMapping = DAOMappingUtil.getMappingExact(srcAddr, dstAddr, srcMask, dstMask, dao);
+ }
+ IMappingKey dstKey = MappingKeyUtil.generateMappingKey(dstAddr, dstMask);
+ ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, DAOSubKeys.LCAF_SRCDST_SUBKEY.toString());
+ if (srcDstDao == null) {
+ srcDstDao = new HashMapDb();
+ dao.put(dstKey, new MappingEntry<>(DAOSubKeys.LCAF_SRCDST_SUBKEY.toString(), srcDstDao));
+ }
+ IMappingKey srcKey = MappingKeyUtil.generateMappingKey(srcAddr, srcMask);
+ srcDstDao.put(srcKey, entries.toArray(new MappingEntry[entries.size()]));
+ if (checkForChanges) {
+ newMapping = DAOMappingUtil.getMappingExact(srcAddr, dstAddr, srcMask, dstMask, dao);
+ return (newMapping.getValue() == null) ? oldMapping.getValue() != null : !newMapping.getValue()
+ .equals(oldMapping.getValue());
+ }
+ } else {
+ List<RLOCGroup> oldLocators = null, newLocators = null;
+ if (checkForChanges) {
+ oldLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterate);
+ }
+ IMappingKey key = MappingKeyUtil.generateMappingKey(eidRecord.getLispAddressContainer(),
+ eidRecord.getMaskLength());
+ dao.put(key, entries.toArray(new MappingEntry[entries.size()]));
+ if (checkForChanges) {
+ newLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterate);
+ return (newLocators == null) ? oldLocators != null : !newLocators.equals(oldLocators);
+ }
+ }
+ return false;
+ }
+
+ public static String getAddressKey(Address address, boolean shouldOverwrite) {
+ if (address instanceof LcafKeyValue) {
+ LcafKeyValue keyVal = (LcafKeyValue) address;
+ if (keyVal.getLcafKeyValueAddressAddr().getKey().getPrimitiveAddress() instanceof DistinguishedName) {
+ return ((DistinguishedName) keyVal.getLcafKeyValueAddressAddr().getKey().getPrimitiveAddress())
+ .getDistinguishedNameAddress().getDistinguishedName();
+ }
+ }
+ if (shouldOverwrite) {
+ return DAOSubKeys.ADDRESS_SUBKEY.toString();
+ } else {
+ return String.valueOf(address.hashCode());
+ }
+ }
+
+ public static void removeAuthenticationKey(LispAddressContainer address, int maskLen, ILispDAO dao) {
+ if (address.getAddress() instanceof LcafSourceDest) {
+ ILispDAO srcDstDao = getSrcDstInnerDao(address, maskLen, dao);
+ if (srcDstDao != null) {
+ IMappingKey srcKey = MappingKeyUtil.generateMappingKey(getSrcForLcafSrcDst(address),
+ getSrcMaskForLcafSrcDst(address));
+ srcDstDao.removeSpecific(srcKey, DAOSubKeys.PASSWORD_SUBKEY.toString());
+ }
+ } else {
+ IMappingKey key = MappingKeyUtil.generateMappingKey(address, maskLen);
+ dao.removeSpecific(key, DAOSubKeys.PASSWORD_SUBKEY.toString());
+ }
+ }
+
+ private static void removeMappingRlocs(Entry<IMappingKey, List<RLOCGroup>> mapping, ILispDAO db,
+ boolean shouldOverwrite) {
+ if (mapping == null || mapping.getValue() == null) {
+ return;
+ }
+ for (RLOCGroup group : mapping.getValue()) {
+ for (LocatorRecord record : group.getRecords()) {
+ db.removeSpecific(mapping.getKey(),
+ DAOMappingUtil.getAddressKey(record.getLispAddressContainer().getAddress(), shouldOverwrite));
+ }
+ }
+ }
+
+ public static void removeMapping(LispAddressContainer address, int maskLen, ILispDAO dao, boolean oerwrite) {
+ Entry<IMappingKey, List<RLOCGroup>> mapping;
+ ILispDAO db;
+ if (address.getAddress() instanceof LcafSourceDest) {
+ db = getSrcDstInnerDao(address, maskLen, dao);
+ LispAFIAddress srcAddr = getSrcForLcafSrcDst(address);
+ short srcMask = getSrcMaskForLcafSrcDst(address);
+ mapping = DAOMappingUtil.getMappingForEid(srcAddr, srcMask, db);
+ } else {
+ db = dao;
+ mapping = DAOMappingUtil.getMappingForEid(LispAFIConvertor.toAFI(address), maskLen, db);
+ }
+ removeMappingRlocs(mapping, db, oerwrite);
+ }
+
+ public static void removeSubscribers(LispAddressContainer address, int maskLen, ILispDAO dao,
+ boolean shouldOverwrite) {
+ Entry<IMappingKey, List<RLOCGroup>> mapping;
+ ILispDAO db;
+ if (address.getAddress() instanceof LcafSourceDest) {
+ db = getSrcDstInnerDao(address, maskLen, dao);
+ LispAFIAddress srcAddr = getSrcForLcafSrcDst(address);
+ short srcMask = getSrcMaskForLcafSrcDst(address);
+ mapping = DAOMappingUtil.getMappingForEid(srcAddr, srcMask, db);
+ } else {
+ db = dao;
+ mapping = DAOMappingUtil.getMappingForEid(LispAFIConvertor.toAFI(address), maskLen, db);
+ }
+ db.removeSpecific(mapping.getKey(), DAOSubKeys.SUBSCRIBERS_SUBKEY.toString());
+ }
+
+ public static void removeEntry(LispAddressContainer address, int maskLen, ILispDAO dao, boolean shouldOverwrite) {
+ Entry<IMappingKey, List<RLOCGroup>> mapping;
+ ILispDAO db;
+ if (address.getAddress() instanceof LcafSourceDest) {
+ db = getSrcDstInnerDao(address, maskLen, dao);
+ LispAFIAddress srcAddr = getSrcForLcafSrcDst(address);
+ short srcMask = getSrcMaskForLcafSrcDst(address);
+ mapping = DAOMappingUtil.getMappingForEid(srcAddr, srcMask, db);
+ } else {
+ db = dao;
+ mapping = DAOMappingUtil.getMappingForEid(LispAFIConvertor.toAFI(address), maskLen, db);
+ }
+
+ db.remove(mapping.getKey());
+ }
+
+ public static LispAFIAddress getSrcForLcafSrcDst(LispAddressContainer container) {
+ return LispAFIConvertor.toAFIfromPrimitive(((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr()
+ .getSrcAddress().getPrimitiveAddress());
+ }
+
+ public static LispAFIAddress getDstForLcafSrcDst(LispAddressContainer container) {
+ return LispAFIConvertor.toAFIfromPrimitive(((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr()
+ .getDstAddress().getPrimitiveAddress());
+ }
+
+ public static short getSrcMaskForLcafSrcDst(LispAddressContainer container) {
+ return ((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr().getSrcMaskLength();
+ }
+
+ public static short getDstMaskForLcafSrcDst(LispAddressContainer container) {
+ return ((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr().getDstMaskLength();
+ }
+