Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 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. |
| 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 | |
| 20 | import net.floodlightcontroller.core.web.serializers.DPIDSerializer; |
| 21 | import net.floodlightcontroller.core.web.serializers.UShortSerializer; |
| 22 | |
| 23 | import org.codehaus.jackson.annotate.JsonProperty; |
| 24 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
Jonathan Hart | c78b8f6 | 2014-08-07 22:31:09 -0700 | [diff] [blame] | 25 | import org.projectfloodlight.openflow.util.HexString; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 27 | public final class Link { |
| 28 | private final long src; |
| 29 | private final short srcPort; |
| 30 | private final long dst; |
| 31 | private final short dstPort; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 32 | |
| 33 | |
| 34 | public Link(long srcId, short srcPort, long dstId, short dstPort) { |
| 35 | this.src = srcId; |
| 36 | this.srcPort = srcPort; |
| 37 | this.dst = dstId; |
| 38 | this.dstPort = dstPort; |
| 39 | } |
| 40 | |
| 41 | // Convenience method |
| 42 | public Link(long srcId, int srcPort, long dstId, int dstPort) { |
| 43 | this.src = srcId; |
| 44 | this.srcPort = (short) srcPort; |
| 45 | this.dst = dstId; |
| 46 | this.dstPort = (short) dstPort; |
| 47 | } |
| 48 | |
| 49 | @JsonProperty("src-switch") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | @JsonSerialize(using = DPIDSerializer.class) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 51 | public long getSrc() { |
| 52 | return src; |
| 53 | } |
| 54 | |
| 55 | @JsonProperty("src-port") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | @JsonSerialize(using = UShortSerializer.class) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 57 | public short getSrcPort() { |
| 58 | return srcPort; |
| 59 | } |
| 60 | |
| 61 | @JsonProperty("dst-switch") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | @JsonSerialize(using = DPIDSerializer.class) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 63 | public long getDst() { |
| 64 | return dst; |
| 65 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 67 | @JsonProperty("dst-port") |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 68 | @JsonSerialize(using = UShortSerializer.class) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 69 | public short getDstPort() { |
| 70 | return dstPort; |
| 71 | } |
| 72 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 73 | public boolean isSrcPort(NodePortTuple npt) { |
| 74 | return npt.getNodeId() == src && npt.getPortId() == srcPort; |
| 75 | } |
| 76 | |
| 77 | public boolean isDstPort(NodePortTuple npt) { |
| 78 | return npt.getNodeId() == dst && npt.getPortId() == dstPort; |
| 79 | } |
| 80 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 81 | @Override |
| 82 | public int hashCode() { |
| 83 | final int prime = 31; |
| 84 | int result = 1; |
| 85 | result = prime * result + (int) (dst ^ (dst >>> 32)); |
| 86 | result = prime * result + dstPort; |
| 87 | result = prime * result + (int) (src ^ (src >>> 32)); |
| 88 | result = prime * result + srcPort; |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 94 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 95 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 96 | } |
| 97 | if (obj == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 98 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 99 | } |
| 100 | if (getClass() != obj.getClass()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 101 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 102 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 103 | Link other = (Link) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 104 | if (dst != other.dst) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 105 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 106 | } |
| 107 | if (dstPort != other.dstPort) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 108 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 109 | } |
| 110 | if (src != other.src) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 111 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 112 | } |
| 113 | if (srcPort != other.srcPort) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 114 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 115 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | @Override |
| 121 | public String toString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 122 | return "Link [src=" + HexString.toHexString(this.src) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 123 | + " outPort=" |
| 124 | + (srcPort & 0xffff) |
| 125 | + ", dst=" + HexString.toHexString(this.dst) |
| 126 | + ", inPort=" |
| 127 | + (dstPort & 0xffff) |
| 128 | + "]"; |
| 129 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 130 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 131 | public String toKeyString() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 132 | return (HexString.toHexString(this.src) + "|" + |
| 133 | (this.srcPort & 0xffff) + "|" + |
| 134 | HexString.toHexString(this.dst) + "|" + |
| 135 | (this.dstPort & 0xffff)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |