blob: 0278fbad2a31be453dadca695228be09e8a0968f [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
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.
31 */
Thomas Vachuska36008462016-01-07 15:38:20 -080032public final class OpticalPortConfig extends Config<ConnectPoint> {
Yuta HIGUCHIb9af6b72016-06-10 10:52:58 -070033
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070034 // optical type {OMS, OCH, ODUClt, fiber}
35 public static final String TYPE = "type";
36
37 // port name. "name" is the alphanumeric name of the port, but "port" refers
38 // to the port number used as a name string (i.e., for ports without
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070039 // alphanumeric names).
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070040 public static final String NAME = "name";
41 public static final String PORT = "port";
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070042 public static final String STATIC_PORT = "staticPort";
43 public static final String STATIC_LAMBDA = "staticLambda";
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070044
Ayaka Koshibe1a002512015-09-03 13:09:23 -070045 // **Linc-OE : remove if it's not needed after all.**
46 public static final String SPEED = "speed";
47
Thomas Vachuska36008462016-01-07 15:38:20 -080048 @Override
49 public boolean isValid() {
50 return hasOnlyFields(TYPE, NAME, PORT, STATIC_PORT, STATIC_LAMBDA, SPEED) &&
51 isNumber(STATIC_LAMBDA, OPTIONAL) && isNumber(SPEED, OPTIONAL);
52 }
53
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070054 /**
55 * Returns the Enum value representing the type of port.
56 *
57 * @return the port type, or null if invalid or unset
58 */
59 public Port.Type type() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070060 JsonNode type = object.path(TYPE);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070061 if (type.isMissingNode()) {
62 return null;
63 }
64 return Port.Type.valueOf(type.asText());
65 }
66
67 /**
68 * Returns the port name associated with this port configuration. The Name
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070069 * is an alphanumeric string.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070070 *
71 * @return the name of this port, else, an empty string
72 */
73 public String name() {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070074 return getStringValue(NAME);
75 }
76
77 /**
78 * Returns a stringified representation of the port number, configured in
79 * some port types without an alphanumeric name as the port name.
80 *
81 * @return A string representation of the port number
82 */
83 public String numberName() {
84 return getStringValue(PORT);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070085 }
86
87 /**
88 * Returns the string-representation of name of the output port. This is
89 * usually an OMS port for an OCH input ports, or an OCH port for ODU input
90 * ports.
91 *
92 * @return the name of this port, else, an empty string
93 */
94 public String staticPort() {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070095 return getStringValue(STATIC_PORT);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070096 }
97
98 private String getStringValue(String field) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070099 JsonNode name = object.path(field);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700100 return name.isMissingNode() ? "" : name.asText();
101 }
102
103 /**
104 * Returns the output lambda configured for this port. The lambda value is
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700105 * expressed as a frequency value. If the port type doesn't have a notion of
106 * lambdas, this returns an empty Optional.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700107 *
108 * @return an Optional that may contain a frequency value.
109 */
110 public Optional<Long> staticLambda() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700111 JsonNode sl = object.path(STATIC_LAMBDA);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700112 if (sl.isMissingNode()) {
113 return Optional.empty();
114 }
115 return Optional.of(sl.asLong());
116 }
117
118 /**
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700119 * Returns the port speed configured for this port. If the port doesn't have
120 * a notion of speed, this returns an empty Optional.
121 *
122 * @return a port speed value whose default is 0.
123 */
124 public Optional<Integer> speed() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700125 JsonNode s = object.path(SPEED);
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700126 if (s.isMissingNode()) {
127 return Optional.empty();
128 }
129 return Optional.of(s.asInt());
130 }
131
132 /**
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700133 * Sets the port type, or updates it if it's already set. A null argument removes
134 * this field.
135 *
136 * @param type the port type
137 * @return this OpticalPortConfig instance
138 */
139 public OpticalPortConfig portType(Port.Type type) {
140 // if unspecified, ideally fall back on FIBER or PACKET.
141 String pt = (type == null) ? null : type.toString();
142 return (OpticalPortConfig) setOrClear(TYPE, pt);
143 }
144
145 /**
146 * Sets the port name, or updates it if already set. A null argument removes
147 * this field.
148 *
149 * @param name the port's name
150 * @return this OpticalPortConfig instance
151 */
152 public OpticalPortConfig portName(String name) {
153 return (OpticalPortConfig) setOrClear(NAME, name);
154 }
155
156 /**
157 * Sets the port name from port number, or updates it if already set. A null
158 * argument removes this field.
159 *
160 * @param name the port number, to be used as name
161 * @return this OpticalPortConfig instance
162 */
163 public OpticalPortConfig portNumberName(Long name) {
164 return (OpticalPortConfig) setOrClear(PORT, name);
165 }
166
167 /**
168 * Sets the output port name, or updates it if already set. A null argument
169 * removes this field.
170 *
171 * @param name the output port's name
172 * @return this OpticalPortConfig instance
173 */
174 public OpticalPortConfig staticPort(String name) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700175 return (OpticalPortConfig) setOrClear(STATIC_PORT, name);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700176 }
177
178 /**
179 * Sets the output lambda index, or updates it if already set. A null argument
180 * removes this field.
181 *
182 * @param index the output lambda
183 * @return this OpticalPortConfig instance
184 */
185 public OpticalPortConfig staticLambda(Long index) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700186 return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700187 }
188
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700189 /**
190 * Sets the port speed, or updates it if already set. A null argument
191 * removes this field.
192 *
193 * @param bw the port bandwidth
194 * @return this OpticalPortConfig instance
195 */
196 public OpticalPortConfig speed(Integer bw) {
197 return (OpticalPortConfig) setOrClear(SPEED, bw);
198 }
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700199}