blob: 0509dfb868d34b7b6e85ceeee5003e5a470e1d0b [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-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 */
Thomas Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.net.config.basics;
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> {
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070033 // optical type {OMS, OCH, ODUClt, fiber}
34 public static final String TYPE = "type";
35
36 // port name. "name" is the alphanumeric name of the port, but "port" refers
37 // to the port number used as a name string (i.e., for ports without
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070038 // alphanumeric names).
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070039 public static final String NAME = "name";
40 public static final String PORT = "port";
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070041 public static final String STATIC_PORT = "staticPort";
42 public static final String STATIC_LAMBDA = "staticLambda";
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070043
Ayaka Koshibe1a002512015-09-03 13:09:23 -070044 // **Linc-OE : remove if it's not needed after all.**
45 public static final String SPEED = "speed";
46
Thomas Vachuska36008462016-01-07 15:38:20 -080047 @Override
48 public boolean isValid() {
49 return hasOnlyFields(TYPE, NAME, PORT, STATIC_PORT, STATIC_LAMBDA, SPEED) &&
50 isNumber(STATIC_LAMBDA, OPTIONAL) && isNumber(SPEED, OPTIONAL);
51 }
52
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070053 /**
54 * Returns the Enum value representing the type of port.
55 *
56 * @return the port type, or null if invalid or unset
57 */
58 public Port.Type type() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070059 JsonNode type = object.path(TYPE);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070060 if (type.isMissingNode()) {
61 return null;
62 }
63 return Port.Type.valueOf(type.asText());
64 }
65
66 /**
67 * Returns the port name associated with this port configuration. The Name
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070068 * is an alphanumeric string.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070069 *
70 * @return the name of this port, else, an empty string
71 */
72 public String name() {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070073 return getStringValue(NAME);
74 }
75
76 /**
77 * Returns a stringified representation of the port number, configured in
78 * some port types without an alphanumeric name as the port name.
79 *
80 * @return A string representation of the port number
81 */
82 public String numberName() {
83 return getStringValue(PORT);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070084 }
85
86 /**
87 * Returns the string-representation of name of the output port. This is
88 * usually an OMS port for an OCH input ports, or an OCH port for ODU input
89 * ports.
90 *
91 * @return the name of this port, else, an empty string
92 */
93 public String staticPort() {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070094 return getStringValue(STATIC_PORT);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070095 }
96
97 private String getStringValue(String field) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070098 JsonNode name = object.path(field);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070099 return name.isMissingNode() ? "" : name.asText();
100 }
101
102 /**
103 * Returns the output lambda configured for this port. The lambda value is
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700104 * expressed as a frequency value. If the port type doesn't have a notion of
105 * lambdas, this returns an empty Optional.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700106 *
107 * @return an Optional that may contain a frequency value.
108 */
109 public Optional<Long> staticLambda() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700110 JsonNode sl = object.path(STATIC_LAMBDA);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700111 if (sl.isMissingNode()) {
112 return Optional.empty();
113 }
114 return Optional.of(sl.asLong());
115 }
116
117 /**
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700118 * Returns the port speed configured for this port. If the port doesn't have
119 * a notion of speed, this returns an empty Optional.
120 *
121 * @return a port speed value whose default is 0.
122 */
123 public Optional<Integer> speed() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700124 JsonNode s = object.path(SPEED);
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700125 if (s.isMissingNode()) {
126 return Optional.empty();
127 }
128 return Optional.of(s.asInt());
129 }
130
131 /**
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700132 * Sets the port type, or updates it if it's already set. A null argument removes
133 * this field.
134 *
135 * @param type the port type
136 * @return this OpticalPortConfig instance
137 */
138 public OpticalPortConfig portType(Port.Type type) {
139 // if unspecified, ideally fall back on FIBER or PACKET.
140 String pt = (type == null) ? null : type.toString();
141 return (OpticalPortConfig) setOrClear(TYPE, pt);
142 }
143
144 /**
145 * Sets the port name, or updates it if already set. A null argument removes
146 * this field.
147 *
148 * @param name the port's name
149 * @return this OpticalPortConfig instance
150 */
151 public OpticalPortConfig portName(String name) {
152 return (OpticalPortConfig) setOrClear(NAME, name);
153 }
154
155 /**
156 * Sets the port name from port number, or updates it if already set. A null
157 * argument removes this field.
158 *
159 * @param name the port number, to be used as name
160 * @return this OpticalPortConfig instance
161 */
162 public OpticalPortConfig portNumberName(Long name) {
163 return (OpticalPortConfig) setOrClear(PORT, name);
164 }
165
166 /**
167 * Sets the output port name, or updates it if already set. A null argument
168 * removes this field.
169 *
170 * @param name the output port's name
171 * @return this OpticalPortConfig instance
172 */
173 public OpticalPortConfig staticPort(String name) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700174 return (OpticalPortConfig) setOrClear(STATIC_PORT, name);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700175 }
176
177 /**
178 * Sets the output lambda index, or updates it if already set. A null argument
179 * removes this field.
180 *
181 * @param index the output lambda
182 * @return this OpticalPortConfig instance
183 */
184 public OpticalPortConfig staticLambda(Long index) {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -0700185 return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700186 }
187
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700188 /**
189 * Sets the port speed, or updates it if already set. A null argument
190 * removes this field.
191 *
192 * @param bw the port bandwidth
193 * @return this OpticalPortConfig instance
194 */
195 public OpticalPortConfig speed(Integer bw) {
196 return (OpticalPortConfig) setOrClear(SPEED, bw);
197 }
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700198}