blob: 5f1d4c97a90a1d05236d968c78380aeaa4302815 [file] [log] [blame]
Frank Wange11a98d2016-10-26 17:04:03 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wange11a98d2016-10-26 17:04:03 +08003 *
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
17package org.onosproject.ovsdb.controller;
18
19import com.google.common.collect.Maps;
20import org.onosproject.net.DefaultAnnotations;
21import org.onosproject.net.behaviour.QosDescription;
22
23import java.util.HashMap;
24import java.util.Map;
25import java.util.Objects;
26import java.util.Optional;
27
28import static com.google.common.base.MoreObjects.toStringHelper;
29import static com.google.common.base.Preconditions.checkNotNull;
30import static org.onosproject.ovsdb.controller.OvsdbConstant.CBS;
31import static org.onosproject.ovsdb.controller.OvsdbConstant.CIR;
32import static org.onosproject.ovsdb.controller.OvsdbConstant.MAX_RATE;
33import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EGRESS_POLICER;
34import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_EXTERNAL_ID_KEY;
35import static org.onosproject.ovsdb.controller.OvsdbConstant.QOS_TYPE_PREFIX;
36
37/**
38 * The class representing an OVSDB Qos.
39 * This class is immutable.
40 */
41public final class OvsdbQos {
42
43 private final String type;
44 private Optional<Map<Long, String>> queues;
45 private Map<String, String> otherConfigs;
46 private Map<String, String> externalIds;
47
48 /**
49 * Creates an OvsdbQos using the given inputs.
50 *
51 * @param type the type of the qos
52 * @param queues rate queues
53 * @param otherConfigs Key-value pairs for configuring rarely used features
54 * @param externalIds Key-value pairs for use by external frameworks, rather than by OVS itself
55 */
56 private OvsdbQos(String type, Optional<Map<Long, String>> queues,
57 Map<String, String> otherConfigs,
58 Map<String, String> externalIds) {
59
60 this.type = checkNotNull(type);
61 this.queues = queues;
62 this.otherConfigs = otherConfigs;
63 this.externalIds = externalIds;
64 }
65
66 /**
67 * Returns the type of qos.
68 *
69 * @return the type of qos
70 */
71 public String qosType() {
72 return type;
73 }
74
75 /**
76 * Returns the map of queues.
77 *
78 * @return the queues.
79 */
80 public Optional<Map<Long, String>> qosQueues() {
81 return queues;
82 }
83
84 /**
85 * Returns other configurations of the qos.
86 *
87 * @return map of configurations
88 */
89 public Map<String, String> otherConfigs() {
90 return otherConfigs;
91 }
92
93 /**
94 * Returns the optional external ids.
95 *
96 * @return the external ids.
97 */
98 public Map<String, String> externalIds() {
99 return externalIds;
100 }
101
102 @Override
103 public int hashCode() {
104 return Objects.hash(type, queues, externalIds);
105 }
106
107 @Override
108 public boolean equals(Object obj) {
109 if (this == obj) {
110 return true;
111 }
112 if (obj instanceof OvsdbQos) {
113 final OvsdbQos other = (OvsdbQos) obj;
114 return Objects.equals(this.type, other.type) &&
115 Objects.equals(this.otherConfigs, other.otherConfigs) &&
116 Objects.equals(this.queues, other.queues) &&
117 Objects.equals(this.externalIds, other.externalIds);
118 }
119 return false;
120 }
121
122 @Override
123 public String toString() {
124 return toStringHelper(this)
125 .add("qosType", qosType())
126 .add("qosQueues", qosQueues())
127 .add("otherConfigs", otherConfigs())
128 .add("externalIds", externalIds())
129 .toString();
130 }
131
132 /**
133 * Returns new OVSDB qos builder.
134 *
135 * @return ovsdb qos builder
136 */
137 public static OvsdbQos.Builder builder() {
138 return new OvsdbQos.Builder();
139 }
140
141 /**
142 * Returns new OVSDB qos builder with Qos description.
143 *
144 * @param qosDesc qos description
145 * @return ovsdb qos builder
146 */
147 public static Builder builder(QosDescription qosDesc) {
148 return new Builder(qosDesc);
149 }
150
151 /**
152 * Builder of OVSDB qos entities.
153 */
154 public static final class Builder {
155 private String type;
156 private Optional<Map<Long, String>> queues = Optional.empty();
157 private Map<String, String> otherConfigs = Maps.newHashMap();
158 private Map<String, String> externalIds = Maps.newHashMap();
159
160 /**
161 * Constructs an empty builder.
162 */
163 private Builder() {
164
165 }
166
167 /**
168 * Constructs a builder with a given Qos description.
169 *
170 * @param qosDesc Qos description
171 */
172 private Builder(QosDescription qosDesc) {
173 if (qosDesc.maxRate().isPresent()) {
174 otherConfigs.put(MAX_RATE, String.valueOf((long) qosDesc.maxRate().get().bps()));
175 }
176 if (qosDesc.cir().isPresent()) {
177 otherConfigs.put(CIR, qosDesc.cir().get().toString());
178 }
179 if (qosDesc.cbs().isPresent()) {
180 otherConfigs.put(CBS, qosDesc.cbs().get().toString());
181 }
182
183 if (qosDesc.queues().isPresent()) {
kdarapufce5abb2018-05-10 19:37:53 +0530184 Map<Long, String> map = new HashMap<>();
Frank Wange11a98d2016-10-26 17:04:03 +0800185 qosDesc.queues().get().forEach((k, v) -> map.put(k, v.queueId().name()));
186 queues = Optional.ofNullable(map);
187 }
188 type = qosDesc.type() == QosDescription.Type.EGRESS_POLICER ?
189 QOS_EGRESS_POLICER :
190 QOS_TYPE_PREFIX.concat(qosDesc.type().name().toLowerCase());
191 externalIds.putAll(((DefaultAnnotations) qosDesc.annotations()).asMap());
192 externalIds.put(QOS_EXTERNAL_ID_KEY, qosDesc.qosId().name());
193 }
194
195 /**
196 * Returns new OVSDB qos.
197 *
198 * @return ovsdb qos
199 */
200 public OvsdbQos build() {
201 return new OvsdbQos(type, queues, otherConfigs, externalIds);
202 }
203
204 /**
205 * Returns OVSDB qos builder with a given type.
206 *
207 * @param type name of the qos
208 * @return ovsdb qos builder
209 */
210 public Builder qosType(String type) {
211 this.type = type;
212 return this;
213 }
214
215 /**
216 * Returns OVSDB qos builder with a given queues.
217 *
218 * @param queues the map of queue
219 * @return ovsdb qos builder
220 */
221 public Builder queues(Map<Long, String> queues) {
222 this.queues = Optional.ofNullable(queues);
223 return this;
224 }
225
226 /**
227 * Returns OVSDB qos builder with given configs.
228 *
229 * @param otherConfigs other configs
230 * @return ovsdb qos builder
231 */
232 public Builder otherConfigs(Map<String, String> otherConfigs) {
233 this.otherConfigs = Maps.newHashMap(otherConfigs);
234 return this;
235 }
236
237 /**
238 * Returns OVSDB qos builder with given external ids.
239 *
240 * @param ids the external ids
241 * @return ovsdb qos builder
242 */
243 public Builder externalIds(Map<String, String> ids) {
244 this.externalIds = ids;
245 return this;
246 }
247
248 }
249
250}