blob: d85ff97542eb3e4671f2e62c91a9ef45f9040d0f [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
18import com.google.common.collect.Maps;
19import org.onosproject.lisp.msg.protocols.LispEidRecord;
20import org.onosproject.lisp.msg.protocols.LispMapRecord;
21import org.onosproject.lisp.msg.types.LispAfiAddress;
22
23import java.util.Optional;
24import java.util.concurrent.ConcurrentMap;
25
26/**
27 * A singleton class that stores EID-RLOC mapping information.
28 */
29public final class LispEidRlocMap {
30
31 private ConcurrentMap<LispEidRecord, LispMapRecord> map = Maps.newConcurrentMap();
32
33 /**
34 * Obtains a singleton instance.
35 *
36 * @return singleton instance
37 */
38 public static LispEidRlocMap getInstance() {
39 return SingletonHelper.INSTANCE;
40 }
41
42 /**
43 * Inserts a new EID-RLOC mapping record.
44 *
45 * @param eid endpoint identifier
46 * @param rloc route locator record
47 */
48 public void insertMapRecord(LispEidRecord eid, LispMapRecord rloc) {
49 map.putIfAbsent(eid, rloc);
50 }
51
52 /**
53 * Removes an EID-RLOC mapping record with given endpoint identifier.
54 *
55 * @param eid endpoint identifier
56 */
57 public void removeMapRecordByEid(LispEidRecord eid) {
58 map.remove(eid);
59 }
60
61 /**
62 * Obtains an EID-RLOC mapping record with given EID record.
63 *
64 * @param eid endpoint identifier record
65 * @return an EID-RLOC mapping record
66 */
67 public LispMapRecord getMapRecordByEidRecord(LispEidRecord eid) {
68 return map.get(eid);
69 }
70
71 /**
72 * Obtains an EID-RLOC mapping record with given EID address.
73 *
74 * @param address endpoint identifier address
75 * @return an EID-RLOC mapping record
76 */
77 public LispMapRecord getMapRecordByEidAddress(LispAfiAddress address) {
78 Optional<LispEidRecord> eidRecord =
79 map.keySet().stream().filter(k -> k.getPrefix().equals(address)).findFirst();
80 if (eidRecord.isPresent()) {
81 return map.get(eidRecord);
82 }
83
84 return null;
85 }
86
87 /**
88 * Prevents object instantiation from external.
89 */
90 private LispEidRlocMap() {
91 }
92
93 private static class SingletonHelper {
94 private static final LispEidRlocMap INSTANCE = new LispEidRlocMap();
95 }
96}