blob: bc11c915497986cbf3001d6af3f8643910c664eb [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Jonathan Hartcd1ab172014-07-03 14:59:48 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07004 *
Jonathan Hartcd1ab172014-07-03 14:59:48 -07005 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Ray Milkey269ffb92014-04-03 14:43:30 -07006 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070016 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
Jonathan Hart23701d12014-04-03 10:45:48 -070018package net.onrc.onos.core.linkdiscovery;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070020import java.util.Set;
21
Jonathan Hart284e70f2014-07-05 12:32:51 -070022import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService.LinkType;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080023
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070024import org.projectfloodlight.openflow.protocol.OFPortState;
25
Jonathan Hartcd1ab172014-07-03 14:59:48 -070026import com.google.common.primitives.Longs;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027
Jonathan Hartcd1ab172014-07-03 14:59:48 -070028/**
29 * Records information about a link.
30 */
31public final class LinkInfo {
Ray Milkey269ffb92014-04-03 14:43:30 -070032 /**
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070033 * The port states stored here are topology's last knowledge of the state of
34 * the port. This mostly mirrors the state maintained in the port list in
35 * IOFSwitch (i.e. the one returned from getPort), except that during a port
36 * status message the IOFSwitch port state will already have been updated
37 * with the new port state, so topology needs to keep its own copy so that
38 * it can determine if the port state has changed and therefore requires the
39 * new state to be written to storage. Note the port state values are
40 * defined in the OF 1.0 spec. These will change in some way once we move to
41 * OF 1.3.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080042 */
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070043 private final Set<OFPortState> srcPortState;
44 private final Set<OFPortState> dstPortState;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080045
Jonathan Hartcd1ab172014-07-03 14:59:48 -070046 private final long firstSeenTime;
47 private final long lastLldpReceivedTime;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080048
Jonathan Hartcd1ab172014-07-03 14:59:48 -070049 /**
50 * Constructs a LinkInfo object.
51 *
52 * @param firstSeenTime the timestamp when the link was first seen
53 * @param lastLldpReceivedTime the timestamp when the link was last seen
54 * @param srcPortState the port state of the source port
55 * @param dstPortState the port state of the destination port
56 */
57 public LinkInfo(long firstSeenTime,
58 long lastLldpReceivedTime,
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070059 Set<OFPortState> srcPortState,
60 Set<OFPortState> dstPortState) {
Jonathan Hartcd1ab172014-07-03 14:59:48 -070061 this.srcPortState = srcPortState;
62 this.dstPortState = dstPortState;
63 this.firstSeenTime = firstSeenTime;
64 this.lastLldpReceivedTime = lastLldpReceivedTime;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080065 }
66
Jonathan Hartcd1ab172014-07-03 14:59:48 -070067 /**
68 * Gets the timestamp when the link was first seen.
69 *
70 * @return the first seen timestamp
71 */
72 public long getFirstSeenTime() {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080073 return firstSeenTime;
74 }
75
Jonathan Hartcd1ab172014-07-03 14:59:48 -070076 /**
77 * Gets the timestamp when the link was last seen.
78 *
79 * @return the last seen timestamp
80 */
81 public long getLastProbeReceivedTime() {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080082 return lastLldpReceivedTime;
83 }
84
Jonathan Hartcd1ab172014-07-03 14:59:48 -070085 /**
86 * Gets the state of the source port.
87 *
88 * @return the source port state, as defined in the OF1.0 spec
89 */
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070090 public Set<OFPortState> getSrcPortState() {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080091 return srcPortState;
92 }
93
Brian O'Connorc67f9fa2014-08-07 18:17:46 -070094 public int getSrcPortStateInteger() {
95 return convertPortState(srcPortState);
96 }
97
Jonathan Hartcd1ab172014-07-03 14:59:48 -070098 /**
99 * Gets the state of the destination port.
100 *
101 * @return the destination port state, as defined in the OF1.0 spec
102 */
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700103 public Set<OFPortState> getDstPortState() {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 return dstPortState;
105 }
106
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700107 /**
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700108 * Gets the state of the destination port.
109 *
110 * @return the destination port state, as defined in the OF1.0 spec
111 */
112 public int getDstPortStateInteger() {
113 return convertPortState(dstPortState);
114 }
115
116 private int convertPortState(Set<OFPortState> ps) {
117 int ret = 0;
118 if (ps.contains(OFPortState.LINK_DOWN)) {
119 ret = 1 << 0;
120 }
121 if (ps.contains(OFPortState.BLOCKED)) {
122 ret = ret | 1 << 1;
123 }
124 if (ps.contains(OFPortState.LIVE)) {
125 ret = ret | 1 << 2;
126 }
127 return ret;
128 }
129
130 /**
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700131 * Gets the link type.
132 *
133 * @return the link type
134 * @see LinkType
135 */
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800136 public LinkType getLinkType() {
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700137 return LinkType.DIRECT_LINK;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800138 }
139
140 /* (non-Javadoc)
141 * @see java.lang.Object#hashCode()
142 */
143 @Override
144 public int hashCode() {
145 final int prime = 5557;
146 int result = 1;
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700147 result = prime * result + Longs.hashCode(firstSeenTime);
148 result = prime * result + Longs.hashCode(lastLldpReceivedTime);
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700149 result = prime * result + convertPortState(srcPortState);
150 result = prime * result + convertPortState(dstPortState);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800151 return result;
152 }
153
154 /* (non-Javadoc)
155 * @see java.lang.Object#equals(java.lang.Object)
156 */
157 @Override
158 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700159 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800160 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700161 }
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700162
Ray Milkeyb29e6262014-04-09 16:02:14 -0700163 if (!(obj instanceof LinkInfo)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800164 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700165 }
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700166
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800167 LinkInfo other = (LinkInfo) obj;
168
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700169 return firstSeenTime == other.firstSeenTime &&
Brian O'Connorc67f9fa2014-08-07 18:17:46 -0700170 lastLldpReceivedTime == other.lastLldpReceivedTime &&
171 srcPortState == other.srcPortState &&
172 dstPortState == other.dstPortState;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800173 }
174
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800175 /* (non-Javadoc)
176 * @see java.lang.Object#toString()
177 */
178 @Override
179 public String toString() {
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700180 return "LinkInfo [unicastValidTime=" + lastLldpReceivedTime
181 + ", srcPortState=" + srcPortState
182 + ", dstPortState=" + dstPortState
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800183 + "]";
184 }
185}