blob: 97bdea46faa0fc1747725af69cc04cc6c5040572 [file] [log] [blame]
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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 SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource;
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() {
48 // Open for extension (adding fields) in the future,
49 // must have CAPACITY field.
50 return isNumber(CAPACITY, FieldPresence.MANDATORY);
51 }
52
53 /**
54 * Sets the Available Bandwidth resource (Capacity).
55 *
56 * @param bandwidth value to set.
57 * @return self
58 */
59 public BandwidthCapacity capacity(Bandwidth bandwidth) {
60 checkNotNull(bandwidth);
61
62 // TODO current Bandwidth API end up value converted to double
63 setOrClear(CAPACITY, bandwidth.bps());
64 return this;
65 }
66
67 /**
68 * Available Bandwidth resource (Capacity).
69 *
70 * @return {@link Bandwidth}
71 */
72 public Bandwidth capacity() {
73 JsonNode v = object.path(CAPACITY);
74
75 if (v.isIntegralNumber()) {
76
77 return Bandwidth.mbps(v.asLong());
78 } else if (v.isFloatingPointNumber()) {
79
80 return Bandwidth.mbps(v.asDouble());
81 } else {
82 log.warn("Unexpected JsonNode for {}: {}", CAPACITY, v);
83 return Bandwidth.mbps(v.asDouble());
84 }
85 }
HIGUCHI Yutadc4394c2016-01-29 15:35:10 -080086
87 @Override
88 public String toString() {
89 return MoreObjects.toStringHelper(this)
90 .add("capacity", capacity())
91 .toString();
92 }
HIGUCHI Yuta1d7c9cb2016-01-20 18:22:36 -080093}