blob: 0debb706d7d1dcf82ccf93e5cde78a12d4c292c4 [file] [log] [blame]
Jonathan Hart062a2e82014-02-03 09:41:57 -08001package net.onrc.onos.ofcontroller.networkgraph;
2
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -08003import net.onrc.onos.datastore.topology.RCLink;
Jonathan Hart062a2e82014-02-03 09:41:57 -08004import net.onrc.onos.datastore.topology.RCPort;
5import net.onrc.onos.datastore.topology.RCSwitch;
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -08006import net.onrc.onos.ofcontroller.util.Dpid;
Jonathan Hart062a2e82014-02-03 09:41:57 -08007
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
10
11import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
12
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080013/**
14 * The "NB" read-only Network Map.
15 *
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080016 * TODO To be synchronized based on TopologyEvent Notification.
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080017 *
18 */
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080019public class NetworkGraphImpl extends AbstractNetworkGraph {
Jonathan Hart062a2e82014-02-03 09:41:57 -080020
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080021 private static final Logger log = LoggerFactory
22 .getLogger(NetworkGraphImpl.class);
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080023
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080024 public NetworkGraphImpl() {
25 super();
26 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080027
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080028 void addSwitch(Switch sw) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080029 if (sw == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080030 throw new IllegalArgumentException("Switch cannot be null");
31 }
32 switches.put(sw.getDpid(), sw);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080033 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080034
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080035 /**
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080036 * Deactivate Switch (and its Ports?)
37 *
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080038 * @param sw
39 */
40 void deactivateSwitch(Switch sw) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080041 if (sw == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080042 throw new IllegalArgumentException("Switch cannot be null");
43 }
44 SwitchImpl s = getSwitchImpl(sw);
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080045 // XXX When modifying existing object should we change the object itself
46 // or create a modified copy and switch them?
47
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080048 // TODO Deactivate Switch
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080049
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080050 // XXX Are we sure we want to deactivate Ports also?
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080051
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080052 // TODO Auto-generated method stub
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080053 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080054
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080055 void addPort(Port port) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080056 if (port == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080057 throw new IllegalArgumentException("Port cannot be null");
58 }
59 // TODO Auto-generated method stub
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080060
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080061 }
62
63 void deactivatePort(Port port) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080064 if (port == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080065 throw new IllegalArgumentException("Port cannot be null");
66 }
67 // TODO Auto-generated method stub
68
69 }
70
71 void addLink(Link link) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080072 if (link == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080073 throw new IllegalArgumentException("Link cannot be null");
74 }
75 // TODO Auto-generated method stub
76
77 }
78
79 void removeLink(Link link) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080080 if (link == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080081 throw new IllegalArgumentException("Link cannot be null");
82 }
83 // TODO Auto-generated method stub
84
85 }
86
87 void updateDevice(Device device) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080088 if (device == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080089 throw new IllegalArgumentException("Device cannot be null");
90 }
91 // TODO Auto-generated method stub
92
93 }
94
95 void removeDevice(Device device) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -080096 if (device == null) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -080097 throw new IllegalArgumentException("Device cannot be null");
98 }
99 // TODO Auto-generated method stub
100
101 }
102
103 private SwitchImpl getSwitchImpl(Switch sw) {
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800104 if (sw instanceof SwitchImpl) {
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800105 return (SwitchImpl) sw;
106 }
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800107 throw new ClassCastException("SwitchImpl expected, but found:"
108 + sw.getClass().getName());
109 }
110
111 public void loadWholeTopologyFromDB() {
112 // XXX clear everything first?
113
114 for (RCSwitch sw : RCSwitch.getAllSwitches()) {
115 try {
116 sw.read();
117 // TODO SwitchImpl probably should have a constructor from
118 // RCSwitch
119 SwitchImpl memSw = new SwitchImpl(this);
120 memSw.setDpid(sw.getDpid());
121
122 addSwitch(memSw);
123 } catch (ObjectDoesntExistException e) {
124 log.error("Read Switch Failed, skipping", e);
125 }
126 }
127
128 for (RCPort p : RCPort.getAllPorts()) {
129 try {
130 p.read();
131
132 // TODO PortImpl probably should have a constructor from RCPort
133 PortImpl memPort = new PortImpl(this);
134 // FIXME remove shortValue()
135 memPort.setPortNumber(p.getNumber().shortValue());
136 Switch sw = this.getSwitch(p.getDpid());
137 if (sw == null) {
138 log.error("Switch {} missing when adding Port {}",
139 new Dpid(p.getDpid()), p);
140 continue;
141 }
142 memPort.setSwitch(sw);
143
144 addPort(memPort);
145 } catch (ObjectDoesntExistException e) {
146 log.error("Read Port Failed, skipping", e);
147 }
148 }
149
150 // TODO Is Device going to be in DB? If so, read from DB.
151 // for (RCDevice d : RCDevice.getAllDevices()) {
152 // try {
153 // d.read();
154 //
155 // } catch (ObjectDoesntExistException e) {
156 // log.debug("Read Device Failed, skipping", e);
157 // }
158 // }
159
160 for (RCLink l : RCLink.getAllLinks()) {
161 try {
162 l.read();
163
164 LinkImpl memLink = new LinkImpl(this);
165
166 Switch srcSw = this.getSwitch(l.getSrc().dpid);
167 if (srcSw == null) {
168 log.error("Switch {} missing when adding Link {}",
169 new Dpid(l.getSrc().dpid), l);
170 continue;
171 }
172 memLink.setSrcSwitch(srcSw);
173 // FIXME remove shortValue()
174 memLink.setSrcPort(srcSw.getPort(l.getSrc().number.shortValue()));
175
176 Switch dstSw = this.getSwitch(l.getDst().dpid);
177 if (dstSw == null) {
178 log.error("Switch {} missing when adding Link {}",
179 new Dpid(l.getDst().dpid), l);
180 continue;
181 }
182 memLink.setDstSwitch(dstSw);
183 // FIXME remove shortValue()
184 memLink.setDstPort(srcSw.getPort(l.getDst().number.shortValue()));
185
186 addLink(memLink);
187 } catch (ObjectDoesntExistException e) {
188 log.debug("Delete Link Failed", e);
189 }
190 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800191 }
192
193 // FIXME To be removed later this class should never read from DB.
194 public void readSwitchFromTopology(long dpid) {
195 SwitchImpl sw = new SwitchImpl(this);
196
197 RCSwitch rcSwitch = new RCSwitch(dpid);
198 try {
199 rcSwitch.read();
200 } catch (ObjectDoesntExistException e) {
201 log.warn("Tried to get a switch that doesn't exist {}", dpid);
202 return;
Jonathan Hart062a2e82014-02-03 09:41:57 -0800203 }
204
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800205 sw.setDpid(rcSwitch.getDpid());
206
207 addSwitch(sw);
208
209 for (byte[] portId : rcSwitch.getAllPortIds()) {
210 RCPort rcPort = RCPort.createFromKey(portId);
211 try {
212 rcPort.read();
213
214 PortImpl port = new PortImpl(this);
215 // port.setDpid(dpid);
216
217 // TODO why are port numbers long?
218 // port.setPortNumber((short)rcPort.getNumber());
219
220 port.setSwitch(sw);
221 sw.addPort(port);
222
223 addPort(port);
224
225 } catch (ObjectDoesntExistException e) {
226 log.warn("Tried to read port that doesn't exist", rcPort);
227 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800228 }
229
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800230 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800231
232}