blob: e88d23be45f5d8a345de142beeeb992c4e49029a [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;
5import java.util.Set;
6
Jonathan Hart25bd53e2014-04-30 23:44:09 -07007import net.onrc.onos.core.util.SwitchPort;
8
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -08009/**
10 * Port Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070011 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080012 * TODO REMOVE following design memo: This object itself may hold the DBObject,
13 * but this Object itself will not issue any read/write to the DataStore.
14 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070015public class PortImpl extends TopologyObject implements Port {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080016
Ray Milkey269ffb92014-04-03 14:43:30 -070017 private Switch sw;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080018
Ray Milkey269ffb92014-04-03 14:43:30 -070019 private Long number;
20 private String description;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080021
Jonathan Hart25bd53e2014-04-30 23:44:09 -070022 private final SwitchPort switchPort;
23
Jonathan Harte37e4e22014-05-13 19:12:02 -070024 // These needs to be ConcurrentCollecton if allowing the topology to be
25 // accessed concurrently
Ray Milkey269ffb92014-04-03 14:43:30 -070026 protected Set<Device> devices;
Jonathan Hart062a2e82014-02-03 09:41:57 -080027
Jonathan Harte37e4e22014-05-13 19:12:02 -070028 public PortImpl(Topology topology, Switch parentSwitch, Long number) {
29 super(topology);
Ray Milkey269ffb92014-04-03 14:43:30 -070030 this.sw = parentSwitch;
31 this.number = number;
32 this.devices = new HashSet<>();
Jonathan Hart25bd53e2014-04-30 23:44:09 -070033
34 switchPort = new SwitchPort(parentSwitch.getDpid(), number.shortValue());
Ray Milkey269ffb92014-04-03 14:43:30 -070035 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080036
Ray Milkey269ffb92014-04-03 14:43:30 -070037 @Override
38 public Long getDpid() {
39 return sw.getDpid();
40 }
Yuta HIGUCHIea516d62014-02-13 15:59:32 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 @Override
43 public Long getNumber() {
44 return number;
45 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080046
Ray Milkey269ffb92014-04-03 14:43:30 -070047 @Override
Jonathan Hart25bd53e2014-04-30 23:44:09 -070048 public SwitchPort asSwitchPort() {
49 return switchPort;
50 }
51
52 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070053 public String getDescription() {
54 return description;
55 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080056
Ray Milkey269ffb92014-04-03 14:43:30 -070057 public void setDescription(String description) {
58 this.description = description;
59 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080060
Ray Milkey269ffb92014-04-03 14:43:30 -070061 @Override
62 public Long getHardwareAddress() {
63 // TODO Auto-generated method stub
64 return 0L;
65 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080066
Ray Milkey269ffb92014-04-03 14:43:30 -070067 @Override
68 public Switch getSwitch() {
69 return sw;
70 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080071
Ray Milkey269ffb92014-04-03 14:43:30 -070072 @Override
73 public Link getOutgoingLink() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070074 return topology.getOutgoingLink(switchPort.dpid().value(),
Jonathan Hart25bd53e2014-04-30 23:44:09 -070075 (long) switchPort.port().value());
Ray Milkey269ffb92014-04-03 14:43:30 -070076 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080077
Ray Milkey269ffb92014-04-03 14:43:30 -070078 @Override
79 public Link getIncomingLink() {
Jonathan Harte37e4e22014-05-13 19:12:02 -070080 return topology.getIncomingLink(switchPort.dpid().value(),
Jonathan Hart25bd53e2014-04-30 23:44:09 -070081 (long) switchPort.port().value());
Ray Milkey269ffb92014-04-03 14:43:30 -070082 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080083
Ray Milkey269ffb92014-04-03 14:43:30 -070084 @Override
85 public Iterable<Device> getDevices() {
86 return Collections.unmodifiableSet(this.devices);
87 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080088
Ray Milkey269ffb92014-04-03 14:43:30 -070089 /**
90 * @param d
91 * @return true if successfully added
92 */
93 public boolean addDevice(Device d) {
94 return this.devices.add(d);
95 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080096
Ray Milkey269ffb92014-04-03 14:43:30 -070097 /**
98 * @param d
99 * @return true if device existed and was removed
100 */
101 public boolean removeDevice(Device d) {
102 return this.devices.remove(d);
103 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800104
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 public void removeAllDevice() {
106 this.devices.clear();
107 }
Yuta HIGUCHIcd922f42014-02-11 18:59:11 -0800108
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 @Override
110 public String toString() {
111 return String.format("%d:%d",
112 getSwitch().getDpid(),
113 getNumber());
114 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800115}