blob: b12ccfaf6b70f2c5ad07f99d10383607059c06e2 [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
HIGUCHI Yuta20514902013-06-12 11:24:16 -07003import net.onrc.onos.ofcontroller.core.ISwitchStorage;
4import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
5import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
Pankaj Berde15193092013-03-21 17:30:14 -07006import net.onrc.onos.util.GraphDBConnection;
Toshio Koide4f3d9eb2013-06-13 13:20:55 -07007import net.onrc.onos.util.GraphDBOperation;
Pankaj Berdeafb20532013-01-08 15:05:24 -08008
Pankaj Berde3200ea02013-01-04 15:48:36 -08009import org.openflow.protocol.OFPhysicalPort;
Pankaj Berde6a4075d2013-01-22 16:42:54 -080010import org.openflow.protocol.OFPhysicalPort.OFPortConfig;
11import org.openflow.protocol.OFPhysicalPort.OFPortState;
Pankaj Berde15193092013-03-21 17:30:14 -070012import org.slf4j.Logger;
13import org.slf4j.LoggerFactory;
Pankaj Berde3200ea02013-01-04 15:48:36 -080014
Teruef33dc32013-06-20 09:54:37 -070015/**
16 * This is the class for storing the information of switches into CassandraDB
17 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080018public class SwitchStorageImpl implements ISwitchStorage {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -070019 protected GraphDBOperation op;
Pankaj Berdeafb20532013-01-08 15:05:24 -080020 protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Teruef33dc32013-06-20 09:54:37 -070021
22 /***
23 * Initialize function. Before you use this class, please call this method
24 * @param conf configuration file for Cassandra DB
25 */
Pankaj Berde3200ea02013-01-04 15:48:36 -080026 @Override
Teruef33dc32013-06-20 09:54:37 -070027 public void init(String conf) {
28 GraphDBConnection conn = GraphDBConnection.getInstance(conf);
29 op = new GraphDBOperation(conn);
30 }
Pankaj Berde3200ea02013-01-04 15:48:36 -080031
Teruef33dc32013-06-20 09:54:37 -070032 /***
33 * Finalize/close function. After you use this class, please call this method.
34 * It will close the DB connection.
35 */
36 public void finalize() {
37 close();
38 }
39
40 /***
41 * Finalize/close function. After you use this class, please call this method.
42 * It will close the DB connection. This is for Java gabage collection.
43 */
44 @Override
45 public void close() {
46 op.close();
47 }
48
49 private void setStatus(String dpid, SwitchState state) {
50 ISwitchObject sw = op.searchSwitch(dpid);
51
52 try {
53 if (sw != null) {
54 sw.setState(state.toString());
55 op.commit();
56 log.info("SwitchStorage:setStatus dpid:{} state: {} done", dpid, state);
57 }
58 } catch(Exception e) {
59 e.printStackTrace();
60 op.rollback();
61 log.info("SwitchStorage:setStatus dpid:{} state: {} failed: switch not found", dpid, state);
62 }
63 }
64
65 /***
66 * This function is for adding the switch into the DB.
67 * @param dpid The switch dpid you want to add into the DB.
68 */
69 @Override
70 public void addSwitch(String dpid) {
71
72 log.info("SwitchStorage:addSwitch(): dpid {} ", dpid);
73 try {
74 ISwitchObject sw = op.searchSwitch(dpid);
75 if (sw != null) {
76 //If existing the switch. set The SW state ACTIVE.
77 log.info("SwitchStorage:addSwitch dpid:{} already exists", dpid);
78 sw.setState(SwitchState.ACTIVE.toString());
79 op.commit();
80 } else {
81 sw = op.newSwitch(dpid);
82
83 if (sw != null) {
84 sw.setState(SwitchState.ACTIVE.toString());
85 op.commit();
86 log.info("SwitchStorage:addSwitch dpid:{} added", dpid);
87 } else {
88 log.error("switchStorage:addSwitch dpid:{} failed -> newSwitch failed", dpid);
89 }
90 }
91 } catch (Exception e) {
92 e.printStackTrace();
93 op.rollback();
94 log.info("SwitchStorage:addSwitch dpid:{} failed", dpid);
95 }
96 }
97
98 /***
99 * This function is for updating the switch into the DB.
100 * @param dpid The switch dpid you want to update from the DB
101 * @param state The state of the switch like ACTIVE, INACTIVE
102 * @param dmope The DM_OPERATION of the switch
103 */
104 @Override
105 public void update(String dpid, SwitchState state, DM_OPERATION dmope) {
106 log.info("SwitchStorage:update dpid:{} state: {} ", dpid, state);
107 switch(dmope) {
Pankaj Berde3200ea02013-01-04 15:48:36 -0800108 case UPDATE:
109 case INSERT:
110 case CREATE:
111 addSwitch(dpid);
112 if (state != SwitchState.ACTIVE) {
113 setStatus(dpid, state);
114 }
115 break;
116 case DELETE:
117 deleteSwitch(dpid);
118 break;
119 default:
120 }
121 }
122
Teruef33dc32013-06-20 09:54:37 -0700123 /***
124 * This function is for deleting the switch into the DB.
125 * @param dpid The switch dpid you want to delete from the DB.
126 */
127 @Override
128 public void deleteSwitch(String dpid) {
129 try {
130 ISwitchObject sw = op.searchSwitch(dpid);
131 if (sw != null) {
132 op.removeSwitch(sw);
133 op.commit();
134 log.info("SwitchStorage:DeleteSwitch dpid:{} done", dpid);
135 }
136 } catch (Exception e) {
137 e.printStackTrace();
138 op.rollback();
139 log.error("SwitchStorage:deleteSwitch {} failed", dpid);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800140 }
Teruef33dc32013-06-20 09:54:37 -0700141
Pankaj Berde3200ea02013-01-04 15:48:36 -0800142 }
143
Teruef33dc32013-06-20 09:54:37 -0700144 /***
145 * This function is for adding the switch port into the DB.
146 * @param dpid The switch dpid that has the port.
147 * @param port The port you want to add the switch.
148 */
Pankaj Berde3200ea02013-01-04 15:48:36 -0800149 @Override
150 public void addPort(String dpid, OFPhysicalPort port) {
Pankaj Berde6a4075d2013-01-22 16:42:54 -0800151
Teruef33dc32013-06-20 09:54:37 -0700152 if(((OFPortConfig.OFPPC_PORT_DOWN.getValue() & port.getConfig()) > 0) ||
153 ((OFPortState.OFPPS_LINK_DOWN.getValue() & port.getState()) > 0)) {
154 deletePort(dpid, port.getPortNumber());
155 return;
Pankaj Berde6a4075d2013-01-22 16:42:54 -0800156 }
Teruef33dc32013-06-20 09:54:37 -0700157
Pankaj Berde3200ea02013-01-04 15:48:36 -0800158 try {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700159 ISwitchObject sw = op.searchSwitch(dpid);
Pankaj Berde15193092013-03-21 17:30:14 -0700160
161 if (sw != null) {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700162 IPortObject p = op.searchPort(dpid, port.getPortNumber());
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800163 log.info("SwitchStorage:addPort dpid:{} port:{}", dpid, port.getPortNumber());
Pankaj Berde15193092013-03-21 17:30:14 -0700164 if (p != null) {
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800165 log.error("SwitchStorage:addPort dpid:{} port:{} exists", dpid, port.getPortNumber());
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800166 } else {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700167 p = op.newPort(port.getPortNumber());
Pankaj Berde15193092013-03-21 17:30:14 -0700168 p.setState("ACTIVE");
169 p.setPortState(port.getState());
170 p.setDesc(port.getName());
171 sw.addPort(p);
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700172 op.commit();
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800173 }
Pankaj Berde62016142013-04-09 15:35:50 -0700174 } else {
175 log.error("SwitchStorage:addPort dpid:{} port:{} : failed switch does not exist", dpid, port.getPortNumber());
Pankaj Berde3200ea02013-01-04 15:48:36 -0800176 }
Pankaj Berde15193092013-03-21 17:30:14 -0700177 } catch (Exception e) {
Pankaj Berde62016142013-04-09 15:35:50 -0700178 e.printStackTrace();
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700179 op.rollback();
Pankaj Berde0a31dab2013-01-15 16:06:36 -0800180 log.error("SwitchStorage:addPort dpid:{} port:{} failed", dpid, port.getPortNumber());
Pankaj Berde3200ea02013-01-04 15:48:36 -0800181 }
182
183 }
Teruef33dc32013-06-20 09:54:37 -0700184
185 /***
186 * This function is for deleting the switch port from the DB.
187 * @param dpid The switch dpid that has the port.
188 * @param port The port you want to delete the switch.
189 */
Pankaj Berde3200ea02013-01-04 15:48:36 -0800190 @Override
191 public void deletePort(String dpid, short port) {
Pankaj Berde3200ea02013-01-04 15:48:36 -0800192 try {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700193 ISwitchObject sw = op.searchSwitch(dpid);
Pankaj Berde15193092013-03-21 17:30:14 -0700194
195 if (sw != null) {
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700196 IPortObject p = op.searchPort(dpid, port);
Pankaj Berde15193092013-03-21 17:30:14 -0700197 if (p != null) {
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800198 log.info("SwitchStorage:deletePort dpid:{} port:{} found and deleted", dpid, port);
Pankaj Berde15193092013-03-21 17:30:14 -0700199 sw.removePort(p);
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700200 op.removePort(p);
201 op.commit();
Pankaj Berde8557a462013-01-07 08:59:31 -0800202 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800203 }
Pankaj Berde15193092013-03-21 17:30:14 -0700204 } catch (Exception e) {
Pankaj Berde2239f0d2013-04-04 09:42:43 -0700205 e.printStackTrace();
Toshio Koide4f3d9eb2013-06-13 13:20:55 -0700206 op.rollback();
Pankaj Berde28cc61c2013-01-08 18:19:33 -0800207 log.info("SwitchStorage:deletePort dpid:{} port:{} failed", dpid, port);
Pankaj Berde3200ea02013-01-04 15:48:36 -0800208 }
Pankaj Berde3200ea02013-01-04 15:48:36 -0800209 }
Teru4fd58642013-06-21 07:54:49 -0700210}