Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | */ |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.config; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.Optional; |
| 19 | |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 20 | import org.onosproject.net.config.Config; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.Port; |
| 23 | |
| 24 | import com.fasterxml.jackson.databind.JsonNode; |
| 25 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 26 | import static org.onosproject.net.config.Config.FieldPresence.OPTIONAL; |
| 27 | |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Configurations for an optical port on a device. |
Yuta HIGUCHI | 5ccaefb | 2016-09-07 11:18:13 -0700 | [diff] [blame] | 31 | * |
| 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 Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 46 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 47 | public final class OpticalPortConfig extends Config<ConnectPoint> { |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 48 | |
Yuta HIGUCHI | 5ccaefb | 2016-09-07 11:18:13 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Configuration key for {@link OpticalPortConfig}. |
| 51 | */ |
| 52 | public static final String CONFIG_KEY = "optical"; |
| 53 | |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 54 | // 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 59 | // alphanumeric names). |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 60 | public static final String NAME = "name"; |
| 61 | public static final String PORT = "port"; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 62 | public static final String STATIC_PORT = "staticPort"; |
| 63 | public static final String STATIC_LAMBDA = "staticLambda"; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 64 | |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 65 | // **Linc-OE : remove if it's not needed after all.** |
| 66 | public static final String SPEED = "speed"; |
| 67 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 68 | @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 Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 74 | /** |
| 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 Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 80 | JsonNode type = object.path(TYPE); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 81 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 89 | * is an alphanumeric string. |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 90 | * |
| 91 | * @return the name of this port, else, an empty string |
| 92 | */ |
| 93 | public String name() { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 94 | 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 Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 105 | } |
| 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 115 | return getStringValue(STATIC_PORT); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private String getStringValue(String field) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 119 | JsonNode name = object.path(field); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 120 | return name.isMissingNode() ? "" : name.asText(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the output lambda configured for this port. The lambda value is |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 125 | * expressed as a frequency value. If the port type doesn't have a notion of |
| 126 | * lambdas, this returns an empty Optional. |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 127 | * |
| 128 | * @return an Optional that may contain a frequency value. |
| 129 | */ |
| 130 | public Optional<Long> staticLambda() { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 131 | JsonNode sl = object.path(STATIC_LAMBDA); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 132 | if (sl.isMissingNode()) { |
| 133 | return Optional.empty(); |
| 134 | } |
| 135 | return Optional.of(sl.asLong()); |
| 136 | } |
| 137 | |
| 138 | /** |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 139 | * 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 Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 145 | JsonNode s = object.path(SPEED); |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 146 | if (s.isMissingNode()) { |
| 147 | return Optional.empty(); |
| 148 | } |
| 149 | return Optional.of(s.asInt()); |
| 150 | } |
| 151 | |
| 152 | /** |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 153 | * 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 195 | return (OpticalPortConfig) setOrClear(STATIC_PORT, name); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 199 | * Sets the output lambda center frequency, or updates it if already set. |
| 200 | * A null argument removes this field. |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 201 | * |
| 202 | * @param index the output lambda |
| 203 | * @return this OpticalPortConfig instance |
| 204 | */ |
| 205 | public OpticalPortConfig staticLambda(Long index) { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 206 | return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 209 | /** |
| 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 Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 219 | } |