afca30c3a1c89a9a1ce254e925e0cc33ae89b142
[controller.git] / opendaylight / switchmanager / 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.HashSet;
15 import java.util.List;
16 import java.util.Set;
17
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;
23
24 /**
25  * The class describes switch related information including L2 address, ports,
26  * span ports and associated node representation
27  */
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;
33     private Node node;
34
35     /*
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
38     public Switch() {
39         this.swPorts = new HashSet<SwitchPortTuple>();
40         this.spanPorts = new ArrayList<Short>(2);
41     }
42      */
43
44     public Switch(Node node) {
45         this.node = node;
46         this.nodeConnectors = new HashSet<NodeConnector>();
47         this.spanPorts = new ArrayList<NodeConnector>(2);
48         this.dataLayerAddress = deriveMacAddress();
49     }
50
51     /**
52      * @return the dataLayerAddress
53      */
54     public byte[] getDataLayerAddress() {
55         return dataLayerAddress;
56     }
57
58     /**
59      * @param dataLayerAddress the dataLayerAddress to set
60      */
61     public void setDataLayerAddress(byte[] dataLayerAddress) {
62         this.dataLayerAddress = (dataLayerAddress == null) ? null
63                 : dataLayerAddress.clone();
64     }
65
66     /**
67      * @return the nodeConnectors
68      */
69     public Set<NodeConnector> getNodeConnectors() {
70         return nodeConnectors;
71     }
72
73     /**
74      * @param nodeConnectors nodeConnector set
75      */
76     public void setNodeConnectors(Set<NodeConnector> nodeConnectors) {
77         this.nodeConnectors = nodeConnectors;
78     }
79
80     public void addNodeConnector(NodeConnector nodeConnector) {
81         if (!nodeConnectors.contains(nodeConnector)) {
82             nodeConnectors.add(nodeConnector);
83         }
84     }
85
86     public void removeNodeConnector(NodeConnector nodeConnector) {
87         nodeConnectors.remove(nodeConnector);
88     }
89
90     public List<NodeConnector> getSpanPorts() {
91         return spanPorts;
92     }
93
94     public Node getNode() {
95         return node;
96     }
97
98     public void setNode(Node node) {
99         this.node = node;
100     }
101
102     private byte[] deriveMacAddress() {
103         long dpid = (Long) this.node.getID();
104         byte[] mac = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
105
106         for (short i = 0; i < 6; i++) {
107             mac[5 - i] = (byte) dpid;
108             dpid >>= 8;
109         }
110
111         return mac;
112     }
113
114     public void addSpanPorts(List<NodeConnector> portList) {
115         for (NodeConnector port : portList) {
116             spanPorts.add(port);
117         }
118     }
119
120     public void removeSpanPorts(List<NodeConnector> portList) {
121         for (NodeConnector port : portList) {
122             spanPorts.remove(port);
123         }
124     }
125
126     @Override
127     public int hashCode() {
128         return HashCodeBuilder.reflectionHashCode(this);
129     }
130
131     @Override
132     public boolean equals(Object obj) {
133         return EqualsBuilder.reflectionEquals(this, obj);
134     }
135
136     @Override
137     public String toString() {
138         return "Switch[" + ReflectionToStringBuilder.toString(this) + "]";
139     }
140 }