Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 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 | */ |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.basics; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 19 | import org.onosproject.net.Link; |
| 20 | import org.onosproject.net.LinkKey; |
| 21 | |
| 22 | import java.time.Duration; |
| 23 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 24 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 25 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Basic configuration for network infrastructure link. |
| 28 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 29 | public final class BasicLinkConfig extends AllowedEntityConfig<LinkKey> { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 30 | |
Simon Hunt | db450ee | 2016-01-09 13:12:11 -0800 | [diff] [blame] | 31 | public static final String TYPE = "type"; |
| 32 | public static final String METRIC = "metric"; |
| 33 | public static final String LATENCY = "latency"; |
| 34 | public static final String BANDWIDTH = "bandwidth"; |
| 35 | public static final String IS_DURABLE = "durable"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 36 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 37 | @Override |
| 38 | public boolean isValid() { |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 39 | return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE) && |
| 40 | isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) && |
| 41 | isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Returns the link type. |
| 46 | * |
| 47 | * @return link type override |
| 48 | */ |
| 49 | public Link.Type type() { |
| 50 | return get(TYPE, Link.Type.DIRECT, Link.Type.class); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Sets the link type. |
| 55 | * |
| 56 | * @param type link type override |
| 57 | * @return self |
| 58 | */ |
| 59 | public BasicLinkConfig type(Link.Type type) { |
| 60 | return (BasicLinkConfig) setOrClear(TYPE, type); |
| 61 | } |
| 62 | |
| 63 | /** |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 64 | * Returns link metric value for use by |
| 65 | * {@link org.onosproject.net.topology.MetricLinkWeight} function. |
| 66 | * |
| 67 | * @return link metric; -1 if not set |
| 68 | */ |
| 69 | public double metric() { |
| 70 | return get(METRIC, -1); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Sets the link metric for use by |
| 75 | * {@link org.onosproject.net.topology.MetricLinkWeight} function. |
| 76 | * |
| 77 | * @param metric new metric; null to clear |
| 78 | * @return self |
| 79 | */ |
| 80 | public BasicLinkConfig metric(Double metric) { |
| 81 | return (BasicLinkConfig) setOrClear(METRIC, metric); |
| 82 | } |
| 83 | |
| 84 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 85 | * Returns link latency in terms of nanos. |
| 86 | * |
| 87 | * @return link latency; -1 if not set |
| 88 | */ |
| 89 | public Duration latency() { |
| 90 | return Duration.ofNanos(get(LATENCY, -1)); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Sets the link latency. |
| 95 | * |
| 96 | * @param latency new latency; null to clear |
| 97 | * @return self |
| 98 | */ |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 99 | public BasicLinkConfig latency(Duration latency) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 100 | Long nanos = latency == null ? null : latency.toNanos(); |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 101 | return (BasicLinkConfig) setOrClear(LATENCY, nanos); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns link bandwidth in terms of Mbps. |
| 106 | * |
| 107 | * @return link bandwidth; -1 if not set |
| 108 | */ |
| 109 | public long bandwidth() { |
| 110 | return get(BANDWIDTH, -1); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Sets the link bandwidth. |
| 115 | * |
| 116 | * @param bandwidth new bandwidth; null to clear |
| 117 | * @return self |
| 118 | */ |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 119 | public BasicLinkConfig bandwidth(Long bandwidth) { |
| 120 | return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Returns if link is durable in the network model or not. |
| 125 | * |
| 126 | * @return true for durable, false otherwise |
| 127 | */ |
| 128 | public Boolean isDurable() { |
| 129 | JsonNode res = object.path(IS_DURABLE); |
| 130 | if (res.isMissingNode()) { |
| 131 | return null; |
| 132 | } |
| 133 | return res.asBoolean(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Sets durability for this link. |
| 138 | * |
| 139 | * @param isDurable true for durable, false otherwise |
| 140 | * @return this BasicLinkConfig |
| 141 | */ |
| 142 | public BasicLinkConfig isDurable(Boolean isDurable) { |
| 143 | return (BasicLinkConfig) setOrClear(IS_DURABLE, isDurable); |
| 144 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 145 | } |