Jian Li | 49f525f | 2017-06-07 02:17:51 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.impl.tree; |
| 17 | |
| 18 | import org.onlab.packet.IpAddress; |
| 19 | import org.onlab.packet.IpAddress.Version; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | |
| 22 | import java.util.List; |
| 23 | |
| 24 | /** |
| 25 | * A radix tree that stores IP address as a key. |
| 26 | */ |
| 27 | public interface IpRadixTree<V> { |
| 28 | |
| 29 | /** |
| 30 | * Associates the specified value with the specified IP address in this |
| 31 | * radix tree. If the radix tree previously contained a mapping for the |
| 32 | * address, the old value is replaced by the specified value. |
| 33 | * |
| 34 | * @param prefix prefix with which the specified value is to be associated |
| 35 | * @param value value to be associated with the specified IP address |
| 36 | * @return The previous value associated with the key, |
| 37 | * if there was one, otherwise null |
| 38 | */ |
| 39 | V put(IpPrefix prefix, V value); |
| 40 | |
| 41 | /** |
| 42 | * If a value is not already associated with the given address in the radix |
| 43 | * tree, associates the given value with the address; otherwise if an |
| 44 | * existing value is already associated, returns the existing value and |
| 45 | * does not overwrite it. |
| 46 | * |
| 47 | * This operation is performed atomically. |
| 48 | * |
| 49 | * @param prefix prefix with which the specified value is to be associated |
| 50 | * @param value value to be associated with the specified IP address |
| 51 | * @return The existing value associated with the address, if there was one; |
| 52 | * otherwise null in which case the new value was successfully associated |
| 53 | */ |
| 54 | V putIfAbsent(IpPrefix prefix, V value); |
| 55 | |
| 56 | /** |
| 57 | * Removes the mapping for a address from this radix tree if it is present |
| 58 | * (optional operation). More formally, if this radix tree contains a |
| 59 | * mapping from address <tt>address</tt> to value <tt>v</tt> such that |
| 60 | * <code>(address==null ? address==null : address.equals(k))</code>, |
| 61 | * that mapping is removed. (The radix tree can contain at most one |
| 62 | * such mapping.) |
| 63 | * |
| 64 | * @param prefix prefix whose mapping is to be removed from the radix tree |
| 65 | * @return True if a value was removed (and therefore was associated with |
| 66 | * the address), false if no value was associated/removed |
| 67 | */ |
| 68 | boolean remove(IpPrefix prefix); |
| 69 | |
| 70 | /** |
| 71 | * Returns the value associated with the given address (exact match), |
| 72 | * or returns null if no such value is associated with the address. |
| 73 | * |
| 74 | * @param prefix The prefix with which a sought value might be associated |
| 75 | * @return A value associated with the given address (exact match), or null |
| 76 | * if no value was associated with the address |
| 77 | */ |
| 78 | V getValueForExactAddress(IpPrefix prefix); |
| 79 | |
| 80 | /** |
| 81 | * Returns the value associated with the closest parent address, |
| 82 | * or returns null if no such value is associated with the address. |
| 83 | * This method simply tries the exact match at the first time, if there is |
| 84 | * no associated value, it tries the exact match with the parent IP prefix. |
| 85 | * |
| 86 | * @param prefix The prefix with which a sought value might be associated |
| 87 | * @return A value associated with the closest parent address, or |
| 88 | * null if no value was associated with the address |
| 89 | */ |
| 90 | V getValueForClosestParentAddress(IpPrefix prefix); |
| 91 | |
| 92 | /** |
| 93 | * Returns a lazy collection which returns the set of addresses in the radix |
| 94 | * tree which start with the given prefix. |
| 95 | * |
| 96 | * This is <i>inclusive</i> - if the given prefix is an exact match for a |
| 97 | * address in the tree, that address is also returned. |
| 98 | * |
| 99 | * @param prefix A prefix of sought addresses in the tree |
| 100 | * @return A set of addresses in the tree which start with the given prefix |
| 101 | */ |
| 102 | List<IpAddress> getAddressesStartingWith(IpPrefix prefix); |
| 103 | |
| 104 | /** |
| 105 | * Returns a lazy collection which returns the set of values associated with |
| 106 | * addresses in the radix tree which start with the given prefix. |
| 107 | * |
| 108 | * This is <i>inclusive</i> - if the given prefix is an exact match for a |
| 109 | * address in the tree, the value associated with that address is |
| 110 | * also returned. |
| 111 | * |
| 112 | * Note that although the same value might originally have been associated |
| 113 | * with multiple addresses, the set returned does not contain duplicates |
| 114 | * (as determined by the value objects' implementation of |
| 115 | * {@link Object#equals(Object)}). |
| 116 | * |
| 117 | * @param prefix A prefix of addresses in the tree for which associated |
| 118 | * values are sought |
| 119 | * @return The set of values associated with addresses in the tree which |
| 120 | * start with the given prefix |
| 121 | */ |
| 122 | List<V> getValuesForAddressesStartingWith(IpPrefix prefix); |
| 123 | |
| 124 | /** |
| 125 | * Counts the number of addresses/values stored in the radix tree. |
| 126 | * |
| 127 | * In the current implementation, <b>this is an expensive operation</b>, |
| 128 | * having O(n) time complexity. |
| 129 | * |
| 130 | * @param version The version of IP address |
| 131 | * @return The number of addresses/values stored in the radix tree |
| 132 | */ |
| 133 | int size(Version version); |
| 134 | |
| 135 | /** |
| 136 | * Removes all of the mappings from this radix tree (optional operation). |
| 137 | * The radix tree will be empty after this call returns. |
| 138 | */ |
| 139 | void clear(); |
| 140 | } |