Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.lisp.ctl; |
| 17 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.Lists; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
| 22 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
| 23 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 24 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame^] | 25 | import java.util.List; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 26 | import java.util.Optional; |
| 27 | import java.util.concurrent.ConcurrentMap; |
| 28 | |
| 29 | /** |
| 30 | * A singleton class that stores EID-RLOC mapping information. |
| 31 | */ |
| 32 | public 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 Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame^] | 75 | * 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 Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 92 | * 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 | } |