blob: ee60d2d23386fe67dfc01f4f26d34a7f46577529 [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
Pavlin Radoslavova5637c02014-07-30 15:55:11 -07007import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hart25bd53e2014-04-30 23:44:09 -07008
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -08009/**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070010 * Handler to Link object stored in In-memory Topology snapshot.
Ray Milkey269ffb92014-04-03 14:43:30 -070011 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080012 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070013public class LinkImpl extends TopologyObject implements Link {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080014
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070015 private final LinkTuple id;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070016
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070017
Ray Milkey269ffb92014-04-03 14:43:30 -070018 /**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070019 * Creates a Link handler object.
Ray Milkey269ffb92014-04-03 14:43:30 -070020 *
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070021 * @param topology Topology instance this object belongs to
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070022 * @param linkTuple Link identifier
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070023 */
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070024 LinkImpl(TopologyInternal topology, LinkTuple linkTuple) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070025 super(topology);
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070026 this.id = checkNotNull(linkTuple);
Ray Milkey269ffb92014-04-03 14:43:30 -070027 }
Toshio Koide2f570c12014-02-06 16:55:32 -080028
Ray Milkey269ffb92014-04-03 14:43:30 -070029 @Override
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070030 public LinkTuple getLinkTuple() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070031 return id;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070032 }
33
34 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070035 public Switch getSrcSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070036 topology.acquireReadLock();
37 try {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070038 return topology.getSwitch(id.getSrc().getDpid());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070039 } finally {
40 topology.releaseReadLock();
41 }
Ray Milkey269ffb92014-04-03 14:43:30 -070042 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 @Override
45 public Port getSrcPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070046 topology.acquireReadLock();
47 try {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070048 return topology.getPort(id.getSrc());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070049 } finally {
50 topology.releaseReadLock();
51 }
Ray Milkey269ffb92014-04-03 14:43:30 -070052 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080053
Ray Milkey269ffb92014-04-03 14:43:30 -070054 @Override
55 public Switch getDstSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070056 topology.acquireReadLock();
57 try {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070058 return topology.getSwitch(id.getDst().getDpid());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070059 } finally {
60 topology.releaseReadLock();
61 }
Ray Milkey269ffb92014-04-03 14:43:30 -070062 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080063
Ray Milkey269ffb92014-04-03 14:43:30 -070064 @Override
65 public Port getDstPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070066 topology.acquireReadLock();
67 try {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070068 return topology.getPort(id.getDst());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070069 } finally {
70 topology.releaseReadLock();
71 }
Ray Milkey269ffb92014-04-03 14:43:30 -070072 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080073
Ray Milkey269ffb92014-04-03 14:43:30 -070074 @Override
75 public long getLastSeenTime() {
76 // TODO Auto-generated method stub
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070077 throw new UnsupportedOperationException("Not implemented yet");
Ray Milkey269ffb92014-04-03 14:43:30 -070078 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080079
Ray Milkey269ffb92014-04-03 14:43:30 -070080 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070081 public Double getCapacity() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070082 return this.topology.getLinkEvent(id).getCapacity();
Ray Milkey269ffb92014-04-03 14:43:30 -070083 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080084
Praseed Balakrishnanfa21be12014-07-15 14:42:26 -070085 /**
86 * Returns the link type of the link.
87 *
88 * @return {@link net.onrc.onos.core.topology.LinkType} for this link
89 */
90 @Override
91 public LinkType getLinkType() {
92 return LinkType.valueOf(getStringAttribute(TopologyElement.ELEMENT_TYPE,
93 LinkType.ETHERNET_LINK.toString()));
94 }
95
Ray Milkey269ffb92014-04-03 14:43:30 -070096 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -070097 public String getStringAttribute(String attr) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070098 return this.topology.getLinkEvent(id).getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -070099 }
100
101 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700102 public String getStringAttribute(String attr, String def) {
103 final String v = getStringAttribute(attr);
104 if (v == null) {
105 return def;
106 } else {
107 return v;
108 }
109 }
110
111 @Override
112 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700113 return this.topology.getLinkEvent(id).getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700114 }
115
116 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 public String toString() {
118 return String.format("%s --(cap:%f Mbps)--> %s",
119 getSrcPort().toString(),
120 getCapacity(),
121 getDstPort().toString());
122 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700123
124
125 /**
126 * Returns the type of topology object.
127 *
128 * @return the type of the topology object
129 */
130 @Override
131 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700132 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700133 }
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -0700134
135 /**
136 * Returns the config state of topology element.
137 *
138 * @return ConfigState
139 */
140 @Override
141 public ConfigState getConfigState() {
142 return ConfigState.valueOf(getStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE));
143 }
144
145 /**
146 * Returns the status of topology element.
147 *
148 * @return AdminStatus
149 */
150 @Override
151 public AdminStatus getStatus() {
152 return AdminStatus.valueOf(getStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS));
153 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800154}