blob: 5ad0194845a632eae79a233b2a5870d53c70d94c [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
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070023
Ray Milkey269ffb92014-04-03 14:43:30 -070024 /**
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070025 * Creates a Link object based on {@link LinkEvent}.
Ray Milkey269ffb92014-04-03 14:43:30 -070026 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027 * @param topology Topology instance this object belongs to
28 * @param scPort self contained {@link LinkEvent}
29 */
30 public LinkImpl(Topology topology, LinkEvent scPort) {
31 super(topology);
32 Validate.notNull(scPort);
33
34 // TODO should we assume linkObj is already frozen before this call
35 // or expect attribute update will happen after .
36 if (scPort.isFrozen()) {
37 this.linkObj = scPort;
38 } else {
39 this.linkObj = new LinkEvent(scPort);
40 this.linkObj.freeze();
41 }
42 }
43
44 /**
45 * Creates a Link object with empty attributes.
46 *
47 * @param topology Topology instance this object belongs to
48 * @param srcPort source port
49 * @param dstPort destination port
Ray Milkey269ffb92014-04-03 14:43:30 -070050 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070051 public LinkImpl(Topology topology, Port srcPort, Port dstPort) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070052 this(topology,
53 new LinkEvent(srcPort.asSwitchPort(),
54 dstPort.asSwitchPort()).freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -070055 }
Toshio Koide2f570c12014-02-06 16:55:32 -080056
Ray Milkey269ffb92014-04-03 14:43:30 -070057 @Override
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070058 public LinkTuple getLinkTuple() {
59 return linkObj.getLinkTuple();
60 }
61
62 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070063 public Switch getSrcSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070064 topology.acquireReadLock();
65 try {
66 return topology.getSwitch(linkObj.getSrc().getDpid());
67 } finally {
68 topology.releaseReadLock();
69 }
Ray Milkey269ffb92014-04-03 14:43:30 -070070 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 @Override
73 public Port getSrcPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070074 topology.acquireReadLock();
75 try {
76 return topology.getPort(linkObj.getSrc());
77 } finally {
78 topology.releaseReadLock();
79 }
Ray Milkey269ffb92014-04-03 14:43:30 -070080 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080081
Ray Milkey269ffb92014-04-03 14:43:30 -070082 @Override
83 public Switch getDstSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070084 topology.acquireReadLock();
85 try {
86 return topology.getSwitch(linkObj.getDst().getDpid());
87 } finally {
88 topology.releaseReadLock();
89 }
Ray Milkey269ffb92014-04-03 14:43:30 -070090 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080091
Ray Milkey269ffb92014-04-03 14:43:30 -070092 @Override
93 public Port getDstPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070094 topology.acquireReadLock();
95 try {
96 return topology.getPort(linkObj.getDst());
97 } finally {
98 topology.releaseReadLock();
99 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800101
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 @Override
103 public long getLastSeenTime() {
104 // TODO Auto-generated method stub
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700105 throw new UnsupportedOperationException("Not implemented yet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800107
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 public Double getCapacity() {
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700110 return this.linkObj.getCapacity();
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800112
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700113 void setCapacity(Double capacity) {
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700114 if (this.linkObj.isFrozen()) {
115 this.linkObj = new LinkEvent(this.linkObj);
116 this.linkObj.setCapacity(capacity);
117 this.linkObj.freeze();
118 } else {
119 this.linkObj.setCapacity(capacity);
120 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800122
Yuta HIGUCHIcb8455e2014-07-15 18:42:10 -0700123 // XXX actually replaces everything
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700124 void replaceStringAttributes(LinkEvent updated) {
125 Validate.isTrue(this.linkObj.getSrc().equals(updated.getSrc()),
126 "Wrong LinkEvent given.");
127 Validate.isTrue(this.linkObj.getDst().equals(updated.getDst()),
128 "Wrong LinkEvent given.");
129
130 // XXX simply replacing whole self-contained object for now
131 if (updated.isFrozen()) {
132 this.linkObj = updated;
133 } else {
134 this.linkObj = new LinkEvent(updated).freeze();
135 }
136 }
137
138
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700140 public String getStringAttribute(String attr) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700141 return linkObj.getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700142 }
143
144 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700145 public String getStringAttribute(String attr, String def) {
146 final String v = getStringAttribute(attr);
147 if (v == null) {
148 return def;
149 } else {
150 return v;
151 }
152 }
153
154 @Override
155 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700156 return linkObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700157 }
158
159 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700160 public String toString() {
161 return String.format("%s --(cap:%f Mbps)--> %s",
162 getSrcPort().toString(),
163 getCapacity(),
164 getDstPort().toString());
165 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700166
167
168 /**
169 * Returns the type of topology object.
170 *
171 * @return the type of the topology object
172 */
173 @Override
174 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700175 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700176 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800177}