3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.topologymanager;
12 import java.io.Serializable;
13 import java.util.ArrayList;
14 import java.util.List;
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlElement;
18 import javax.xml.bind.annotation.XmlRootElement;
20 import org.apache.commons.lang3.builder.HashCodeBuilder;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.core.Node.NodeIDType;
23 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25 import org.opendaylight.controller.sal.utils.GUIField;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The Interface provides methods to manipulate user configured link.
33 @XmlAccessorType(XmlAccessType.NONE)
34 public class TopologyUserLinkConfig implements Serializable {
35 private static final long serialVersionUID = 1L;
36 private static final String regexDatapathID = "^([0-9a-fA-F]{1,2}[:-]){7}[0-9a-fA-F]{1,2}$";
37 private static final String regexDatapathIDLong = "^[0-9a-fA-F]{1,16}$";
38 private static final String guiFields[] = { GUIField.STATUS.toString(),
39 GUIField.NAME.toString(), GUIField.SRCNODE.toString(),
40 GUIField.SRCPORT.toString(), GUIField.DSTNODE.toString(),
41 GUIField.DSTPORT.toString() };
42 private static final Logger logger = LoggerFactory
43 .getLogger(TopologyUserLinkConfig.class);
46 SUCCESS("Success"), LINKDOWN("Link Down"), INCORRECT(
47 "Incorrect Connection");
48 private STATUS(String name) {
54 public String toString() {
58 public static STATUS fromString(String str) {
61 if (str.equals(SUCCESS.toString()))
63 if (str.equals(LINKDOWN.toString()))
65 if (str.equals(INCORRECT.toString()))
72 private String status;
76 private String srcNodeIDType;
78 private String srcSwitchId;
80 private String srcNodeConnectorIDType;
82 private String srcPort;
84 private String dstNodeIDType;
86 private String dstSwitchId;
88 private String dstNodeConnectorIDType;
90 private String dstPort;
92 public TopologyUserLinkConfig() {
94 status = STATUS.LINKDOWN.toString();
97 public TopologyUserLinkConfig(String name, String srcNodeIDType,
98 String srcSwitchId, String srcNodeConnectorIDType, String srcPort,
99 String dstNodeIDType, String dstSwitchId,
100 String dstNodeConnectorIDType, String dstPort) {
103 this.srcNodeIDType = srcNodeIDType;
104 this.srcSwitchId = srcSwitchId;
105 this.dstNodeIDType = dstNodeIDType;
106 this.dstSwitchId = dstSwitchId;
107 this.srcNodeConnectorIDType = srcNodeConnectorIDType;
108 this.srcPort = srcPort;
109 this.dstNodeConnectorIDType = dstNodeConnectorIDType;
110 this.dstPort = dstPort;
113 public String getSrcNodeIDType() {
114 return srcNodeIDType;
117 public void setSrcNodeIDType(String srcNodeIDType) {
118 this.srcNodeIDType = srcNodeIDType;
121 public String getSrcNodeConnectorIDType() {
122 return srcNodeConnectorIDType;
125 public void setSrcNodeConnectorIDType(String srcNodeConnectorIDType) {
126 this.srcNodeConnectorIDType = srcNodeConnectorIDType;
129 public String getDstNodeIDType() {
130 return dstNodeIDType;
133 public void setDstNodeIDType(String dstNodeIDType) {
134 this.dstNodeIDType = dstNodeIDType;
137 public String getDstNodeConnectorIDType() {
138 return dstNodeConnectorIDType;
141 public void setDstNodeConnectorIDType(String dstNodeConnectorIDType) {
142 this.dstNodeConnectorIDType = dstNodeConnectorIDType;
145 public String getName() {
149 public void setName(String name) {
153 public String getSrcSwitchId() {
157 public long getSrcSwitchIDLong() {
158 return getSwitchIDLong(srcSwitchId);
161 public void setSrcSwitchId(String srcSwitchId) {
162 this.srcSwitchId = srcSwitchId;
165 public String getDstSwitchId() {
169 public long getDstSwitchIDLong() {
170 return getSwitchIDLong(dstSwitchId);
173 public void setDstSwitchId(String dstSwitchId) {
174 this.dstSwitchId = dstSwitchId;
177 public String getSrcPort() {
181 public void setSrcPort(String srcPort) {
182 this.srcPort = srcPort;
185 public String getDstPort() {
189 public void setDstPort(String dstPort) {
190 this.dstPort = dstPort;
193 public STATUS getStatus() {
194 return STATUS.fromString(status);
197 public void setStatus(STATUS s) {
198 this.status = s.toString();
201 private boolean isValidSwitchId(String switchId) {
202 return (switchId != null && (switchId.matches(regexDatapathID) || switchId
203 .matches(regexDatapathIDLong)));
206 private boolean isValidSwitchId(String switchId, String typeStr) {
207 if (typeStr.equals(NodeIDType.OPENFLOW)) {
208 return isValidSwitchId(switchId);
209 } else if (typeStr.equals(NodeIDType.ONEPK) ||
210 typeStr.equals(NodeIDType.PCEP) ||
211 typeStr.equals(NodeIDType.PRODUCTION)) {
214 logger.warn("Invalid node id type {}", typeStr);
219 private boolean isValidPortId(String portId, String nodeConnectorType) {
220 if (NodeConnectorIDType.getClassType(nodeConnectorType) == null) {
221 logger.warn("Invalid node connector id type {}", nodeConnectorType);
228 private long getSwitchIDLong(String switchId) {
230 String switchString = "0";
232 if (isValidSwitchId(switchId)) {
233 if (switchId.contains(":")) {
234 // Handle the 00:00:AA:BB:CC:DD:EE:FF notation
235 switchString = switchId.replace(":", "");
236 } else if (switchId.contains("-")) {
237 // Handle the 00-00-AA-BB-CC-DD-EE-FF notation
238 switchString = switchId.replace("-", "");
240 // Handle the 0123456789ABCDEF notation
241 switchString = switchId;
244 return Long.parseLong(switchString, radix);
247 public boolean isValid() {
248 if (name == null || srcSwitchId == null || dstSwitchId == null
249 || srcPort == null || dstPort == null || srcNodeIDType == null
250 || dstNodeIDType == null || srcNodeConnectorIDType == null
251 || dstNodeConnectorIDType == null) {
255 if (!isValidSwitchId(srcSwitchId, srcNodeIDType) ||
256 !isValidSwitchId(dstSwitchId, dstNodeIDType)) {
257 logger.warn("Invalid switch id");
261 if (!isValidPortId(srcPort, srcNodeConnectorIDType) ||
262 !isValidPortId(dstPort, dstNodeConnectorIDType)) {
263 logger.warn("Invalid port id");
270 public boolean isSrcPortByName() {
272 Short.parseShort(srcPort);
273 } catch (Exception e) {
279 public boolean isDstPortByName() {
281 Short.parseShort(dstPort);
282 } catch (Exception e) {
288 public static List<String> getGuiFieldsNames() {
289 List<String> fieldList = new ArrayList<String>();
290 for (String str : guiFields) {
297 public String toString() {
298 return "ITopologyUserLinkConfig [status=" + status + ", name=" + name
299 + ", srcNodeIDType=" + srcNodeIDType + ", srcSwitchId="
300 + srcSwitchId + ", srcNodeConnectorIDType="
301 + srcNodeConnectorIDType + ", srcPort=" + srcPort
302 + ", dstNodeIDType=" + dstNodeIDType + ", dstId="
303 + dstSwitchId + ", dstNodeConnectorIDType="
304 + dstNodeConnectorIDType + ", dstPort=" + dstPort + "]";
308 public int hashCode() {
309 return HashCodeBuilder.reflectionHashCode(this);
312 public boolean equals(Long srcNid, String srcPortName, Long dstNid,
313 String dstPortName) {
314 if (srcNid.equals(getSrcSwitchIDLong())
315 && dstNid.equals(getDstSwitchIDLong())
316 && srcPortName.equals(getSrcPort())
317 && dstPortName.equals(getDstPort())) {
324 public boolean equals(Object obj) {
329 if (getClass() != obj.getClass())
331 TopologyUserLinkConfig other = (TopologyUserLinkConfig) obj;
332 if (dstPort == null) {
333 if (other.dstPort != null)
335 } else if (!dstPort.equals(other.dstPort))
337 if (dstSwitchId == null) {
338 if (other.dstSwitchId != null)
340 } else if (!dstSwitchId.equals(other.dstSwitchId))
342 if (srcPort == null) {
343 if (other.srcPort != null)
345 } else if (!srcPort.equals(other.srcPort))
347 if (srcSwitchId == null) {
348 if (other.srcSwitchId != null)
350 } else if (!srcSwitchId.equals(other.srcSwitchId))