blob: 6a5e02657d80f0ac02cbddfea1a2ef977198ccb2 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska96d55b12015-05-11 08:52:03 -07003 *
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 Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.net.config.basics;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070017
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080018import com.fasterxml.jackson.databind.JsonNode;
Thomas Vachuska96d55b12015-05-11 08:52:03 -070019import org.onosproject.net.Link;
20import org.onosproject.net.LinkKey;
21
22import java.time.Duration;
23
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080024import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
25
Thomas Vachuska96d55b12015-05-11 08:52:03 -070026/**
27 * Basic configuration for network infrastructure link.
28 */
Thomas Vachuska36008462016-01-07 15:38:20 -080029public final class BasicLinkConfig extends AllowedEntityConfig<LinkKey> {
Thomas Vachuska96d55b12015-05-11 08:52:03 -070030
Simon Huntdb450ee2016-01-09 13:12:11 -080031 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 Leenheerec551d32017-03-10 15:48:18 -080036 public static final String IS_BIDIRECTIONAL = "bidirectional";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070037
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080038 @Override
39 public boolean isValid() {
Marc De Leenheerec551d32017-03-10 15:48:18 -080040 return hasOnlyFields(ALLOWED, TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE, IS_BIDIRECTIONAL) &&
Thomas Vachuska36008462016-01-07 15:38:20 -080041 isBoolean(ALLOWED, OPTIONAL) && isNumber(METRIC, OPTIONAL) &&
Marc De Leenheerec551d32017-03-10 15:48:18 -080042 isNumber(LATENCY, OPTIONAL) && isNumber(BANDWIDTH, OPTIONAL) &&
43 isBoolean(IS_BIDIRECTIONAL, OPTIONAL);
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080044 }
45
Thomas Vachuska96d55b12015-05-11 08:52:03 -070046 /**
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 Vachuska41fe1ec2015-12-03 23:17:02 -080066 * 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 Vachuska96d55b12015-05-11 08:52:03 -070087 * 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 Koshibecc260d22015-08-04 17:13:38 -0700101 public BasicLinkConfig latency(Duration latency) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700102 Long nanos = latency == null ? null : latency.toNanos();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700103 return (BasicLinkConfig) setOrClear(LATENCY, nanos);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700104 }
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 Koshibecc260d22015-08-04 17:13:38 -0700121 public BasicLinkConfig bandwidth(Long bandwidth) {
122 return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700123 }
124
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700125 /**
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 Leenheerec551d32017-03-10 15:48:18 -0800147
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 Vachuska96d55b12015-05-11 08:52:03 -0700170}