blob: 5886c8e21949246d37c01000b2c01882a12b82cb [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/**
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);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070026 Validate.notNull(linkTuple);
27 this.id = linkTuple;
Ray Milkey269ffb92014-04-03 14:43:30 -070028 }
Toshio Koide2f570c12014-02-06 16:55:32 -080029
Ray Milkey269ffb92014-04-03 14:43:30 -070030 @Override
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070031 public LinkTuple getLinkTuple() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070032 return id;
Yuta HIGUCHI02ccb8c2014-07-10 11:29:45 -070033 }
34
35 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070036 public Switch getSrcSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070037 topology.acquireReadLock();
38 try {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070039 return topology.getSwitch(id.getSrc().getDpid());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070040 } finally {
41 topology.releaseReadLock();
42 }
Ray Milkey269ffb92014-04-03 14:43:30 -070043 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 @Override
46 public Port getSrcPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070047 topology.acquireReadLock();
48 try {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070049 return topology.getPort(id.getSrc());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070050 } finally {
51 topology.releaseReadLock();
52 }
Ray Milkey269ffb92014-04-03 14:43:30 -070053 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080054
Ray Milkey269ffb92014-04-03 14:43:30 -070055 @Override
56 public Switch getDstSwitch() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070057 topology.acquireReadLock();
58 try {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070059 return topology.getSwitch(id.getDst().getDpid());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070060 } finally {
61 topology.releaseReadLock();
62 }
Ray Milkey269ffb92014-04-03 14:43:30 -070063 }
Pavlin Radoslavov7c8f69a2014-02-19 19:01:45 -080064
Ray Milkey269ffb92014-04-03 14:43:30 -070065 @Override
66 public Port getDstPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070067 topology.acquireReadLock();
68 try {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070069 return topology.getPort(id.getDst());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070070 } finally {
71 topology.releaseReadLock();
72 }
Ray Milkey269ffb92014-04-03 14:43:30 -070073 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080074
Ray Milkey269ffb92014-04-03 14:43:30 -070075 @Override
76 public long getLastSeenTime() {
77 // TODO Auto-generated method stub
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070078 throw new UnsupportedOperationException("Not implemented yet");
Ray Milkey269ffb92014-04-03 14:43:30 -070079 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070082 public Double getCapacity() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070083 return this.topology.getLinkEvent(id).getCapacity();
Ray Milkey269ffb92014-04-03 14:43:30 -070084 }
Toshio Koide0c9106d2014-02-19 15:26:38 -080085
Praseed Balakrishnanfa21be12014-07-15 14:42:26 -070086 /**
87 * Returns the link type of the link.
88 *
89 * @return {@link net.onrc.onos.core.topology.LinkType} for this link
90 */
91 @Override
92 public LinkType getLinkType() {
93 return LinkType.valueOf(getStringAttribute(TopologyElement.ELEMENT_TYPE,
94 LinkType.ETHERNET_LINK.toString()));
95 }
96
Ray Milkey269ffb92014-04-03 14:43:30 -070097 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -070098 public String getStringAttribute(String attr) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070099 return this.topology.getLinkEvent(id).getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700100 }
101
102 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700103 public String getStringAttribute(String attr, String def) {
104 final String v = getStringAttribute(attr);
105 if (v == null) {
106 return def;
107 } else {
108 return v;
109 }
110 }
111
112 @Override
113 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700114 return this.topology.getLinkEvent(id).getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700115 }
116
117 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 public String toString() {
119 return String.format("%s --(cap:%f Mbps)--> %s",
120 getSrcPort().toString(),
121 getCapacity(),
122 getDstPort().toString());
123 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700124
125
126 /**
127 * Returns the type of topology object.
128 *
129 * @return the type of the topology object
130 */
131 @Override
132 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700133 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700134 }
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -0700135
136 /**
137 * Returns the config state of topology element.
138 *
139 * @return ConfigState
140 */
141 @Override
142 public ConfigState getConfigState() {
143 return ConfigState.valueOf(getStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE));
144 }
145
146 /**
147 * Returns the status of topology element.
148 *
149 * @return AdminStatus
150 */
151 @Override
152 public AdminStatus getStatus() {
153 return AdminStatus.valueOf(getStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS));
154 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800155}