blob: dfb494d60186229a994fac31a5db88246b25b9e7 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
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 Vachuska4998caa2015-08-26 13:28:38 -070016package org.onosproject.net.config.basics;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070017
18import java.util.Optional;
19
Ray Milkeya4122362015-08-18 15:19:08 -070020import org.onosproject.net.config.Config;
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070021import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.Port;
23
24import com.fasterxml.jackson.databind.JsonNode;
25
26
27/**
28 * Configurations for an optical port on a device.
29 */
30public 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 Koshibee39c23a2015-08-03 18:17:43 -070036 // alphanumeric names).
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070037 public static final String NAME = "name";
38 public static final String PORT = "port";
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070039 public static final String STATIC_PORT = "staticPort";
40 public static final String STATIC_LAMBDA = "staticLambda";
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070041
Ayaka Koshibe1a002512015-09-03 13:09:23 -070042 // **Linc-OE : remove if it's not needed after all.**
43 public static final String SPEED = "speed";
44
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070045 /**
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 Vachuska0a400ea2015-09-04 11:25:03 -070051 JsonNode type = object.path(TYPE);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070052 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 Koshibee39c23a2015-08-03 18:17:43 -070060 * is an alphanumeric string.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070061 *
62 * @return the name of this port, else, an empty string
63 */
64 public String name() {
Ayaka Koshibee39c23a2015-08-03 18:17:43 -070065 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 Koshibe2cfc65c2015-07-29 22:46:54 -070076 }
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 Koshibee39c23a2015-08-03 18:17:43 -070086 return getStringValue(STATIC_PORT);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070087 }
88
89 private String getStringValue(String field) {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070090 JsonNode name = object.path(field);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070091 return name.isMissingNode() ? "" : name.asText();
92 }
93
94 /**
95 * Returns the output lambda configured for this port. The lambda value is
Ayaka Koshibe1a002512015-09-03 13:09:23 -070096 * expressed as a frequency value. If the port type doesn't have a notion of
97 * lambdas, this returns an empty Optional.
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -070098 *
99 * @return an Optional that may contain a frequency value.
100 */
101 public Optional<Long> staticLambda() {
Thomas Vachuska0a400ea2015-09-04 11:25:03 -0700102 JsonNode sl = object.path(STATIC_LAMBDA);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700103 if (sl.isMissingNode()) {
104 return Optional.empty();
105 }
106 return Optional.of(sl.asLong());
107 }
108
109 /**
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700110 * 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 Vachuska0a400ea2015-09-04 11:25:03 -0700116 JsonNode s = object.path(SPEED);
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700117 if (s.isMissingNode()) {
118 return Optional.empty();
119 }
120 return Optional.of(s.asInt());
121 }
122
123 /**
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700124 * 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 Koshibee39c23a2015-08-03 18:17:43 -0700166 return (OpticalPortConfig) setOrClear(STATIC_PORT, name);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700167 }
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 Koshibee39c23a2015-08-03 18:17:43 -0700177 return (OpticalPortConfig) setOrClear(STATIC_LAMBDA, index);
Ayaka Koshibe2cfc65c2015-07-29 22:46:54 -0700178 }
179
Ayaka Koshibe1a002512015-09-03 13:09:23 -0700180 /**
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 Koshibe2cfc65c2015-07-29 22:46:54 -0700190}