Reorganize mappingservice.implementation
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / util / DAOMappingUtil.java
1 /*
2  * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.lispflowmapping.implementation.util;
10
11 import java.util.AbstractMap;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.Map.Entry;
18
19 import org.opendaylight.lispflowmapping.implementation.dao.MappingKeyUtil;
20 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
21 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingKey;
22 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
23 import org.opendaylight.lispflowmapping.interfaces.dao.RLOCGroup;
24 import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidrecords.EidRecord;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecord;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafsourcedestaddress.DstAddressBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lcafsourcedestaddress.SrcAddressBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.Address;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.LcafKeyValue;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.LcafSourceDest;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcafsourcedest.LcafSourceDestAddr;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.lcafsourcedest.LcafSourceDestAddrBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispsimpleaddress.primitiveaddress.DistinguishedName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.locatorrecords.LocatorRecord;
38 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
39 import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class DAOMappingUtil {
44     private static final Logger LOG = LoggerFactory.getLogger(DAOMappingUtil.class);
45
46     public static List<RLOCGroup> getLocatorsByEidRecord(EidRecord eid, ILispDAO dao, boolean iterateMask) {
47         IMappingKey key = MappingKeyUtil.generateMappingKey(eid.getLispAddressContainer(), eid.getMask());
48         return getLocators(key, dao, iterateMask);
49     }
50
51     public static Object getLocatorsSpecificByEidRecord(EidRecord eid, ILispDAO dao, String subkey,
52             boolean iterateMask) {
53         IMappingKey key = MappingKeyUtil.generateMappingKey(eid.getLispAddressContainer(), eid.getMask());
54         return getLocatorsSpecific(key, dao, subkey, iterateMask);
55     }
56
57     public static List<RLOCGroup> getLocatorsByEidToLocatorRecord(EidToLocatorRecord eid, ILispDAO dao,
58             boolean iterateMask) {
59         IMappingKey key = MappingKeyUtil.generateMappingKey(eid.getLispAddressContainer(), eid.getMaskLength());
60         return getLocators(key, dao, iterateMask);
61     }
62
63     public static Object getLocatorsSpecificByEidtoLocatorRecord(EidToLocatorRecord eid, ILispDAO dao, String subkey,
64             boolean iterateMask) {
65         IMappingKey key = MappingKeyUtil.generateMappingKey(eid.getLispAddressContainer(), eid.getMaskLength());
66         return getLocatorsSpecific(key, dao, subkey, iterateMask);
67     }
68
69     public static List<RLOCGroup> getLocators(IMappingKey key, ILispDAO dao, boolean iterateMask) {
70         Map<String, ?> locators = dao.get(key);
71         List<RLOCGroup> result = aggregateLocators(locators);
72         LispAFIAddress eid = LispAFIConvertor.toAFI(key.getEID());
73         if (iterateMask && result.isEmpty() && MaskUtil.isMaskable(eid)) {
74             result = findMaskLocators(dao, key);
75         }
76         return result;
77     }
78
79     private static List<RLOCGroup> aggregateLocators(Map<String, ?> locators) {
80         List<RLOCGroup> result = new ArrayList<RLOCGroup>();
81         if (locators != null) {
82             for (Object value : locators.values()) {
83                 if (value != null && value instanceof RLOCGroup) {
84                     result.add((RLOCGroup) value);
85                 }
86             }
87         }
88         return result;
89     }
90
91     public static Object getLocatorsSpecific(IMappingKey key, ILispDAO dao, String subkey, boolean iterateMask) {
92         Object locators = dao.getSpecific(key, subkey);
93         LispAFIAddress eid = LispAFIConvertor.toAFI(key.getEID());
94         if (iterateMask && locators == null && MaskUtil.isMaskable(eid)) {
95             locators = findMaskLocatorsSpecific(key, dao, subkey);
96         }
97         return locators;
98     }
99
100     private static Object findMaskLocatorsSpecific(IMappingKey key, ILispDAO dao, String subkey) {
101         int mask = key.getMask();
102         while (mask > 0) {
103             LispAFIAddress eid = LispAFIConvertor.toAFI(key.getEID());
104             key = MappingKeyUtil.generateMappingKey(LispAFIConvertor.toContainer(MaskUtil.normalize(eid, mask)), mask);
105             mask--;
106             Object locators = dao.getSpecific(key, subkey);
107             if (locators != null) {
108                 return locators;
109             }
110         }
111         return null;
112     }
113
114     private static List<RLOCGroup> findMaskLocators(ILispDAO dao, IMappingKey key) {
115         int mask = key.getMask();
116         while (mask > 0) {
117             LispAFIAddress eid = LispAFIConvertor.toAFI(key.getEID());
118             key = MappingKeyUtil.generateMappingKey(LispAFIConvertor.toContainer(MaskUtil.normalize(eid, mask)), mask);
119             mask--;
120             Map<String, ?> locators = dao.get(key);
121             if (locators != null) {
122                 List<RLOCGroup> result = aggregateLocators(locators);
123                 if (result != null && !result.isEmpty()) {
124                     return result;
125                 }
126             }
127         }
128         return null;
129     }
130
131     public static Entry<IMappingKey, List<RLOCGroup>> getMapping(LispAFIAddress srcEid, EidRecord eidRecord,
132             ILispDAO dao) {
133         // a map-request for an actual SrcDst LCAF, ignore src eid
134         if (eidRecord.getLispAddressContainer().getAddress() instanceof LcafSourceDest) {
135             LcafSourceDestAddr eid = ((LcafSourceDest) eidRecord.getLispAddressContainer().getAddress())
136                     .getLcafSourceDestAddr();
137             LispAFIAddress srcAddr = LispAFIConvertor.toAFIfromPrimitive(eid.getSrcAddress().getPrimitiveAddress());
138             LispAFIAddress dstAddr = LispAFIConvertor.toAFIfromPrimitive(eid.getDstAddress().getPrimitiveAddress());
139             return getMapping(srcAddr, dstAddr, eid.getSrcMaskLength(), eid.getDstMaskLength(), dao);
140         }
141
142         // potential map-request for SrcDst LCAF from non SrcDst capable devices
143         Entry<IMappingKey, List<RLOCGroup>> mapping = getMapping(srcEid,
144                 LispAFIConvertor.toAFI(eidRecord.getLispAddressContainer()), (short) MaskUtil.getMaxMask(srcEid),
145                 eidRecord.getMask(), dao);
146
147         // if indeed SrcDst LCAF change the key to matched dst eid
148         if (mapping.getKey().getEID().getAddress() instanceof LcafSourceDest) {
149             LcafSourceDestAddr srcDst = ((LcafSourceDest) mapping.getKey().getEID().getAddress())
150                     .getLcafSourceDestAddr();
151             IMappingKey newKey = MappingKeyUtil.generateMappingKey(
152                     LispAFIConvertor.toAFIfromPrimitive(srcDst.getDstAddress().getPrimitiveAddress()),
153                     srcDst.getDstMaskLength());
154             return new AbstractMap.SimpleImmutableEntry<>(newKey, mapping.getValue());
155         }
156
157         return mapping;
158     }
159
160     public static Entry<IMappingKey, List<RLOCGroup>> getMapping(LispAFIAddress srcEid, LispAFIAddress dstEid,
161             short srcMask, short dstMask, ILispDAO dao) {
162         IMappingKey key = MappingKeyUtil.generateMappingKey(dstEid, dstMask);
163         Entry<IMappingKey, Map<String, ?>> daoEntry = getDaoEntry(key, dao);
164         if (daoEntry != null) {
165             // try SrcDst eid lookup
166             ILispDAO srcDstDao = (ILispDAO) daoEntry.getValue().get(DAOSubKeys.LCAF_SRCDST_SUBKEY.toString());
167             if (srcDstDao != null) {
168                 Entry<IMappingKey, List<RLOCGroup>> mapping = getMappingForEid(srcEid, srcMask, srcDstDao);
169                 // if lookup fails, return whatever is found for dst eid
170                 if (mapping.getValue() != null) {
171                     LispAFIAddress newDst = LispAFIConvertor.toAFI(daoEntry.getKey().getEID());
172                     LispAFIAddress newSrc = LispAFIConvertor.toAFI(mapping.getKey().getEID());
173
174                     LispAFIAddress newEid = new LcafSourceDestAddrBuilder()
175                             .setDstAddress(
176                                     new DstAddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(newDst))
177                                             .build())
178                             .setSrcAddress(
179                                     new SrcAddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(newSrc))
180                                             .build()).setDstMaskLength((short) daoEntry.getKey().getMask())
181                             .setSrcMaskLength((short) mapping.getKey().getMask()).setAfi((short) 16387)
182                             .setLcafType((short) 12).build();
183                     IMappingKey newKey = MappingKeyUtil.generateMappingKey(newEid, dstMask);
184                     return new AbstractMap.SimpleImmutableEntry<>(newKey, mapping.getValue());
185                 }
186             }
187
188             // dst eid lookup
189             return makeMappingEntry(daoEntry.getKey(), daoEntry.getValue());
190         }
191         return new AbstractMap.SimpleImmutableEntry<>(key, null);
192     }
193
194     public static Entry<IMappingKey, List<RLOCGroup>> getMappingExact(LispAFIAddress srcEid, LispAFIAddress dstEid,
195             short srcMask, short dstMask, ILispDAO dao) {
196         IMappingKey dstKey = MappingKeyUtil.generateMappingKey(dstEid, dstMask);
197         Map<String, ?> daoEntry = dao.get(dstKey);
198         if (daoEntry != null) {
199             // try SrcDst eid lookup
200             ILispDAO srcDstDao = (ILispDAO) daoEntry.get(DAOSubKeys.LCAF_SRCDST_SUBKEY.toString());
201             // if lookup fails, return whatever is found for dst eid
202             if (srcDstDao != null) {
203                 IMappingKey srcKey = MappingKeyUtil.generateMappingKey(srcEid, srcMask);
204                 Map<String, ?> mapping = srcDstDao.get(srcKey);
205                 LispAFIAddress newEid = new LcafSourceDestAddrBuilder()
206                         .setDstAddress(
207                                 new DstAddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(dstEid))
208                                         .build())
209                         .setSrcAddress(
210                                 new SrcAddressBuilder().setPrimitiveAddress(LispAFIConvertor.toPrimitive(srcEid))
211                                         .build()).setDstMaskLength((short) dstMask).setSrcMaskLength((short) srcMask)
212                         .setAfi((short) 16387).setLcafType((short) 12).build();
213                 IMappingKey newKey = MappingKeyUtil.generateMappingKey(newEid, dstMask);
214                 return makeMappingEntry(newKey, mapping);
215             }
216
217             // dst eid lookup
218             return makeMappingEntry(dstKey, daoEntry);
219         }
220         return new AbstractMap.SimpleImmutableEntry<>(dstKey, null);
221     }
222
223     public static Entry<IMappingKey, List<RLOCGroup>> getMappingForEidRecord(EidRecord eid, ILispDAO dao) {
224         return getMappingForEid(LispAFIConvertor.toAFI(eid.getLispAddressContainer()), eid.getMask(), dao);
225     }
226
227     private static Entry<IMappingKey, List<RLOCGroup>> makeMappingEntry(IMappingKey key, Map<String, ?> locators) {
228         if (locators != null) {
229             List<RLOCGroup> locatorsList = aggregateLocators(locators);
230             if (locatorsList != null && !locatorsList.isEmpty()) {
231                 return new AbstractMap.SimpleImmutableEntry<>(key, locatorsList);
232             }
233         }
234         return new AbstractMap.SimpleImmutableEntry<>(key, null);
235     }
236
237     public static Entry<IMappingKey, List<RLOCGroup>> getMappingForEid(LispAFIAddress eid, int maskLen, ILispDAO dao) {
238         if (eid instanceof LcafSourceDestAddr) {
239             LispAFIAddress srcAddr = LispAFIConvertor.toAFIfromPrimitive(((LcafSourceDestAddr) eid).getSrcAddress()
240                     .getPrimitiveAddress());
241             LispAFIAddress dstAddr = LispAFIConvertor.toAFIfromPrimitive(((LcafSourceDestAddr) eid).getDstAddress()
242                     .getPrimitiveAddress());
243
244             return getMapping(srcAddr, dstAddr, ((LcafSourceDestAddr) eid).getSrcMaskLength(),
245                     ((LcafSourceDestAddr) eid).getDstMaskLength(), dao);
246         }
247         IMappingKey key = MappingKeyUtil.generateMappingKey(eid, maskLen);
248         Entry<IMappingKey, Map<String, ?>> entry = getDaoEntry(key, dao);
249         if (entry == null) {
250             return makeMappingEntry(key, null);
251         } else {
252             return makeMappingEntry(entry.getKey(), entry.getValue());
253         }
254     }
255
256     public static Entry<IMappingKey, Map<String, ?>> getDaoEntry(IMappingKey lookupKey, ILispDAO dao) {
257         LispAFIAddress eidAddress = LispAFIConvertor.toAFI(lookupKey.getEID());
258         if (MaskUtil.isMaskable(eidAddress)) {
259             IMappingKey key;
260             int mask = lookupKey.getMask();
261             while (mask > 0) {
262                 key = MappingKeyUtil.generateMappingKey(eidAddress, mask);
263                 mask--;
264                 Map<String, ?> entry = dao.get(key);
265                 if (entry != null) {
266                     return new AbstractMap.SimpleImmutableEntry<IMappingKey, Map<String, ?>>(key, entry);
267                 }
268             }
269             return null;
270         } else {
271             Map<String, ?> entry = dao.get(lookupKey);
272             if (entry != null) {
273                 return new AbstractMap.SimpleImmutableEntry<IMappingKey, Map<String, ?>>(lookupKey,
274                         dao.get(lookupKey));
275             } else {
276                 return null;
277             }
278         }
279     }
280
281     public static void addSubscribers(LispAddressContainer eid, int mask, Set<SubscriberRLOC> subscribers,
282             ILispDAO dao) {
283         IMappingKey key = MappingKeyUtil.generateMappingKey(eid, mask);
284         dao.put(key, new MappingEntry<Set<SubscriberRLOC>>(DAOSubKeys.SUBSCRIBERS_SUBKEY.toString(), subscribers));
285     }
286
287     public static void addAuthenticationKey(LispAddressContainer address, int maskLen, String key, ILispDAO dao) {
288         IMappingKey mappingServiceKey = MappingKeyUtil.generateMappingKey(address, maskLen);
289         if (address.getAddress() instanceof LcafSourceDest) {
290             IMappingKey srcKey = MappingKeyUtil.generateMappingKey(getSrcForLcafSrcDst(address),
291                     getSrcMaskForLcafSrcDst(address));
292             ILispDAO srcDstDao = getOrInstantiateSrcDstInnerDao(address, maskLen, dao);
293             srcDstDao.put(srcKey, new MappingEntry<String>(DAOSubKeys.PASSWORD_SUBKEY.toString(), key));
294         } else {
295             dao.put(mappingServiceKey, new MappingEntry<String>(DAOSubKeys.PASSWORD_SUBKEY.toString(), key));
296         }
297     }
298
299     public static String getPasswordForMaskable(LispAddressContainer prefix, int maskLength, ILispDAO db,
300             boolean shouldIterate) {
301         while (maskLength >= 0) {
302             IMappingKey key = MappingKeyUtil.generateMappingKey(prefix, maskLength);
303             Object password = db.getSpecific(key, DAOSubKeys.PASSWORD_SUBKEY.toString());
304             if (password != null && password instanceof String) {
305                 return (String) password;
306             } else if (shouldIterate) {
307                 maskLength -= 1;
308             } else {
309                 LOG.warn("Failed to find password!");
310                 return null;
311             }
312         }
313         return null;
314     }
315
316     public static String getPassword(LispAddressContainer prefix, int maskLength, ILispDAO dao, boolean iterate) {
317         if (MaskUtil.isMaskable(LispAFIConvertor.toAFI(prefix))) {
318             return getPasswordForMaskable(prefix, maskLength, dao, iterate);
319         } else if (prefix.getAddress() instanceof LcafSourceDest) {
320             ILispDAO srcDstDao = getSrcDstInnerDao(prefix, maskLength, dao);
321             if (srcDstDao != null) {
322                 return getPasswordForMaskable(LispAFIConvertor.toContainer(getSrcForLcafSrcDst(prefix)),
323                         getSrcMaskForLcafSrcDst(prefix), srcDstDao, iterate);
324             }
325             return null;
326         } else {
327             IMappingKey key = MappingKeyUtil.generateMappingKey(prefix, maskLength);
328             Object password = dao.getSpecific(key, DAOSubKeys.PASSWORD_SUBKEY.toString());
329             if (password != null && password instanceof String) {
330                 return (String) password;
331             } else {
332                 LOG.warn("Failed to find password!");
333                 return null;
334             }
335         }
336     }
337
338     @SuppressWarnings("unchecked")
339     public static Set<SubscriberRLOC> getSubscribers(LispAddressContainer prefix, int maskLength, ILispDAO dao) {
340         Object subscribers;
341         if (prefix.getAddress() instanceof LcafSourceDest) {
342             IMappingKey srcKey = MappingKeyUtil.generateMappingKey(getSrcForLcafSrcDst(prefix),
343                     getSrcMaskForLcafSrcDst(prefix));
344             ILispDAO srcDstDao = getSrcDstInnerDao(prefix, maskLength, dao);
345             subscribers = srcDstDao.getSpecific(srcKey, DAOSubKeys.SUBSCRIBERS_SUBKEY.toString());
346         } else {
347             IMappingKey key = MappingKeyUtil.generateMappingKey(prefix, maskLength);
348             subscribers = dao.getSpecific(key, DAOSubKeys.SUBSCRIBERS_SUBKEY.toString());
349         }
350
351         if (subscribers != null && subscribers instanceof Set<?>) {
352             return (Set<SubscriberRLOC>) subscribers;
353         }
354         return null;
355     }
356
357     // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over
358     // src. This method returns the DAO
359     // associated to a dst or creates it if it doesn't exist.
360     public static ILispDAO getOrInstantiateSrcDstInnerDao(LispAddressContainer address, int maskLen, ILispDAO dao) {
361         IMappingKey dstKey = MappingKeyUtil.generateMappingKey(getDstForLcafSrcDst(address),
362                 getDstMaskForLcafSrcDst(address));
363         ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, DAOSubKeys.LCAF_SRCDST_SUBKEY.toString());
364         if (srcDstDao == null) {
365             srcDstDao = new HashMapDb();
366             dao.put(dstKey, new MappingEntry<>(DAOSubKeys.LCAF_SRCDST_SUBKEY.toString(), srcDstDao));
367         }
368         return srcDstDao;
369     }
370
371     // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over
372     // src. This method returns the DAO
373     // associated to a dst or null if it doesn't exist.
374     public static ILispDAO getSrcDstInnerDao(LispAddressContainer address, int maskLen, ILispDAO dao) {
375         IMappingKey dstKey = MappingKeyUtil.generateMappingKey(getDstForLcafSrcDst(address),
376                 getDstMaskForLcafSrcDst(address));
377         return (ILispDAO) dao.getSpecific(dstKey, DAOSubKeys.LCAF_SRCDST_SUBKEY.toString());
378     }
379
380     public static boolean saveRlocs(EidToLocatorRecord eidRecord, boolean checkForChanges, ILispDAO dao,
381             boolean shouldIterate, boolean shouldOverwrite) {
382         Map<String, RLOCGroup> rlocGroups = new HashMap<String, RLOCGroup>();
383         if (eidRecord.getLocatorRecord() != null) {
384             for (LocatorRecord locatorRecord : eidRecord.getLocatorRecord()) {
385                 String subkey = getAddressKey(locatorRecord.getLispAddressContainer().getAddress(), shouldOverwrite);
386                 if (!rlocGroups.containsKey(subkey)) {
387                     rlocGroups
388                             .put(subkey,
389                                     new RLOCGroup(eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord
390                                             .isAuthoritative()));
391                 }
392                 rlocGroups.get(subkey).addRecord(locatorRecord);
393             }
394         } else {
395             rlocGroups.put(DAOSubKeys.ADDRESS_SUBKEY.toString(),
396                     new RLOCGroup(eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord.isAuthoritative()));
397         }
398         List<MappingEntry<RLOCGroup>> entries = new ArrayList<>();
399         for (String subkey : rlocGroups.keySet()) {
400             entries.add(new MappingEntry<>(subkey, rlocGroups.get(subkey)));
401         }
402
403         if (eidRecord.getLispAddressContainer().getAddress() instanceof LcafSourceDest) {
404             Entry<IMappingKey, List<RLOCGroup>> oldMapping = null, newMapping = null;
405             LispAFIAddress srcAddr = getSrcForLcafSrcDst(eidRecord.getLispAddressContainer());
406             LispAFIAddress dstAddr = getDstForLcafSrcDst(eidRecord.getLispAddressContainer());
407             short srcMask = getSrcMaskForLcafSrcDst(eidRecord.getLispAddressContainer());
408             short dstMask = getDstMaskForLcafSrcDst(eidRecord.getLispAddressContainer());
409
410             if (checkForChanges) {
411                 oldMapping = DAOMappingUtil.getMappingExact(srcAddr, dstAddr, srcMask, dstMask, dao);
412             }
413             IMappingKey dstKey = MappingKeyUtil.generateMappingKey(dstAddr, dstMask);
414             ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, DAOSubKeys.LCAF_SRCDST_SUBKEY.toString());
415             if (srcDstDao == null) {
416                 srcDstDao = new HashMapDb();
417                 dao.put(dstKey, new MappingEntry<>(DAOSubKeys.LCAF_SRCDST_SUBKEY.toString(), srcDstDao));
418             }
419             IMappingKey srcKey = MappingKeyUtil.generateMappingKey(srcAddr, srcMask);
420             srcDstDao.put(srcKey, entries.toArray(new MappingEntry[entries.size()]));
421             if (checkForChanges) {
422                 newMapping = DAOMappingUtil.getMappingExact(srcAddr, dstAddr, srcMask, dstMask, dao);
423                 return (newMapping.getValue() == null) ? oldMapping.getValue() != null : !newMapping.getValue()
424                         .equals(oldMapping.getValue());
425             }
426         } else {
427             List<RLOCGroup> oldLocators = null, newLocators = null;
428             if (checkForChanges) {
429                 oldLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterate);
430             }
431             IMappingKey key = MappingKeyUtil.generateMappingKey(eidRecord.getLispAddressContainer(),
432                     eidRecord.getMaskLength());
433             dao.put(key, entries.toArray(new MappingEntry[entries.size()]));
434             if (checkForChanges) {
435                 newLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterate);
436                 return (newLocators == null) ? oldLocators != null : !newLocators.equals(oldLocators);
437             }
438         }
439         return false;
440     }
441
442     public static String getAddressKey(Address address, boolean shouldOverwrite) {
443         if (address instanceof LcafKeyValue) {
444             LcafKeyValue keyVal = (LcafKeyValue) address;
445             if (keyVal.getLcafKeyValueAddressAddr().getKey().getPrimitiveAddress() instanceof DistinguishedName) {
446                 return ((DistinguishedName) keyVal.getLcafKeyValueAddressAddr().getKey().getPrimitiveAddress())
447                         .getDistinguishedNameAddress().getDistinguishedName();
448             }
449         }
450         if (shouldOverwrite) {
451             return DAOSubKeys.ADDRESS_SUBKEY.toString();
452         } else {
453             return String.valueOf(address.hashCode());
454         }
455     }
456
457     public static void removeAuthenticationKey(LispAddressContainer address, int maskLen, ILispDAO dao) {
458         if (address.getAddress() instanceof LcafSourceDest) {
459             ILispDAO srcDstDao = getSrcDstInnerDao(address, maskLen, dao);
460             if (srcDstDao != null) {
461                 IMappingKey srcKey = MappingKeyUtil.generateMappingKey(getSrcForLcafSrcDst(address),
462                         getSrcMaskForLcafSrcDst(address));
463                 srcDstDao.removeSpecific(srcKey, DAOSubKeys.PASSWORD_SUBKEY.toString());
464             }
465         } else {
466             IMappingKey key = MappingKeyUtil.generateMappingKey(address, maskLen);
467             dao.removeSpecific(key, DAOSubKeys.PASSWORD_SUBKEY.toString());
468         }
469     }
470
471     private static void removeMappingRlocs(Entry<IMappingKey, List<RLOCGroup>> mapping, ILispDAO db,
472             boolean shouldOverwrite) {
473         if (mapping == null || mapping.getValue() == null) {
474             return;
475         }
476         for (RLOCGroup group : mapping.getValue()) {
477             for (LocatorRecord record : group.getRecords()) {
478                 db.removeSpecific(mapping.getKey(),
479                         DAOMappingUtil.getAddressKey(record.getLispAddressContainer().getAddress(), shouldOverwrite));
480             }
481         }
482     }
483
484     public static void removeMapping(LispAddressContainer address, int maskLen, ILispDAO dao, boolean oerwrite) {
485         Entry<IMappingKey, List<RLOCGroup>> mapping;
486         ILispDAO db;
487         if (address.getAddress() instanceof LcafSourceDest) {
488             db = getSrcDstInnerDao(address, maskLen, dao);
489             LispAFIAddress srcAddr = getSrcForLcafSrcDst(address);
490             short srcMask = getSrcMaskForLcafSrcDst(address);
491             mapping = DAOMappingUtil.getMappingForEid(srcAddr, srcMask, db);
492         } else {
493             db = dao;
494             mapping = DAOMappingUtil.getMappingForEid(LispAFIConvertor.toAFI(address), maskLen, db);
495         }
496         removeMappingRlocs(mapping, db, oerwrite);
497     }
498
499     public static void removeSubscribers(LispAddressContainer address, int maskLen, ILispDAO dao,
500             boolean shouldOverwrite) {
501         Entry<IMappingKey, List<RLOCGroup>> mapping;
502         ILispDAO db;
503         if (address.getAddress() instanceof LcafSourceDest) {
504             db = getSrcDstInnerDao(address, maskLen, dao);
505             LispAFIAddress srcAddr = getSrcForLcafSrcDst(address);
506             short srcMask = getSrcMaskForLcafSrcDst(address);
507             mapping = DAOMappingUtil.getMappingForEid(srcAddr, srcMask, db);
508         } else {
509             db = dao;
510             mapping = DAOMappingUtil.getMappingForEid(LispAFIConvertor.toAFI(address), maskLen, db);
511         }
512         db.removeSpecific(mapping.getKey(), DAOSubKeys.SUBSCRIBERS_SUBKEY.toString());
513     }
514
515     public static void removeEntry(LispAddressContainer address, int maskLen, ILispDAO dao, boolean shouldOverwrite) {
516         Entry<IMappingKey, List<RLOCGroup>> mapping;
517         ILispDAO db;
518         if (address.getAddress() instanceof LcafSourceDest) {
519             db = getSrcDstInnerDao(address, maskLen, dao);
520             LispAFIAddress srcAddr = getSrcForLcafSrcDst(address);
521             short srcMask = getSrcMaskForLcafSrcDst(address);
522             mapping = DAOMappingUtil.getMappingForEid(srcAddr, srcMask, db);
523         } else {
524             db = dao;
525             mapping = DAOMappingUtil.getMappingForEid(LispAFIConvertor.toAFI(address), maskLen, db);
526         }
527
528         db.remove(mapping.getKey());
529     }
530
531     public static LispAFIAddress getSrcForLcafSrcDst(LispAddressContainer container) {
532         return LispAFIConvertor.toAFIfromPrimitive(((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr()
533                 .getSrcAddress().getPrimitiveAddress());
534     }
535
536     public static LispAFIAddress getDstForLcafSrcDst(LispAddressContainer container) {
537         return LispAFIConvertor.toAFIfromPrimitive(((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr()
538                 .getDstAddress().getPrimitiveAddress());
539     }
540
541     public static short getSrcMaskForLcafSrcDst(LispAddressContainer container) {
542         return ((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr().getSrcMaskLength();
543     }
544
545     public static short getDstMaskForLcafSrcDst(LispAddressContainer container) {
546         return ((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr().getDstMaskLength();
547     }
548
549 }