d5b499c42b9737cfac8783eb8ec4943180d75aad
[controller.git] / opendaylight / topologymanager / src / main / java / org / opendaylight / controller / topologymanager / TopologyUserLinkConfig.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.topologymanager;
11
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;
19
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;
28
29 /**
30  * The Interface provides methods to manipulate user configured link.
31  */
32 @XmlRootElement
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);
44
45     public enum STATUS {
46         SUCCESS("Success"), LINKDOWN("Link Down"), INCORRECT(
47                 "Incorrect Connection");
48         private STATUS(String name) {
49             this.name = name;
50         }
51
52         private String name;
53
54         public String toString() {
55             return name;
56         }
57
58         public static STATUS fromString(String str) {
59             if (str == null)
60                 return LINKDOWN;
61             if (str.equals(SUCCESS.toString()))
62                 return SUCCESS;
63             if (str.equals(LINKDOWN.toString()))
64                 return LINKDOWN;
65             if (str.equals(INCORRECT.toString()))
66                 return INCORRECT;
67             return LINKDOWN;
68         }
69     }
70
71     @XmlElement
72     private String status;
73     @XmlElement
74     private String name;
75     @XmlElement
76     private String srcNodeIDType;
77     @XmlElement
78     private String srcSwitchId;
79     @XmlElement
80     private String srcNodeConnectorIDType;
81     @XmlElement
82     private String srcPort;
83     @XmlElement
84     private String dstNodeIDType;
85     @XmlElement
86     private String dstSwitchId;
87     @XmlElement
88     private String dstNodeConnectorIDType;
89     @XmlElement
90     private String dstPort;
91
92     public TopologyUserLinkConfig() {
93         super();
94         status = STATUS.LINKDOWN.toString();
95     }
96
97         public TopologyUserLinkConfig(String name, String srcNodeIDType,
98                         String srcSwitchId, String srcNodeConnectorIDType, String srcPort,
99                         String dstNodeIDType, String dstSwitchId,
100                         String dstNodeConnectorIDType, String dstPort) {
101         super();
102         this.name = name;
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;
111     }
112
113     public String getSrcNodeIDType() {
114                 return srcNodeIDType;
115         }
116
117         public void setSrcNodeIDType(String srcNodeIDType) {
118                 this.srcNodeIDType = srcNodeIDType;
119         }
120
121         public String getSrcNodeConnectorIDType() {
122                 return srcNodeConnectorIDType;
123         }
124
125         public void setSrcNodeConnectorIDType(String srcNodeConnectorIDType) {
126                 this.srcNodeConnectorIDType = srcNodeConnectorIDType;
127         }
128
129         public String getDstNodeIDType() {
130                 return dstNodeIDType;
131         }
132
133         public void setDstNodeIDType(String dstNodeIDType) {
134                 this.dstNodeIDType = dstNodeIDType;
135         }
136
137         public String getDstNodeConnectorIDType() {
138                 return dstNodeConnectorIDType;
139         }
140
141         public void setDstNodeConnectorIDType(String dstNodeConnectorIDType) {
142                 this.dstNodeConnectorIDType = dstNodeConnectorIDType;
143         }
144
145         public String getName() {
146         return name;
147     }
148
149     public void setName(String name) {
150         this.name = name;
151     }
152
153     public String getSrcSwitchId() {
154         return srcSwitchId;
155     }
156
157     public long getSrcSwitchIDLong() {
158         return getSwitchIDLong(srcSwitchId);
159     }
160
161     public void setSrcSwitchId(String srcSwitchId) {
162         this.srcSwitchId = srcSwitchId;
163     }
164
165     public String getDstSwitchId() {
166         return dstSwitchId;
167     }
168
169     public long getDstSwitchIDLong() {
170         return getSwitchIDLong(dstSwitchId);
171     }
172
173     public void setDstSwitchId(String dstSwitchId) {
174         this.dstSwitchId = dstSwitchId;
175     }
176
177     public String getSrcPort() {
178         return srcPort;
179     }
180
181     public void setSrcPort(String srcPort) {
182         this.srcPort = srcPort;
183     }
184
185     public String getDstPort() {
186         return dstPort;
187     }
188
189     public void setDstPort(String dstPort) {
190         this.dstPort = dstPort;
191     }
192
193     public STATUS getStatus() {
194         return STATUS.fromString(status);
195     }
196
197     public void setStatus(STATUS s) {
198         this.status = s.toString();
199     }
200
201     private boolean isValidSwitchId(String switchId) {
202         return (switchId != null && (switchId.matches(regexDatapathID) || switchId
203                 .matches(regexDatapathIDLong)));
204     }
205
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)) {
212             return true;
213         } else {
214                         logger.warn("Invalid node id type {}", typeStr);
215                 return false;
216         }
217     }
218
219     private boolean isValidPortId(String portId, String nodeConnectorType) {
220                 if (NodeConnectorIDType.getClassType(nodeConnectorType) == null) {
221                         logger.warn("Invalid node connector id type {}", nodeConnectorType);
222                         return false; 
223                 }
224                 
225                 return true;
226         }
227
228     private long getSwitchIDLong(String switchId) {
229         int radix = 16;
230         String switchString = "0";
231
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("-", "");
239             } else {
240                 // Handle the 0123456789ABCDEF notation
241                 switchString = switchId;
242             }
243         }
244         return Long.parseLong(switchString, radix);
245     }
246
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) {
252             return false;
253                 }
254                 
255                 if (!isValidSwitchId(srcSwitchId, srcNodeIDType) || 
256                         !isValidSwitchId(dstSwitchId, dstNodeIDType)) {
257                         logger.warn("Invalid switch id");
258                         return false;
259                 }
260                 
261                 if (!isValidPortId(srcPort, srcNodeConnectorIDType) || 
262                         !isValidPortId(dstPort, dstNodeConnectorIDType)) {
263                         logger.warn("Invalid port id");
264                         return false;
265                 }
266                         
267                 return true;
268     }
269
270     public boolean isSrcPortByName() {
271         try {
272             Short.parseShort(srcPort);
273         } catch (Exception e) {
274             return true;
275         }
276         return false;
277     }
278
279     public boolean isDstPortByName() {
280         try {
281             Short.parseShort(dstPort);
282         } catch (Exception e) {
283             return true;
284         }
285         return false;
286     }
287
288     public static List<String> getGuiFieldsNames() {
289         List<String> fieldList = new ArrayList<String>();
290         for (String str : guiFields) {
291             fieldList.add(str);
292         }
293         return fieldList;
294     }
295
296     @Override
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 + "]";
305     }
306
307     @Override
308     public int hashCode() {
309         return HashCodeBuilder.reflectionHashCode(this);
310     }
311
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())) {
318             return true;
319         }
320         return false;
321     }
322
323     @Override
324     public boolean equals(Object obj) {
325         if (this == obj)
326             return true;
327         if (obj == null)
328             return false;
329         if (getClass() != obj.getClass())
330             return false;
331         TopologyUserLinkConfig other = (TopologyUserLinkConfig) obj;
332         if (dstPort == null) {
333             if (other.dstPort != null)
334                 return false;
335         } else if (!dstPort.equals(other.dstPort))
336             return false;
337         if (dstSwitchId == null) {
338             if (other.dstSwitchId != null)
339                 return false;
340         } else if (!dstSwitchId.equals(other.dstSwitchId))
341             return false;
342         if (srcPort == null) {
343             if (other.srcPort != null)
344                 return false;
345         } else if (!srcPort.equals(other.srcPort))
346             return false;
347         if (srcSwitchId == null) {
348             if (other.srcSwitchId != null)
349                 return false;
350         } else if (!srcSwitchId.equals(other.srcSwitchId))
351             return false;
352         return true;
353     }
354 }