HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 16 | package org.onosproject.net.resource; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import org.onlab.util.Bandwidth; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.config.Config; |
| 23 | import org.slf4j.Logger; |
| 24 | import org.slf4j.LoggerFactory; |
| 25 | |
| 26 | import com.fasterxml.jackson.databind.JsonNode; |
| 27 | import com.google.common.annotations.Beta; |
HIGUCHI Yuta | dc4394c | 2016-01-29 15:35:10 -0800 | [diff] [blame] | 28 | import com.google.common.base.MoreObjects; |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Configuration to specify maximum available bandwidth resource (Capacity) on a port. |
| 32 | */ |
| 33 | @Beta |
| 34 | public 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 Yuta | dc4394c | 2016-01-29 15:35:10 -0800 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | public String toString() { |
| 89 | return MoreObjects.toStringHelper(this) |
| 90 | .add("capacity", capacity()) |
| 91 | .toString(); |
| 92 | } |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 93 | } |