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 | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 20 | import org.onosproject.lisp.ctl.impl.map.ExpireMap; |
| 21 | import org.onosproject.lisp.ctl.impl.map.ExpireHashMap; |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 22 | import org.onosproject.lisp.msg.protocols.DefaultLispProxyMapRecord.DefaultMapWithProxyBuilder; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
| 24 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 25 | import org.onosproject.lisp.msg.protocols.LispProxyMapRecord; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
Jian Li | a5f0c58 | 2017-05-13 03:52:36 +0900 | [diff] [blame] | 27 | import org.slf4j.Logger; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 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 | |
Jian Li | a5f0c58 | 2017-05-13 03:52:36 +0900 | [diff] [blame] | 32 | import static org.slf4j.LoggerFactory.getLogger; |
Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 33 | import static org.onosproject.lisp.ctl.impl.util.LispMapUtil.isInRange; |
Jian Li | a5f0c58 | 2017-05-13 03:52:36 +0900 | [diff] [blame] | 34 | |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 35 | /** |
| 36 | * A singleton class that stores EID-RLOC mapping information. |
| 37 | */ |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 38 | public final class LispMappingDatabase { |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 39 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 40 | private static final long MINUTE_TO_MS_UNIT = 60 * 1000; |
| 41 | |
Jian Li | a5f0c58 | 2017-05-13 03:52:36 +0900 | [diff] [blame] | 42 | private static final Logger log = getLogger(LispMappingDatabase.class); |
| 43 | |
| 44 | private final ExpireMap<LispEidRecord, LispProxyMapRecord> map = new ExpireHashMap<>(); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Prevents object instantiation from external. |
| 48 | */ |
| 49 | private LispMappingDatabase() { |
| 50 | } |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Obtains a singleton instance. |
| 54 | * |
| 55 | * @return singleton instance |
| 56 | */ |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 57 | public static LispMappingDatabase getInstance() { |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 58 | return SingletonHelper.INSTANCE; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Inserts a new EID-RLOC mapping record. |
| 63 | * |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 64 | * @param eid endpoint identifier |
| 65 | * @param rloc route locator record |
| 66 | * @param proxyMapReply proxy map reply flag |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 67 | */ |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 68 | public void putMapRecord(LispEidRecord eid, LispMapRecord rloc, |
| 69 | boolean proxyMapReply) { |
| 70 | LispProxyMapRecord mapWithProxy = new DefaultMapWithProxyBuilder() |
| 71 | .withMapRecord(rloc) |
| 72 | .withIsProxyMapReply(proxyMapReply) |
| 73 | .build(); |
| 74 | map.put(eid, mapWithProxy, rloc.getRecordTtl() * MINUTE_TO_MS_UNIT); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the results whether a given EidRecord is contained in the map. |
| 79 | * |
| 80 | * @param eid endpoint identifier |
| 81 | * @return the results whether a given EidRecord is contained in the map |
| 82 | */ |
| 83 | public boolean hasEidRecord(LispEidRecord eid) { |
| 84 | return map.containsKey(eid); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Removes an EID-RLOC mapping record with given endpoint identifier. |
| 89 | * |
| 90 | * @param eid endpoint identifier |
| 91 | */ |
| 92 | public void removeMapRecordByEid(LispEidRecord eid) { |
| 93 | map.remove(eid); |
| 94 | } |
| 95 | |
| 96 | /** |
Jian Li | e530690 | 2016-12-09 00:09:46 +0900 | [diff] [blame] | 97 | * Obtains all of the EID-RLOC mapping records. |
| 98 | * |
| 99 | * @return all of the EID-RLOC mapping records |
| 100 | */ |
| 101 | public List<LispMapRecord> getAllMapRecords() { |
| 102 | |
| 103 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 104 | |
| 105 | map.values().forEach(value -> mapRecords.add(value.getMapRecord())); |
| 106 | |
| 107 | return mapRecords; |
| 108 | } |
| 109 | |
| 110 | /** |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 111 | * Obtains an EID-RLOC mapping record in accordance with the proxy map reply |
| 112 | * flag bit and EID record. |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 113 | * |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 114 | * @param eid endpoint identifier record |
| 115 | * @param proxyMapReply proxy map reply flag |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 116 | * @return an EID-RLOC mapping record |
| 117 | */ |
Jian Li | a5f0c58 | 2017-05-13 03:52:36 +0900 | [diff] [blame] | 118 | public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid, |
| 119 | boolean proxyMapReply) { |
| 120 | Optional<LispEidRecord> filteredEidRecord = map.keySet().parallelStream() |
| 121 | .filter(k -> isInRange(k, eid)).findAny(); |
| 122 | if (filteredEidRecord.isPresent()) { |
| 123 | LispProxyMapRecord record = map.get(filteredEidRecord.get()); |
| 124 | if (record != null && record.isProxyMapReply() == proxyMapReply) { |
| 125 | return record.getMapRecord(); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | return null; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 133 | * Obtains a collection of EID-RLOC mapping record in accordance with the |
| 134 | * proxy map reply flag bit and EID record. |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 135 | * |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 136 | * @param eids endpoint identifier records |
| 137 | * @param proxyMapReply proxy map reply flag |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 138 | * @return a collection of EID-RLOC mapping records |
| 139 | */ |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 140 | public List<LispMapRecord> getMapRecordByEidRecords(List<LispEidRecord> eids, |
| 141 | boolean proxyMapReply) { |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 142 | List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 143 | eids.forEach(eidRecord -> { |
Jian Li | a5f0c58 | 2017-05-13 03:52:36 +0900 | [diff] [blame] | 144 | LispMapRecord mapRecord = |
| 145 | getMapRecordByEidRecord(eidRecord, proxyMapReply); |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 146 | if (mapRecord != null) { |
| 147 | mapRecords.add(mapRecord); |
| 148 | } |
| 149 | }); |
| 150 | return ImmutableList.copyOf(mapRecords); |
| 151 | } |
| 152 | |
| 153 | /** |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 154 | * Obtains an EID-RLOC mapping record with given EID address. |
| 155 | * |
| 156 | * @param address endpoint identifier address |
| 157 | * @return an EID-RLOC mapping record |
| 158 | */ |
| 159 | public LispMapRecord getMapRecordByEidAddress(LispAfiAddress address) { |
| 160 | Optional<LispEidRecord> eidRecord = |
Jian Li | a5f0c58 | 2017-05-13 03:52:36 +0900 | [diff] [blame] | 161 | map.keySet().stream().filter(k -> |
| 162 | k.getPrefix().equals(address)).findFirst(); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 163 | return eidRecord.map(lispEidRecord -> |
| 164 | map.get(lispEidRecord).getMapRecord()).orElse(null); |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 165 | } |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 166 | |
| 167 | /** |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 168 | * Prevents object instantiation from external. |
| 169 | */ |
| 170 | private static final class SingletonHelper { |
| 171 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
| 172 | private static final LispMappingDatabase INSTANCE = new LispMappingDatabase(); |
| 173 | |
| 174 | private SingletonHelper() { |
| 175 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 176 | } |
| 177 | } |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 178 | } |