blob: f7cb3c42a7e1a8fe840fd352594c79318aa5156a [file] [log] [blame]
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -08001/*
2 * Copyright 2016 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 */
16package org.onosproject.net.newresource;
17
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;
28
29/**
30 * Configuration to specify maximum available bandwidth resource (Capacity) on a port.
31 */
32@Beta
33public class BandwidthCapacity extends Config<ConnectPoint> {
34
35 /**
36 * netcfg ConfigKey for {@link BandwidthCapacity}.
37 */
38 public static final String CONFIG_KEY = "bandwidthCapacity";
39
40 // JSON key
41 private static final String CAPACITY = "capacityMbps";
42
43 private static final Logger log = LoggerFactory.getLogger(BandwidthCapacity.class);
44
45 @Override
46 public boolean isValid() {
47 // Open for extension (adding fields) in the future,
48 // must have CAPACITY field.
49 return isNumber(CAPACITY, FieldPresence.MANDATORY);
50 }
51
52 /**
53 * Sets the Available Bandwidth resource (Capacity).
54 *
55 * @param bandwidth value to set.
56 * @return self
57 */
58 public BandwidthCapacity capacity(Bandwidth bandwidth) {
59 checkNotNull(bandwidth);
60
61 // TODO current Bandwidth API end up value converted to double
62 setOrClear(CAPACITY, bandwidth.bps());
63 return this;
64 }
65
66 /**
67 * Available Bandwidth resource (Capacity).
68 *
69 * @return {@link Bandwidth}
70 */
71 public Bandwidth capacity() {
72 JsonNode v = object.path(CAPACITY);
73
74 if (v.isIntegralNumber()) {
75
76 return Bandwidth.mbps(v.asLong());
77 } else if (v.isFloatingPointNumber()) {
78
79 return Bandwidth.mbps(v.asDouble());
80 } else {
81 log.warn("Unexpected JsonNode for {}: {}", CAPACITY, v);
82 return Bandwidth.mbps(v.asDouble());
83 }
84 }
85}