blob: 05933669be82e5cd8477d07610f84995802753d3 [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;
21import org.onosproject.lisp.msg.protocols.LispEidRecord;
22import org.onosproject.lisp.msg.protocols.LispMapRecord;
23import org.onosproject.lisp.msg.types.LispAfiAddress;
24
Jian Li1118c122016-11-01 21:58:15 +090025import java.util.List;
Jian Li24f6cc02016-11-01 16:38:40 +090026import java.util.Optional;
27import java.util.concurrent.ConcurrentMap;
28
29/**
30 * A singleton class that stores EID-RLOC mapping information.
31 */
32public final class LispEidRlocMap {
33
34 private ConcurrentMap<LispEidRecord, LispMapRecord> map = Maps.newConcurrentMap();
35
36 /**
37 * Obtains a singleton instance.
38 *
39 * @return singleton instance
40 */
41 public static LispEidRlocMap getInstance() {
42 return SingletonHelper.INSTANCE;
43 }
44
45 /**
46 * Inserts a new EID-RLOC mapping record.
47 *
48 * @param eid endpoint identifier
49 * @param rloc route locator record
50 */
51 public void insertMapRecord(LispEidRecord eid, LispMapRecord rloc) {
52 map.putIfAbsent(eid, rloc);
53 }
54
55 /**
56 * Removes an EID-RLOC mapping record with given endpoint identifier.
57 *
58 * @param eid endpoint identifier
59 */
60 public void removeMapRecordByEid(LispEidRecord eid) {
61 map.remove(eid);
62 }
63
64 /**
65 * Obtains an EID-RLOC mapping record with given EID record.
66 *
67 * @param eid endpoint identifier record
68 * @return an EID-RLOC mapping record
69 */
70 public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid) {
71 return map.get(eid);
72 }
73
74 /**
Jian Li1118c122016-11-01 21:58:15 +090075 * Obtains a collection of EID-RLOC mapping records with given EID records.
76 *
77 * @param eids endpoint identifier records
78 * @return a collection of EID-RLOC mapping records
79 */
80 public List<LispMapRecord> getMapRecordByEidRecords(List<LispEidRecord> eids) {
81 List<LispMapRecord> mapRecords = Lists.newArrayList();
82 eids.forEach(eidRecord -> {
83 LispMapRecord mapRecord = getMapRecordByEidRecord(eidRecord);
84 if (mapRecord != null) {
85 mapRecords.add(mapRecord);
86 }
87 });
88 return ImmutableList.copyOf(mapRecords);
89 }
90
91 /**
Jian Li24f6cc02016-11-01 16:38:40 +090092 * Obtains an EID-RLOC mapping record with given EID address.
93 *
94 * @param address endpoint identifier address
95 * @return an EID-RLOC mapping record
96 */
97 public LispMapRecord getMapRecordByEidAddress(LispAfiAddress address) {
98 Optional<LispEidRecord> eidRecord =
99 map.keySet().stream().filter(k -> k.getPrefix().equals(address)).findFirst();
100 if (eidRecord.isPresent()) {
101 return map.get(eidRecord);
102 }
103
104 return null;
105 }
106
107 /**
108 * Prevents object instantiation from external.
109 */
110 private LispEidRlocMap() {
111 }
112
113 private static class SingletonHelper {
114 private static final LispEidRlocMap INSTANCE = new LispEidRlocMap();
115 }
116}