Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
| 16 | package org.onosproject.lisp.ctl.impl; |
| 17 | |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onosproject.lisp.ctl.impl.tree.IpConcurrentRadixTree; |
| 22 | import org.onosproject.lisp.msg.protocols.DefaultLispProxyMapRecord.DefaultMapWithProxyBuilder; |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
| 24 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 25 | import org.onosproject.lisp.msg.protocols.LispProxyMapRecord; |
| 26 | import org.slf4j.Logger; |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 27 | |
| 28 | import java.util.List; |
| 29 | |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 30 | import static org.onosproject.lisp.ctl.impl.util.LispMapUtil.cidrfy; |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 33 | /** |
| 34 | * A radix tree based LISP mapping database. |
| 35 | */ |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 36 | public final class LispRadixTreeDatabase implements LispMappingDatabase { |
| 37 | |
| 38 | private static final Logger log = getLogger(LispRadixTreeDatabase.class); |
| 39 | |
| 40 | private final IpConcurrentRadixTree<LispProxyMapRecord> radixTree = |
| 41 | new IpConcurrentRadixTree<>(); |
| 42 | |
| 43 | /** |
| 44 | * Prevents object instantiation from external. |
| 45 | */ |
| 46 | private LispRadixTreeDatabase() { |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Obtains a singleton instance. |
| 51 | * |
| 52 | * @return singleton instance |
| 53 | */ |
| 54 | public static LispRadixTreeDatabase getInstance() { |
| 55 | return SingletonHelper.INSTANCE; |
| 56 | } |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 57 | |
| 58 | @Override |
| 59 | public void putMapRecord(LispEidRecord eid, LispMapRecord rloc, |
| 60 | boolean proxyMapReply) { |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 61 | final LispProxyMapRecord mapWithProxy = new DefaultMapWithProxyBuilder() |
| 62 | .withMapRecord(rloc) |
| 63 | .withIsProxyMapReply(proxyMapReply) |
| 64 | .build(); |
| 65 | radixTree.put(getIpPrefix(eid), mapWithProxy); |
| 66 | log.debug("Inserted a new map record for key {}", eid.toString()); |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void removeMapRecordByEid(LispEidRecord eid) { |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 71 | if (radixTree.remove(getIpPrefix(eid))) { |
| 72 | log.debug("Removed a map record with key {}", eid.toString()); |
| 73 | } |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void removeAllMapRecords() { |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 78 | radixTree.clear(); |
| 79 | log.debug("Clear all map records"); |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid, |
| 84 | boolean proxyMapReply) { |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 85 | final LispProxyMapRecord record = |
| 86 | radixTree.getValueForClosestParentAddress(getIpPrefix(eid)); |
| 87 | if (record != null && record.isProxyMapReply() == proxyMapReply) { |
| 88 | return record.getMapRecord(); |
| 89 | } |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 90 | return null; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public List<LispMapRecord> getMapRecordByEidRecords(List<LispEidRecord> eids, |
| 95 | boolean proxyMapReply) { |
Jian Li | 882e190 | 2017-06-08 04:06:01 +0900 | [diff] [blame^] | 96 | final List<LispMapRecord> mapRecords = Lists.newArrayList(); |
| 97 | eids.parallelStream().forEach(eidRecord -> { |
| 98 | final LispMapRecord mapRecord = |
| 99 | getMapRecordByEidRecord(eidRecord, proxyMapReply); |
| 100 | if (mapRecord != null) { |
| 101 | mapRecords.add(mapRecord); |
| 102 | } |
| 103 | }); |
| 104 | return ImmutableList.copyOf(mapRecords); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Prevents object instantiation from external. |
| 109 | */ |
| 110 | private static final class SingletonHelper { |
| 111 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
| 112 | private static final LispRadixTreeDatabase INSTANCE = new LispRadixTreeDatabase(); |
| 113 | |
| 114 | private SingletonHelper() { |
| 115 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Obtains the IP prefix from LISP EID record. |
| 121 | * |
| 122 | * @param eidRecord LISP EID record |
| 123 | * @return IP prefix object |
| 124 | */ |
| 125 | private IpPrefix getIpPrefix(LispEidRecord eidRecord) { |
| 126 | return IpPrefix.valueOf(cidrfy(eidRecord)); |
Jian Li | 2dbd8a2 | 2017-06-08 02:11:25 +0900 | [diff] [blame] | 127 | } |
| 128 | } |