blob: e5a23cfb5f9e0a03a6302c0b4d031940c3398821 [file] [log] [blame]
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -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 */
Sho SHIMIZUb6c63a32016-05-26 12:07:19 -070016package org.onosproject.net.config.basics;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080017
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import org.onlab.util.Bandwidth;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.config.Config;
23import org.slf4j.Logger;
24import org.slf4j.LoggerFactory;
25
26import com.fasterxml.jackson.databind.JsonNode;
27import com.google.common.annotations.Beta;
HIGUCHI Yutadc4394c2016-01-29 15:35:10 -080028import com.google.common.base.MoreObjects;
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080029
30/**
31 * Configuration to specify maximum available bandwidth resource (Capacity) on a port.
32 */
33@Beta
34public class BandwidthCapacity extends Config<ConnectPoint> {
35
36 /**
37 * netcfg ConfigKey for {@link BandwidthCapacity}.
38 */
39 public static final String CONFIG_KEY = "bandwidthCapacity";
40
41 // JSON key
42 private static final String CAPACITY = "capacityMbps";
43
44 private static final Logger log = LoggerFactory.getLogger(BandwidthCapacity.class);
45
46 @Override
47 public boolean isValid() {
Jordan Halterman83949a12017-06-21 10:35:38 -070048 // Validate the capacity
49 capacity();
50
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080051 // Open for extension (adding fields) in the future,
52 // must have CAPACITY field.
53 return isNumber(CAPACITY, FieldPresence.MANDATORY);
54 }
55
56 /**
57 * Sets the Available Bandwidth resource (Capacity).
58 *
59 * @param bandwidth value to set.
60 * @return self
61 */
62 public BandwidthCapacity capacity(Bandwidth bandwidth) {
63 checkNotNull(bandwidth);
64
65 // TODO current Bandwidth API end up value converted to double
66 setOrClear(CAPACITY, bandwidth.bps());
67 return this;
68 }
69
70 /**
71 * Available Bandwidth resource (Capacity).
72 *
73 * @return {@link Bandwidth}
74 */
75 public Bandwidth capacity() {
76 JsonNode v = object.path(CAPACITY);
77
78 if (v.isIntegralNumber()) {
79
80 return Bandwidth.mbps(v.asLong());
81 } else if (v.isFloatingPointNumber()) {
82
83 return Bandwidth.mbps(v.asDouble());
84 } else {
85 log.warn("Unexpected JsonNode for {}: {}", CAPACITY, v);
86 return Bandwidth.mbps(v.asDouble());
87 }
88 }
HIGUCHI Yutadc4394c2016-01-29 15:35:10 -080089
90 @Override
91 public String toString() {
92 return MoreObjects.toStringHelper(this)
93 .add("capacity", capacity())
94 .toString();
95 }
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080096}