blob: 5d460051300c010eb6055fb39935e3320356b4a9 [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;
Jonathan Hart25bd53e2014-04-30 23:44:09 -07009import net.onrc.onos.core.util.SwitchPort;
10
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080011/**
12 * Port Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070013 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080014 * TODO REMOVE following design memo: This object itself may hold the DBObject,
15 * but this Object itself will not issue any read/write to the DataStore.
16 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070017public class PortImpl extends TopologyObject implements Port {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080018
Ray Milkey269ffb92014-04-03 14:43:30 -070019 private Switch sw;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080020
Ray Milkey269ffb92014-04-03 14:43:30 -070021 private Long number;
22 private String description;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080023
Jonathan Hart25bd53e2014-04-30 23:44:09 -070024 private final SwitchPort switchPort;
25
Jonathan Harte37e4e22014-05-13 19:12:02 -070026 // These needs to be ConcurrentCollecton if allowing the topology to be
27 // accessed concurrently
Ray Milkey269ffb92014-04-03 14:43:30 -070028 protected Set<Device> devices;
Jonathan Hart062a2e82014-02-03 09:41:57 -080029
Jonathan Harte37e4e22014-05-13 19:12:02 -070030 public PortImpl(Topology topology, Switch parentSwitch, Long number) {
31 super(topology);
Ray Milkey269ffb92014-04-03 14:43:30 -070032 this.sw = parentSwitch;
33 this.number = number;
34 this.devices = new HashSet<>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -070035
36 switchPort = new SwitchPort(parentSwitch.getDpid(), number.shortValue());
Ray Milkey269ffb92014-04-03 14:43:30 -070037 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080038
Ray Milkey269ffb92014-04-03 14:43:30 -070039 @Override
40 public Long getDpid() {
41 return sw.getDpid();
42 }
Yuta HIGUCHIea516d62014-02-13 15:59:32 -080043
Ray Milkey269ffb92014-04-03 14:43:30 -070044 @Override
45 public Long getNumber() {
46 return number;
47 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080048
Ray Milkey269ffb92014-04-03 14:43:30 -070049 @Override
Jonathan Hart25bd53e2014-04-30 23:44:09 -070050 public SwitchPort asSwitchPort() {
51 return switchPort;
52 }
53
54 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070055 public String getDescription() {
56 return description;
57 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080058
Ray Milkey269ffb92014-04-03 14:43:30 -070059 public void setDescription(String description) {
60 this.description = description;
61 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 @Override
64 public Long getHardwareAddress() {
65 // TODO Auto-generated method stub
66 return 0L;
67 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080068
Ray Milkey269ffb92014-04-03 14:43:30 -070069 @Override
70 public Switch getSwitch() {
71 return sw;
72 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080073
Ray Milkey269ffb92014-04-03 14:43:30 -070074 @Override
75 public Link getOutgoingLink() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070076 return topology.getOutgoingLink(switchPort.dpid().value(),
Jonathan Hart25bd53e2014-04-30 23:44:09 -070077 (long) switchPort.port().value());
Ray Milkey269ffb92014-04-03 14:43:30 -070078 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080079
Ray Milkey269ffb92014-04-03 14:43:30 -070080 @Override
81 public Link getIncomingLink() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070082 return topology.getIncomingLink(switchPort.dpid().value(),
Jonathan Hart25bd53e2014-04-30 23:44:09 -070083 (long) switchPort.port().value());
Ray Milkey269ffb92014-04-03 14:43:30 -070084 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080085
Ray Milkey269ffb92014-04-03 14:43:30 -070086 @Override
87 public Iterable<Device> getDevices() {
88 return Collections.unmodifiableSet(this.devices);
89 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080090
Ray Milkey269ffb92014-04-03 14:43:30 -070091 /**
92 * @param d
93 * @return true if successfully added
94 */
95 public boolean addDevice(Device d) {
96 return this.devices.add(d);
97 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 /**
100 * @param d
101 * @return true if device existed and was removed
102 */
103 public boolean removeDevice(Device d) {
104 return this.devices.remove(d);
105 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800106
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 public void removeAllDevice() {
108 this.devices.clear();
109 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800110
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700112 public String getStringAttribute(String attr) {
113 throw new UnsupportedOperationException("Not implemented yet");
114 }
115
116 @Override
117 @SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE",
118 justification = "getStringAttribute might return null once implemented")
119 public String getStringAttribute(String attr, String def) {
120 final String v = getStringAttribute(attr);
121 if (v == null) {
122 return def;
123 } else {
124 return v;
125 }
126 }
127
128 @Override
129 public Map<String, String> getAllStringAttributes() {
130 throw new UnsupportedOperationException("Not implemented yet");
131 }
132
133 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700134 public String toString() {
135 return String.format("%d:%d",
136 getSwitch().getDpid(),
137 getNumber());
138 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800139}