blob: 126efb3f7b26af2e3eb95170713f3ee3b8db4e4c [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/**
17 * This is the class for storing the information of switches into CassandraDB
18 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080019public class SwitchStorageImpl implements ISwitchStorage {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -070020 protected GraphDBOperation op;
Pankaj Berdeafb20532013-01-08 15:05:24 -080021 protected 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
50 private void setStatus(String dpid, SwitchState state) {
51 ISwitchObject sw = op.searchSwitch(dpid);
52
53 try {
54 if (sw != null) {
55 sw.setState(state.toString());
56 op.commit();
57 log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state);
58 }
59 } catch(Exception e) {
60 e.printStackTrace();
61 op.rollback();
62 log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state);
63 }
64 }
65
66 /***
67 * This function is for adding the switch into the DB.
68 * @param dpid The switch dpid you want to add into the DB.
69 */
70 @Override
71 public void addSwitch(String dpid) {
72
73 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
74 try {
HIGUCHI Yutafabb0032013-06-28 11:31:33 -070075 ISwitchObject sw = newSwitch(dpid);
76 if ( sw == null ) throw new RuntimeException();
Pankaj Berdebbd38612013-06-22 05:59:12 -070077 op.commit();
Teruef33dc32013-06-20 09:54:37 -070078 } catch (Exception e) {
79 e.printStackTrace();
80 op.rollback();
81 log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
82 }
83 }
84
Pankaj Berdebbd38612013-06-22 05:59:12 -070085 private ISwitchObject newSwitch(String dpid) {
86 ISwitchObject sw = op.searchSwitch(dpid);
87 if (sw != null) {
88 //If existing the switch. set The SW state ACTIVE.
89 log.info("SwitchStorage:newSwitch dpid:{} already exists", dpid);
90 sw.setState(SwitchState.ACTIVE.toString());
91 } else {
92 sw = op.newSwitch(dpid);
93 if (sw != null) {
94 sw.setState(SwitchState.ACTIVE.toString());
95 log.info("SwitchStorage:newSwitch dpid:{} added", dpid);
96 } else {
97 log.error("switchStorage:newSwitch dpid:{} failed -> newSwitch failed", dpid);
98 }
99 }
100 return sw;
101 }
102
Teruef33dc32013-06-20 09:54:37 -0700103 /***
104 * This function is for updating the switch into the DB.
105 * @param dpid The switch dpid you want to update from the DB
106 * @param state The state of the switch like ACTIVE, INACTIVE
107 * @param dmope The DM_OPERATION of the switch
108 */
109 @Override
110 public void update(String dpid, SwitchState state, DM_OPERATION dmope) {
111 log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state);
112 switch(dmope) {
Pankaj Berde3200ea02013-01-04 15:48:36 -0800113 case UPDATE:
114 case INSERT:
115 case CREATE:
116 addSwitch(dpid);
117 if (state != SwitchState.ACTIVE) {
118 setStatus(dpid, state);
119 }
120 break;
121 case DELETE:
122 deleteSwitch(dpid);
123 break;
124 default:
125 }
126 }
127
Teruef33dc32013-06-20 09:54:37 -0700128 /***
129 * This function is for deleting the switch into the DB.
130 * @param dpid The switch dpid you want to delete from the DB.
131 */
132 @Override
133 public void deleteSwitch(String dpid) {
134 try {
135 ISwitchObject sw = op.searchSwitch(dpid);
136 if (sw != null) {
137 op.removeSwitch(sw);
138 op.commit();
139 log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid);
140 }
141 } catch (Exception e) {
142 e.printStackTrace();
143 op.rollback();
144 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800145 }
Teruef33dc32013-06-20 09:54:37 -0700146
Pankaj Berde3200ea02013-01-04 15:48:36 -0800147 }
148
Teruef33dc32013-06-20 09:54:37 -0700149 /***
150 * This function is for adding the switch port into the DB.
151 * @param dpid The switch dpid that has the port.
152 * @param port The port you want to add the switch.
153 */
Pankaj Berde3200ea02013-01-04 15:48:36 -0800154 @Override
155 public void addPort(String dpid, OFPhysicalPort port) {
Pankaj Berde6a4075d2013-01-22 16:42:54 -0800156
Teruef33dc32013-06-20 09:54:37 -0700157 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
158 ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)) {
159 deletePort(dpid, port.getPortNumber());
160 return;
Pankaj Berde6a4075d2013-01-22 16:42:54 -0800161 }
Teruef33dc32013-06-20 09:54:37 -0700162
Pankaj Berde3200ea02013-01-04 15:48:36 -0800163 try {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700164 ISwitchObject sw = op.searchSwitch(dpid);
Pankaj Berde15193092013-03-21 17:30:14 -0700165
166 if (sw != null) {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700167 IPortObject p = op.searchPort(dpid, port.getPortNumber());
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800168 log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber());
Pankaj Berde15193092013-03-21 17:30:14 -0700169 if (p != null) {
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800170 log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800171 } else {
Pankaj Berdebbd38612013-06-22 05:59:12 -0700172 p = op.newPort(dpid, port.getPortNumber());
Pankaj Berde15193092013-03-21 17:30:14 -0700173 p.setState("ACTIVE");
174 p.setPortState(port.getState());
175 p.setDesc(port.getName());
176 sw.addPort(p);
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700177 op.commit();
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800178 }
Pankaj Berde62016142013-04-09 15:35:50 -0700179 } else {
180 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, port.getPortNumber());
Pankaj Berde3200ea02013-01-04 15:48:36 -0800181 }
Pankaj Berde15193092013-03-21 17:30:14 -0700182 } catch (Exception e) {
Pankaj Berde62016142013-04-09 15:35:50 -0700183 e.printStackTrace();
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700184 op.rollback();
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800185 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber());
Pankaj Berde3200ea02013-01-04 15:48:36 -0800186 }
187
188 }
Teruef33dc32013-06-20 09:54:37 -0700189
190 /***
191 * This function is for deleting the switch port from the DB.
192 * @param dpid The switch dpid that has the port.
193 * @param port The port you want to delete the switch.
194 */
Pankaj Berde3200ea02013-01-04 15:48:36 -0800195 @Override
196 public void deletePort(String dpid, short port) {
Pankaj Berde3200ea02013-01-04 15:48:36 -0800197 try {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700198 ISwitchObject sw = op.searchSwitch(dpid);
Pankaj Berde15193092013-03-21 17:30:14 -0700199
200 if (sw != null) {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700201 IPortObject p = op.searchPort(dpid, port);
Pankaj Berde15193092013-03-21 17:30:14 -0700202 if (p != null) {
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800203 log.info("SwitchStorage:deletePort dpid:{} port:{} found and deleted", dpid, port);
Pankaj Berde15193092013-03-21 17:30:14 -0700204 sw.removePort(p);
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700205 op.removePort(p);
206 op.commit();
Pankaj Berde8557a462013-01-07 08:59:31 -0800207 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800208 }
Pankaj Berde15193092013-03-21 17:30:14 -0700209 } catch (Exception e) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700210 e.printStackTrace();
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700211 op.rollback();
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800212 log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800213 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800214 }
Pankaj Berdebbd38612013-06-22 05:59:12 -0700215
216 @Override
217 public void addSwitch(IOFSwitch sw) {
218 // TODO Auto-generated method stub
219 String dpid = sw.getStringId();
220 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
221 try {
222 ISwitchObject switchObject = newSwitch(dpid);
223 for (OFPhysicalPort port: sw.getPorts()) {
224 IPortObject p = op.searchPort(dpid, port.getPortNumber());
225 if (p != null) {
226 log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
227 } else {
228 p = op.newPort(dpid, port.getPortNumber());
229 p.setState("ACTIVE");
230 p.setPortState(port.getState());
231 p.setDesc(port.getName());
232 switchObject.addPort(p);
233 }
234 }
235 op.commit();
236 } catch (Exception e) {
237 e.printStackTrace();
238 op.rollback();
239 log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
240 }
241
242 }
Teru4fd58642013-06-21 07:54:49 -0700243}