Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 16 | package org.onosproject.lisp.ctl.impl; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 17 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 20 | import org.onlab.packet.IpPrefix; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 21 | import org.onosproject.lisp.ctl.impl.map.ExpireMap; |
| 22 | import org.onosproject.lisp.ctl.impl.map.ExpireHashMap; |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.protocols.DefaultLispProxyMapRecord.DefaultMapWithProxyBuilder; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 24 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
| 25 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.protocols.LispProxyMapRecord; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 27 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 28 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 29 | import java.util.List; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 30 | import java.util.Optional; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * A singleton class that stores EID-RLOC mapping information. |
| 34 | */ |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 35 | public final class LispMappingDatabase { |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 36 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 37 | private static final long MINUTE_TO_MS_UNIT = 60 * 1000; |
| 38 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 39 | private ExpireMap<LispEidRecord, LispProxyMapRecord> map = new ExpireHashMap<>(); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Prevents object instantiation from external. |
| 43 | */ |
| 44 | private LispMappingDatabase() { |
| 45 | } |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Obtains a singleton instance. |
| 49 | * |
| 50 | * @return singleton instance |
| 51 | */ |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 52 | public static LispMappingDatabase getInstance() { |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 53 | return SingletonHelper.INSTANCE; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Inserts a new EID-RLOC mapping record. |
| 58 | * |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 59 | * @param eid endpoint identifier |
| 60 | * @param rloc route locator record |
| 61 | * @param proxyMapReply proxy map reply flag |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 62 | */ |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 63 | public void putMapRecord(LispEidRecord eid, LispMapRecord rloc, |
| 64 | boolean proxyMapReply) { |
| 65 | LispProxyMapRecord mapWithProxy = new DefaultMapWithProxyBuilder() |
| 66 | .withMapRecord(rloc) |
| 67 | .withIsProxyMapReply(proxyMapReply) |
| 68 | .build(); |
| 69 | map.put(eid, mapWithProxy, rloc.getRecordTtl() * MINUTE_TO_MS_UNIT); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the results whether a given EidRecord is contained in the map. |
| 74 | * |
| 75 | * @param eid endpoint identifier |
| 76 | * @return the results whether a given EidRecord is contained in the map |
| 77 | */ |
| 78 | public boolean hasEidRecord(LispEidRecord eid) { |
| 79 | return map.containsKey(eid); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Removes an EID-RLOC mapping record with given endpoint identifier. |
| 84 | * |
| 85 | * @param eid endpoint identifier |
| 86 | */ |
| 87 | public void removeMapRecordByEid(LispEidRecord eid) { |
| 88 | map.remove(eid); |
| 89 | } |
| 90 | |
| 91 | /** |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 92 | * Obtains an EID-RLOC mapping record in accordance with the proxy map reply |
| 93 | * flag bit and EID record. |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 94 | * |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 95 | * @param eid endpoint identifier record |
| 96 | * @param proxyMapReply proxy map reply flag |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 97 | * @return an EID-RLOC mapping record |
| 98 | */ |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 99 | public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid, boolean proxyMapReply) { |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 100 | |
| 101 | for (LispEidRecord key : map.keySet()) { |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 102 | if (isInRange(key, eid) && map.get(key) != null |
| 103 | && map.get(key).isProxyMapReply() == proxyMapReply) { |
| 104 | return map.get(key).getMapRecord(); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | return null; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 112 | * Obtains a collection of EID-RLOC mapping record in accordance with the |
| 113 | * proxy map reply flag bit and EID record. |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 114 | * |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 115 | * @param eids endpoint identifier records |
| 116 | * @param proxyMapReply proxy map reply flag |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 117 | * @return a collection of EID-RLOC mapping records |
| 118 | */ |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 119 | public List<LispMapRecord> getMapRecordByEidRecords(List<LispEidRecord> eids, |
| 120 | boolean proxyMapReply) { |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 121 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 122 | eids.forEach(eidRecord -> { |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 123 | LispMapRecord mapRecord = getMapRecordByEidRecord(eidRecord, proxyMapReply); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 124 | if (mapRecord != null) { |
| 125 | mapRecords.add(mapRecord); |
| 126 | } |
| 127 | }); |
| 128 | return ImmutableList.copyOf(mapRecords); |
| 129 | } |
| 130 | |
| 131 | /** |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 132 | * Obtains an EID-RLOC mapping record with given EID address. |
| 133 | * |
| 134 | * @param address endpoint identifier address |
| 135 | * @return an EID-RLOC mapping record |
| 136 | */ |
| 137 | public LispMapRecord getMapRecordByEidAddress(LispAfiAddress address) { |
| 138 | Optional<LispEidRecord> eidRecord = |
| 139 | map.keySet().stream().filter(k -> k.getPrefix().equals(address)).findFirst(); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 140 | return eidRecord.map(lispEidRecord -> |
| 141 | map.get(lispEidRecord).getMapRecord()).orElse(null); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 142 | } |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Generates CIDR style string from EID record. |
| 146 | * |
| 147 | * @param eidRecord EID record |
| 148 | * @return CIDR style string |
| 149 | */ |
| 150 | private String cidrfy(LispEidRecord eidRecord) { |
| 151 | StringBuilder sb = new StringBuilder(); |
| 152 | sb.append(eidRecord.getPrefix().toString()); |
| 153 | sb.append("/"); |
| 154 | sb.append(eidRecord.getMaskLength()); |
| 155 | return sb.toString(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Checks whether the EID record is included in the given EID record. |
| 160 | * |
| 161 | * @param origin the EID record to be compared |
| 162 | * @param compare the EID record to compare |
| 163 | * @return boolean result |
| 164 | */ |
| 165 | private boolean isInRange(LispEidRecord origin, LispEidRecord compare) { |
| 166 | |
| 167 | IpPrefix originIpPrefix = IpPrefix.valueOf(cidrfy(origin)); |
| 168 | IpPrefix compareIpPrefix = IpPrefix.valueOf(cidrfy(compare)); |
| 169 | |
| 170 | return originIpPrefix.contains(compareIpPrefix); |
| 171 | } |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Prevents object instantiation from external. |
| 175 | */ |
| 176 | private static final class SingletonHelper { |
| 177 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
| 178 | private static final LispMappingDatabase INSTANCE = new LispMappingDatabase(); |
| 179 | |
| 180 | private SingletonHelper() { |
| 181 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 182 | } |
| 183 | } |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 184 | } |