Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Frank Wang | e11a98d | 2016-10-26 17:04:03 +0800 | [diff] [blame] | 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.ovsdb.controller; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.onosproject.net.DefaultAnnotations; |
| 21 | import org.onosproject.net.behaviour.QueueDescription; |
| 22 | import java.util.Map; |
| 23 | import java.util.Objects; |
| 24 | import java.util.Optional; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | import static org.onosproject.ovsdb.controller.OvsdbConstant.BURST; |
| 28 | import static org.onosproject.ovsdb.controller.OvsdbConstant.MAX_RATE; |
| 29 | import static org.onosproject.ovsdb.controller.OvsdbConstant.MIN_RATE; |
| 30 | import static org.onosproject.ovsdb.controller.OvsdbConstant.PRIORITY; |
| 31 | import static org.onosproject.ovsdb.controller.OvsdbConstant.QUEUE_EXTERNAL_ID_KEY; |
| 32 | |
| 33 | /** |
| 34 | * The class representing an OVSDB Queue. |
| 35 | * This class is immutable. |
| 36 | */ |
| 37 | public final class OvsdbQueue { |
| 38 | private final Optional<Long> dscp; |
| 39 | private final Map<String, String> otherConfigs; |
| 40 | private final Map<String, String> externalIds; |
| 41 | |
| 42 | /** |
| 43 | * Creates an OvsdbQueue using the given inputs. |
| 44 | * |
| 45 | * @param dscp the dscp of queue |
| 46 | * @param otherConfigs Key-value pairs for configuring rarely used features |
| 47 | * @param externalIds Key-value pairs for use by external frameworks, rather than by OVS itself |
| 48 | */ |
| 49 | private OvsdbQueue(Optional<Long> dscp, Map<String, String> otherConfigs, |
| 50 | Map<String, String> externalIds) { |
| 51 | this.dscp = dscp; |
| 52 | this.otherConfigs = otherConfigs; |
| 53 | this.externalIds = externalIds; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the dscp of queue. |
| 58 | * |
| 59 | * @return the dscp |
| 60 | */ |
| 61 | public Optional<Long> dscp() { |
| 62 | return dscp; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns other configurations of the queue. |
| 67 | * |
| 68 | * @return map of configurations |
| 69 | */ |
| 70 | public Map<String, String> otherConfigs() { |
| 71 | return otherConfigs; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the optional external ids. |
| 76 | * |
| 77 | * @return the external ids. |
| 78 | */ |
| 79 | public Map<String, String> externalIds() { |
| 80 | return externalIds; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int hashCode() { |
| 85 | return Objects.hash(dscp, otherConfigs, externalIds); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean equals(Object obj) { |
| 90 | if (this == obj) { |
| 91 | return true; |
| 92 | } |
| 93 | if (obj instanceof OvsdbQueue) { |
| 94 | final OvsdbQueue other = (OvsdbQueue) obj; |
| 95 | return Objects.equals(this.dscp, other.dscp) && |
| 96 | Objects.equals(this.otherConfigs, other.otherConfigs) && |
| 97 | Objects.equals(this.externalIds, other.externalIds); |
| 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | return toStringHelper(this) |
| 105 | .add("dscp", dscp()) |
| 106 | .add("maxRate", otherConfigs()) |
| 107 | .add("externalIds", externalIds()) |
| 108 | .toString(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Returns new OVSDB queue builder. |
| 113 | * |
| 114 | * @return ovsdb queue builder |
| 115 | */ |
| 116 | public static OvsdbQueue.Builder builder() { |
| 117 | return new OvsdbQueue.Builder(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Returns new OVSDB queue builder with queue description. |
| 122 | * |
| 123 | * @param queueDescription queue description |
| 124 | * @return ovsdb queue builder |
| 125 | */ |
| 126 | public static Builder builder(QueueDescription queueDescription) { |
| 127 | return new Builder(queueDescription); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Builder of OVSDB queue entities. |
| 132 | */ |
| 133 | public static final class Builder { |
| 134 | private Optional<Long> dscp = Optional.empty(); |
| 135 | private Map<String, String> otherConfigs = Maps.newHashMap(); |
| 136 | private Map<String, String> externalIds = Maps.newHashMap(); |
| 137 | |
| 138 | /** |
| 139 | * Constructs an empty builder. |
| 140 | */ |
| 141 | private Builder() { |
| 142 | |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Constructs a builder with a given queue description. |
| 147 | * |
| 148 | * @param queueDescription queue description |
| 149 | */ |
| 150 | private Builder(QueueDescription queueDescription) { |
| 151 | if (queueDescription.maxRate().isPresent()) { |
| 152 | otherConfigs.put(MAX_RATE, String.valueOf((long) queueDescription.maxRate().get().bps())); |
| 153 | } |
| 154 | if (queueDescription.minRate().isPresent()) { |
| 155 | otherConfigs.put(MIN_RATE, String.valueOf((long) queueDescription.minRate().get().bps())); |
| 156 | } |
| 157 | if (queueDescription.burst().isPresent()) { |
| 158 | otherConfigs.put(BURST, queueDescription.burst().get().toString()); |
| 159 | } |
| 160 | if (queueDescription.priority().isPresent()) { |
| 161 | otherConfigs.put(PRIORITY, queueDescription.priority().get().toString()); |
| 162 | } |
| 163 | if (queueDescription.dscp().isPresent()) { |
| 164 | dscp = Optional.of(queueDescription.dscp().get().longValue()); |
| 165 | } |
| 166 | |
| 167 | externalIds.putAll(((DefaultAnnotations) queueDescription.annotations()).asMap()); |
| 168 | externalIds.put(QUEUE_EXTERNAL_ID_KEY, queueDescription.queueId().name()); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Returns new OVSDB queue. |
| 173 | * |
| 174 | * @return ovsdb queue |
| 175 | */ |
| 176 | public OvsdbQueue build() { |
| 177 | return new OvsdbQueue(dscp, otherConfigs, externalIds); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns OVSDB queue builder with dscp. |
| 182 | * |
| 183 | * @param dscp dscp |
| 184 | * @return ovsdb queue builder |
| 185 | */ |
| 186 | public Builder dscp(Long dscp) { |
| 187 | this.dscp = Optional.ofNullable(dscp); |
| 188 | return this; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns OVSDB queue builder with given configs. |
| 193 | * |
| 194 | * @param otherConfigs other configs |
| 195 | * @return ovsdb queue builder |
| 196 | */ |
| 197 | public Builder otherConfigs(Map<String, String> otherConfigs) { |
| 198 | this.otherConfigs = Maps.newHashMap(otherConfigs); |
| 199 | return this; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns OVSDB queue builder with given external ids. |
| 204 | * |
| 205 | * @param ids the external ids |
| 206 | * @return ovsdb queue builder |
| 207 | */ |
| 208 | public Builder externalIds(Map<String, String> ids) { |
| 209 | this.externalIds = ids; |
| 210 | return this; |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | } |