3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.switchmanager;
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;
19 import org.opendaylight.controller.sal.core.Node;
20 import org.opendaylight.controller.sal.core.NodeConnector;
21 import org.opendaylight.controller.sal.utils.HexEncode;
24 * The class describes switch related information including L2 address, ports,
25 * span ports and associated node representation
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;
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
38 this.swPorts = new HashSet<SwitchPortTuple>();
39 this.spanPorts = new ArrayList<Short>(2);
43 public Switch(Node node) {
45 this.nodeConnectors = new HashSet<NodeConnector>();
46 this.spanPorts = new ArrayList<NodeConnector>(2);
47 this.dataLayerAddress = null;
51 * @return the dataLayerAddress
53 public byte[] getDataLayerAddress() {
54 return dataLayerAddress;
58 * @param dataLayerAddress the dataLayerAddress to set
60 public void setDataLayerAddress(byte[] dataLayerAddress) {
61 this.dataLayerAddress = (dataLayerAddress == null) ? null
62 : dataLayerAddress.clone();
66 * @return the nodeConnectors
68 public Set<NodeConnector> getNodeConnectors() {
69 return nodeConnectors;
73 * @param nodeConnectors nodeConnector set
75 public void setNodeConnectors(Set<NodeConnector> nodeConnectors) {
76 this.nodeConnectors = nodeConnectors;
79 public void addNodeConnector(NodeConnector nodeConnector) {
80 if (!nodeConnectors.contains(nodeConnector)) {
81 nodeConnectors.add(nodeConnector);
85 public void removeNodeConnector(NodeConnector nodeConnector) {
86 nodeConnectors.remove(nodeConnector);
89 public List<NodeConnector> getSpanPorts() {
93 public Node getNode() {
97 public void setNode(Node node) {
101 public void addSpanPorts(List<NodeConnector> portList) {
102 for (NodeConnector port : portList) {
107 public void removeSpanPorts(List<NodeConnector> portList) {
108 for (NodeConnector port : portList) {
109 spanPorts.remove(port);
114 public int hashCode() {
115 final int prime = 31;
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());
127 public boolean equals(Object obj) {
134 if (getClass() != obj.getClass()) {
137 Switch other = (Switch) obj;
138 if (!Arrays.equals(dataLayerAddress, other.dataLayerAddress)) {
142 if (other.node != null) {
145 } else if (!node.equals(other.node)) {
148 if (nodeConnectors == null) {
149 if (other.nodeConnectors != null) {
152 } else if (!nodeConnectors.equals(other.nodeConnectors)) {
155 if (spanPorts == null) {
156 if (other.spanPorts != null) {
159 } else if (!spanPorts.equals(other.spanPorts)) {
166 public String toString() {
167 return "Switch [dataLayerAddress=" + HexEncode.bytesToHexStringFormat(dataLayerAddress)
168 + ", nodeConnectors=" + nodeConnectors + ", spanPorts="
169 + spanPorts + ", node=" + node + "]";