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