blob: d9f24e0663d88b53c9f2fd141211137b76102ca1 [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
Naoki Shiotab2d17e82013-10-18 18:08:16 -07003import java.util.ArrayList;
4import java.util.List;
5
Pankaj Berdebbd38612013-06-22 05:59:12 -07006import net.floodlightcontroller.core.IOFSwitch;
Pankaj Berde38646d62013-06-21 11:34:04 -07007import net.onrc.onos.graph.GraphDBConnection;
8import net.onrc.onos.graph.GraphDBOperation;
HIGUCHI Yuta20514902013-06-12 11:24:16 -07009import net.onrc.onos.ofcontroller.core.ISwitchStorage;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berdeafb20532013-01-08 15:05:24 -080012
Pankaj Berde3200ea02013-01-04 15:48:36 -080013import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -080014import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
15import org.openflow.protocol.OFPhysicalPort.OFPortState;
Pankaj Berde15193092013-03-21 17:30:14 -070016import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
Pankaj Berde3200ea02013-01-04 15:48:36 -080018
Teruef33dc32013-06-20 09:54:37 -070019/**
20 * This is the class for storing the information of switches into CassandraDB
21 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080022public class SwitchStorageImpl implements ISwitchStorage {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -070023 protected GraphDBOperation op;
Pankaj Berdeafb20532013-01-08 15:05:24 -080024 protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Teruef33dc32013-06-20 09:54:37 -070025
26 /***
27 * Initialize function. Before you use this class, please call this method
28 * @param conf configuration file for Cassandra DB
29 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080030 @Override
Teruef33dc32013-06-20 09:54:37 -070031 public void init(String conf) {
32 GraphDBConnection conn = GraphDBConnection.getInstance(conf);
33 op = new GraphDBOperation(conn);
34 }
Pankaj Berde3200ea02013-01-04 15:48:36 -080035
Teruef33dc32013-06-20 09:54:37 -070036 /***
37 * Finalize/close function. After you use this class, please call this method.
38 * It will close the DB connection.
39 */
40 public void finalize() {
41 close();
42 }
43
44 /***
45 * Finalize/close function. After you use this class, please call this method.
Pavlin Radoslavovef0cb002013-06-21 14:55:23 -070046 * It will close the DB connection. This is for Java garbage collection.
Teruef33dc32013-06-20 09:54:37 -070047 */
48 @Override
49 public void close() {
50 op.close();
51 }
52
53 private void setStatus(String dpid, SwitchState state) {
54 ISwitchObject sw = op.searchSwitch(dpid);
55
56 try {
57 if (sw != null) {
58 sw.setState(state.toString());
59 op.commit();
60 log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070061 // TODO publish UPDATE_SWITCH event here
Teruef33dc32013-06-20 09:54:37 -070062 }
63 } catch(Exception e) {
64 e.printStackTrace();
65 op.rollback();
66 log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state);
67 }
68 }
69
70 /***
Teruef33dc32013-06-20 09:54:37 -070071 * This function is for updating the switch into the DB.
72 * @param dpid The switch dpid you want to update from the DB
73 * @param state The state of the switch like ACTIVE, INACTIVE
74 * @param dmope The DM_OPERATION of the switch
75 */
76 @Override
77 public void update(String dpid, SwitchState state, DM_OPERATION dmope) {
78 log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070079 switch(dmope) {
80 case UPDATE:
81 case INSERT:
82 case CREATE:
83 addSwitch(dpid);
84 if (state != SwitchState.ACTIVE) {
85 setStatus(dpid, state);
86 }
87 break;
88 case DELETE:
89 deleteSwitch(dpid);
90 break;
91 default:
92 }
93 }
94
95 /***
96 * This function is for adding the switch port into the DB.
97 * @param dpid The switch dpid that has the port.
98 * @param port The port you want to add the switch.
99 */
100 @Override
101 public void addPort(String dpid, OFPhysicalPort port) {
102
103 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
104 ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)) {
105 deletePort(dpid, port.getPortNumber());
106 return;
107 }
108
109 try {
110 ISwitchObject sw = op.searchSwitch(dpid);
111
112 if (sw != null) {
113 IPortObject p = op.searchPort(dpid, port.getPortNumber());
114 log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber());
115 if (p != null) {
116 log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, port.getPortNumber());
117 p.setState("ACTIVE");
118 p.setPortState(port.getState());
119 p.setDesc(port.getName());
120 op.commit();
121
122 // TODO publish UPDATE_PORT event here
123 } else {
124 p = op.newPort(dpid, port.getPortNumber());
125 p.setState("ACTIVE");
126 p.setPortState(port.getState());
127 p.setDesc(port.getName());
128 sw.addPort(p);
129 op.commit();
130
131 // TODO publish ADD_PORT event here
132 }
133 } else {
134 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, port.getPortNumber());
135 }
136 } catch (Exception e) {
137 e.printStackTrace();
138 op.rollback();
139 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber());
140 }
141
142 }
143
144 /***
145 * This function is for deleting the switch port from the DB.
146 * @param dpid The switch dpid that has the port.
147 * @param port The port you want to delete the switch.
148 */
149 @Override
150 public void deletePort(String dpid, short port) {
151 try {
152 ISwitchObject sw = op.searchSwitch(dpid);
153
154 if (sw != null) {
155 IPortObject p = op.searchPort(dpid, port);
156 if (p != null) {
157 log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
158 p.setState("INACTIVE");
159 op.commit();
160 // TODO publish DELETE_PORT event here
161 }
162 }
163 } catch (Exception e) {
164 e.printStackTrace();
165 op.rollback();
166 log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
167 }
168 }
169
170 @Override
171 public void addSwitch(IOFSwitch sw) {
172 String dpid = sw.getStringId();
173 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
174
175 ISwitchObject curr = op.searchSwitch(dpid);
176 if (curr != null) {
177 //If existing the switch. set The SW state ACTIVE.
178 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
179 setSwitchStateImpl(curr, SwitchState.ACTIVE);
180 } else {
181 curr = addSwitchImpl(dpid);
182 }
183
184 try {
185 List<IPortObject> added_ports = new ArrayList<IPortObject>();
186 List<IPortObject> updated_ports = new ArrayList<IPortObject>();
187 for (OFPhysicalPort port: sw.getPorts()) {
188 IPortObject p = op.searchPort(dpid, port.getPortNumber());
189 if (p != null) {
190 log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
191 p.setState("ACTIVE");
192 p.setPortState(port.getState());
193 p.setDesc(port.getName());
194
195 added_ports.add(p);
196 } else {
197 p = op.newPort(dpid, port.getPortNumber());
198 p.setState("ACTIVE");
199 p.setPortState(port.getState());
200 p.setDesc(port.getName());
201 curr.addPort(p);
202
203 updated_ports.add(p);
204 }
205 }
206
207 op.commit();
208
209 // TODO publish ADD_PORT event here
210 // TODO publish UPDATE_PORT event here
211 } catch (Exception e) {
212 e.printStackTrace();
213 op.rollback();
214 log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
215 }
216 }
217
218 /***
219 * This function is for adding the switch into the DB.
220 * @param dpid The switch dpid you want to add into the DB.
221 */
222 @Override
223 public void addSwitch(String dpid) {
224 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
225
226 ISwitchObject sw = op.searchSwitch(dpid);
227 if (sw != null) {
228 //If existing the switch. set The SW state ACTIVE.
229 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
230 setSwitchStateImpl(sw, SwitchState.ACTIVE);
231 } else {
232 addSwitchImpl(dpid);
233 }
234 }
235
236 private ISwitchObject addSwitchImpl(String dpid) {
237 try {
238 ISwitchObject sw = op.newSwitch(dpid);
239 if (sw != null) {
240 sw.setState(SwitchState.ACTIVE.toString());
241 log.info("SwitchStorage:addSwitchImpl dpid:{} added", dpid);
242 } else {
243 log.error("switchStorage:addSwitchImpl dpid:{} failed", dpid);
244 throw new RuntimeException();
245 }
246
247 op.commit();
248
249 // TODO publish ADD_SWITCH event here
250 return sw;
251 } catch (Exception e) {
252 e.printStackTrace();
253 op.rollback();
254 log.info("SwitchStorage:addSwitchImpl dpid:{} failed", dpid);
255 return null;
256 }
257 }
258
259 private void setSwitchStateImpl(ISwitchObject sw, SwitchState state) {
260 if (sw != null && state != null) {
261 sw.setState(state.toString());
262 op.commit();
263
264 // TODO publish UPDATE_SWITCH event here
265 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800266 }
267
Teruef33dc32013-06-20 09:54:37 -0700268 /***
269 * This function is for deleting the switch into the DB.
270 * @param dpid The switch dpid you want to delete from the DB.
271 */
272 @Override
273 public void deleteSwitch(String dpid) {
274 try {
275 ISwitchObject sw = op.searchSwitch(dpid);
276 if (sw != null) {
277 op.removeSwitch(sw);
278 op.commit();
279 log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid);
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700280
281 // TODO publish DELETE_SWITCH event here
Teruef33dc32013-06-20 09:54:37 -0700282 }
283 } catch (Exception e) {
284 e.printStackTrace();
285 op.rollback();
286 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800287 }
Teruef33dc32013-06-20 09:54:37 -0700288
Pankaj Berde3200ea02013-01-04 15:48:36 -0800289 }
Teruef33dc32013-06-20 09:54:37 -0700290
Teru4fd58642013-06-21 07:54:49 -0700291}