Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
Thomas Vachuska | 4998caa | 2015-08-26 13:28:38 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config.basics; |
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 | |
| 26 | |
| 27 | /** |
| 28 | * Configurations for an optical port on a device. |
| 29 | */ |
| 30 | public class OpticalPortConfig extends Config<ConnectPoint> { |
| 31 | // optical type {OMS, OCH, ODUClt, fiber} |
| 32 | public static final String TYPE = "type"; |
| 33 | |
| 34 | // port name. "name" is the alphanumeric name of the port, but "port" refers |
| 35 | // 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] | 36 | // alphanumeric names). |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 37 | public static final String NAME = "name"; |
| 38 | public static final String PORT = "port"; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 39 | public static final String STATIC_PORT = "staticPort"; |
| 40 | public static final String STATIC_LAMBDA = "staticLambda"; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 41 | |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 42 | // **Linc-OE : remove if it's not needed after all.** |
| 43 | public static final String SPEED = "speed"; |
| 44 | |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Returns the Enum value representing the type of port. |
| 47 | * |
| 48 | * @return the port type, or null if invalid or unset |
| 49 | */ |
| 50 | public Port.Type type() { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 51 | JsonNode type = object.path(TYPE); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 52 | if (type.isMissingNode()) { |
| 53 | return null; |
| 54 | } |
| 55 | return Port.Type.valueOf(type.asText()); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the port name associated with this port configuration. The Name |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 60 | * is an alphanumeric string. |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 61 | * |
| 62 | * @return the name of this port, else, an empty string |
| 63 | */ |
| 64 | public String name() { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 65 | return getStringValue(NAME); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns a stringified representation of the port number, configured in |
| 70 | * some port types without an alphanumeric name as the port name. |
| 71 | * |
| 72 | * @return A string representation of the port number |
| 73 | */ |
| 74 | public String numberName() { |
| 75 | return getStringValue(PORT); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the string-representation of name of the output port. This is |
| 80 | * usually an OMS port for an OCH input ports, or an OCH port for ODU input |
| 81 | * ports. |
| 82 | * |
| 83 | * @return the name of this port, else, an empty string |
| 84 | */ |
| 85 | public String staticPort() { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 86 | return getStringValue(STATIC_PORT); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | private String getStringValue(String field) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 90 | JsonNode name = object.path(field); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 91 | return name.isMissingNode() ? "" : name.asText(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the output lambda configured for this port. The lambda value is |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 96 | * expressed as a frequency value. If the port type doesn't have a notion of |
| 97 | * lambdas, this returns an empty Optional. |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 98 | * |
| 99 | * @return an Optional that may contain a frequency value. |
| 100 | */ |
| 101 | public Optional<Long> staticLambda() { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 102 | JsonNode sl = object.path(STATIC_LAMBDA); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 103 | if (sl.isMissingNode()) { |
| 104 | return Optional.empty(); |
| 105 | } |
| 106 | return Optional.of(sl.asLong()); |
| 107 | } |
| 108 | |
| 109 | /** |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 110 | * Returns the port speed configured for this port. If the port doesn't have |
| 111 | * a notion of speed, this returns an empty Optional. |
| 112 | * |
| 113 | * @return a port speed value whose default is 0. |
| 114 | */ |
| 115 | public Optional<Integer> speed() { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 116 | JsonNode s = object.path(SPEED); |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 117 | if (s.isMissingNode()) { |
| 118 | return Optional.empty(); |
| 119 | } |
| 120 | return Optional.of(s.asInt()); |
| 121 | } |
| 122 | |
| 123 | /** |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 124 | * Sets the port type, or updates it if it's already set. A null argument removes |
| 125 | * this field. |
| 126 | * |
| 127 | * @param type the port type |
| 128 | * @return this OpticalPortConfig instance |
| 129 | */ |
| 130 | public OpticalPortConfig portType(Port.Type type) { |
| 131 | // if unspecified, ideally fall back on FIBER or PACKET. |
| 132 | String pt = (type == null) ? null : type.toString(); |
| 133 | return (OpticalPortConfig) setOrClear(TYPE, pt); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Sets the port name, or updates it if already set. A null argument removes |
| 138 | * this field. |
| 139 | * |
| 140 | * @param name the port's name |
| 141 | * @return this OpticalPortConfig instance |
| 142 | */ |
| 143 | public OpticalPortConfig portName(String name) { |
| 144 | return (OpticalPortConfig) setOrClear(NAME, name); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Sets the port name from port number, or updates it if already set. A null |
| 149 | * argument removes this field. |
| 150 | * |
| 151 | * @param name the port number, to be used as name |
| 152 | * @return this OpticalPortConfig instance |
| 153 | */ |
| 154 | public OpticalPortConfig portNumberName(Long name) { |
| 155 | return (OpticalPortConfig) setOrClear(PORT, name); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Sets the output port name, or updates it if already set. A null argument |
| 160 | * removes this field. |
| 161 | * |
| 162 | * @param name the output port's name |
| 163 | * @return this OpticalPortConfig instance |
| 164 | */ |
| 165 | public OpticalPortConfig staticPort(String name) { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 166 | return (OpticalPortConfig) setOrClear(STATIC_PORT, name); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets the output lambda index, or updates it if already set. A null argument |
| 171 | * removes this field. |
| 172 | * |
| 173 | * @param index the output lambda |
| 174 | * @return this OpticalPortConfig instance |
| 175 | */ |
| 176 | public OpticalPortConfig staticLambda(Long index) { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 177 | return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Ayaka Koshibe | 1a00251 | 2015-09-03 13:09:23 -0700 | [diff] [blame] | 180 | /** |
| 181 | * Sets the port speed, or updates it if already set. A null argument |
| 182 | * removes this field. |
| 183 | * |
| 184 | * @param bw the port bandwidth |
| 185 | * @return this OpticalPortConfig instance |
| 186 | */ |
| 187 | public OpticalPortConfig speed(Integer bw) { |
| 188 | return (OpticalPortConfig) setOrClear(SPEED, bw); |
| 189 | } |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 190 | } |