Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 1 | package org.onosproject.incubator.net.config.basics; |
| 2 | |
| 3 | import java.util.Optional; |
| 4 | |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame^] | 5 | import org.onosproject.net.config.Config; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 6 | import org.onosproject.net.ConnectPoint; |
| 7 | import org.onosproject.net.Port; |
| 8 | |
| 9 | import com.fasterxml.jackson.databind.JsonNode; |
| 10 | |
| 11 | |
| 12 | /** |
| 13 | * Configurations for an optical port on a device. |
| 14 | */ |
| 15 | public class OpticalPortConfig extends Config<ConnectPoint> { |
| 16 | // optical type {OMS, OCH, ODUClt, fiber} |
| 17 | public static final String TYPE = "type"; |
| 18 | |
| 19 | // port name. "name" is the alphanumeric name of the port, but "port" refers |
| 20 | // 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] | 21 | // alphanumeric names). |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 22 | public static final String NAME = "name"; |
| 23 | public static final String PORT = "port"; |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 24 | public static final String STATIC_PORT = "staticPort"; |
| 25 | public static final String STATIC_LAMBDA = "staticLambda"; |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Returns the Enum value representing the type of port. |
| 29 | * |
| 30 | * @return the port type, or null if invalid or unset |
| 31 | */ |
| 32 | public Port.Type type() { |
| 33 | JsonNode type = node.path(TYPE); |
| 34 | if (type.isMissingNode()) { |
| 35 | return null; |
| 36 | } |
| 37 | return Port.Type.valueOf(type.asText()); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns the port name associated with this port configuration. The Name |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 42 | * is an alphanumeric string. |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 43 | * |
| 44 | * @return the name of this port, else, an empty string |
| 45 | */ |
| 46 | public String name() { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 47 | return getStringValue(NAME); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns a stringified representation of the port number, configured in |
| 52 | * some port types without an alphanumeric name as the port name. |
| 53 | * |
| 54 | * @return A string representation of the port number |
| 55 | */ |
| 56 | public String numberName() { |
| 57 | return getStringValue(PORT); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns the string-representation of name of the output port. This is |
| 62 | * usually an OMS port for an OCH input ports, or an OCH port for ODU input |
| 63 | * ports. |
| 64 | * |
| 65 | * @return the name of this port, else, an empty string |
| 66 | */ |
| 67 | public String staticPort() { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 68 | return getStringValue(STATIC_PORT); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private String getStringValue(String field) { |
| 72 | JsonNode name = node.path(field); |
| 73 | return name.isMissingNode() ? "" : name.asText(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the output lambda configured for this port. The lambda value is |
| 78 | * expressed as a frequency value. |
| 79 | * |
| 80 | * @return an Optional that may contain a frequency value. |
| 81 | */ |
| 82 | public Optional<Long> staticLambda() { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 83 | JsonNode sl = node.path(STATIC_LAMBDA); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 84 | if (sl.isMissingNode()) { |
| 85 | return Optional.empty(); |
| 86 | } |
| 87 | return Optional.of(sl.asLong()); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets the port type, or updates it if it's already set. A null argument removes |
| 92 | * this field. |
| 93 | * |
| 94 | * @param type the port type |
| 95 | * @return this OpticalPortConfig instance |
| 96 | */ |
| 97 | public OpticalPortConfig portType(Port.Type type) { |
| 98 | // if unspecified, ideally fall back on FIBER or PACKET. |
| 99 | String pt = (type == null) ? null : type.toString(); |
| 100 | return (OpticalPortConfig) setOrClear(TYPE, pt); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets the port name, or updates it if already set. A null argument removes |
| 105 | * this field. |
| 106 | * |
| 107 | * @param name the port's name |
| 108 | * @return this OpticalPortConfig instance |
| 109 | */ |
| 110 | public OpticalPortConfig portName(String name) { |
| 111 | return (OpticalPortConfig) setOrClear(NAME, name); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Sets the port name from port number, or updates it if already set. A null |
| 116 | * argument removes this field. |
| 117 | * |
| 118 | * @param name the port number, to be used as name |
| 119 | * @return this OpticalPortConfig instance |
| 120 | */ |
| 121 | public OpticalPortConfig portNumberName(Long name) { |
| 122 | return (OpticalPortConfig) setOrClear(PORT, name); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Sets the output port name, or updates it if already set. A null argument |
| 127 | * removes this field. |
| 128 | * |
| 129 | * @param name the output port's name |
| 130 | * @return this OpticalPortConfig instance |
| 131 | */ |
| 132 | public OpticalPortConfig staticPort(String name) { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 133 | return (OpticalPortConfig) setOrClear(STATIC_PORT, name); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Sets the output lambda index, or updates it if already set. A null argument |
| 138 | * removes this field. |
| 139 | * |
| 140 | * @param index the output lambda |
| 141 | * @return this OpticalPortConfig instance |
| 142 | */ |
| 143 | public OpticalPortConfig staticLambda(Long index) { |
Ayaka Koshibe | e39c23a | 2015-08-03 18:17:43 -0700 | [diff] [blame] | 144 | return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index); |
Ayaka Koshibe | 2cfc65c | 2015-07-29 22:46:54 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } |