blob: ed807b8fac6789cd5191279076bb4d717021d542 [file] [log] [blame]
Thomas Vachuska96d55b12015-05-11 08:52:03 -07001/*
2 * Copyright 2015 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 */
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 */
29public class BasicLinkConfig extends AllowedEntityConfig<LinkKey> {
30
31 public static final String TYPE = "type";
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080032 public static final String METRIC = "metric";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070033 public static final String LATENCY = "latency";
34 public static final String BANDWIDTH = "bandwidth";
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -070035 public static final String IS_DURABLE = "durable";
Thomas Vachuska96d55b12015-05-11 08:52:03 -070036
Thomas Vachuska41fe1ec2015-12-03 23:17:02 -080037 @Override
38 public boolean isValid() {
39 return hasOnlyFields(TYPE, METRIC, LATENCY, BANDWIDTH, IS_DURABLE) &&
40 isNumber(METRIC, OPTIONAL) && isNumber(LATENCY, OPTIONAL) &&
41 isNumber(BANDWIDTH, OPTIONAL);
42 }
43
Thomas Vachuska96d55b12015-05-11 08:52:03 -070044 /**
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 Vachuska41fe1ec2015-12-03 23:17:02 -080064 * 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 Vachuska96d55b12015-05-11 08:52:03 -070085 * 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 Koshibecc260d22015-08-04 17:13:38 -070099 public BasicLinkConfig latency(Duration latency) {
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700100 Long nanos = latency == null ? null : latency.toNanos();
Ayaka Koshibecc260d22015-08-04 17:13:38 -0700101 return (BasicLinkConfig) setOrClear(LATENCY, nanos);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700102 }
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 Koshibecc260d22015-08-04 17:13:38 -0700119 public BasicLinkConfig bandwidth(Long bandwidth) {
120 return (BasicLinkConfig) setOrClear(BANDWIDTH, bandwidth);
Thomas Vachuska96d55b12015-05-11 08:52:03 -0700121 }
122
Ayaka Koshibe2c59acf2015-09-08 15:37:47 -0700123 /**
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 Vachuska96d55b12015-05-11 08:52:03 -0700145}