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