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"; |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 36 | public static final String IS_BIDIRECTIONAL = "bidirectional"; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 37 | |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 38 | @Override |
| 39 | public boolean isValid() { |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 40 | return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE, IS_BIDIRECTIONAL) && |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 41 | isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) && |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 42 | isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) && |
| 43 | isBoolean(IS_BIDIRECTIONAL, OPTIONAL); |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Returns the link type. |
| 48 | * |
| 49 | * @return link type override |
| 50 | */ |
| 51 | public Link.Type type() { |
| 52 | return get(TYPE, Link.Type.DIRECT, Link.Type.class); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Sets the link type. |
| 57 | * |
| 58 | * @param type link type override |
| 59 | * @return self |
| 60 | */ |
| 61 | public BasicLinkConfig type(Link.Type type) { |
| 62 | return (BasicLinkConfig) setOrClear(TYPE, type); |
| 63 | } |
| 64 | |
| 65 | /** |
Thomas Vachuska | 41fe1ec | 2015-12-03 23:17:02 -0800 | [diff] [blame] | 66 | * Returns link metric value for use by |
| 67 | * {@link org.onosproject.net.topology.MetricLinkWeight} function. |
| 68 | * |
| 69 | * @return link metric; -1 if not set |
| 70 | */ |
| 71 | public double metric() { |
| 72 | return get(METRIC, -1); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sets the link metric for use by |
| 77 | * {@link org.onosproject.net.topology.MetricLinkWeight} function. |
| 78 | * |
| 79 | * @param metric new metric; null to clear |
| 80 | * @return self |
| 81 | */ |
| 82 | public BasicLinkConfig metric(Double metric) { |
| 83 | return (BasicLinkConfig) setOrClear(METRIC, metric); |
| 84 | } |
| 85 | |
| 86 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 87 | * Returns link latency in terms of nanos. |
| 88 | * |
| 89 | * @return link latency; -1 if not set |
| 90 | */ |
| 91 | public Duration latency() { |
| 92 | return Duration.ofNanos(get(LATENCY, -1)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sets the link latency. |
| 97 | * |
| 98 | * @param latency new latency; null to clear |
| 99 | * @return self |
| 100 | */ |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 101 | public BasicLinkConfig latency(Duration latency) { |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 102 | Long nanos = latency == null ? null : latency.toNanos(); |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 103 | return (BasicLinkConfig) setOrClear(LATENCY, nanos); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns link bandwidth in terms of Mbps. |
| 108 | * |
| 109 | * @return link bandwidth; -1 if not set |
| 110 | */ |
| 111 | public long bandwidth() { |
| 112 | return get(BANDWIDTH, -1); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Sets the link bandwidth. |
| 117 | * |
| 118 | * @param bandwidth new bandwidth; null to clear |
| 119 | * @return self |
| 120 | */ |
Ayaka Koshibe | cc260d2 | 2015-08-04 17:13:38 -0700 | [diff] [blame] | 121 | public BasicLinkConfig bandwidth(Long bandwidth) { |
| 122 | return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Ayaka Koshibe | 2c59acf | 2015-09-08 15:37:47 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Returns if link is durable in the network model or not. |
| 127 | * |
| 128 | * @return true for durable, false otherwise |
| 129 | */ |
| 130 | public Boolean isDurable() { |
| 131 | JsonNode res = object.path(IS_DURABLE); |
| 132 | if (res.isMissingNode()) { |
| 133 | return null; |
| 134 | } |
| 135 | return res.asBoolean(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Sets durability for this link. |
| 140 | * |
| 141 | * @param isDurable true for durable, false otherwise |
| 142 | * @return this BasicLinkConfig |
| 143 | */ |
| 144 | public BasicLinkConfig isDurable(Boolean isDurable) { |
| 145 | return (BasicLinkConfig) setOrClear(IS_DURABLE, isDurable); |
| 146 | } |
Marc De Leenheer | ec551d3 | 2017-03-10 15:48:18 -0800 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Returns if link is bidirectional in the network model or not. |
| 150 | * |
| 151 | * @return true for bidirectional, false otherwise |
| 152 | */ |
| 153 | public Boolean isBidirectional() { |
| 154 | JsonNode res = object.path(IS_BIDIRECTIONAL); |
| 155 | if (res.isMissingNode()) { |
| 156 | return true; |
| 157 | } |
| 158 | return res.asBoolean(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Sets durability for this link. |
| 163 | * |
| 164 | * @param isBidirectional true for directional, false otherwise |
| 165 | * @return this BasicLinkConfig |
| 166 | */ |
| 167 | public BasicLinkConfig isBidirectional(Boolean isBidirectional) { |
| 168 | return (BasicLinkConfig) setOrClear(IS_BIDIRECTIONAL, isBidirectional); |
| 169 | } |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 170 | } |