Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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. |
| 31 | */ |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 32 | public final class OpticalPortConfig extends Config<ConnectPoint> { |
Yuta HIGUCHI | b9af6b7 | 2016-06-10 10:52:58 -0700 | [diff] [blame] | 33 | |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 34 | // 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 39 | // alphanumeric names). |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 40 | public static final String NAME = "name"; |
| 41 | public static final String PORT = "port"; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 42 | public static final String STATIC_PORT = "staticPort"; |
| 43 | public static final String STATIC_LAMBDA = "staticLambda"; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 44 | |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 45 | // **Linc-OE : remove if it's not needed after all.** |
| 46 | public static final String SPEED = "speed"; |
| 47 | |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 48 | @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 Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 54 | /** |
| 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 Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 60 | JsonNode type = object.path(TYPE); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 61 | 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 69 | * is an alphanumeric string. |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 70 | * |
| 71 | * @return the name of this port, else, an empty string |
| 72 | */ |
| 73 | public String name() { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 74 | 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 Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 85 | } |
| 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 95 | return getStringValue(STATIC_PORT); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | private String getStringValue(String field) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 99 | JsonNode name = object.path(field); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 100 | return name.isMissingNode() ? "" : name.asText(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the output lambda configured for this port. The lambda value is |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 105 | * expressed as a frequency value. If the port type doesn't have a notion of |
| 106 | * lambdas, this returns an empty Optional. |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 107 | * |
| 108 | * @return an Optional that may contain a frequency value. |
| 109 | */ |
| 110 | public Optional<Long> staticLambda() { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 111 | JsonNode sl = object.path(STATIC_LAMBDA); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 112 | if (sl.isMissingNode()) { |
| 113 | return Optional.empty(); |
| 114 | } |
| 115 | return Optional.of(sl.asLong()); |
| 116 | } |
| 117 | |
| 118 | /** |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 119 | * 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 Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 125 | JsonNode s = object.path(SPEED); |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 126 | if (s.isMissingNode()) { |
| 127 | return Optional.empty(); |
| 128 | } |
| 129 | return Optional.of(s.asInt()); |
| 130 | } |
| 131 | |
| 132 | /** |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 133 | * 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 175 | return (OpticalPortConfig) setOrClear(STATIC_PORT, name); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 176 | } |
| 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 Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 186 | return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 189 | /** |
| 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 Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 199 | } |