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; |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame^] | 21 | import org.onlab.packet.IpPrefix; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 22 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
| 23 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
| 24 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 25 | |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 26 | import java.util.List; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 27 | import java.util.Optional; |
| 28 | import java.util.concurrent.ConcurrentMap; |
| 29 | |
| 30 | /** |
| 31 | * A singleton class that stores EID-RLOC mapping information. |
| 32 | */ |
| 33 | public 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 Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame^] | 72 | |
| 73 | for (LispEidRecord key : map.keySet()) { |
| 74 | if (isInRange(key, eid)) { |
| 75 | return map.get(key); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return null; |
Jian Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Jian Li | 1118c12 | 2016-11-01 21:58:15 +0900 | [diff] [blame] | 83 | * 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 Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 100 | * 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 Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame^] | 124 | |
| 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 Li | 24f6cc0 | 2016-11-01 16:38:40 +0900 | [diff] [blame] | 153 | } |