blob: 77bc942abf9a63ed8534aaf37b9a3aff362e3c83 [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 HIGUCHI4bfdd532014-02-07 13:47:36 -08003import java.util.Collections;
4import java.util.HashSet;
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -07005import java.util.Map;
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -08006import java.util.Set;
7
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -07008import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07009import net.onrc.onos.core.util.Dpid;
10import net.onrc.onos.core.util.PortNumber;
Jonathan Hart25bd53e2014-04-30 23:44:09 -070011import net.onrc.onos.core.util.SwitchPort;
12
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080013/**
14 * Port Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070015 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080016 * TODO REMOVE following design memo: This object itself may hold the DBObject,
17 * but this Object itself will not issue any read/write to the DataStore.
18 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070019public class PortImpl extends TopologyObject implements Port {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080020
Ray Milkey269ffb92014-04-03 14:43:30 -070021 private Switch sw;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080022
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070023 private PortNumber number;
Ray Milkey269ffb92014-04-03 14:43:30 -070024 private String description;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080025
Jonathan Hart25bd53e2014-04-30 23:44:09 -070026 private final SwitchPort switchPort;
27
Jonathan Harte37e4e22014-05-13 19:12:02 -070028 // These needs to be ConcurrentCollecton if allowing the topology to be
29 // accessed concurrently
Ray Milkey269ffb92014-04-03 14:43:30 -070030 protected Set<Device> devices;
Jonathan Hart062a2e82014-02-03 09:41:57 -080031
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070032 public PortImpl(Topology topology, Switch parentSwitch, PortNumber number) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070033 super(topology);
Ray Milkey269ffb92014-04-03 14:43:30 -070034 this.sw = parentSwitch;
35 this.number = number;
36 this.devices = new HashSet<>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -070037
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070038 switchPort = new SwitchPort(parentSwitch.getDpid(),
39 number);
40 }
41
42 public PortImpl(Topology topology, Switch parentSwitch, Long number) {
43 this(topology, parentSwitch, new PortNumber(number.shortValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -070044 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080045
Ray Milkey269ffb92014-04-03 14:43:30 -070046 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070047 public Dpid getDpid() {
Ray Milkey269ffb92014-04-03 14:43:30 -070048 return sw.getDpid();
49 }
Yuta HIGUCHIea516d62014-02-13 15:59:32 -080050
Ray Milkey269ffb92014-04-03 14:43:30 -070051 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070052 public PortNumber getNumber() {
Ray Milkey269ffb92014-04-03 14:43:30 -070053 return number;
54 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080055
Ray Milkey269ffb92014-04-03 14:43:30 -070056 @Override
Jonathan Hart25bd53e2014-04-30 23:44:09 -070057 public SwitchPort asSwitchPort() {
58 return switchPort;
59 }
60
61 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070062 public String getDescription() {
63 return description;
64 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 public void setDescription(String description) {
67 this.description = description;
68 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080069
Ray Milkey269ffb92014-04-03 14:43:30 -070070 @Override
71 public Long getHardwareAddress() {
72 // TODO Auto-generated method stub
73 return 0L;
74 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080075
Ray Milkey269ffb92014-04-03 14:43:30 -070076 @Override
77 public Switch getSwitch() {
78 return sw;
79 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080080
Ray Milkey269ffb92014-04-03 14:43:30 -070081 @Override
82 public Link getOutgoingLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070083 return topology.getOutgoingLink(switchPort.dpid(),
84 switchPort.port());
Ray Milkey269ffb92014-04-03 14:43:30 -070085 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080086
Ray Milkey269ffb92014-04-03 14:43:30 -070087 @Override
88 public Link getIncomingLink() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070089 return topology.getIncomingLink(switchPort.dpid(),
90 switchPort.port());
Ray Milkey269ffb92014-04-03 14:43:30 -070091 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080092
Ray Milkey269ffb92014-04-03 14:43:30 -070093 @Override
94 public Iterable<Device> getDevices() {
95 return Collections.unmodifiableSet(this.devices);
96 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080097
Ray Milkey269ffb92014-04-03 14:43:30 -070098 /**
99 * @param d
100 * @return true if successfully added
101 */
102 public boolean addDevice(Device d) {
103 return this.devices.add(d);
104 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800105
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 /**
107 * @param d
108 * @return true if device existed and was removed
109 */
110 public boolean removeDevice(Device d) {
111 return this.devices.remove(d);
112 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800113
Ray Milkey269ffb92014-04-03 14:43:30 -0700114 public void removeAllDevice() {
115 this.devices.clear();
116 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800117
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700119 public String getStringAttribute(String attr) {
120 throw new UnsupportedOperationException("Not implemented yet");
121 }
122
123 @Override
124 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
125 justification = "getStringAttribute might return null once implemented")
126 public String getStringAttribute(String attr, String def) {
127 final String v = getStringAttribute(attr);
128 if (v == null) {
129 return def;
130 } else {
131 return v;
132 }
133 }
134
135 @Override
136 public Map<String, String> getAllStringAttributes() {
137 throw new UnsupportedOperationException("Not implemented yet");
138 }
139
140 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700141 public String toString() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700142 return String.format("%s:%s",
Ray Milkey269ffb92014-04-03 14:43:30 -0700143 getSwitch().getDpid(),
144 getNumber());
145 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800146}