blob: e1844233d161ed7e2e174f726e5df2301db0d6c4 [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;
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
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 Berdee7c21522013-08-01 16:52:29 -0700170 log.error("SwitchStorage:addPort dpid:{} port:{} exists setting as ACTIVE", dpid, port.getPortNumber());
171 p.setState("ACTIVE");
172 p.setPortState(port.getState());
173 p.setDesc(port.getName());
174 op.commit();
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800175 } else {
Pankaj Berdebbd38612013-06-22 05:59:12 -0700176 p = op.newPort(dpid, port.getPortNumber());
Pankaj Berde15193092013-03-21 17:30:14 -0700177 p.setState("ACTIVE");
178 p.setPortState(port.getState());
179 p.setDesc(port.getName());
180 sw.addPort(p);
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700181 op.commit();
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800182 }
Pankaj Berde62016142013-04-09 15:35:50 -0700183 } else {
184 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, port.getPortNumber());
Pankaj Berde3200ea02013-01-04 15:48:36 -0800185 }
Pankaj Berde15193092013-03-21 17:30:14 -0700186 } catch (Exception e) {
Pankaj Berde62016142013-04-09 15:35:50 -0700187 e.printStackTrace();
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700188 op.rollback();
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800189 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber());
Pankaj Berde3200ea02013-01-04 15:48:36 -0800190 }
191
192 }
Teruef33dc32013-06-20 09:54:37 -0700193
194 /***
195 * This function is for deleting the switch port from the DB.
196 * @param dpid The switch dpid that has the port.
197 * @param port The port you want to delete the switch.
198 */
Pankaj Berde3200ea02013-01-04 15:48:36 -0800199 @Override
200 public void deletePort(String dpid, short port) {
Pankaj Berde3200ea02013-01-04 15:48:36 -0800201 try {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700202 ISwitchObject sw = op.searchSwitch(dpid);
Pankaj Berde15193092013-03-21 17:30:14 -0700203
204 if (sw != null) {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700205 IPortObject p = op.searchPort(dpid, port);
Pankaj Berde15193092013-03-21 17:30:14 -0700206 if (p != null) {
Pankaj Berdee7c21522013-08-01 16:52:29 -0700207 log.info("SwitchStorage:deletePort dpid:{} port:{} found and set INACTIVE", dpid, port);
208 p.setState("INACTIVE");
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700209 op.commit();
Pankaj Berde8557a462013-01-07 08:59:31 -0800210 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800211 }
Pankaj Berde15193092013-03-21 17:30:14 -0700212 } catch (Exception e) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700213 e.printStackTrace();
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700214 op.rollback();
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800215 log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800216 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800217 }
Pankaj Berdebbd38612013-06-22 05:59:12 -0700218
219 @Override
220 public void addSwitch(IOFSwitch sw) {
221 // TODO Auto-generated method stub
222 String dpid = sw.getStringId();
223 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
224 try {
225 ISwitchObject switchObject = newSwitch(dpid);
226 for (OFPhysicalPort port: sw.getPorts()) {
227 IPortObject p = op.searchPort(dpid, port.getPortNumber());
228 if (p != null) {
229 log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
Jonathan Hart55a6aff2013-09-22 17:23:37 +1200230 p.setState("ACTIVE");
231 p.setPortState(port.getState());
232 p.setDesc(port.getName());
Pankaj Berdebbd38612013-06-22 05:59:12 -0700233 } else {
234 p = op.newPort(dpid, port.getPortNumber());
235 p.setState("ACTIVE");
236 p.setPortState(port.getState());
237 p.setDesc(port.getName());
238 switchObject.addPort(p);
239 }
240 }
241 op.commit();
242 } catch (Exception e) {
243 e.printStackTrace();
244 op.rollback();
245 log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
246 }
247
248 }
Teru4fd58642013-06-21 07:54:49 -0700249}