Resubmitted with source code synchronized. Added integration test for hosttracker...
[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.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 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The class represents a Span Port configuration for a network node.
31  */
32 public class SpanConfig implements Serializable {
33     protected static final Logger logger = LoggerFactory
34     .getLogger(SpanConfig.class);
35     private static final long serialVersionUID = 1L;
36     private static final String guiFields[] = { GUIField.NODE.toString(),
37             GUIField.SPANPORTS.toString() };
38
39     // Order matters: JSP file expects following fields in the following order
40     private String nodeId;
41     private String spanPort;
42
43     public SpanConfig() {
44     }
45
46     public String getNodeId() {
47         return nodeId;
48     }
49
50     public String getSpanPort() {
51         return spanPort;
52     }
53
54     public Node getNode() {
55         return Node.fromString(nodeId);
56     }
57
58     private boolean hasValidNodeId() {
59         return (getNode() != null);
60     }
61
62     private boolean hasValidSpanPort() {
63         return (spanPort != null && !spanPort.isEmpty());
64     }
65
66     public boolean isValidConfig() {
67         return (hasValidNodeId() && hasValidSpanPort());
68     }
69
70     @Override
71     public int hashCode() {
72         return HashCodeBuilder.reflectionHashCode(this);
73     }
74
75     @Override
76     public boolean equals(Object obj) {
77         return EqualsBuilder.reflectionEquals(this, obj);
78     }
79
80     public static ArrayList<String> getFieldsNames() {
81         ArrayList<String> fieldList = new ArrayList<String>();
82         for (Field fld : SpanConfig.class.getDeclaredFields()) {
83             fieldList.add(fld.getName());
84         }
85         //remove the two static fields
86         for (short i = 0; i < 2; i++) {
87             fieldList.remove(0);
88         }
89         return fieldList;
90     }
91
92     public static List<String> getGuiFieldsNames() {
93         List<String> fieldList = new ArrayList<String>();
94         for (String str : guiFields) {
95             fieldList.add(str);
96         }
97         return fieldList;
98     }
99
100     public ArrayList<NodeConnector> getPortArrayList() {
101         Node node = Node.fromString(nodeId);
102         ArrayList<NodeConnector> portList = new ArrayList<NodeConnector>();
103         String[] elemArray = spanPort.split(",");
104         for (String elem : elemArray) {
105             if (elem.contains("-")) {
106                 String[] limits = elem.split("-");
107                 for (short j = Short.valueOf(limits[0]); j <= Short
108                         .valueOf(limits[1]); j++) {
109                     try {
110                         portList.add(new NodeConnector(
111                                 NodeConnectorIDType.OPENFLOW, Short.valueOf(j),
112                                 node));
113                     } catch (ConstructionException e) {
114                         logger.error("",e);
115                     }
116                 }
117             } else {
118                 try {
119                     portList.add(new NodeConnector(
120                             NodeConnectorIDType.OPENFLOW, Short.valueOf(elem),
121                             node));
122                 } catch (NumberFormatException e) {
123                     logger.error("",e);
124                 } catch (ConstructionException e) {
125                     logger.error("",e);
126                 }
127             }
128         }
129         return portList;
130     }
131
132     public boolean matchNode(String nodeId) {
133         return this.nodeId.equals(nodeId);
134     }
135 }