OpenDaylight Controller functional modules.
[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
16 import org.opendaylight.controller.sal.utils.GUIField;
17
18 /**
19  * Interface class that provides methods to manipulate user configured link
20  */
21 public class TopologyUserLinkConfig implements Serializable {
22     private static final long serialVersionUID = 1L;
23     private static final String regexDatapathID = "^([0-9a-fA-F]{1,2}[:-]){7}[0-9a-fA-F]{1,2}$";
24     private static final String regexDatapathIDLong = "^[0-9a-fA-F]{1,16}$";
25     private static final String guiFields[] = { GUIField.STATUS.toString(),
26             GUIField.NAME.toString(), GUIField.SRCNODE.toString(),
27             GUIField.SRCPORT.toString(), GUIField.DSTNODE.toString(),
28             GUIField.DSTPORT.toString() };
29
30     public enum STATUS {
31         SUCCESS("Success"), LINKDOWN("Link Down"), INCORRECT(
32                 "Incorrect Connection");
33         private STATUS(String name) {
34             this.name = name;
35         }
36
37         private String name;
38
39         public String toString() {
40             return name;
41         }
42
43         public static STATUS fromString(String str) {
44             if (str == null)
45                 return LINKDOWN;
46             if (str.equals(SUCCESS.toString()))
47                 return SUCCESS;
48             if (str.equals(LINKDOWN.toString()))
49                 return LINKDOWN;
50             if (str.equals(INCORRECT.toString()))
51                 return INCORRECT;
52             return LINKDOWN;
53         }
54     }
55
56     private String status;
57     private String name;
58     private String srcSwitchId;
59     private String srcPort;
60     private String dstSwitchId;
61     private String dstPort;
62
63     public TopologyUserLinkConfig() {
64         super();
65         status = STATUS.LINKDOWN.toString();
66     }
67
68     public TopologyUserLinkConfig(String name, String srcSwitchId,
69             String srcPort, String dstSwitchId, String dstPort) {
70         super();
71         this.name = name;
72         this.srcSwitchId = srcSwitchId;
73         this.dstSwitchId = dstSwitchId;
74         this.srcPort = srcPort;
75         this.dstPort = dstPort;
76     }
77
78     public String getName() {
79         return name;
80     }
81
82     public void setName(String name) {
83         this.name = name;
84     }
85
86     public String getSrcSwitchId() {
87         return srcSwitchId;
88     }
89
90     public long getSrcSwitchIDLong() {
91         return getSwitchIDLong(srcSwitchId);
92     }
93
94     public void setSrcSwitchId(String srcSwitchId) {
95         this.srcSwitchId = srcSwitchId;
96     }
97
98     public String getDstSwitchId() {
99         return dstSwitchId;
100     }
101
102     public long getDstSwitchIDLong() {
103         return getSwitchIDLong(dstSwitchId);
104     }
105
106     public void setDstSwitchId(String dstSwitchId) {
107         this.dstSwitchId = dstSwitchId;
108     }
109
110     public String getSrcPort() {
111         return srcPort;
112     }
113
114     public void setSrcPort(String srcPort) {
115         this.srcPort = srcPort;
116     }
117
118     public String getDstPort() {
119         return dstPort;
120     }
121
122     public void setDstPort(String dstPort) {
123         this.dstPort = dstPort;
124     }
125
126     public STATUS getStatus() {
127         return STATUS.fromString(status);
128     }
129
130     public void setStatus(STATUS s) {
131         this.status = s.toString();
132     }
133
134     private boolean isValidSwitchId(String switchId) {
135         return (switchId != null && (switchId.matches(regexDatapathID) || switchId
136                 .matches(regexDatapathIDLong)));
137     }
138
139     private long getSwitchIDLong(String switchId) {
140         int radix = 16;
141         String switchString = "0";
142
143         if (isValidSwitchId(switchId)) {
144             if (switchId.contains(":")) {
145                 // Handle the 00:00:AA:BB:CC:DD:EE:FF notation
146                 switchString = switchId.replace(":", "");
147             } else if (switchId.contains("-")) {
148                 // Handle the 00-00-AA-BB-CC-DD-EE-FF notation
149                 switchString = switchId.replace("-", "");
150             } else {
151                 // Handle the 0123456789ABCDEF notation
152                 switchString = switchId;
153             }
154         }
155         return Long.parseLong(switchString, radix);
156     }
157
158     public boolean isValid() {
159         if (name == null || srcSwitchId == null || dstSwitchId == null
160                 || srcPort == null || dstPort == null)
161             return false;
162         if (!isValidSwitchId(srcSwitchId) || !isValidSwitchId(dstSwitchId))
163             return false;
164         return true;
165     }
166
167     public boolean isSrcPortByName() {
168         try {
169             Short.parseShort(srcPort);
170         } catch (Exception e) {
171             return true;
172         }
173         return false;
174     }
175
176     public boolean isDstPortByName() {
177         try {
178             Short.parseShort(dstPort);
179         } catch (Exception e) {
180             return true;
181         }
182         return false;
183     }
184
185     public static List<String> getGuiFieldsNames() {
186         List<String> fieldList = new ArrayList<String>();
187         for (String str : guiFields) {
188             fieldList.add(str);
189         }
190         return fieldList;
191     }
192
193     @Override
194     public String toString() {
195         return "ITopologyUserLinkConfig [status=" + status + ", name=" + name
196                 + ", srcSwitchId=" + srcSwitchId + ", srcPort=" + srcPort
197                 + ", dstSwitchId=" + dstSwitchId + ", dstPort=" + dstPort + "]";
198     }
199
200     @Override
201     public int hashCode() {
202         final int prime = 31;
203         int result = 1;
204         result = prime * result + ((dstPort == null) ? 0 : dstPort.hashCode());
205         result = prime * result
206                 + ((dstSwitchId == null) ? 0 : dstSwitchId.hashCode());
207         result = prime * result + ((srcPort == null) ? 0 : srcPort.hashCode());
208         result = prime * result
209                 + ((srcSwitchId == null) ? 0 : srcSwitchId.hashCode());
210         return result;
211     }
212
213     public boolean equals(Long srcNid, String srcPortName, Long dstNid,
214             String dstPortName) {
215         if (srcNid.equals(getSrcSwitchIDLong())
216                 && dstNid.equals(getDstSwitchIDLong())
217                 && srcPortName.equals(getSrcPort())
218                 && dstPortName.equals(getDstPort())) {
219             return true;
220         }
221         return false;
222     }
223
224     @Override
225     public boolean equals(Object obj) {
226         if (this == obj)
227             return true;
228         if (obj == null)
229             return false;
230         if (getClass() != obj.getClass())
231             return false;
232         TopologyUserLinkConfig other = (TopologyUserLinkConfig) obj;
233         if (dstPort == null) {
234             if (other.dstPort != null)
235                 return false;
236         } else if (!dstPort.equals(other.dstPort))
237             return false;
238         if (dstSwitchId == null) {
239             if (other.dstSwitchId != null)
240                 return false;
241         } else if (!dstSwitchId.equals(other.dstSwitchId))
242             return false;
243         if (srcPort == null) {
244             if (other.srcPort != null)
245                 return false;
246         } else if (!srcPort.equals(other.srcPort))
247             return false;
248         if (srcSwitchId == null) {
249             if (other.srcSwitchId != null)
250                 return false;
251         } else if (!srcSwitchId.equals(other.srcSwitchId))
252             return false;
253         return true;
254     }
255 }