Removing references to a prior branded controller
[controller.git] / opendaylight / switchmanager / 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.apache.commons.lang3.builder.EqualsBuilder;
18 import org.apache.commons.lang3.builder.HashCodeBuilder;
19 import org.opendaylight.controller.sal.core.ConstructionException;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
23 import org.opendaylight.controller.sal.utils.GUIField;
24
25 import org.opendaylight.controller.switchmanager.SpanConfig;
26
27 /**
28  * The class represents a Span Port configuration for a network node.
29  */
30 public class SpanConfig implements Serializable {
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         return HashCodeBuilder.reflectionHashCode(this);
69     }
70
71     @Override
72     public boolean equals(Object obj) {
73         return EqualsBuilder.reflectionEquals(this, obj);
74     }
75
76     public static ArrayList<String> getFieldsNames() {
77         ArrayList<String> fieldList = new ArrayList<String>();
78         for (Field fld : SpanConfig.class.getDeclaredFields()) {
79             fieldList.add(fld.getName());
80         }
81         //remove the two static fields
82         for (short i = 0; i < 2; i++) {
83             fieldList.remove(0);
84         }
85         return fieldList;
86     }
87
88     public static List<String> getGuiFieldsNames() {
89         List<String> fieldList = new ArrayList<String>();
90         for (String str : guiFields) {
91             fieldList.add(str);
92         }
93         return fieldList;
94     }
95
96     public ArrayList<NodeConnector> getPortArrayList() {
97         Node node = Node.fromString(nodeId);
98         ArrayList<NodeConnector> portList = new ArrayList<NodeConnector>();
99         String[] elemArray = spanPort.split(",");
100         for (String elem : elemArray) {
101             if (elem.contains("-")) {
102                 String[] limits = elem.split("-");
103                 for (short j = Short.valueOf(limits[0]); j <= Short
104                         .valueOf(limits[1]); j++) {
105                     try {
106                         portList.add(new NodeConnector(
107                                 NodeConnectorIDType.OPENFLOW, Short.valueOf(j),
108                                 node));
109                     } catch (ConstructionException e) {
110                         e.printStackTrace();
111                     }
112                 }
113             } else {
114                 try {
115                     portList.add(new NodeConnector(
116                             NodeConnectorIDType.OPENFLOW, Short.valueOf(elem),
117                             node));
118                 } catch (NumberFormatException e) {
119                     e.printStackTrace();
120                 } catch (ConstructionException e) {
121                     e.printStackTrace();
122                 }
123             }
124         }
125         return portList;
126     }
127
128     public boolean matchNode(String nodeId) {
129         return this.nodeId.equals(nodeId);
130     }
131 }