blob: cfc411c705314b012cd4184a96a25e5183637e7b [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
Jonathan Hartd6ed62b2013-11-01 13:18:25 -07003import java.util.Arrays;
Pankaj Berdeb6031342013-02-19 18:51:51 -08004import java.util.List;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -07005
6import net.floodlightcontroller.devicemanager.IDevice;
7import net.floodlightcontroller.devicemanager.SwitchPort;
8import net.onrc.onos.graph.GraphDBOperation;
9import net.onrc.onos.ofcontroller.core.IDeviceStorage;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IIpv4Address;
12import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
13
Pankaj Berdeb6031342013-02-19 18:51:51 -080014import org.openflow.util.HexString;
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
18import com.google.common.collect.Lists;
19import com.thinkaurelius.titan.core.TitanException;
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 {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070026 protected final static Logger log = LoggerFactory.getLogger(SwitchStorageImpl.class);
Pankaj Berdeb6031342013-02-19 18:51:51 -080027
Teru87cd2da2013-06-15 20:33:08 -070028 private GraphDBOperation ope;
Pankaj Berdeb6031342013-02-19 18:51:51 -080029
Teru87cd2da2013-06-15 20:33:08 -070030 /***
31 * Initialize function. Before you use this class, please call this method
32 * @param conf configuration file for Cassandra DB
33 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080034 @Override
35 public void init(String conf) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070036 try {
Toshio Koidebfe9b922013-06-18 10:56:05 -070037 ope = new GraphDBOperation(conf);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070038 } catch (TitanException e) {
39 log.error("Couldn't open graph operation", e);
Teru87cd2da2013-06-15 20:33:08 -070040 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080041 }
Pankaj Berdeb6031342013-02-19 18:51:51 -080042
Teru87cd2da2013-06-15 20:33:08 -070043 /***
44 * Finalize/close function. After you use this class, please call this method.
45 * It will close the DB connection.
46 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080047 @Override
48 public void close() {
Toshio Koidebfe9b922013-06-18 10:56:05 -070049 ope.close();
Teru87cd2da2013-06-15 20:33:08 -070050 }
51
52 /***
53 * Finalize/close function. After you use this class, please call this method.
Pavlin Radoslavovef0cb002013-06-21 14:55:23 -070054 * It will close the DB connection. This is for Java garbage collection.
Teru87cd2da2013-06-15 20:33:08 -070055 */
56 @Override
57 public void finalize() {
58 close();
Pankaj Berdeb6031342013-02-19 18:51:51 -080059 }
60
Teru87cd2da2013-06-15 20:33:08 -070061 /***
62 * This function is for adding the device into the DB.
63 * @param device The device you want to add into the DB.
64 * @return IDeviceObject which was added in the DB.
65 */
Pankaj Berdeb6031342013-02-19 18:51:51 -080066 @Override
67 public IDeviceObject addDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -080068 IDeviceObject obj = null;
Pankaj Berdeb6031342013-02-19 18:51:51 -080069 try {
Teru87cd2da2013-06-15 20:33:08 -070070 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070071 log.debug("Adding device {}: found existing device", device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080072 } else {
Teru87cd2da2013-06-15 20:33:08 -070073 obj = ope.newDevice();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070074 log.debug("Adding device {}: creating new device", device.getMACAddressString());
Pankaj Berdeb6031342013-02-19 18:51:51 -080075 }
Pankaj Berdeda809572013-02-22 15:31:20 -080076
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070077 changeDeviceAttachments(device, obj);
Jonathan Hartc8ae36c2013-11-03 19:06:40 -080078
79 changeDeviceIpv4Addresses(device, obj);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070080
81 obj.setMACAddress(device.getMACAddressString());
82 obj.setType("device");
83 obj.setState("ACTIVE");
84 ope.commit();
85
86 //log.debug("Adding device {}",device.getMACAddressString());
87 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -070088 ope.rollback();
Jonathan Hart9ec4c6a2013-11-04 22:01:50 -080089 log.error("Adding device {} failed", device.getMACAddressString(), e);
Teru87cd2da2013-06-15 20:33:08 -070090 obj = null;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070091 }
92
93 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -080094 }
95
Teru87cd2da2013-06-15 20:33:08 -070096 /***
97 * This function is for updating the Device properties.
98 * @param device The device you want to add into the DB.
99 * @return IDeviceObject which was added in the DB.
100 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800101 @Override
102 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800103 return addDevice(device);
104 }
105
Teru87cd2da2013-06-15 20:33:08 -0700106 /***
107 * This function is for removing the Device from the DB.
108 * @param device The device you want to delete from the DB.
109 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800110 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800111 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800112 IDeviceObject dev;
113 try {
Teru87cd2da2013-06-15 20:33:08 -0700114 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700115 for (IIpv4Address ipv4AddressVertex : dev.getIpv4Addresses()) {
116 ope.removeIpv4Address(ipv4AddressVertex);
117 }
118
Teru87cd2da2013-06-15 20:33:08 -0700119 ope.removeDevice(dev);
120 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800121 log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
122 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700123 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700124 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800125 log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
126 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800127 }
128
Teru87cd2da2013-06-15 20:33:08 -0700129 /***
130 * This function is for getting the Device from the DB by Mac address of the device.
131 * @param mac The device mac address you want to get from the DB.
132 * @return IDeviceObject you want to get.
133 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800134 @Override
135 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700136 return ope.searchDevice(mac);
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 IP address of the device.
141 * @param ip The device ip 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
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700145 public IDeviceObject getDeviceByIP(int ipv4Address) {
146 try {
147 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
148 if (ipv4AddressVertex != null) {
149 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700150 }
151 return null;
152 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700153 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700154 log.error("DeviceStorage:getDeviceByIP:{} failed");
155 return null;
156 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800157 }
158
Teru87cd2da2013-06-15 20:33:08 -0700159 /***
160 * This function is for changing the Device attachment point.
161 * @param device The device you want change the attachment point
162 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800163 @Override
164 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800165 IDeviceObject obj = null;
166 try {
Teru87cd2da2013-06-15 20:33:08 -0700167 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700168 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800169 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700170 ope.commit();
171 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700172 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800173 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700174 }
175 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700176 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800177 log.error(":addDevice mac:{} failed", device.getMACAddressString());
178 }
179 }
180
Teru87cd2da2013-06-15 20:33:08 -0700181 /***
182 * This function is for changing the Device attachment point.
183 * @param device The new device you want change the attachment point
184 * @param obj The old device IDeviceObject that is going to change the attachment point.
185 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800186 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
187 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
188 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700189
190 for (SwitchPort ap : attachmentPoints) {
191 //Check if there is the port
192 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
193 (short) ap.getPort());
194 log.debug("New Switch Port is {},{}",
195 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
196
197 if (port != null){
198 if (attachedPorts.contains(port)) {
199 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
200 //This port will be remained, so remove from the removed port lists.
201 attachedPorts.remove(port);
202 } else {
203 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
204 port.setDevice(obj);
205 }
206
207 log.debug("port number is {}", port.getNumber().toString());
208 log.debug("port desc is {}", port.getDesc());
209 }
210 }
211
212 for (IPortObject port: attachedPorts) {
213 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
214 port.removeDevice(obj);
215 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800216 }
217
Teru87cd2da2013-06-15 20:33:08 -0700218 /***
219 * This function is for changing the Device IPv4 address.
220 * @param device The new device you want change the ipaddress
221 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800222 @Override
223 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700224 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
225 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800226 IDeviceObject obj;
227 try {
Teru87cd2da2013-06-15 20:33:08 -0700228 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800229 changeDeviceIpv4Addresses(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700230
231 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800232 } else {
233 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700234 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800235 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700236 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700237 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800238 }
239 }
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800240
241 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
242 for (int ipv4Address : device.getIPv4Addresses()) {
243 if (deviceObject.getIpv4Address(ipv4Address) == null) {
244 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
245 deviceObject.addIpv4Address(dbIpv4Address);
246 }
247 }
248
249 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
250 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
251 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
252 deviceObject.removeIpv4Address(dbIpv4Address);
253 }
254 }
255 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800256
Pankaj Berdeb6031342013-02-19 18:51:51 -0800257}