blob: 81437bd4a21b68787c51b152e95899de727d9c4f [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 */
16package org.onosproject.lisp.ctl;
17
Jian Li1118c122016-11-01 21:58:15 +090018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.Lists;
Jian Li24f6cc02016-11-01 16:38:40 +090020import com.google.common.collect.Maps;
Jian Licdd276b2016-11-15 15:53:21 +090021import org.onlab.packet.IpPrefix;
Jian Li24f6cc02016-11-01 16:38:40 +090022import org.onosproject.lisp.msg.protocols.LispEidRecord;
23import org.onosproject.lisp.msg.protocols.LispMapRecord;
24import org.onosproject.lisp.msg.types.LispAfiAddress;
25
Jian Li1118c122016-11-01 21:58:15 +090026import java.util.List;
Jian Li24f6cc02016-11-01 16:38:40 +090027import java.util.Optional;
28import java.util.concurrent.ConcurrentMap;
29
30/**
31 * A singleton class that stores EID-RLOC mapping information.
32 */
33public final class LispEidRlocMap {
34
35 private ConcurrentMap<LispEidRecord, LispMapRecord> map = Maps.newConcurrentMap();
36
37 /**
38 * Obtains a singleton instance.
39 *
40 * @return singleton instance
41 */
42 public static LispEidRlocMap getInstance() {
43 return SingletonHelper.INSTANCE;
44 }
45
46 /**
47 * Inserts a new EID-RLOC mapping record.
48 *
49 * @param eid endpoint identifier
50 * @param rloc route locator record
51 */
52 public void insertMapRecord(LispEidRecord eid, LispMapRecord rloc) {
53 map.putIfAbsent(eid, rloc);
54 }
55
56 /**
57 * Removes an EID-RLOC mapping record with given endpoint identifier.
58 *
59 * @param eid endpoint identifier
60 */
61 public void removeMapRecordByEid(LispEidRecord eid) {
62 map.remove(eid);
63 }
64
65 /**
66 * Obtains an EID-RLOC mapping record with given EID record.
67 *
68 * @param eid endpoint identifier record
69 * @return an EID-RLOC mapping record
70 */
71 public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid) {
Jian Licdd276b2016-11-15 15:53:21 +090072
73 for (LispEidRecord key : map.keySet()) {
74 if (isInRange(key, eid)) {
75 return map.get(key);
76 }
77 }
78
79 return null;
Jian Li24f6cc02016-11-01 16:38:40 +090080 }
81
82 /**
Jian Li1118c122016-11-01 21:58:15 +090083 * Obtains a collection of EID-RLOC mapping records with given EID records.
84 *
85 * @param eids endpoint identifier records
86 * @return a collection of EID-RLOC mapping records
87 */
88 public List<LispMapRecord> getMapRecordByEidRecords(List<LispEidRecord> eids) {
89 List<LispMapRecord> mapRecords = Lists.newArrayList();
90 eids.forEach(eidRecord -> {
91 LispMapRecord mapRecord = getMapRecordByEidRecord(eidRecord);
92 if (mapRecord != null) {
93 mapRecords.add(mapRecord);
94 }
95 });
96 return ImmutableList.copyOf(mapRecords);
97 }
98
99 /**
Jian Li24f6cc02016-11-01 16:38:40 +0900100 * Obtains an EID-RLOC mapping record with given EID address.
101 *
102 * @param address endpoint identifier address
103 * @return an EID-RLOC mapping record
104 */
105 public LispMapRecord getMapRecordByEidAddress(LispAfiAddress address) {
106 Optional<LispEidRecord> eidRecord =
107 map.keySet().stream().filter(k -> k.getPrefix().equals(address)).findFirst();
108 if (eidRecord.isPresent()) {
109 return map.get(eidRecord);
110 }
111
112 return null;
113 }
114
115 /**
116 * Prevents object instantiation from external.
117 */
118 private LispEidRlocMap() {
119 }
120
121 private static class SingletonHelper {
122 private static final LispEidRlocMap INSTANCE = new LispEidRlocMap();
123 }
Jian Licdd276b2016-11-15 15:53:21 +0900124
125 /**
126 * Generates CIDR style string from EID record.
127 *
128 * @param eidRecord EID record
129 * @return CIDR style string
130 */
131 private String cidrfy(LispEidRecord eidRecord) {
132 StringBuilder sb = new StringBuilder();
133 sb.append(eidRecord.getPrefix().toString());
134 sb.append("/");
135 sb.append(eidRecord.getMaskLength());
136 return sb.toString();
137 }
138
139 /**
140 * Checks whether the EID record is included in the given EID record.
141 *
142 * @param origin the EID record to be compared
143 * @param compare the EID record to compare
144 * @return boolean result
145 */
146 private boolean isInRange(LispEidRecord origin, LispEidRecord compare) {
147
148 IpPrefix originIpPrefix = IpPrefix.valueOf(cidrfy(origin));
149 IpPrefix compareIpPrefix = IpPrefix.valueOf(cidrfy(compare));
150
151 return originIpPrefix.contains(compareIpPrefix);
152 }
Jian Li24f6cc02016-11-01 16:38:40 +0900153}