Five more Equals/HashCode/StringBuilder replacements
[controller.git] / opendaylight / switchmanager / api / src / main / java / org / opendaylight / controller / switchmanager / Switch.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.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Set;
18
19 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22
23 /**
24  * The class describes switch related information including L2 address, ports,
25  * span ports and associated node representation
26  */
27 public class Switch implements Serializable {
28     private static final long serialVersionUID = 1L;
29     private byte[] dataLayerAddress;
30     private Set<NodeConnector> nodeConnectors;
31     private List<NodeConnector> spanPorts;
32     private Node node;
33
34     /*
35      * As we are adding switches on per event basis in a map, we do not need a default constructor
36      * This way we can keep the validations internal, in the proper constructor
37     public Switch() {
38         this.swPorts = new HashSet<SwitchPortTuple>();
39         this.spanPorts = new ArrayList<Short>(2);
40     }
41      */
42
43     public Switch(Node node) {
44         this.node = node;
45         this.nodeConnectors = new HashSet<NodeConnector>();
46         this.spanPorts = new ArrayList<NodeConnector>(2);
47         this.dataLayerAddress = deriveMacAddress();
48     }
49
50     /**
51      * @return the dataLayerAddress
52      */
53     public byte[] getDataLayerAddress() {
54         return dataLayerAddress;
55     }
56
57     /**
58      * @param dataLayerAddress the dataLayerAddress to set
59      */
60     public void setDataLayerAddress(byte[] dataLayerAddress) {
61         this.dataLayerAddress = (dataLayerAddress == null) ? null
62                 : dataLayerAddress.clone();
63     }
64
65     /**
66      * @return the nodeConnectors
67      */
68     public Set<NodeConnector> getNodeConnectors() {
69         return nodeConnectors;
70     }
71
72     /**
73      * @param nodeConnectors nodeConnector set
74      */
75     public void setNodeConnectors(Set<NodeConnector> nodeConnectors) {
76         this.nodeConnectors = nodeConnectors;
77     }
78
79     public void addNodeConnector(NodeConnector nodeConnector) {
80         if (!nodeConnectors.contains(nodeConnector)) {
81             nodeConnectors.add(nodeConnector);
82         }
83     }
84
85     public void removeNodeConnector(NodeConnector nodeConnector) {
86         nodeConnectors.remove(nodeConnector);
87     }
88
89     public List<NodeConnector> getSpanPorts() {
90         return spanPorts;
91     }
92
93     public Node getNode() {
94         return node;
95     }
96
97     public void setNode(Node node) {
98         this.node = node;
99     }
100
101     private byte[] deriveMacAddress() {
102         long dpid = (Long) this.node.getID();
103         byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
104
105         for (short i = 0; i < 6; i++) {
106             mac[5 - i] = (byte) dpid;
107             dpid >>= 8;
108         }
109
110         return mac;
111     }
112
113     public void addSpanPorts(List<NodeConnector> portList) {
114         for (NodeConnector port : portList) {
115             spanPorts.add(port);
116         }
117     }
118
119     public void removeSpanPorts(List<NodeConnector> portList) {
120         for (NodeConnector port : portList) {
121             spanPorts.remove(port);
122         }
123     }
124
125     @Override
126     public int hashCode() {
127         final int prime = 31;
128         int result = 1;
129         result = prime * result + Arrays.hashCode(dataLayerAddress);
130         result = prime * result + ((node == null) ? 0 : node.hashCode());
131         result = prime * result
132                 + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode());
133         result = prime * result
134                 + ((spanPorts == null) ? 0 : spanPorts.hashCode());
135         return result;
136     }
137
138     @Override
139     public boolean equals(Object obj) {
140         if (this == obj)
141             return true;
142         if (obj == null)
143             return false;
144         if (getClass() != obj.getClass())
145             return false;
146         Switch other = (Switch) obj;
147         if (!Arrays.equals(dataLayerAddress, other.dataLayerAddress))
148             return false;
149         if (node == null) {
150             if (other.node != null)
151                 return false;
152         } else if (!node.equals(other.node))
153             return false;
154         if (nodeConnectors == null) {
155             if (other.nodeConnectors != null)
156                 return false;
157         } else if (!nodeConnectors.equals(other.nodeConnectors))
158             return false;
159         if (spanPorts == null) {
160             if (other.spanPorts != null)
161                 return false;
162         } else if (!spanPorts.equals(other.spanPorts))
163             return false;
164         return true;
165     }
166
167     @Override
168     public String toString() {
169         return "Switch[" + ReflectionToStringBuilder.toString(this) + "]";
170     }
171 }