a665096e8b529ee54e7b851c566a04aaddb51b7b
[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.opendaylight.controller.sal.core.Node;
20 import org.opendaylight.controller.sal.core.NodeConnector;
21 import org.opendaylight.controller.sal.utils.HexEncode;
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 final 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 = null;
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     public void addSpanPorts(List<NodeConnector> portList) {
102         for (NodeConnector port : portList) {
103             spanPorts.add(port);
104         }
105     }
106
107     public void removeSpanPorts(List<NodeConnector> portList) {
108         for (NodeConnector port : portList) {
109             spanPorts.remove(port);
110         }
111     }
112
113     @Override
114     public int hashCode() {
115         final int prime = 31;
116         int result = 1;
117         result = prime * result + Arrays.hashCode(dataLayerAddress);
118         result = prime * result + ((node == null) ? 0 : node.hashCode());
119         result = prime * result
120                 + ((nodeConnectors == null) ? 0 : nodeConnectors.hashCode());
121         result = prime * result
122                 + ((spanPorts == null) ? 0 : spanPorts.hashCode());
123         return result;
124     }
125
126     @Override
127     public boolean equals(Object obj) {
128         if (this == obj) {
129             return true;
130         }
131         if (obj == null) {
132             return false;
133         }
134         if (getClass() != obj.getClass()) {
135             return false;
136         }
137         Switch other = (Switch) obj;
138         if (!Arrays.equals(dataLayerAddress, other.dataLayerAddress)) {
139             return false;
140         }
141         if (node == null) {
142             if (other.node != null) {
143                 return false;
144             }
145         } else if (!node.equals(other.node)) {
146             return false;
147         }
148         if (nodeConnectors == null) {
149             if (other.nodeConnectors != null) {
150                 return false;
151             }
152         } else if (!nodeConnectors.equals(other.nodeConnectors)) {
153             return false;
154         }
155         if (spanPorts == null) {
156             if (other.spanPorts != null) {
157                 return false;
158             }
159         } else if (!spanPorts.equals(other.spanPorts)) {
160             return false;
161         }
162         return true;
163     }
164
165     @Override
166     public String toString() {
167         return "Switch [dataLayerAddress=" + HexEncode.bytesToHexStringFormat(dataLayerAddress)
168                 + ", nodeConnectors=" + nodeConnectors + ", spanPorts="
169                 + spanPorts + ", node=" + node + "]";
170     }
171 }