blob: a068586fff22cb9febc38087decae1e600a40203 [file] [log] [blame]
Pankaj Berde3200ea02013-01-04 15:48:36 -08001package net.floodlightcontroller.core.internal;
2
3import java.util.Collection;
Pankaj Berde3200ea02013-01-04 15:48:36 -08004
Pankaj Berde15193092013-03-21 17:30:14 -07005import net.floodlightcontroller.core.INetMapTopologyObjects.IPortObject;
6import net.floodlightcontroller.core.INetMapTopologyObjects.ISwitchObject;
7import net.floodlightcontroller.core.ISwitchStorage;
8import net.onrc.onos.util.GraphDBConnection;
9import net.onrc.onos.util.GraphDBConnection.Transaction;
Pankaj Berdeafb20532013-01-08 15:05:24 -080010
Pankaj Berde3200ea02013-01-04 15:48:36 -080011import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -080012import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
13import org.openflow.protocol.OFPhysicalPort.OFPortState;
Pankaj Berde15193092013-03-21 17:30:14 -070014import org.slf4j.Logger;
15import org.slf4j.LoggerFactory;
Pankaj Berde3200ea02013-01-04 15:48:36 -080016
17public class SwitchStorageImpl implements ISwitchStorage {
Pankaj Berde15193092013-03-21 17:30:14 -070018 public GraphDBConnection conn;
Pankaj Berdeafb20532013-01-08 15:05:24 -080019 protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Pankaj Berde3200ea02013-01-04 15:48:36 -080020
21 @Override
22 public void update(String dpid, SwitchState state, DM_OPERATION op) {
23 // TODO Auto-generated method stub
Pankaj Berde28cc61c2013-01-08 18:19:33 -080024 log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state);
Pankaj Berde3200ea02013-01-04 15:48:36 -080025 switch(op) {
26
27 case UPDATE:
28 case INSERT:
29 case CREATE:
30 addSwitch(dpid);
31 if (state != SwitchState.ACTIVE) {
32 setStatus(dpid, state);
33 }
34 break;
35 case DELETE:
36 deleteSwitch(dpid);
37 break;
38 default:
39 }
40 }
41
42 private void setStatus(String dpid, SwitchState state) {
Pankaj Berde15193092013-03-21 17:30:14 -070043 ISwitchObject sw = conn.utils().searchSwitch(conn, dpid);
44 if (sw != null) {
45 sw.setState(state.toString());
46 conn.endTx(Transaction.COMMIT);
47 log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state);
48 } else {
49 conn.endTx(Transaction.ROLLBACK);
Pankaj Berde5024ec12013-01-31 17:07:29 -080050 log.info("SwitchStorage:setStatus dpid:{} state: {} failed", dpid, state);
Pankaj Berde3200ea02013-01-04 15:48:36 -080051 }
Pankaj Berde3200ea02013-01-04 15:48:36 -080052 }
53
54 @Override
55 public void addPort(String dpid, OFPhysicalPort port) {
56 // TODO Auto-generated method stub
Pankaj Berde6a4075d2013-01-22 16:42:54 -080057
58 boolean portDown = ((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
59 ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0);
60 if (portDown) {
61 deletePort(dpid, port.getPortNumber());
62 return;
63 }
Pankaj Berde15193092013-03-21 17:30:14 -070064
Pankaj Berde3200ea02013-01-04 15:48:36 -080065 try {
Pankaj Berde15193092013-03-21 17:30:14 -070066 ISwitchObject sw = conn.utils().searchSwitch(conn, dpid);
67
68 if (sw != null) {
69 IPortObject p = conn.utils().searchPort(conn, dpid, port.getPortNumber());
Pankaj Berde0fc4e432013-01-12 09:47:22 -080070 log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber());
Pankaj Berde15193092013-03-21 17:30:14 -070071 if (p != null) {
Pankaj Berde0a31dab2013-01-15 16:06:36 -080072 log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
Pankaj Berde0fc4e432013-01-12 09:47:22 -080073 } else {
Pankaj Berde15193092013-03-21 17:30:14 -070074 p = conn.utils().newPort(conn);
75
76 p.setType("port");
77 p.setNumber(port.getPortNumber());
78 p.setState("ACTIVE");
79 p.setPortState(port.getState());
80 p.setDesc(port.getName());
81 sw.addPort(p);
82 conn.endTx(Transaction.COMMIT);
83
Pankaj Berde0fc4e432013-01-12 09:47:22 -080084 }
Pankaj Berde3200ea02013-01-04 15:48:36 -080085 }
Pankaj Berde15193092013-03-21 17:30:14 -070086 } catch (Exception e) {
Pankaj Berde3200ea02013-01-04 15:48:36 -080087 // TODO: handle exceptions
Pankaj Berde15193092013-03-21 17:30:14 -070088 conn.endTx(Transaction.ROLLBACK);
Pankaj Berde0a31dab2013-01-15 16:06:36 -080089 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber());
Pankaj Berde3200ea02013-01-04 15:48:36 -080090 }
91
92 }
93
94 @Override
95 public Collection<OFPhysicalPort> getPorts(long dpid) {
96 // TODO Auto-generated method stub
97 return null;
98 }
99
100 @Override
101 public OFPhysicalPort getPort(String dpid, short portnum) {
102 // TODO Auto-generated method stub
103 return null;
104 }
105
106 @Override
107 public OFPhysicalPort getPort(String dpid, String portName) {
108 // TODO Auto-generated method stub
109 return null;
110 }
111
112 @Override
113 public void addSwitch(String dpid) {
114
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800115 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
Pankaj Berdeafb20532013-01-08 15:05:24 -0800116
Pankaj Berde3200ea02013-01-04 15:48:36 -0800117 try {
Pankaj Berde15193092013-03-21 17:30:14 -0700118 ISwitchObject sw = conn.utils().searchSwitch(conn, dpid);
119 if (sw != null) {
Pankaj Berde3200ea02013-01-04 15:48:36 -0800120 /*
121 * Do nothing or throw exception?
122 */
Pankaj Berde1ccc7fe2013-01-22 17:31:06 -0800123
124 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
Pankaj Berde15193092013-03-21 17:30:14 -0700125 sw.setState(SwitchState.ACTIVE.toString());
126 conn.endTx(Transaction.COMMIT);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800127 } else {
Pankaj Berde15193092013-03-21 17:30:14 -0700128 sw = conn.utils().newSwitch(conn);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800129
Pankaj Berde15193092013-03-21 17:30:14 -0700130 sw.setType("switch");
131 sw.setDPID(dpid);
132 sw.setState(SwitchState.ACTIVE.toString());
133 conn.endTx(Transaction.COMMIT);
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800134 log.info("SwitchStorage:addSwitch dpid:{} added", dpid);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800135 }
Pankaj Berde15193092013-03-21 17:30:14 -0700136 } catch (Exception e) {
Pankaj Berde3200ea02013-01-04 15:48:36 -0800137 /*
Pankaj Berde15193092013-03-21 17:30:14 -0700138 * retry?
Pankaj Berde3200ea02013-01-04 15:48:36 -0800139 */
Pankaj Berde15193092013-03-21 17:30:14 -0700140 conn.endTx(Transaction.ROLLBACK);
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800141 log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800142 }
143
144
145 }
146
147 @Override
148 public void deleteSwitch(String dpid) {
149 // TODO Setting inactive but we need to eventually remove data
Pankaj Berde15193092013-03-21 17:30:14 -0700150
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800151 try {
Pankaj Berde15193092013-03-21 17:30:14 -0700152
153 ISwitchObject sw = conn.utils().searchSwitch(conn, dpid);
154 if (sw != null) {
155 conn.utils().removeSwitch(conn, sw);
156
157 conn.endTx(Transaction.COMMIT);
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800158 log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid);
159 }
Pankaj Berde15193092013-03-21 17:30:14 -0700160 } catch (Exception e) {
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800161 // TODO: handle exceptions
Pankaj Berde15193092013-03-21 17:30:14 -0700162 conn.endTx(Transaction.ROLLBACK);
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800163 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
164 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800165
166 }
167
168 @Override
169 public void deletePort(String dpid, short port) {
170 // TODO Auto-generated method stub
Pankaj Berde3200ea02013-01-04 15:48:36 -0800171 try {
Pankaj Berde15193092013-03-21 17:30:14 -0700172 ISwitchObject sw = conn.utils().searchSwitch(conn, dpid);
173
174 if (sw != null) {
175 IPortObject p = conn.utils().searchPort(conn, dpid, port);
176 if (p != null) {
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800177 log.info("SwitchStorage:deletePort dpid:{} port:{} found and deleted", dpid, port);
Pankaj Berde15193092013-03-21 17:30:14 -0700178 sw.removePort(p);
179 conn.utils().removePort(conn, p);
180 conn.endTx(Transaction.COMMIT);
Pankaj Berde8557a462013-01-07 08:59:31 -0800181 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800182 }
Pankaj Berde15193092013-03-21 17:30:14 -0700183 } catch (Exception e) {
Pankaj Berde3200ea02013-01-04 15:48:36 -0800184 // TODO: handle exceptions
Pankaj Berde15193092013-03-21 17:30:14 -0700185 conn.endTx(Transaction.ROLLBACK);
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800186 log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800187 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800188 }
189
190 @Override
191 public void deletePort(String dpid, String portName) {
192 // TODO Auto-generated method stub
193
194 }
195
Pankaj Berde5024ec12013-01-31 17:07:29 -0800196
Pankaj Berde3200ea02013-01-04 15:48:36 -0800197
198 @Override
199 public void init(String conf) {
Pankaj Berde67f88fb2013-01-15 17:16:01 -0800200
Pankaj Berde15193092013-03-21 17:30:14 -0700201 conn = GraphDBConnection.getInstance(conf);
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800202
Pankaj Berde3200ea02013-01-04 15:48:36 -0800203 }
204
Pankaj Berdeff421802013-01-29 20:28:52 -0800205
Pankaj Berdeff421802013-01-29 20:28:52 -0800206
Pankaj Berded18c7622013-02-04 10:28:35 -0800207 public void finalize() {
208 close();
209 }
210
211 @Override
212 public void close() {
Pankaj Berde15193092013-03-21 17:30:14 -0700213 conn.close();
Pankaj Berded18c7622013-02-04 10:28:35 -0800214 }
215
Pankaj Berded1259e82013-01-23 14:10:00 -0800216
Pankaj Berde3200ea02013-01-04 15:48:36 -0800217}