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