56e6b4319db73f2219019934d2179dbc6c656dc9
[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.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16
17 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
18 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
19 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
20 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
21 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
22 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
23 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
24 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.Address;
25
26 public class DAOMappingUtil {
27
28     public static List<MappingServiceRLOCGroup> getLocatorsByEidRecord(EidRecord eid, ILispDAO dao, boolean iterateMask) {
29         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
30         return getLocators(key, dao, iterateMask);
31     }
32
33     public static Object getLocatorsSpecificByEidRecord(EidRecord eid,  ILispDAO dao, String subkey, boolean iterateMask) {
34         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
35         return getLocatorsSpecific(key, dao, subkey, iterateMask);
36     }
37
38     public static List<MappingServiceRLOCGroup> getLocatorsByEidToLocatorRecord(EidToLocatorRecord eid, ILispDAO dao, boolean iterateMask) {
39         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMaskLength());
40         return getLocators(key, dao, iterateMask);
41     }
42
43     public static Object getLocatorsSpecificByEidtoLocatorRecord(EidToLocatorRecord eid,  ILispDAO dao, String subkey, boolean iterateMask) {
44         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMaskLength());
45         return getLocatorsSpecific(key, dao, subkey, iterateMask);
46     }
47
48     public static List<MappingServiceRLOCGroup> getLocators(IMappingServiceKey key, ILispDAO dao, boolean iterateMask) {
49         Map<String, ?> locators = dao.get(key);
50         List<MappingServiceRLOCGroup> result = aggregateLocators(locators);
51         if (iterateMask && result.isEmpty() && MaskUtil.isMaskable(key.getEID().getAddress())) {
52             result = findMaskLocators(dao, key);
53         }
54         return result;
55     }
56
57     private static List<MappingServiceRLOCGroup> aggregateLocators(Map<String, ?> locators) {
58         List<MappingServiceRLOCGroup> result = new ArrayList<MappingServiceRLOCGroup>();
59         if (locators != null) {
60             for (Object value : locators.values()) {
61                 if (value != null && value instanceof MappingServiceRLOCGroup) {
62                     if (!((MappingServiceRLOCGroup) value).getRecords().isEmpty()) {
63                         result.add((MappingServiceRLOCGroup) value);
64                     }
65                 }
66             }
67         }
68         return result;
69     }
70
71     public static Object getLocatorsSpecific(IMappingServiceKey key, ILispDAO dao, String subkey, boolean iterateMask) {
72         Object locators = dao.getSpecific(key, subkey);
73         if (iterateMask && locators == null && MaskUtil.isMaskable(key.getEID().getAddress())) {
74             locators = findMaskLocatorsSpecific(key, dao, subkey);
75         }
76         return locators;
77     }
78
79     private static Object findMaskLocatorsSpecific(IMappingServiceKey key, ILispDAO dao, String subkey) {
80         int mask = key.getMask();
81         while (mask > 0) {
82             key = MappingServiceKeyUtil.generateMappingServiceKey(
83                     new LispAddressContainerBuilder().setAddress(MaskUtil.normalize(key.getEID().getAddress(), mask)).build(), mask);
84             mask--;
85             Object locators = dao.getSpecific(key, subkey);
86             if (locators != null) {
87                 return locators;
88             }
89         }
90         return null;
91     }
92
93     private static List<MappingServiceRLOCGroup> findMaskLocators(ILispDAO dao, IMappingServiceKey key) {
94         int mask = key.getMask();
95         while (mask > 0) {
96             key = MappingServiceKeyUtil.generateMappingServiceKey(
97                     new LispAddressContainerBuilder().setAddress(MaskUtil.normalize(key.getEID().getAddress(), mask)).build(), mask);
98             mask--;
99             Map<String, ?> locators = dao.get(key);
100             if (locators != null) {
101                 List<MappingServiceRLOCGroup> result = aggregateLocators(locators);
102                 if (result != null && !result.isEmpty()) {
103                     return result;
104                 }
105             }
106         }
107         return null;
108     }
109
110     public static Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> getMappingForEidRecord(EidRecord eid, ILispDAO dao) {
111         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
112         if (MaskUtil.isMaskable(key.getEID().getAddress())) {
113             int mask = eid.getMask();
114             while (mask > 0) {
115                 Address eidAddress = MaskUtil.normalize(key.getEID().getAddress(), mask);
116                 key = MappingServiceKeyUtil.generateMappingServiceKey(
117                         new LispAddressContainerBuilder().setAddress(eidAddress).build(), mask);
118                 mask--;
119                 Map<String, ?> locators = dao.get(key);
120                 if (locators != null) {
121                     List<MappingServiceRLOCGroup> locatorsList = aggregateLocators(locators);
122                     if (locatorsList != null && !locatorsList.isEmpty()) {
123                         Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> result = new AbstractMap.SimpleImmutableEntry<>(key, locatorsList);
124                         return result;
125                     }
126                 }
127             }
128             // empty mapping
129             key = MappingServiceKeyUtil.generateMappingServiceKey(eid.getLispAddressContainer(), eid.getMask());
130             return new AbstractMap.SimpleImmutableEntry<>(key, null);
131         } else {
132             Map<String, ?> locators = dao.get(key);
133             List<MappingServiceRLOCGroup> aggLocators = aggregateLocators(locators);
134             return new AbstractMap.SimpleImmutableEntry<>(key, aggLocators);
135         }
136     }
137 }