blob: cd56faec054dd220ab12296dc44d802d93e3248f [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070016package org.onosproject.net.optical.config;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070017
18import java.util.Optional;
19
Ray Milkeya4122362015-08-18 15:19:08 -070020import org.onosproject.net.config.Config;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070021import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.Port;
23
24import com.fasterxml.jackson.databind.JsonNode;
25
Thomas Vachuska36008462016-01-07 15:38:20 -080026import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL;
27
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070028
29/**
30 * Configurations for an optical port on a device.
Yuta HIGUCHI5ccaefb2016-09-07 11:18:13 -070031 *
32 * <p>
33 * Example:
34 * <pre>
35 * "ports": {
36 * "(device Id)/(port number)": {
37 * {@value #CONFIG_KEY}: {
38 * {@value #TYPE}: "OCH",
39 * {@value #SPEED}: 0,
40 * {@value #PORT}: 10
41 * ...
42 * }
43 * }
44 * }
45 * </pre>
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070046 */
Thomas Vachuska36008462016-01-07 15:38:20 -080047public final class OpticalPortConfig extends Config<ConnectPoint> {
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070048
Yuta HIGUCHI5ccaefb2016-09-07 11:18:13 -070049 /**
50 * Configuration key for {@link OpticalPortConfig}.
51 */
52 public static final String CONFIG_KEY = "optical";
53
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070054 // optical type {OMS, OCH, ODUClt, fiber}
55 public static final String TYPE = "type";
56
57 // port name. "name" is the alphanumeric name of the port, but "port" refers
58 // to the port number used as a name string (i.e., for ports without
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070059 // alphanumeric names).
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070060 public static final String NAME = "name";
61 public static final String PORT = "port";
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070062 public static final String STATIC_PORT = "staticPort";
63 public static final String STATIC_LAMBDA = "staticLambda";
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070064
Ayaka Koshibe1a002512015-09-03 13:09:23 -070065 // **Linc-OE : remove if it's not needed after all.**
66 public static final String SPEED = "speed";
67
Thomas Vachuska36008462016-01-07 15:38:20 -080068 @Override
69 public boolean isValid() {
70 return hasOnlyFields(TYPE, NAME, PORT, STATIC_PORT, STATIC_LAMBDA, SPEED) &&
71 isNumber(STATIC_LAMBDA, OPTIONAL) && isNumber(SPEED, OPTIONAL);
72 }
73
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070074 /**
75 * Returns the Enum value representing the type of port.
76 *
77 * @return the port type, or null if invalid or unset
78 */
79 public Port.Type type() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070080 JsonNode type = object.path(TYPE);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070081 if (type.isMissingNode()) {
82 return null;
83 }
84 return Port.Type.valueOf(type.asText());
85 }
86
87 /**
88 * Returns the port name associated with this port configuration. The Name
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070089 * is an alphanumeric string.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070090 *
91 * @return the name of this port, else, an empty string
92 */
93 public String name() {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070094 return getStringValue(NAME);
95 }
96
97 /**
98 * Returns a stringified representation of the port number, configured in
99 * some port types without an alphanumeric name as the port name.
100 *
101 * @return A string representation of the port number
102 */
103 public String numberName() {
104 return getStringValue(PORT);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700105 }
106
107 /**
108 * Returns the string-representation of name of the output port. This is
109 * usually an OMS port for an OCH input ports, or an OCH port for ODU input
110 * ports.
111 *
112 * @return the name of this port, else, an empty string
113 */
114 public String staticPort() {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700115 return getStringValue(STATIC_PORT);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700116 }
117
118 private String getStringValue(String field) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700119 JsonNode name = object.path(field);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700120 return name.isMissingNode() ? "" : name.asText();
121 }
122
123 /**
124 * Returns the output lambda configured for this port. The lambda value is
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700125 * expressed as a frequency value. If the port type doesn't have a notion of
126 * lambdas, this returns an empty Optional.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700127 *
128 * @return an Optional that may contain a frequency value.
129 */
130 public Optional<Long> staticLambda() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700131 JsonNode sl = object.path(STATIC_LAMBDA);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700132 if (sl.isMissingNode()) {
133 return Optional.empty();
134 }
135 return Optional.of(sl.asLong());
136 }
137
138 /**
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700139 * Returns the port speed configured for this port. If the port doesn't have
140 * a notion of speed, this returns an empty Optional.
141 *
142 * @return a port speed value whose default is 0.
143 */
144 public Optional<Integer> speed() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700145 JsonNode s = object.path(SPEED);
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700146 if (s.isMissingNode()) {
147 return Optional.empty();
148 }
149 return Optional.of(s.asInt());
150 }
151
152 /**
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700153 * Sets the port type, or updates it if it's already set. A null argument removes
154 * this field.
155 *
156 * @param type the port type
157 * @return this OpticalPortConfig instance
158 */
159 public OpticalPortConfig portType(Port.Type type) {
160 // if unspecified, ideally fall back on FIBER or PACKET.
161 String pt = (type == null) ? null : type.toString();
162 return (OpticalPortConfig) setOrClear(TYPE, pt);
163 }
164
165 /**
166 * Sets the port name, or updates it if already set. A null argument removes
167 * this field.
168 *
169 * @param name the port's name
170 * @return this OpticalPortConfig instance
171 */
172 public OpticalPortConfig portName(String name) {
173 return (OpticalPortConfig) setOrClear(NAME, name);
174 }
175
176 /**
177 * Sets the port name from port number, or updates it if already set. A null
178 * argument removes this field.
179 *
180 * @param name the port number, to be used as name
181 * @return this OpticalPortConfig instance
182 */
183 public OpticalPortConfig portNumberName(Long name) {
184 return (OpticalPortConfig) setOrClear(PORT, name);
185 }
186
187 /**
188 * Sets the output port name, or updates it if already set. A null argument
189 * removes this field.
190 *
191 * @param name the output port's name
192 * @return this OpticalPortConfig instance
193 */
194 public OpticalPortConfig staticPort(String name) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700195 return (OpticalPortConfig) setOrClear(STATIC_PORT, name);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700196 }
197
198 /**
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800199 * Sets the output lambda center frequency, or updates it if already set.
200 * A null argument removes this field.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700201 *
202 * @param index the output lambda
203 * @return this OpticalPortConfig instance
204 */
205 public OpticalPortConfig staticLambda(Long index) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700206 return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700207 }
208
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700209 /**
210 * Sets the port speed, or updates it if already set. A null argument
211 * removes this field.
212 *
213 * @param bw the port bandwidth
214 * @return this OpticalPortConfig instance
215 */
216 public OpticalPortConfig speed(Integer bw) {
217 return (OpticalPortConfig) setOrClear(SPEED, bw);
218 }
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700219}