blob: 193db0776da82c9f9230ced8f3364e03b8714ba2 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 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
20import net.floodlightcontroller.core.web.serializers.DPIDSerializer;
21import net.floodlightcontroller.core.web.serializers.UShortSerializer;
22
23import org.codehaus.jackson.annotate.JsonProperty;
24import org.codehaus.jackson.map.annotate.JsonSerialize;
Jonathan Hartc78b8f62014-08-07 22:31:09 -070025import org.projectfloodlight.openflow.util.HexString;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026
Jonathan Hart284e70f2014-07-05 12:32:51 -070027public final class Link {
28 private final long src;
29 private final short srcPort;
30 private final long dst;
31 private final short dstPort;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080032
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 Milkey269ffb92014-04-03 14:43:30 -070050 @JsonSerialize(using = DPIDSerializer.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051 public long getSrc() {
52 return src;
53 }
54
55 @JsonProperty("src-port")
Ray Milkey269ffb92014-04-03 14:43:30 -070056 @JsonSerialize(using = UShortSerializer.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080057 public short getSrcPort() {
58 return srcPort;
59 }
60
61 @JsonProperty("dst-switch")
Ray Milkey269ffb92014-04-03 14:43:30 -070062 @JsonSerialize(using = DPIDSerializer.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063 public long getDst() {
64 return dst;
65 }
Ray Milkey269ffb92014-04-03 14:43:30 -070066
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080067 @JsonProperty("dst-port")
Ray Milkey269ffb92014-04-03 14:43:30 -070068 @JsonSerialize(using = UShortSerializer.class)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080069 public short getDstPort() {
70 return dstPort;
71 }
72
Jonathan Hartcd1ab172014-07-03 14:59:48 -070073 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 Krishnaswamy345ee992012-12-13 20:29:48 -080081 @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 Milkeyb29e6262014-04-09 16:02:14 -070094 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080095 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -070096 }
97 if (obj == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080098 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -070099 }
100 if (getClass() != obj.getClass()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800101 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700102 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800103 Link other = (Link) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700104 if (dst != other.dst) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700106 }
107 if (dstPort != other.dstPort) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800108 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700109 }
110 if (src != other.src) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800111 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700112 }
113 if (srcPort != other.srcPort) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700115 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800116 return true;
117 }
118
119
120 @Override
121 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 return "Link [src=" + HexString.toHexString(this.src)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800123 + " outPort="
124 + (srcPort & 0xffff)
125 + ", dst=" + HexString.toHexString(this.dst)
126 + ", inPort="
127 + (dstPort & 0xffff)
128 + "]";
129 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700130
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800131 public String toKeyString() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700132 return (HexString.toHexString(this.src) + "|" +
133 (this.srcPort & 0xffff) + "|" +
134 HexString.toHexString(this.dst) + "|" +
135 (this.dstPort & 0xffff));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800136 }
137}
138