blob: 6d9874063f0bae5128db80bdcd0c3d5dfd9f40e5 [file] [log] [blame]
Jonathan Hart062a2e82014-02-03 09:41:57 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
3import java.util.Collection;
4import java.util.Collections;
5import java.util.HashMap;
Toshio Koide2f570c12014-02-06 16:55:32 -08006import java.util.LinkedList;
Jonathan Hart062a2e82014-02-03 09:41:57 -08007import java.util.Map;
8
Jonathan Hart062a2e82014-02-03 09:41:57 -08009import net.onrc.onos.datastore.topology.RCPort;
10import net.onrc.onos.datastore.topology.RCSwitch;
Yuta HIGUCHI1929b5d2014-02-05 20:14:51 -080011import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
12import edu.stanford.ramcloud.JRamCloud.WrongVersionException;
Jonathan Hart062a2e82014-02-03 09:41:57 -080013
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080014/**
15 * Switch Object stored in In-memory Topology.
16 *
17 * TODO REMOVE following design memo: This object itself may hold the DBObject,
18 * but this Object itself will not issue any read/write to the DataStore.
19 */
Jonathan Hart062a2e82014-02-03 09:41:57 -080020public class SwitchImpl extends NetworkGraphObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080021
Toshio Koide2f570c12014-02-06 16:55:32 -080022 private Long dpid;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080023 // These needs to be ConcurrentCollecton if allowing Graph to be accessed Concurrently
Toshio Koide2f570c12014-02-06 16:55:32 -080024 private final Map<Long, Port> ports;
Jonathan Hart062a2e82014-02-03 09:41:57 -080025
Toshio Koide2f570c12014-02-06 16:55:32 -080026 public SwitchImpl(NetworkGraph graph, Long dpid) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080027 super(graph);
Toshio Koide2f570c12014-02-06 16:55:32 -080028 this.dpid = dpid;
29 ports = new HashMap<Long, Port>();
Jonathan Hart062a2e82014-02-03 09:41:57 -080030 }
31
32 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080033 public Long getDpid() {
Jonathan Hart062a2e82014-02-03 09:41:57 -080034 return dpid;
35 }
36
37 @Override
38 public Collection<Port> getPorts() {
39 return Collections.unmodifiableCollection(ports.values());
40 }
41
42 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080043 public Port getPort(Long number) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080044 return ports.get(number);
45 }
46
47 @Override
Jonathan Hart062a2e82014-02-03 09:41:57 -080048 public Iterable<Switch> getNeighbors() {
49 // TODO Auto-generated method stub
50 return null;
51 }
52
53 @Override
Toshio Koide2f570c12014-02-06 16:55:32 -080054 public Link getLinkToNeighbor(Long neighborDpid) {
55 for (Link link : graph.getOutgoingLinksFromSwitch(dpid)) {
Yuta HIGUCHIcb951982014-02-11 13:31:44 -080056 if (link.getDestinationSwitch().getDpid().equals(neighborDpid) ) {
Jonathan Hart062a2e82014-02-03 09:41:57 -080057 return link;
58 }
59 }
60 return null;
61 }
62
63 @Override
64 public Collection<Device> getDevices() {
65 // TODO Auto-generated method stub
66 return null;
67 }
68
Jonathan Hart062a2e82014-02-03 09:41:57 -080069 public void addPort(Port port) {
70 this.ports.put(port.getNumber(), port);
71 }
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -080072
73 public Port removePort(Port port) {
74 Port p = this.ports.remove(port.getNumber());
75 // XXX Do we need to validate instance equality?
76 assert( p == port );
77 return p;
78 }
79
Toshio Koide2f570c12014-02-06 16:55:32 -080080 public Port addPort(Long portNumber) {
81 PortImpl port = new PortImpl(graph, this, portNumber);
82 ports.put(port.getNumber(), port);
83 return port;
Jonathan Hart062a2e82014-02-03 09:41:57 -080084 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080085
Jonathan Hart062a2e82014-02-03 09:41:57 -080086 public void store() {
87 RCSwitch rcSwitch = new RCSwitch(dpid);
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080088
Jonathan Hart062a2e82014-02-03 09:41:57 -080089 for (Port port : ports.values()) {
90 RCPort rcPort = new RCPort(dpid, (long)port.getNumber());
91 rcSwitch.addPortId(rcPort.getId());
92 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080093
94
Jonathan Hart062a2e82014-02-03 09:41:57 -080095 try {
96 rcSwitch.update();
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080097
Jonathan Hart062a2e82014-02-03 09:41:57 -080098 } catch (ObjectDoesntExistException | WrongVersionException e) {
99 // TODO Auto-generated catch block
100 e.printStackTrace();
101 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800102
Jonathan Hart062a2e82014-02-03 09:41:57 -0800103 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800104
105 @Override
106 public Iterable<Link> getOutgoingLinks() {
107 LinkedList<Link> links = new LinkedList<Link>();
108 for (Port port: getPorts()) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800109 Link link = port.getOutgoingLink();
Toshio Koide2f570c12014-02-06 16:55:32 -0800110 if (link != null) {
111 links.add(link);
112 }
113 }
114 return links;
115 }
116
117 @Override
118 public Iterable<Link> getIncomingLinks() {
119 LinkedList<Link> links = new LinkedList<Link>();
120 for (Port port: getPorts()) {
Yuta HIGUCHI4bfdd532014-02-07 13:47:36 -0800121 Link link = port.getIncomingLink();
Toshio Koide2f570c12014-02-06 16:55:32 -0800122 if (link != null) {
123 links.add(link);
124 }
125 }
126 return links;
127 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800128}