blob: 26d4e40b0d500579fed1df0d849abd88abe1630f [file] [log] [blame]
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -07001package net.onrc.onos.ofcontroller.core.internal;
Pankaj Berde3200ea02013-01-04 15:48:36 -08002
Pankaj Berdebbd38612013-06-22 05:59:12 -07003import net.floodlightcontroller.core.IOFSwitch;
Pankaj Berde38646d62013-06-21 11:34:04 -07004import net.onrc.onos.graph.GraphDBConnection;
5import net.onrc.onos.graph.GraphDBOperation;
HIGUCHI Yuta20514902013-06-12 11:24:16 -07006import net.onrc.onos.ofcontroller.core.ISwitchStorage;
7import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
8import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berdeafb20532013-01-08 15:05:24 -08009
Pankaj Berde3200ea02013-01-04 15:48:36 -080010import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -080011import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
12import org.openflow.protocol.OFPhysicalPort.OFPortState;
Pankaj Berde15193092013-03-21 17:30:14 -070013import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
Pankaj Berde3200ea02013-01-04 15:48:36 -080015
Teruef33dc32013-06-20 09:54:37 -070016/**
Naoki Shiota987a5722013-10-23 11:59:36 -070017 * This is the class for storing the information of switches into GraphDB
Teruef33dc32013-06-20 09:54:37 -070018 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080019public class SwitchStorageImpl implements ISwitchStorage {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -070020 protected GraphDBOperation op;
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070021 protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Teruef33dc32013-06-20 09:54:37 -070022
23 /***
24 * Initialize function. Before you use this class, please call this method
25 * @param conf configuration file for Cassandra DB
26 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080027 @Override
Teruef33dc32013-06-20 09:54:37 -070028 public void init(String conf) {
29 GraphDBConnection conn = GraphDBConnection.getInstance(conf);
30 op = new GraphDBOperation(conn);
31 }
Pankaj Berde3200ea02013-01-04 15:48:36 -080032
Teruef33dc32013-06-20 09:54:37 -070033 /***
34 * Finalize/close function. After you use this class, please call this method.
35 * It will close the DB connection.
36 */
37 public void finalize() {
38 close();
39 }
40
41 /***
42 * Finalize/close function. After you use this class, please call this method.
Pavlin Radoslavovef0cb002013-06-21 14:55:23 -070043 * It will close the DB connection. This is for Java garbage collection.
Teruef33dc32013-06-20 09:54:37 -070044 */
45 @Override
46 public void close() {
47 op.close();
48 }
49
Naoki Shiota987a5722013-10-23 11:59:36 -070050 // Method designing policy:
51 // op.commit() and op.rollback() MUST called in public (first-class) methods.
52 // A first-class method MUST NOT call other first-class method.
53 // Routine process should be implemented in private method.
54 // A private method MUST NOT call commit or rollback.
55
Teruef33dc32013-06-20 09:54:37 -070056 /***
Teruef33dc32013-06-20 09:54:37 -070057 * This function is for updating the switch into the DB.
58 * @param dpid The switch dpid you want to update from the DB
59 * @param state The state of the switch like ACTIVE, INACTIVE
60 * @param dmope The DM_OPERATION of the switch
61 */
62 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070063 public boolean updateSwitch(String dpid, SwitchState state, DM_OPERATION dmope) {
64 boolean success = false;
65 ISwitchObject sw = null;
66
67 log.info("SwitchStorage:update {} dpid:{}", dmope, dpid);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070068 switch(dmope) {
69 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070070 try {
71 sw = op.searchSwitch(dpid);
72 if (sw != null) {
73 setSwitchStateImpl(sw, state);
74 op.commit();
75 success = true;
76 }
77 } catch (Exception e) {
78 op.rollback();
79 e.printStackTrace();
80 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
81 }
82 break;
Naoki Shiotab2d17e82013-10-18 18:08:16 -070083 case INSERT:
84 case CREATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070085 try {
86 sw = addSwitchImpl(dpid);
87 if (sw != null) {
88 if (state != SwitchState.ACTIVE) {
89 setSwitchStateImpl(sw, state);
90 }
91 op.commit();
92 success = true;
93 }
94 } catch (Exception e) {
95 op.rollback();
96 e.printStackTrace();
97 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
98 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070099 break;
100 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -0700101 try {
102 sw = op.searchSwitch(dpid);
103 if (sw != null) {
104 deleteSwitchImpl(sw);
105 op.commit();
106 success = true;
107 }
108 } catch (Exception e) {
109 op.rollback();
110 e.printStackTrace();
111 log.info("SwitchStorage:update {} dpid:{} failed", dmope, dpid);
112 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700113 break;
114 default:
115 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700116
117 return success;
118 }
119
120 @Override
121 public boolean addSwitch(IOFSwitch sw) {
122 boolean success = false;
123
124 String dpid = sw.getStringId();
125 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
126
127 try {
128 ISwitchObject curr = op.searchSwitch(dpid);
129 if (curr != null) {
130 //If existing the switch. set The SW state ACTIVE.
131 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
132 setSwitchStateImpl(curr, SwitchState.ACTIVE);
133 } else {
134 curr = addSwitchImpl(dpid);
135 }
136
137 for (OFPhysicalPort port: sw.getPorts()) {
138 IPortObject p = op.searchPort(dpid, port.getPortNumber());
139 if (p != null) {
140 log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
141 setPortStateImpl(p, port.getState(), port.getName());
142 p.setState("ACTIVE");
143 } else {
144 p = addPortImpl(curr, port.getPortNumber());
145 setPortStateImpl(p, port.getState(), port.getName());
146 }
147 }
148 op.commit();
149 success = true;
150 } catch (Exception e) {
151 op.rollback();
152 e.printStackTrace();
153 log.error("SwitchStorage:addSwitch dpid:{} failed", dpid);
154 }
155
156 return success;
157 }
158
159 /***
160 * This function is for adding the switch into the DB.
161 * @param dpid The switch dpid you want to add into the DB.
162 */
163 @Override
164 public boolean addSwitch(String dpid) {
165 boolean success = false;
166
167 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
168 try {
169 ISwitchObject sw = op.searchSwitch(dpid);
170 if (sw != null) {
171 //If existing the switch. set The SW state ACTIVE.
172 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
173 setSwitchStateImpl(sw, SwitchState.ACTIVE);
174 } else {
175 addSwitchImpl(dpid);
176 }
177 op.commit();
178 success = true;
179 } catch (Exception e) {
180 op.rollback();
181 e.printStackTrace();
182 log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
183 }
184
185 return success;
186 }
187
188 /***
189 * This function is for deleting the switch into the DB.
190 * @param dpid The switch dpid you want to delete from the DB.
191 */
192 @Override
193 public boolean deleteSwitch(String dpid) {
194 boolean success = false;
195
196 try {
197 ISwitchObject sw = op.searchSwitch(dpid);
198 if (sw != null) {
199 deleteSwitchImpl(sw);
200 op.commit();
201 }
202 success = true;
203 } catch (Exception e) {
204 op.rollback();
205 e.printStackTrace();
206 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
207 }
208
209 return success;
210 }
211
212 public boolean updatePort(String dpid, short portNum, int state, String desc) {
213 boolean success = false;
214
215 try {
216 ISwitchObject sw = op.searchSwitch(dpid);
217
218 if (sw != null) {
219 IPortObject p = sw.getPort(portNum);
220 log.info("SwitchStorage:updatePort dpid:{} port:{}", dpid, portNum);
221 if (p != null) {
222 setPortStateImpl(p, state, desc);
223 }
224 op.commit();
225 success = true;
226 } else {
227 log.error("SwitchStorage:updatePort dpid:{} port:{} : failed switch does not exist", dpid, portNum);
228 }
229 } catch (Exception e) {
230 op.rollback();
231 e.printStackTrace();
232 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, portNum);
233 }
234
235 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700236 }
237
238 /***
239 * This function is for adding the switch port into the DB.
240 * @param dpid The switch dpid that has the port.
Naoki Shiota987a5722013-10-23 11:59:36 -0700241 * @param phport The port you want to add the switch.
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700242 */
243 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700244 public boolean addPort(String dpid, OFPhysicalPort phport) {
245 boolean success = false;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700246
Naoki Shiota987a5722013-10-23 11:59:36 -0700247 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & phport.getConfig()) > 0) ||
248 ((OFPortState.OFPPS_LINK_DOWN.getValue() & phport.getState()) > 0)) {
249 // just dispatch to deletePort()
250 return deletePort(dpid, phport.getPortNumber());
251 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700252
253 try {
254 ISwitchObject sw = op.searchSwitch(dpid);
255
256 if (sw != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700257 IPortObject p = sw.getPort(phport.getPortNumber());
258 log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700259 if (p != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700260 setPortStateImpl(p, phport.getState(), phport.getName());
261 log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700262 } else {
Naoki Shiota987a5722013-10-23 11:59:36 -0700263 addPortImpl(sw, phport.getPortNumber());
264 setPortStateImpl(p, phport.getState(), phport.getName());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700265 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700266 op.commit();
267 success = true;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700268 } else {
Naoki Shiota987a5722013-10-23 11:59:36 -0700269 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700270 }
271 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700272 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700273 e.printStackTrace();
274 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, phport.getPortNumber());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700275 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700276
277 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700278 }
279
280 /***
281 * This function is for deleting the switch port from the DB.
282 * @param dpid The switch dpid that has the port.
283 * @param port The port you want to delete the switch.
284 */
285 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700286 public boolean deletePort(String dpid, short port) {
287 boolean success = false;
288
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700289 try {
290 ISwitchObject sw = op.searchSwitch(dpid);
291
292 if (sw != null) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700293 IPortObject p = sw.getPort(port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700294 if (p != null) {
295 log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
Naoki Shiota987a5722013-10-23 11:59:36 -0700296 deletePortImpl(p);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700297 op.commit();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700298 }
299 }
300 } catch (Exception e) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700301 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700302 e.printStackTrace();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700303 log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700304 }
305
Naoki Shiota987a5722013-10-23 11:59:36 -0700306 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700307 }
308
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700309 private ISwitchObject addSwitchImpl(String dpid) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700310 if (dpid != null) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700311 ISwitchObject sw = op.newSwitch(dpid);
Naoki Shiota987a5722013-10-23 11:59:36 -0700312 sw.setState(SwitchState.ACTIVE.toString());
313 log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
314 return sw;
315 } else {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700316 return null;
317 }
318 }
319
320 private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) {
321 if (sw != null && state != null) {
322 sw.setState(state.toString());
Naoki Shiota987a5722013-10-23 11:59:36 -0700323 log.info("SwitchStorage:setSwitchStateImpl dpid:{} updated {}",
324 sw.getDPID(), state.toString());
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700325 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800326 }
Teruef33dc32013-06-20 09:54:37 -0700327
Naoki Shiota987a5722013-10-23 11:59:36 -0700328 private void deleteSwitchImpl(ISwitchObject sw) {
329 if (sw != null) {
330 op.removeSwitch(sw);
331 log.info("SwitchStorage:DeleteSwitchImpl dpid:{} done",
332 sw.getDPID());
333 }
334 }
335
336 private IPortObject addPortImpl(ISwitchObject sw, short portNum) {
337 IPortObject p = op.newPort(sw.getDPID(), portNum);
338 p.setState("ACTIVE");
339 sw.addPort(p);
340 log.info("SwitchStorage:addPortImpl dpid:{} port:{} done",
341 sw.getDPID(), portNum);
342
343 return p;
344 }
345
346 private void setPortStateImpl(IPortObject port, Integer state, String desc) {
347 if (port != null) {
348 if (state != null) {
349 port.setPortState(state);
350 }
351 if (desc != null) {
352 port.setDesc(desc);
353 }
354
355 log.info("SwitchStorage:setPortStateImpl port:{} state:{} desc:{} done",
356 new Object[] {port.getPortId(), state, desc});
357 }
358 }
359
360 private void deletePortImpl(IPortObject port) {
361 if (port != null) {
362 op.removePort(port);
363 log.info("SwitchStorage:deletePortImpl port:{} done",
364 port.getPortId());
365 }
366 }
Teru4fd58642013-06-21 07:54:49 -0700367}