blob: 0826d24b067bf881bc05b0fdd02ce04b46b20644 [file] [log] [blame]
Jian Li2dbd8a22017-06-08 02:11:25 +09001/*
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 */
16package org.onosproject.lisp.ctl.impl;
17
Jian Li882e1902017-06-08 04:06:01 +090018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
20import org.onlab.packet.IpPrefix;
21import org.onosproject.lisp.ctl.impl.tree.IpConcurrentRadixTree;
22import org.onosproject.lisp.msg.protocols.DefaultLispProxyMapRecord.DefaultMapWithProxyBuilder;
Jian Li2dbd8a22017-06-08 02:11:25 +090023import org.onosproject.lisp.msg.protocols.LispEidRecord;
24import org.onosproject.lisp.msg.protocols.LispMapRecord;
Jian Li882e1902017-06-08 04:06:01 +090025import org.onosproject.lisp.msg.protocols.LispProxyMapRecord;
26import org.slf4j.Logger;
Jian Li2dbd8a22017-06-08 02:11:25 +090027
28import java.util.List;
29
Jian Li882e1902017-06-08 04:06:01 +090030import static org.onosproject.lisp.ctl.impl.util.LispMapUtil.cidrfy;
31import static org.slf4j.LoggerFactory.getLogger;
32
Jian Li2dbd8a22017-06-08 02:11:25 +090033/**
34 * A radix tree based LISP mapping database.
35 */
Jian Li882e1902017-06-08 04:06:01 +090036public 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 Li2dbd8a22017-06-08 02:11:25 +090057
58 @Override
59 public void putMapRecord(LispEidRecord eid, LispMapRecord rloc,
60 boolean proxyMapReply) {
Jian Li882e1902017-06-08 04:06:01 +090061 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 Li2dbd8a22017-06-08 02:11:25 +090067 }
68
69 @Override
70 public void removeMapRecordByEid(LispEidRecord eid) {
Jian Li882e1902017-06-08 04:06:01 +090071 if (radixTree.remove(getIpPrefix(eid))) {
72 log.debug("Removed a map record with key {}", eid.toString());
73 }
Jian Li2dbd8a22017-06-08 02:11:25 +090074 }
75
76 @Override
77 public void removeAllMapRecords() {
Jian Li882e1902017-06-08 04:06:01 +090078 radixTree.clear();
79 log.debug("Clear all map records");
Jian Li2dbd8a22017-06-08 02:11:25 +090080 }
81
82 @Override
83 public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid,
84 boolean proxyMapReply) {
Jian Li882e1902017-06-08 04:06:01 +090085 final LispProxyMapRecord record =
86 radixTree.getValueForClosestParentAddress(getIpPrefix(eid));
87 if (record != null && record.isProxyMapReply() == proxyMapReply) {
88 return record.getMapRecord();
89 }
Jian Li2dbd8a22017-06-08 02:11:25 +090090 return null;
91 }
92
93 @Override
94 public List<LispMapRecord> getMapRecordByEidRecords(List<LispEidRecord> eids,
95 boolean proxyMapReply) {
Jian Li882e1902017-06-08 04:06:01 +090096 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 Li2dbd8a22017-06-08 02:11:25 +0900127 }
128}