blob: aff24a2d48462e1c966560232f0653ffc7e7ebe0 [file] [log] [blame]
Jian Li24f6cc02016-11-01 16:38:40 +09001/*
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 Li5e505c62016-12-05 02:44:24 +090016package org.onosproject.lisp.ctl.impl;
Jian Li24f6cc02016-11-01 16:38:40 +090017
Jian Li1118c122016-11-01 21:58:15 +090018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
Jian Li5e505c62016-12-05 02:44:24 +090020import org.onosproject.lisp.ctl.impl.map.ExpireMap;
21import org.onosproject.lisp.ctl.impl.map.ExpireHashMap;
Jian Licdbc0872016-12-05 17:23:53 +090022import org.onosproject.lisp.msg.protocols.DefaultLispProxyMapRecord.DefaultMapWithProxyBuilder;
Jian Li24f6cc02016-11-01 16:38:40 +090023import org.onosproject.lisp.msg.protocols.LispEidRecord;
24import org.onosproject.lisp.msg.protocols.LispMapRecord;
Jian Licdbc0872016-12-05 17:23:53 +090025import org.onosproject.lisp.msg.protocols.LispProxyMapRecord;
Jian Li24f6cc02016-11-01 16:38:40 +090026import org.onosproject.lisp.msg.types.LispAfiAddress;
Jian Lia5f0c582017-05-13 03:52:36 +090027import org.slf4j.Logger;
Jian Li24f6cc02016-11-01 16:38:40 +090028
Jian Li1118c122016-11-01 21:58:15 +090029import java.util.List;
Jian Li24f6cc02016-11-01 16:38:40 +090030import java.util.Optional;
Jian Li24f6cc02016-11-01 16:38:40 +090031
Jian Lia5f0c582017-05-13 03:52:36 +090032import static org.slf4j.LoggerFactory.getLogger;
Jian Lib86d8ad2017-05-03 02:53:44 +090033import static org.onosproject.lisp.ctl.impl.util.LispMapUtil.isInRange;
Jian Lia5f0c582017-05-13 03:52:36 +090034
Jian Li24f6cc02016-11-01 16:38:40 +090035/**
36 * A singleton class that stores EID-RLOC mapping information.
37 */
Jian Li29986d82016-12-01 03:25:12 +090038public final class LispMappingDatabase {
Jian Li24f6cc02016-11-01 16:38:40 +090039
Jian Li29986d82016-12-01 03:25:12 +090040 private static final long MINUTE_TO_MS_UNIT = 60 * 1000;
41
Jian Lia5f0c582017-05-13 03:52:36 +090042 private static final Logger log = getLogger(LispMappingDatabase.class);
43
44 private final ExpireMap<LispEidRecord, LispProxyMapRecord> map = new ExpireHashMap<>();
Jian Li29986d82016-12-01 03:25:12 +090045
46 /**
47 * Prevents object instantiation from external.
48 */
49 private LispMappingDatabase() {
50 }
Jian Li24f6cc02016-11-01 16:38:40 +090051
52 /**
53 * Obtains a singleton instance.
54 *
55 * @return singleton instance
56 */
Jian Li29986d82016-12-01 03:25:12 +090057 public static LispMappingDatabase getInstance() {
Jian Li24f6cc02016-11-01 16:38:40 +090058 return SingletonHelper.INSTANCE;
59 }
60
61 /**
62 * Inserts a new EID-RLOC mapping record.
63 *
Jian Licdbc0872016-12-05 17:23:53 +090064 * @param eid endpoint identifier
65 * @param rloc route locator record
66 * @param proxyMapReply proxy map reply flag
Jian Li24f6cc02016-11-01 16:38:40 +090067 */
Jian Licdbc0872016-12-05 17:23:53 +090068 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 Li29986d82016-12-01 03:25:12 +090075 }
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 Li24f6cc02016-11-01 16:38:40 +090085 }
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 Lie5306902016-12-09 00:09:46 +090097 * 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 Licdbc0872016-12-05 17:23:53 +0900111 * Obtains an EID-RLOC mapping record in accordance with the proxy map reply
112 * flag bit and EID record.
Jian Li24f6cc02016-11-01 16:38:40 +0900113 *
Jian Licdbc0872016-12-05 17:23:53 +0900114 * @param eid endpoint identifier record
115 * @param proxyMapReply proxy map reply flag
Jian Li24f6cc02016-11-01 16:38:40 +0900116 * @return an EID-RLOC mapping record
117 */
Jian Lia5f0c582017-05-13 03:52:36 +0900118 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 Licdd276b2016-11-15 15:53:21 +0900126 }
127 }
128
129 return null;
Jian Li24f6cc02016-11-01 16:38:40 +0900130 }
131
132 /**
Jian Licdbc0872016-12-05 17:23:53 +0900133 * Obtains a collection of EID-RLOC mapping record in accordance with the
134 * proxy map reply flag bit and EID record.
Jian Li1118c122016-11-01 21:58:15 +0900135 *
Jian Licdbc0872016-12-05 17:23:53 +0900136 * @param eids endpoint identifier records
137 * @param proxyMapReply proxy map reply flag
Jian Li1118c122016-11-01 21:58:15 +0900138 * @return a collection of EID-RLOC mapping records
139 */
Jian Licdbc0872016-12-05 17:23:53 +0900140 public List<LispMapRecord> getMapRecordByEidRecords(List<LispEidRecord> eids,
141 boolean proxyMapReply) {
Jian Li1118c122016-11-01 21:58:15 +0900142 List<LispMapRecord> mapRecords = Lists.newArrayList();
143 eids.forEach(eidRecord -> {
Jian Lia5f0c582017-05-13 03:52:36 +0900144 LispMapRecord mapRecord =
145 getMapRecordByEidRecord(eidRecord, proxyMapReply);
Jian Li1118c122016-11-01 21:58:15 +0900146 if (mapRecord != null) {
147 mapRecords.add(mapRecord);
148 }
149 });
150 return ImmutableList.copyOf(mapRecords);
151 }
152
153 /**
Jian Li24f6cc02016-11-01 16:38:40 +0900154 * 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 Lia5f0c582017-05-13 03:52:36 +0900161 map.keySet().stream().filter(k ->
162 k.getPrefix().equals(address)).findFirst();
Jian Licdbc0872016-12-05 17:23:53 +0900163 return eidRecord.map(lispEidRecord ->
164 map.get(lispEidRecord).getMapRecord()).orElse(null);
Jian Li24f6cc02016-11-01 16:38:40 +0900165 }
Jian Licdd276b2016-11-15 15:53:21 +0900166
167 /**
Jian Li29986d82016-12-01 03:25:12 +0900168 * 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 Li24f6cc02016-11-01 16:38:40 +0900178}