blob: 98b4a88189a6752e49a804a33bfb50bb84c86cd3 [file] [log] [blame]
HIGUCHI Yuta351edcb2013-06-12 11:58:13 -07001package net.onrc.onos.ofcontroller.devicemanager.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 Yuta20514902013-06-12 11:24:16 -070014import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
15import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
HIGUCHI Yutaed49ef72013-06-12 11:34:10 -070016import net.onrc.onos.ofcontroller.core.internal.SwitchStorageImpl;
HIGUCHI Yutad3fa44d2013-06-12 11:53:09 -070017import net.onrc.onos.ofcontroller.devicemanager.IDeviceStorage;
Pankaj Berdeda809572013-02-22 15:31:20 -080018import net.onrc.onos.util.GraphDBConnection;
Teru87cd2da2013-06-15 20:33:08 -070019import net.onrc.onos.util.GraphDBConnection.Transaction;
Toshio Koide4f629f32013-06-13 13:39:38 -070020import net.onrc.onos.util.GraphDBOperation;
Pankaj Berdeb6031342013-02-19 18:51:51 -080021
Teru87cd2da2013-06-15 20:33:08 -070022/**
23 * This is the class for storing the information of devices into CassandraDB
24 * @author Pankaj
25 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080026public class DeviceStorageImpl implements IDeviceStorage {
27
Teru87cd2da2013-06-15 20:33:08 -070028 private GraphDBConnection conn;
29 private GraphDBOperation ope;
Pankaj Berdeb6031342013-02-19 18:51:51 -080030 protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080031
Teru87cd2da2013-06-15 20:33:08 -070032 /***
33 * Initialize function. Before you use this class, please call this method
34 * @param conf configuration file for Cassandra DB
35 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080036 @Override
37 public void init(String conf) {
Teru87cd2da2013-06-15 20:33:08 -070038 try{
39 if((conn = GraphDBConnection.getInstance(conf)) != null)
40 {
41 ope = new GraphDBOperation(conn);
42 }
43 } catch(Exception e) {
44 log.error(e.getMessage());
45 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080046 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080047
Teru87cd2da2013-06-15 20:33:08 -070048 /***
49 * Finalize/close function. After you use this class, please call this method.
50 * It will close the DB connection.
51 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080052 @Override
53 public void close() {
Teru87cd2da2013-06-15 20:33:08 -070054 conn.close();
55 }
56
57 /***
58 * Finalize/close function. After you use this class, please call this method.
59 * It will close the DB connection. This is for Java gabage collection.
60 */
61 @Override
62 public void finalize() {
63 close();
Pankaj Berdeb6031342013-02-19 18:51:51 -080064 }
65
Teru87cd2da2013-06-15 20:33:08 -070066 /***
67 * This function is for adding the device into the DB.
68 * @param device The device you want to add into the DB.
69 * @return IDeviceObject which was added in the DB.
70 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080071 @Override
72 public IDeviceObject addDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -080073 IDeviceObject obj = null;
Pankaj Berdeb6031342013-02-19 18:51:51 -080074 try {
Teru87cd2da2013-06-15 20:33:08 -070075 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
76 log.debug("Adding device {}: found existing device",device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080077 } else {
Teru87cd2da2013-06-15 20:33:08 -070078 obj = ope.newDevice();
Pankaj Berdeda809572013-02-22 15:31:20 -080079 log.debug("Adding device {}: creating new device",device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080080 }
Teru87cd2da2013-06-15 20:33:08 -070081 changeDeviceAttachments(device, obj);
82
83 String multiIntString = "";
84 for(Integer intValue : device.getIPv4Addresses()) {
85 if (multiIntString == null || multiIntString.isEmpty()){
86 multiIntString = IPv4.fromIPv4Address(intValue);
87 multiIntString = "[" + IPv4.fromIPv4Address(intValue);
88 }else{
89 multiIntString += "," + IPv4.fromIPv4Address(intValue);
90 }
91 }
92
93 if(multiIntString.toString() != null && !multiIntString.toString().isEmpty()){
94 obj.setIPAddress(multiIntString + "]");
95 }
96
97 obj.setMACAddress(device.getMACAddressString());
98 obj.setType("device");
99 obj.setState("ACTIVE");
100 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800101
Teru87cd2da2013-06-15 20:33:08 -0700102 log.debug("Adding device {}",device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800103 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -0700104 ope.rollback();
Pankaj Berdeb6031342013-02-19 18:51:51 -0800105 log.error(":addDevice mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700106 obj = null;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800107 }
Teru87cd2da2013-06-15 20:33:08 -0700108 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800109 }
110
Teru87cd2da2013-06-15 20:33:08 -0700111 /***
112 * This function is for updating the Device properties.
113 * @param device The device you want to add into the DB.
114 * @return IDeviceObject which was added in the DB.
115 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800116 @Override
117 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800118 return addDevice(device);
119 }
120
Teru87cd2da2013-06-15 20:33:08 -0700121 /***
122 * This function is for removing the Device from the DB.
123 * @param device The device you want to delete from the DB.
124 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800125 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800126 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800127 IDeviceObject dev;
128 try {
Teru87cd2da2013-06-15 20:33:08 -0700129 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
130 ope.removeDevice(dev);
131 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800132 log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
133 }
134 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -0700135 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800136 log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
137 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800138 }
139
Teru87cd2da2013-06-15 20:33:08 -0700140 /***
141 * This function is for getting the Device from the DB by Mac address of the device.
142 * @param mac The device mac address you want to get from the DB.
143 * @return IDeviceObject you want to get.
144 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800145 @Override
146 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700147 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800148 }
149
Teru87cd2da2013-06-15 20:33:08 -0700150 /***
151 * This function is for getting the Device from the DB by IP address of the device.
152 * @param ip The device ip address you want to get from the DB.
153 * @return IDeviceObject you want to get.
154 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800155 @Override
156 public IDeviceObject getDeviceByIP(String ip) {
Teru87cd2da2013-06-15 20:33:08 -0700157 try
158 {
159 for(IDeviceObject dev : ope.getDevices()){
160 String ips;
161 if((ips = dev.getIPAddress()) != null){
162 String nw_addr_wob = ips.replace("[", "").replace("]", "");
163 ArrayList<String> iplists = Lists.newArrayList(nw_addr_wob.split(","));
164 if(iplists.contains(ip)){
165 return dev;
166 }
167 }
168 }
169 return null;
170 }
171 catch (Exception e)
172 {
173 log.error("DeviceStorage:getDeviceByIP:{} failed");
174 return null;
175 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800176 }
177
Teru87cd2da2013-06-15 20:33:08 -0700178 /***
179 * This function is for changing the Device attachment point.
180 * @param device The device you want change the attachment point
181 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800182 @Override
183 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800184 IDeviceObject obj = null;
185 try {
Teru87cd2da2013-06-15 20:33:08 -0700186 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800187 log.debug("Changing device ports {}: found existing device",device.getMACAddressString());
188 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700189 ope.commit();
190 } else {
Pankaj Berdeda809572013-02-22 15:31:20 -0800191 log.debug("failed to search device...now adding {}",device.getMACAddressString());
192 addDevice(device);
Teru87cd2da2013-06-15 20:33:08 -0700193 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800194 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -0700195 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800196 log.error(":addDevice mac:{} failed", device.getMACAddressString());
197 }
198 }
199
Teru87cd2da2013-06-15 20:33:08 -0700200 /***
201 * This function is for changing the Device attachment point.
202 * @param device The new device you want change the attachment point
203 * @param obj The old device IDeviceObject that is going to change the attachment point.
204 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800205 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
206 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
207 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Teru87cd2da2013-06-15 20:33:08 -0700208
Pankaj Berdeda809572013-02-22 15:31:20 -0800209 for (SwitchPort ap : attachmentPoints) {
Teru87cd2da2013-06-15 20:33:08 -0700210 //Check weather there is the port
211 IPortObject port = ope.searchPort( HexString.toHexString(ap.getSwitchDPID()),
212 (short) ap.getPort());
213 log.debug("New Switch Port is {},{}", HexString.toHexString(ap.getSwitchDPID()),(short) ap.getPort());
214
215 if(port != null){
216 if(attachedPorts.contains(port))
217 {
218 log.debug("This is the port you already attached {}: do nothing",device.getMACAddressString());
219 //This port will be remained, so remove from the removed port lists.
220 attachedPorts.remove(port);
221 } else {
222 log.debug("Adding device {}: attaching to port",device.getMACAddressString());
223 port.setDevice(obj);
224 }
225
226 log.debug("port number is {}", port.getNumber().toString());
227 log.debug("port desc is {}", port.getDesc());
228 }
229 }
230
231 for (IPortObject port: attachedPorts) {
232 log.debug("Detouching the device {}: detouching from port",device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800233 port.removeDevice(obj);
Teru87cd2da2013-06-15 20:33:08 -0700234 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800235 }
236
Teru87cd2da2013-06-15 20:33:08 -0700237 /***
238 * This function is for changing the Device IPv4 address.
239 * @param device The new device you want change the ipaddress
240 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800241 @Override
242 public void changeDeviceIPv4Address(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800243 IDeviceObject obj;
244 try {
Teru87cd2da2013-06-15 20:33:08 -0700245 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
246
247 String multiIntString = "";
248 for(Integer intValue : device.getIPv4Addresses()){
249 if (multiIntString == null || multiIntString.isEmpty()){
250 multiIntString = "[" + IPv4.fromIPv4Address(intValue);
251 } else {
252 multiIntString += "," + IPv4.fromIPv4Address(intValue);
253 }
254 }
255
256 if(multiIntString != null && !multiIntString.isEmpty()){
257 obj.setIPAddress(multiIntString + "]");
258 }
259
260 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800261 } else {
262 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700263 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800264 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700265 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800266 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(),e);
267 }
268 }
269
Pankaj Berdeb6031342013-02-19 18:51:51 -0800270}