blob: 5b3ed749a3b91a73b03e09ec6694f4bb13561531 [file] [log] [blame]
HIGUCHI Yuta2d011582013-06-15 01:47:11 -07001package net.onrc.onos.ofcontroller.core.internal;
Pankaj Berdeb6031342013-02-19 18:51:51 -08002
Teru87cd2da2013-06-15 20:33:08 -07003import java.util.ArrayList;
Pankaj Berdeb6031342013-02-19 18:51:51 -08004import java.util.List;
Pankaj Berdeb6031342013-02-19 18:51:51 -08005import org.openflow.util.HexString;
6import org.slf4j.Logger;
7import org.slf4j.LoggerFactory;
8
9import com.google.common.collect.Lists;
10import com.thinkaurelius.titan.core.TitanException;
Pankaj Berdeb6031342013-02-19 18:51:51 -080011import net.floodlightcontroller.devicemanager.IDevice;
Pankaj Berdeb6031342013-02-19 18:51:51 -080012import net.floodlightcontroller.devicemanager.SwitchPort;
Teru87cd2da2013-06-15 20:33:08 -070013import net.floodlightcontroller.packet.IPv4;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -070014import net.onrc.onos.ofcontroller.core.IDeviceStorage;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070015import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
16import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
Pankaj Berdeda809572013-02-22 15:31:20 -080017import net.onrc.onos.util.GraphDBConnection;
Teru87cd2da2013-06-15 20:33:08 -070018import net.onrc.onos.util.GraphDBConnection.Transaction;
Toshio Koide4f629f32013-06-13 13:39:38 -070019import net.onrc.onos.util.GraphDBOperation;
Pankaj Berdeb6031342013-02-19 18:51:51 -080020
Teru87cd2da2013-06-15 20:33:08 -070021/**
22 * This is the class for storing the information of devices into CassandraDB
23 * @author Pankaj
24 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080025public class DeviceStorageImpl implements IDeviceStorage {
26
Teru87cd2da2013-06-15 20:33:08 -070027 private GraphDBConnection conn;
28 private GraphDBOperation ope;
Pankaj Berdeb6031342013-02-19 18:51:51 -080029 protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080030
Teru87cd2da2013-06-15 20:33:08 -070031 /***
32 * Initialize function. Before you use this class, please call this method
33 * @param conf configuration file for Cassandra DB
34 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080035 @Override
36 public void init(String conf) {
Teru87cd2da2013-06-15 20:33:08 -070037 try{
38 if((conn = GraphDBConnection.getInstance(conf)) != null)
39 {
40 ope = new GraphDBOperation(conn);
41 }
42 } catch(Exception e) {
43 log.error(e.getMessage());
44 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080045 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080046
Teru87cd2da2013-06-15 20:33:08 -070047 /***
48 * Finalize/close function. After you use this class, please call this method.
49 * It will close the DB connection.
50 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080051 @Override
52 public void close() {
Teru87cd2da2013-06-15 20:33:08 -070053 conn.close();
54 }
55
56 /***
57 * Finalize/close function. After you use this class, please call this method.
58 * It will close the DB connection. This is for Java gabage collection.
59 */
60 @Override
61 public void finalize() {
62 close();
Pankaj Berdeb6031342013-02-19 18:51:51 -080063 }
64
Teru87cd2da2013-06-15 20:33:08 -070065 /***
66 * This function is for adding the device into the DB.
67 * @param device The device you want to add into the DB.
68 * @return IDeviceObject which was added in the DB.
69 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080070 @Override
71 public IDeviceObject addDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -080072 IDeviceObject obj = null;
Pankaj Berdeb6031342013-02-19 18:51:51 -080073 try {
Teru87cd2da2013-06-15 20:33:08 -070074 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
75 log.debug("Adding device {}: found existing device",device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080076 } else {
Teru87cd2da2013-06-15 20:33:08 -070077 obj = ope.newDevice();
Pankaj Berdeda809572013-02-22 15:31:20 -080078 log.debug("Adding device {}: creating new device",device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080079 }
Teru87cd2da2013-06-15 20:33:08 -070080 changeDeviceAttachments(device, obj);
81
82 String multiIntString = "";
83 for(Integer intValue : device.getIPv4Addresses()) {
84 if (multiIntString == null || multiIntString.isEmpty()){
85 multiIntString = IPv4.fromIPv4Address(intValue);
86 multiIntString = "[" + IPv4.fromIPv4Address(intValue);
87 }else{
88 multiIntString += "," + IPv4.fromIPv4Address(intValue);
89 }
90 }
91
92 if(multiIntString.toString() != null && !multiIntString.toString().isEmpty()){
93 obj.setIPAddress(multiIntString + "]");
94 }
95
96 obj.setMACAddress(device.getMACAddressString());
97 obj.setType("device");
98 obj.setState("ACTIVE");
99 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800100
Teru87cd2da2013-06-15 20:33:08 -0700101 log.debug("Adding device {}",device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800102 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -0700103 ope.rollback();
Pankaj Berdeb6031342013-02-19 18:51:51 -0800104 log.error(":addDevice mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700105 obj = null;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800106 }
Teru87cd2da2013-06-15 20:33:08 -0700107 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800108 }
109
Teru87cd2da2013-06-15 20:33:08 -0700110 /***
111 * This function is for updating the Device properties.
112 * @param device The device you want to add into the DB.
113 * @return IDeviceObject which was added in the DB.
114 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800115 @Override
116 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800117 return addDevice(device);
118 }
119
Teru87cd2da2013-06-15 20:33:08 -0700120 /***
121 * This function is for removing the Device from the DB.
122 * @param device The device you want to delete from the DB.
123 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800124 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800125 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800126 IDeviceObject dev;
127 try {
Teru87cd2da2013-06-15 20:33:08 -0700128 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
129 ope.removeDevice(dev);
130 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800131 log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
132 }
133 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -0700134 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800135 log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
136 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800137 }
138
Teru87cd2da2013-06-15 20:33:08 -0700139 /***
140 * This function is for getting the Device from the DB by Mac address of the device.
141 * @param mac The device mac address you want to get from the DB.
142 * @return IDeviceObject you want to get.
143 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800144 @Override
145 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700146 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800147 }
148
Teru87cd2da2013-06-15 20:33:08 -0700149 /***
150 * This function is for getting the Device from the DB by IP address of the device.
151 * @param ip The device ip address you want to get from the DB.
152 * @return IDeviceObject you want to get.
153 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800154 @Override
155 public IDeviceObject getDeviceByIP(String ip) {
Teru87cd2da2013-06-15 20:33:08 -0700156 try
157 {
158 for(IDeviceObject dev : ope.getDevices()){
159 String ips;
160 if((ips = dev.getIPAddress()) != null){
161 String nw_addr_wob = ips.replace("[", "").replace("]", "");
162 ArrayList<String> iplists = Lists.newArrayList(nw_addr_wob.split(","));
163 if(iplists.contains(ip)){
164 return dev;
165 }
166 }
167 }
168 return null;
169 }
170 catch (Exception e)
171 {
172 log.error("DeviceStorage:getDeviceByIP:{} failed");
173 return null;
174 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800175 }
176
Teru87cd2da2013-06-15 20:33:08 -0700177 /***
178 * This function is for changing the Device attachment point.
179 * @param device The device you want change the attachment point
180 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800181 @Override
182 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800183 IDeviceObject obj = null;
184 try {
Teru87cd2da2013-06-15 20:33:08 -0700185 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800186 log.debug("Changing device ports {}: found existing device",device.getMACAddressString());
187 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700188 ope.commit();
189 } else {
Pankaj Berdeda809572013-02-22 15:31:20 -0800190 log.debug("failed to search device...now adding {}",device.getMACAddressString());
191 addDevice(device);
Teru87cd2da2013-06-15 20:33:08 -0700192 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800193 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -0700194 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800195 log.error(":addDevice mac:{} failed", device.getMACAddressString());
196 }
197 }
198
Teru87cd2da2013-06-15 20:33:08 -0700199 /***
200 * This function is for changing the Device attachment point.
201 * @param device The new device you want change the attachment point
202 * @param obj The old device IDeviceObject that is going to change the attachment point.
203 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800204 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
205 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
206 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Teru87cd2da2013-06-15 20:33:08 -0700207
Pankaj Berdeda809572013-02-22 15:31:20 -0800208 for (SwitchPort ap : attachmentPoints) {
Teru87cd2da2013-06-15 20:33:08 -0700209 //Check weather there is the port
210 IPortObject port = ope.searchPort( HexString.toHexString(ap.getSwitchDPID()),
211 (short) ap.getPort());
212 log.debug("New Switch Port is {},{}", HexString.toHexString(ap.getSwitchDPID()),(short) ap.getPort());
213
214 if(port != null){
215 if(attachedPorts.contains(port))
216 {
217 log.debug("This is the port you already attached {}: do nothing",device.getMACAddressString());
218 //This port will be remained, so remove from the removed port lists.
219 attachedPorts.remove(port);
220 } else {
221 log.debug("Adding device {}: attaching to port",device.getMACAddressString());
222 port.setDevice(obj);
223 }
224
225 log.debug("port number is {}", port.getNumber().toString());
226 log.debug("port desc is {}", port.getDesc());
227 }
228 }
229
230 for (IPortObject port: attachedPorts) {
231 log.debug("Detouching the device {}: detouching from port",device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800232 port.removeDevice(obj);
Teru87cd2da2013-06-15 20:33:08 -0700233 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800234 }
235
Teru87cd2da2013-06-15 20:33:08 -0700236 /***
237 * This function is for changing the Device IPv4 address.
238 * @param device The new device you want change the ipaddress
239 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800240 @Override
241 public void changeDeviceIPv4Address(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800242 IDeviceObject obj;
243 try {
Teru87cd2da2013-06-15 20:33:08 -0700244 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
245
246 String multiIntString = "";
247 for(Integer intValue : device.getIPv4Addresses()){
248 if (multiIntString == null || multiIntString.isEmpty()){
249 multiIntString = "[" + IPv4.fromIPv4Address(intValue);
250 } else {
251 multiIntString += "," + IPv4.fromIPv4Address(intValue);
252 }
253 }
254
255 if(multiIntString != null && !multiIntString.isEmpty()){
256 obj.setIPAddress(multiIntString + "]");
257 }
258
259 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800260 } else {
261 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700262 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800263 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700264 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800265 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(),e);
266 }
267 }
268
Pankaj Berdeb6031342013-02-19 18:51:51 -0800269}