Carmelo Cascone | da15af8 | 2019-12-09 22:36:48 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.pipelines.fabric.impl.behaviour.bng; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.base.Objects; |
| 21 | import org.onlab.packet.MacAddress; |
| 22 | import org.onlab.packet.VlanId; |
| 23 | import org.onosproject.net.behaviour.BngProgrammable; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * An allocator of line IDs from a fixed range/pool. Used to map attachments to |
| 29 | * IDs for counters and other indirect P4 resources. |
| 30 | * <p> |
| 31 | * An implementation of this interface should use the {@link Handle} class to |
| 32 | * uniquely identify attachments and determine whether an ID has already been |
| 33 | * allocated or not. |
| 34 | */ |
| 35 | public interface FabricBngLineIdAllocator { |
| 36 | |
| 37 | /** |
| 38 | * Returns a new ID for the given attachment. The implementation is expected |
| 39 | * to be idempotent, i.e., if an ID was previously allocated, then no new |
| 40 | * IDs should be allocated but the previous one should be returned. |
| 41 | * |
| 42 | * @param attachment the attachment instance |
| 43 | * @return the ID |
| 44 | * @throws IdExhaustedException if all IDs are currently allocated. |
| 45 | */ |
| 46 | long allocate(BngProgrammable.Attachment attachment) throws IdExhaustedException; |
| 47 | |
| 48 | /** |
| 49 | * Releases any ID previously allocated for the given attachment. If one was |
| 50 | * not allocated, calling this method should be a no-op. |
| 51 | * |
| 52 | * @param attachment the attachment instance |
| 53 | */ |
| 54 | void release(BngProgrammable.Attachment attachment); |
| 55 | |
| 56 | /** |
| 57 | * Releases the given ID, if allocated, otherwise calling this method should |
| 58 | * be a no-op. |
| 59 | * |
| 60 | * @param id the ID to release |
| 61 | */ |
| 62 | void release(long id); |
| 63 | |
| 64 | /** |
| 65 | * Returns the maximum number of IDs that can be allocated, independently of |
| 66 | * the current state. |
| 67 | * |
| 68 | * @return maximum number of IDs |
| 69 | */ |
| 70 | long size(); |
| 71 | |
| 72 | /** |
| 73 | * Returns the number of currently available IDs that can be allocated for |
| 74 | * new attachments. |
| 75 | * |
| 76 | * @return free ID count |
| 77 | */ |
| 78 | long freeCount(); |
| 79 | |
| 80 | /** |
| 81 | * Returns the number of currently allocated IDs. |
| 82 | * |
| 83 | * @return allocated ID count |
| 84 | */ |
| 85 | long allocatedCount(); |
| 86 | |
| 87 | /** |
| 88 | * An identifier of an attachment in the scope of the same instance of a |
| 89 | * {@link FabricBngLineIdAllocator}. |
| 90 | */ |
| 91 | class Handle { |
| 92 | |
| 93 | private final VlanId stag; |
| 94 | private final VlanId ctag; |
| 95 | private final MacAddress macAddress; |
| 96 | |
| 97 | public Handle(BngProgrammable.Attachment attachment) { |
| 98 | this.stag = checkNotNull(attachment.sTag()); |
| 99 | this.ctag = checkNotNull(attachment.cTag()); |
| 100 | this.macAddress = checkNotNull(attachment.macAddress()); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean equals(Object o) { |
| 105 | if (this == o) { |
| 106 | return true; |
| 107 | } |
| 108 | if (o == null || getClass() != o.getClass()) { |
| 109 | return false; |
| 110 | } |
| 111 | final Handle that = (Handle) o; |
| 112 | return Objects.equal(stag, that.stag) && |
| 113 | Objects.equal(ctag, that.ctag) && |
| 114 | Objects.equal(macAddress, that.macAddress); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public int hashCode() { |
| 119 | return Objects.hashCode(stag, ctag, macAddress); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public String toString() { |
| 124 | return MoreObjects.toStringHelper(this) |
| 125 | .add("stag", stag) |
| 126 | .add("ctag", ctag) |
| 127 | .add("macAddress", macAddress) |
| 128 | .toString(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Signals that no more IDs are currently available, but some have to be |
| 134 | * released before allocating a new one. |
| 135 | */ |
| 136 | class IdExhaustedException extends Exception { |
| 137 | } |
| 138 | } |