Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 4 | * |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 6 | * 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.linkdiscovery; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 20 | import java.util.Set; |
| 21 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService.LinkType; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 23 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 24 | import org.projectfloodlight.openflow.protocol.OFPortState; |
| 25 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 26 | import com.google.common.primitives.Longs; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 27 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Records information about a link. |
| 30 | */ |
| 31 | public final class LinkInfo { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 32 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 33 | * 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 42 | */ |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 43 | private final Set<OFPortState> srcPortState; |
| 44 | private final Set<OFPortState> dstPortState; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 45 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 46 | private final long firstSeenTime; |
| 47 | private final long lastLldpReceivedTime; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 48 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 49 | /** |
| 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 59 | Set<OFPortState> srcPortState, |
| 60 | Set<OFPortState> dstPortState) { |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 61 | this.srcPortState = srcPortState; |
| 62 | this.dstPortState = dstPortState; |
| 63 | this.firstSeenTime = firstSeenTime; |
| 64 | this.lastLldpReceivedTime = lastLldpReceivedTime; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 67 | /** |
| 68 | * Gets the timestamp when the link was first seen. |
| 69 | * |
| 70 | * @return the first seen timestamp |
| 71 | */ |
| 72 | public long getFirstSeenTime() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 73 | return firstSeenTime; |
| 74 | } |
| 75 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 76 | /** |
| 77 | * Gets the timestamp when the link was last seen. |
| 78 | * |
| 79 | * @return the last seen timestamp |
| 80 | */ |
| 81 | public long getLastProbeReceivedTime() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 82 | return lastLldpReceivedTime; |
| 83 | } |
| 84 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 85 | /** |
| 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 90 | public Set<OFPortState> getSrcPortState() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 91 | return srcPortState; |
| 92 | } |
| 93 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 94 | public int getSrcPortStateInteger() { |
| 95 | return convertPortState(srcPortState); |
| 96 | } |
| 97 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 98 | /** |
| 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'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 103 | public Set<OFPortState> getDstPortState() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | return dstPortState; |
| 105 | } |
| 106 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 107 | /** |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 108 | * 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 Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 131 | * Gets the link type. |
| 132 | * |
| 133 | * @return the link type |
| 134 | * @see LinkType |
| 135 | */ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 136 | public LinkType getLinkType() { |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 137 | return LinkType.DIRECT_LINK; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 138 | } |
| 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 Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 147 | result = prime * result + Longs.hashCode(firstSeenTime); |
| 148 | result = prime * result + Longs.hashCode(lastLldpReceivedTime); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 149 | result = prime * result + convertPortState(srcPortState); |
| 150 | result = prime * result + convertPortState(dstPortState); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 151 | 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 Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 159 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 160 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 161 | } |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 162 | |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 163 | if (!(obj instanceof LinkInfo)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 164 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 165 | } |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 166 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 167 | LinkInfo other = (LinkInfo) obj; |
| 168 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 169 | return firstSeenTime == other.firstSeenTime && |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 170 | lastLldpReceivedTime == other.lastLldpReceivedTime && |
| 171 | srcPortState == other.srcPortState && |
| 172 | dstPortState == other.dstPortState; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 175 | /* (non-Javadoc) |
| 176 | * @see java.lang.Object#toString() |
| 177 | */ |
| 178 | @Override |
| 179 | public String toString() { |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 180 | return "LinkInfo [unicastValidTime=" + lastLldpReceivedTime |
| 181 | + ", srcPortState=" + srcPortState |
| 182 | + ", dstPortState=" + dstPortState |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 183 | + "]"; |
| 184 | } |
| 185 | } |