blob: a57354087a2dc5a8929451584ab6820aad99c3b8 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Jonathan Hart062a2e82014-02-03 09:41:57 -08002
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -07003import java.util.Map;
4
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -07005import net.onrc.onos.core.util.LinkTuple;
6
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07007import org.apache.commons.lang.Validate;
Jonathan Hart25bd53e2014-04-30 23:44:09 -07008
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -08009/**
10 * Link Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070011 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080012 * TODO REMOVE following design memo: This object itself may hold the DBObject,
13 * but this Object itself will not issue any read/write to the DataStore.
14 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070015public class LinkImpl extends TopologyObject implements Link {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080016
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070017 //////////////////////////////////////////////////////
18 /// Topology element attributes
19 /// - any changes made here needs to be replicated.
20 //////////////////////////////////////////////////////
21 private LinkEvent linkObj;
22
23 // TODO remove?
Ray Milkey269ffb92014-04-03 14:43:30 -070024 protected static final Double DEFAULT_CAPACITY = Double.POSITIVE_INFINITY;
25 protected Double capacity = DEFAULT_CAPACITY;
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080026
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027 ///////////////////
28 /// In-memory index
29 ///////////////////
30
31 // none
32
Ray Milkey269ffb92014-04-03 14:43:30 -070033 /**
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070034 * Creates a Link object based on {@link LinkEvent}.
Ray Milkey269ffb92014-04-03 14:43:30 -070035 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070036 * @param topology Topology instance this object belongs to
37 * @param scPort self contained {@link LinkEvent}
38 */
39 public LinkImpl(Topology topology, LinkEvent scPort) {
40 super(topology);
41 Validate.notNull(scPort);
42
43 // TODO should we assume linkObj is already frozen before this call
44 // or expect attribute update will happen after .
45 if (scPort.isFrozen()) {
46 this.linkObj = scPort;
47 } else {
48 this.linkObj = new LinkEvent(scPort);
49 this.linkObj.freeze();
50 }
51 }
52
53 /**
54 * Creates a Link object with empty attributes.
55 *
56 * @param topology Topology instance this object belongs to
57 * @param srcPort source port
58 * @param dstPort destination port
Ray Milkey269ffb92014-04-03 14:43:30 -070059 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070060 public LinkImpl(Topology topology, Port srcPort, Port dstPort) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070061 this(topology,
62 new LinkEvent(srcPort.asSwitchPort(),
63 dstPort.asSwitchPort()).freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -070064 }
Toshio Koide2f570c12014-02-06 16:55:32 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 @Override
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070067 public LinkTuple getLinkTuple() {
68 return linkObj.getLinkTuple();
69 }
70
71 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070072 public Switch getSrcSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070073 topology.acquireReadLock();
74 try {
75 return topology.getSwitch(linkObj.getSrc().getDpid());
76 } finally {
77 topology.releaseReadLock();
78 }
Ray Milkey269ffb92014-04-03 14:43:30 -070079 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 @Override
82 public Port getSrcPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070083 topology.acquireReadLock();
84 try {
85 return topology.getPort(linkObj.getSrc());
86 } finally {
87 topology.releaseReadLock();
88 }
Ray Milkey269ffb92014-04-03 14:43:30 -070089 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080090
Ray Milkey269ffb92014-04-03 14:43:30 -070091 @Override
92 public Switch getDstSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070093 topology.acquireReadLock();
94 try {
95 return topology.getSwitch(linkObj.getDst().getDpid());
96 } finally {
97 topology.releaseReadLock();
98 }
Ray Milkey269ffb92014-04-03 14:43:30 -070099 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -0800100
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 @Override
102 public Port getDstPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700103 topology.acquireReadLock();
104 try {
105 return topology.getPort(linkObj.getDst());
106 } finally {
107 topology.releaseReadLock();
108 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800110
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 @Override
112 public long getLastSeenTime() {
113 // TODO Auto-generated method stub
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700114 throw new UnsupportedOperationException("Not implemented yet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800116
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 public Double getCapacity() {
119 return capacity;
120 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800121
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700122 void setCapacity(Double capacity) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 this.capacity = capacity;
124 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800125
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700126 void replaceStringAttributes(LinkEvent updated) {
127 Validate.isTrue(this.linkObj.getSrc().equals(updated.getSrc()),
128 "Wrong LinkEvent given.");
129 Validate.isTrue(this.linkObj.getDst().equals(updated.getDst()),
130 "Wrong LinkEvent given.");
131
132 // XXX simply replacing whole self-contained object for now
133 if (updated.isFrozen()) {
134 this.linkObj = updated;
135 } else {
136 this.linkObj = new LinkEvent(updated).freeze();
137 }
138 }
139
140
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700142 public String getStringAttribute(String attr) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700143 return linkObj.getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700144 }
145
146 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700147 public String getStringAttribute(String attr, String def) {
148 final String v = getStringAttribute(attr);
149 if (v == null) {
150 return def;
151 } else {
152 return v;
153 }
154 }
155
156 @Override
157 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700158 return linkObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700159 }
160
161 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 public String toString() {
163 return String.format("%s --(cap:%f Mbps)--> %s",
164 getSrcPort().toString(),
165 getCapacity(),
166 getDstPort().toString());
167 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700168
169
170 /**
171 * Returns the type of topology object.
172 *
173 * @return the type of the topology object
174 */
175 @Override
176 public String getType() {
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700177 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700178 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800179}