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.HashSet;
15 import java.util.List;
18 import org.apache.commons.lang3.builder.EqualsBuilder;
19 import org.apache.commons.lang3.builder.HashCodeBuilder;
20 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.core.NodeConnector;
25 * The class describes switch related information including L2 address, ports,
26 * span ports and associated node representation
28 public class Switch implements Serializable {
29 private static final long serialVersionUID = 1L;
30 private byte[] dataLayerAddress;
31 private Set<NodeConnector> nodeConnectors;
32 private List<NodeConnector> spanPorts;
36 * As we are adding switches on per event basis in a map, we do not need a default constructor
37 * This way we can keep the validations internal, in the proper constructor
39 this.swPorts = new HashSet<ONESwitchPortTuple>();
40 this.spanPorts = new ArrayList<Short>(2);
44 public Switch(Node node) {
46 this.nodeConnectors = new HashSet<NodeConnector>();
47 this.spanPorts = new ArrayList<NodeConnector>(2);
48 this.dataLayerAddress = deriveMacAddress();
52 * @return the dataLayerAddress
54 public byte[] getDataLayerAddress() {
55 return dataLayerAddress;
59 * @param dataLayerAddress the dataLayerAddress to set
61 public void setDataLayerAddress(byte[] dataLayerAddress) {
62 this.dataLayerAddress = (dataLayerAddress == null) ? null
63 : dataLayerAddress.clone();
67 * @return the nodeConnectors
69 public Set<NodeConnector> getNodeConnectors() {
70 return nodeConnectors;
74 * @param nodeConnectors nodeConnector set
76 public void setNodeConnectors(Set<NodeConnector> nodeConnectors) {
77 this.nodeConnectors = nodeConnectors;
80 public void addNodeConnector(NodeConnector nodeConnector) {
81 if (!nodeConnectors.contains(nodeConnector)) {
82 nodeConnectors.add(nodeConnector);
86 public void removeNodeConnector(NodeConnector nodeConnector) {
87 nodeConnectors.remove(nodeConnector);
90 public List<NodeConnector> getSpanPorts() {
94 public Node getNode() {
98 public void setNode(Node node) {
102 private byte[] deriveMacAddress() {
103 long dpid = (Long) this.node.getID();
104 byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
106 for (short i = 0; i < 6; i++) {
107 mac[5 - i] = (byte) dpid;
114 public void addSpanPorts(List<NodeConnector> portList) {
115 for (NodeConnector port : portList) {
120 public void removeSpanPorts(List<NodeConnector> portList) {
121 for (NodeConnector port : portList) {
122 spanPorts.remove(port);
127 public int hashCode() {
128 return HashCodeBuilder.reflectionHashCode(this);
132 public boolean equals(Object obj) {
133 return EqualsBuilder.reflectionEquals(this, obj);
137 public String toString() {
138 return "Switch[" + ReflectionToStringBuilder.toString(this) + "]";