Only send SMR when there is a change in mapping
[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.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14
15 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
18 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
19 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
20 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
21 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
22
23 public class DAOMappingUtil {
24
25     public static List<MappingServiceRLOCGroup> getLocatorsByEidRecord(EidRecord eid, ILispDAO dao, boolean iterateMask) {
26         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
27         return getLocators(key, dao, iterateMask);
28     }
29
30     public static Object getLocatorsSpecificByEidRecord(EidRecord eid,  ILispDAO dao, String subkey, boolean iterateMask) {
31         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
32         return getLocatorsSpecific(key, dao, subkey, iterateMask);
33     }
34
35     public static List<MappingServiceRLOCGroup> getLocatorsByEidToLocatorRecord(EidToLocatorRecord eid, ILispDAO dao, boolean iterateMask) {
36         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMaskLength());
37         return getLocators(key, dao, iterateMask);
38     }
39
40     public static Object getLocatorsSpecificByEidtoLocatorRecord(EidToLocatorRecord eid,  ILispDAO dao, String subkey, boolean iterateMask) {
41         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMaskLength());
42         return getLocatorsSpecific(key, dao, subkey, iterateMask);
43     }
44
45     public static List<MappingServiceRLOCGroup> getLocators(IMappingServiceKey key, ILispDAO dao, boolean iterateMask) {
46         Map<String, ?> locators = dao.get(key);
47         List<MappingServiceRLOCGroup> result = aggregateLocators(locators);
48         if (iterateMask && result.isEmpty() && MaskUtil.isMaskable(key.getEID().getAddress())) {
49             result = findMaskLocators(dao, key);
50         }
51         return result;
52     }
53
54     private static List<MappingServiceRLOCGroup> aggregateLocators(Map<String, ?> locators) {
55         List<MappingServiceRLOCGroup> result = new ArrayList<MappingServiceRLOCGroup>();
56         if (locators != null) {
57             for (Object value : locators.values()) {
58                 if (value != null && value instanceof MappingServiceRLOCGroup) {
59                     if (!((MappingServiceRLOCGroup) value).getRecords().isEmpty()) {
60                         result.add((MappingServiceRLOCGroup) value);
61                     }
62                 }
63             }
64         }
65         return result;
66     }
67
68     public static Object getLocatorsSpecific(IMappingServiceKey key, ILispDAO dao, String subkey, boolean iterateMask) {
69         Object locators = dao.getSpecific(key, subkey);
70         if (iterateMask && locators == null && MaskUtil.isMaskable(key.getEID().getAddress())) {
71             locators = findMaskLocatorsSpecific(key, dao, subkey);
72         }
73         return locators;
74     }
75
76     private static Object findMaskLocatorsSpecific(IMappingServiceKey key, ILispDAO dao, String subkey) {
77         int mask = key.getMask();
78         while (mask > 0) {
79             key = MappingServiceKeyUtil.generateMappingServiceKey(
80                     new LispAddressContainerBuilder().setAddress(MaskUtil.normalize(key.getEID().getAddress(), mask)).build(), mask);
81             mask--;
82             Object locators = dao.getSpecific(key, subkey);
83             if (locators != null) {
84                 return locators;
85             }
86         }
87         return null;
88     }
89
90     private static List<MappingServiceRLOCGroup> findMaskLocators(ILispDAO dao, IMappingServiceKey key) {
91         int mask = key.getMask();
92         while (mask > 0) {
93             key = MappingServiceKeyUtil.generateMappingServiceKey(
94                     new LispAddressContainerBuilder().setAddress(MaskUtil.normalize(key.getEID().getAddress(), mask)).build(), mask);
95             mask--;
96             Map<String, ?> locators = dao.get(key);
97             if (locators != null) {
98                 List<MappingServiceRLOCGroup> result = aggregateLocators(locators);
99                 if (result != null && !result.isEmpty()) {
100                     return result;
101                 }
102             }
103         }
104         return null;
105     }
106 }