blob: aa635efe517693bb36aea3e2c0c703c32d8ce2fc [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 HIGUCHIbf0a8712014-06-30 18:59:46 -07005import org.apache.commons.lang.Validate;
Jonathan Hart25bd53e2014-04-30 23:44:09 -07006
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -08007/**
8 * Link Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -07009 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080010 * TODO REMOVE following design memo: This object itself may hold the DBObject,
11 * but this Object itself will not issue any read/write to the DataStore.
12 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070013public class LinkImpl extends TopologyObject implements Link {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080014
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070015 //////////////////////////////////////////////////////
16 /// Topology element attributes
17 /// - any changes made here needs to be replicated.
18 //////////////////////////////////////////////////////
19 private LinkEvent linkObj;
20
21 // TODO remove?
Ray Milkey269ffb92014-04-03 14:43:30 -070022 protected static final Double DEFAULT_CAPACITY = Double.POSITIVE_INFINITY;
23 protected Double capacity = DEFAULT_CAPACITY;
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080024
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070025 ///////////////////
26 /// In-memory index
27 ///////////////////
28
29 // none
30
Ray Milkey269ffb92014-04-03 14:43:30 -070031 /**
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070032 * Creates a Link object based on {@link LinkEvent}.
Ray Milkey269ffb92014-04-03 14:43:30 -070033 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070034 * @param topology Topology instance this object belongs to
35 * @param scPort self contained {@link LinkEvent}
36 */
37 public LinkImpl(Topology topology, LinkEvent scPort) {
38 super(topology);
39 Validate.notNull(scPort);
40
41 // TODO should we assume linkObj is already frozen before this call
42 // or expect attribute update will happen after .
43 if (scPort.isFrozen()) {
44 this.linkObj = scPort;
45 } else {
46 this.linkObj = new LinkEvent(scPort);
47 this.linkObj.freeze();
48 }
49 }
50
51 /**
52 * Creates a Link object with empty attributes.
53 *
54 * @param topology Topology instance this object belongs to
55 * @param srcPort source port
56 * @param dstPort destination port
Ray Milkey269ffb92014-04-03 14:43:30 -070057 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070058 public LinkImpl(Topology topology, Port srcPort, Port dstPort) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070059 this(topology,
60 new LinkEvent(srcPort.asSwitchPort(),
61 dstPort.asSwitchPort()).freeze());
Ray Milkey269ffb92014-04-03 14:43:30 -070062 }
Toshio Koide2f570c12014-02-06 16:55:32 -080063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 @Override
65 public Switch getSrcSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070066 topology.acquireReadLock();
67 try {
68 return topology.getSwitch(linkObj.getSrc().getDpid());
69 } finally {
70 topology.releaseReadLock();
71 }
Ray Milkey269ffb92014-04-03 14:43:30 -070072 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080073
Ray Milkey269ffb92014-04-03 14:43:30 -070074 @Override
75 public Port getSrcPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070076 topology.acquireReadLock();
77 try {
78 return topology.getPort(linkObj.getSrc());
79 } finally {
80 topology.releaseReadLock();
81 }
Ray Milkey269ffb92014-04-03 14:43:30 -070082 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 @Override
85 public Switch getDstSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070086 topology.acquireReadLock();
87 try {
88 return topology.getSwitch(linkObj.getDst().getDpid());
89 } finally {
90 topology.releaseReadLock();
91 }
Ray Milkey269ffb92014-04-03 14:43:30 -070092 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 @Override
95 public Port getDstPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070096 topology.acquireReadLock();
97 try {
98 return topology.getPort(linkObj.getDst());
99 } finally {
100 topology.releaseReadLock();
101 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800103
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 @Override
105 public long getLastSeenTime() {
106 // TODO Auto-generated method stub
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700107 throw new UnsupportedOperationException("Not implemented yet");
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800109
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 public Double getCapacity() {
112 return capacity;
113 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800114
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700115 void setCapacity(Double capacity) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 this.capacity = capacity;
117 }
Toshio Koide0c9106d2014-02-19 15:26:38 -0800118
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700119 void replaceStringAttributes(LinkEvent updated) {
120 Validate.isTrue(this.linkObj.getSrc().equals(updated.getSrc()),
121 "Wrong LinkEvent given.");
122 Validate.isTrue(this.linkObj.getDst().equals(updated.getDst()),
123 "Wrong LinkEvent given.");
124
125 // XXX simply replacing whole self-contained object for now
126 if (updated.isFrozen()) {
127 this.linkObj = updated;
128 } else {
129 this.linkObj = new LinkEvent(updated).freeze();
130 }
131 }
132
133
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700135 public String getStringAttribute(String attr) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700136 return linkObj.getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700137 }
138
139 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700140 public String getStringAttribute(String attr, String def) {
141 final String v = getStringAttribute(attr);
142 if (v == null) {
143 return def;
144 } else {
145 return v;
146 }
147 }
148
149 @Override
150 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700151 return linkObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700152 }
153
154 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700155 public String toString() {
156 return String.format("%s --(cap:%f Mbps)--> %s",
157 getSrcPort().toString(),
158 getCapacity(),
159 getDstPort().toString());
160 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700161
162
163 /**
164 * Returns the type of topology object.
165 *
166 * @return the type of the topology object
167 */
168 @Override
169 public String getType() {
170 throw new UnsupportedOperationException("Not implemented yet");
171 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800172}