blob: 0428779320eec1d8f869135adf662dc3ecab76a8 [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;
Toshio Koide4f629f32013-06-13 13:39:38 -070018import net.onrc.onos.util.GraphDBOperation;
Pankaj Berdeb6031342013-02-19 18:51:51 -080019
Teru87cd2da2013-06-15 20:33:08 -070020/**
21 * This is the class for storing the information of devices into CassandraDB
22 * @author Pankaj
23 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080024public class DeviceStorageImpl implements IDeviceStorage {
25
Teru87cd2da2013-06-15 20:33:08 -070026 private GraphDBOperation ope;
Pankaj Berdeb6031342013-02-19 18:51:51 -080027 protected static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080028
Teru87cd2da2013-06-15 20:33:08 -070029 /***
30 * Initialize function. Before you use this class, please call this method
31 * @param conf configuration file for Cassandra DB
32 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080033 @Override
34 public void init(String conf) {
Teru87cd2da2013-06-15 20:33:08 -070035 try{
Toshio Koidebfe9b922013-06-18 10:56:05 -070036 ope = new GraphDBOperation(conf);
Teru87cd2da2013-06-15 20:33:08 -070037 } catch(Exception e) {
38 log.error(e.getMessage());
39 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080040 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080041
Teru87cd2da2013-06-15 20:33:08 -070042 /***
43 * Finalize/close function. After you use this class, please call this method.
44 * It will close the DB connection.
45 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080046 @Override
47 public void close() {
Toshio Koidebfe9b922013-06-18 10:56:05 -070048 ope.close();
Teru87cd2da2013-06-15 20:33:08 -070049 }
50
51 /***
52 * Finalize/close function. After you use this class, please call this method.
53 * It will close the DB connection. This is for Java gabage collection.
54 */
55 @Override
56 public void finalize() {
57 close();
Pankaj Berdeb6031342013-02-19 18:51:51 -080058 }
59
Teru87cd2da2013-06-15 20:33:08 -070060 /***
61 * This function is for adding the device into the DB.
62 * @param device The device you want to add into the DB.
63 * @return IDeviceObject which was added in the DB.
64 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080065 @Override
66 public IDeviceObject addDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -080067 IDeviceObject obj = null;
Pankaj Berdeb6031342013-02-19 18:51:51 -080068 try {
Teru87cd2da2013-06-15 20:33:08 -070069 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
70 log.debug("Adding device {}: found existing device",device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080071 } else {
Teru87cd2da2013-06-15 20:33:08 -070072 obj = ope.newDevice();
Pankaj Berdeda809572013-02-22 15:31:20 -080073 log.debug("Adding device {}: creating new device",device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080074 }
Teru87cd2da2013-06-15 20:33:08 -070075 changeDeviceAttachments(device, obj);
76
77 String multiIntString = "";
78 for(Integer intValue : device.getIPv4Addresses()) {
79 if (multiIntString == null || multiIntString.isEmpty()){
80 multiIntString = IPv4.fromIPv4Address(intValue);
81 multiIntString = "[" + IPv4.fromIPv4Address(intValue);
82 }else{
83 multiIntString += "," + IPv4.fromIPv4Address(intValue);
84 }
85 }
86
87 if(multiIntString.toString() != null && !multiIntString.toString().isEmpty()){
88 obj.setIPAddress(multiIntString + "]");
89 }
90
91 obj.setMACAddress(device.getMACAddressString());
92 obj.setType("device");
93 obj.setState("ACTIVE");
94 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -080095
Teru87cd2da2013-06-15 20:33:08 -070096 log.debug("Adding device {}",device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -080097 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -070098 ope.rollback();
Pankaj Berdeb6031342013-02-19 18:51:51 -080099 log.error(":addDevice mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700100 obj = null;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800101 }
Teru87cd2da2013-06-15 20:33:08 -0700102 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -0800103 }
104
Teru87cd2da2013-06-15 20:33:08 -0700105 /***
106 * This function is for updating the Device properties.
107 * @param device The device you want to add into the DB.
108 * @return IDeviceObject which was added in the DB.
109 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800110 @Override
111 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800112 return addDevice(device);
113 }
114
Teru87cd2da2013-06-15 20:33:08 -0700115 /***
116 * This function is for removing the Device from the DB.
117 * @param device The device you want to delete from the DB.
118 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800119 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800120 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800121 IDeviceObject dev;
122 try {
Teru87cd2da2013-06-15 20:33:08 -0700123 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
124 ope.removeDevice(dev);
125 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800126 log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
127 }
128 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -0700129 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800130 log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
131 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800132 }
133
Teru87cd2da2013-06-15 20:33:08 -0700134 /***
135 * This function is for getting the Device from the DB by Mac address of the device.
136 * @param mac The device mac address you want to get from the DB.
137 * @return IDeviceObject you want to get.
138 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800139 @Override
140 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700141 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800142 }
143
Teru87cd2da2013-06-15 20:33:08 -0700144 /***
145 * This function is for getting the Device from the DB by IP address of the device.
146 * @param ip The device ip address you want to get from the DB.
147 * @return IDeviceObject you want to get.
148 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800149 @Override
150 public IDeviceObject getDeviceByIP(String ip) {
Teru87cd2da2013-06-15 20:33:08 -0700151 try
152 {
153 for(IDeviceObject dev : ope.getDevices()){
154 String ips;
155 if((ips = dev.getIPAddress()) != null){
156 String nw_addr_wob = ips.replace("[", "").replace("]", "");
157 ArrayList<String> iplists = Lists.newArrayList(nw_addr_wob.split(","));
158 if(iplists.contains(ip)){
159 return dev;
160 }
161 }
162 }
163 return null;
164 }
165 catch (Exception e)
166 {
167 log.error("DeviceStorage:getDeviceByIP:{} failed");
168 return null;
169 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800170 }
171
Teru87cd2da2013-06-15 20:33:08 -0700172 /***
173 * This function is for changing the Device attachment point.
174 * @param device The device you want change the attachment point
175 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800176 @Override
177 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800178 IDeviceObject obj = null;
179 try {
Teru87cd2da2013-06-15 20:33:08 -0700180 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800181 log.debug("Changing device ports {}: found existing device",device.getMACAddressString());
182 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700183 ope.commit();
184 } else {
Pankaj Berdeda809572013-02-22 15:31:20 -0800185 log.debug("failed to search device...now adding {}",device.getMACAddressString());
186 addDevice(device);
Teru87cd2da2013-06-15 20:33:08 -0700187 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800188 } catch (Exception e) {
Teru87cd2da2013-06-15 20:33:08 -0700189 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800190 log.error(":addDevice mac:{} failed", device.getMACAddressString());
191 }
192 }
193
Teru87cd2da2013-06-15 20:33:08 -0700194 /***
195 * This function is for changing the Device attachment point.
196 * @param device The new device you want change the attachment point
197 * @param obj The old device IDeviceObject that is going to change the attachment point.
198 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800199 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
200 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
201 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Teru87cd2da2013-06-15 20:33:08 -0700202
Pankaj Berdeda809572013-02-22 15:31:20 -0800203 for (SwitchPort ap : attachmentPoints) {
Teru87cd2da2013-06-15 20:33:08 -0700204 //Check weather there is the port
205 IPortObject port = ope.searchPort( HexString.toHexString(ap.getSwitchDPID()),
206 (short) ap.getPort());
207 log.debug("New Switch Port is {},{}", HexString.toHexString(ap.getSwitchDPID()),(short) ap.getPort());
208
209 if(port != null){
210 if(attachedPorts.contains(port))
211 {
212 log.debug("This is the port you already attached {}: do nothing",device.getMACAddressString());
213 //This port will be remained, so remove from the removed port lists.
214 attachedPorts.remove(port);
215 } else {
216 log.debug("Adding device {}: attaching to port",device.getMACAddressString());
217 port.setDevice(obj);
218 }
219
220 log.debug("port number is {}", port.getNumber().toString());
221 log.debug("port desc is {}", port.getDesc());
222 }
223 }
224
225 for (IPortObject port: attachedPorts) {
226 log.debug("Detouching the device {}: detouching from port",device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800227 port.removeDevice(obj);
Teru87cd2da2013-06-15 20:33:08 -0700228 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800229 }
230
Teru87cd2da2013-06-15 20:33:08 -0700231 /***
232 * This function is for changing the Device IPv4 address.
233 * @param device The new device you want change the ipaddress
234 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800235 @Override
236 public void changeDeviceIPv4Address(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800237 IDeviceObject obj;
238 try {
Teru87cd2da2013-06-15 20:33:08 -0700239 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
240
241 String multiIntString = "";
242 for(Integer intValue : device.getIPv4Addresses()){
243 if (multiIntString == null || multiIntString.isEmpty()){
244 multiIntString = "[" + IPv4.fromIPv4Address(intValue);
245 } else {
246 multiIntString += "," + IPv4.fromIPv4Address(intValue);
247 }
248 }
249
250 if(multiIntString != null && !multiIntString.isEmpty()){
251 obj.setIPAddress(multiIntString + "]");
252 }
253
254 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800255 } else {
256 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700257 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800258 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700259 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800260 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(),e);
261 }
262 }
263
Pankaj Berdeb6031342013-02-19 18:51:51 -0800264}