83cb8b1cc9786f42757e1f609dedf724943c0f19
[controller.git] / opendaylight / switchmanager / api / src / main / java / org / opendaylight / controller / switchmanager / SpanConfig.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.switchmanager;
11
12 import java.io.Serializable;
13 import java.lang.reflect.Field;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.opendaylight.controller.sal.core.ConstructionException;
18 import org.opendaylight.controller.sal.core.Node;
19 import org.opendaylight.controller.sal.core.NodeConnector;
20 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
21 import org.opendaylight.controller.sal.utils.GUIField;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The class represents a Span Port configuration for a network node.
27  */
28 public class SpanConfig implements Serializable {
29     protected static final Logger logger = LoggerFactory
30     .getLogger(SpanConfig.class);
31     private static final long serialVersionUID = 1L;
32     private static final String guiFields[] = { GUIField.NODE.toString(),
33             GUIField.SPANPORTS.toString() };
34
35     // Order matters: JSP file expects following fields in the following order
36     private String nodeId;
37     private String spanPort;
38
39     public SpanConfig() {
40     }
41
42     public String getNodeId() {
43         return nodeId;
44     }
45
46     public String getSpanPort() {
47         return spanPort;
48     }
49
50     public Node getNode() {
51         return Node.fromString(nodeId);
52     }
53
54     private boolean hasValidNodeId() {
55         return (getNode() != null);
56     }
57
58     private boolean hasValidSpanPort() {
59         return (spanPort != null && !spanPort.isEmpty());
60     }
61
62     public boolean isValidConfig() {
63         return (hasValidNodeId() && hasValidSpanPort());
64     }
65
66     @Override
67     public int hashCode() {
68         final int prime = 31;
69         int result = 1;
70         result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
71         result = prime * result
72                 + ((spanPort == null) ? 0 : spanPort.hashCode());
73         return result;
74     }
75
76     @Override
77     public boolean equals(Object obj) {
78         if (this == obj)
79             return true;
80         if (obj == null)
81             return false;
82         if (getClass() != obj.getClass())
83             return false;
84         SpanConfig other = (SpanConfig) obj;
85         if (nodeId == null) {
86             if (other.nodeId != null)
87                 return false;
88         } else if (!nodeId.equals(other.nodeId))
89             return false;
90         if (spanPort == null) {
91             if (other.spanPort != null)
92                 return false;
93         } else if (!spanPort.equals(other.spanPort))
94             return false;
95         return true;
96     }
97
98     public static ArrayList<String> getFieldsNames() {
99         ArrayList<String> fieldList = new ArrayList<String>();
100         for (Field fld : SpanConfig.class.getDeclaredFields()) {
101             fieldList.add(fld.getName());
102         }
103         //remove the two static fields
104         for (short i = 0; i < 2; i++) {
105             fieldList.remove(0);
106         }
107         return fieldList;
108     }
109
110     public static List<String> getGuiFieldsNames() {
111         List<String> fieldList = new ArrayList<String>();
112         for (String str : guiFields) {
113             fieldList.add(str);
114         }
115         return fieldList;
116     }
117
118     public ArrayList<NodeConnector> getPortArrayList() {
119         Node node = Node.fromString(nodeId);
120         ArrayList<NodeConnector> portList = new ArrayList<NodeConnector>();
121         String[] elemArray = spanPort.split(",");
122         for (String elem : elemArray) {
123             if (elem.contains("-")) {
124                 String[] limits = elem.split("-");
125                 for (short j = Short.valueOf(limits[0]); j <= Short
126                         .valueOf(limits[1]); j++) {
127                     try {
128                         portList.add(new NodeConnector(
129                                 NodeConnectorIDType.OPENFLOW, Short.valueOf(j),
130                                 node));
131                     } catch (ConstructionException e) {
132                         logger.error("",e);
133                     }
134                 }
135             } else {
136                 try {
137                     portList.add(new NodeConnector(
138                             NodeConnectorIDType.OPENFLOW, Short.valueOf(elem),
139                             node));
140                 } catch (NumberFormatException e) {
141                     logger.error("",e);
142                 } catch (ConstructionException e) {
143                     logger.error("",e);
144                 }
145             }
146         }
147         return portList;
148     }
149
150     public boolean matchNode(String nodeId) {
151         return this.nodeId.equals(nodeId);
152     }
153
154     @Override
155     public String toString() {
156         return ("Span Config [nodeId=" + nodeId + " spanPort=" + spanPort + "]");
157     }
158 }