blob: 50fda95064d7c4477f785721f52f5d0a08cbff74 [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
Toshio Koide2f570c12014-02-06 16:55:32 -0800119 SwitchImpl memSw = new SwitchImpl(this, sw.getDpid());
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800120
121 addSwitch(memSw);
122 } catch (ObjectDoesntExistException e) {
123 log.error("Read Switch Failed, skipping", e);
124 }
125 }
126
127 for (RCPort p : RCPort.getAllPorts()) {
128 try {
129 p.read();
130
131 // TODO PortImpl probably should have a constructor from RCPort
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800132 Switch sw = this.getSwitch(p.getDpid());
133 if (sw == null) {
134 log.error("Switch {} missing when adding Port {}",
135 new Dpid(p.getDpid()), p);
136 continue;
137 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800138 PortImpl memPort = new PortImpl(this, sw, p.getNumber());
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800139
140 addPort(memPort);
141 } catch (ObjectDoesntExistException e) {
142 log.error("Read Port Failed, skipping", e);
143 }
144 }
145
146 // TODO Is Device going to be in DB? If so, read from DB.
147 // for (RCDevice d : RCDevice.getAllDevices()) {
148 // try {
149 // d.read();
150 //
151 // } catch (ObjectDoesntExistException e) {
152 // log.debug("Read Device Failed, skipping", e);
153 // }
154 // }
155
156 for (RCLink l : RCLink.getAllLinks()) {
Toshio Koide2f570c12014-02-06 16:55:32 -0800157 try {
158 l.read();
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800159
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800160
Toshio Koide2f570c12014-02-06 16:55:32 -0800161 Switch srcSw = this.getSwitch(l.getSrc().dpid);
162 if (srcSw == null) {
163 log.error("Switch {} missing when adding Link {}",
164 new Dpid(l.getSrc().dpid), l);
165 continue;
166 }
167
168 Switch dstSw = this.getSwitch(l.getDst().dpid);
169 if (dstSw == null) {
170 log.error("Switch {} missing when adding Link {}",
171 new Dpid(l.getDst().dpid), l);
172 continue;
173 }
174
175 LinkImpl memLink = new LinkImpl(this,
176 srcSw.getPort(l.getSrc().number),
177 dstSw.getPort(l.getDst().number));
178
179 addLink(memLink);
180 } catch (ObjectDoesntExistException e) {
181 log.debug("Delete Link Failed", e);
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800182 }
Yuta HIGUCHI765cd0d2014-02-06 12:46:41 -0800183 }
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800184 }
185
186 // FIXME To be removed later this class should never read from DB.
Toshio Koide2f570c12014-02-06 16:55:32 -0800187 public void readSwitchFromTopology(Long dpid) {
188 SwitchImpl sw = new SwitchImpl(this, dpid);
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800189
190 RCSwitch rcSwitch = new RCSwitch(dpid);
191 try {
192 rcSwitch.read();
193 } catch (ObjectDoesntExistException e) {
194 log.warn("Tried to get a switch that doesn't exist {}", dpid);
195 return;
Jonathan Hart062a2e82014-02-03 09:41:57 -0800196 }
197
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800198 addSwitch(sw);
199
200 for (byte[] portId : rcSwitch.getAllPortIds()) {
201 RCPort rcPort = RCPort.createFromKey(portId);
202 try {
203 rcPort.read();
204
Toshio Koide2f570c12014-02-06 16:55:32 -0800205 PortImpl port = new PortImpl(this, sw, rcPort.getNumber());
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800206
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800207 sw.addPort(port);
208
209 addPort(port);
210
211 } catch (ObjectDoesntExistException e) {
212 log.warn("Tried to read port that doesn't exist", rcPort);
213 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800214 }
215
Yuta HIGUCHI80829d12014-02-05 20:16:56 -0800216 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800217
218}