blob: ed6d85fba415763b1c9ccaa619bf4621d98f6175 [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);
78
79 for (Integer intIpv4Address : device.getIPv4Addresses()) {
80 obj.addIpv4Address(ope.ensureIpv4Address(intIpv4Address));
81 }
82
83 obj.setMACAddress(device.getMACAddressString());
84 obj.setType("device");
85 obj.setState("ACTIVE");
86 ope.commit();
87
88 //log.debug("Adding device {}",device.getMACAddressString());
89 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -070090 ope.rollback();
Pankaj Berdeb6031342013-02-19 18:51:51 -080091 log.error(":addDevice mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -070092 obj = null;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070093 }
94
95 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -080096 }
97
Teru87cd2da2013-06-15 20:33:08 -070098 /***
99 * This function is for updating the Device properties.
100 * @param device The device you want to add into the DB.
101 * @return IDeviceObject which was added in the DB.
102 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800103 @Override
104 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800105 return addDevice(device);
106 }
107
Teru87cd2da2013-06-15 20:33:08 -0700108 /***
109 * This function is for removing the Device from the DB.
110 * @param device The device you want to delete from the DB.
111 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800112 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800113 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800114 IDeviceObject dev;
115 try {
Teru87cd2da2013-06-15 20:33:08 -0700116 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700117 for (IIpv4Address ipv4AddressVertex : dev.getIpv4Addresses()) {
118 ope.removeIpv4Address(ipv4AddressVertex);
119 }
120
Teru87cd2da2013-06-15 20:33:08 -0700121 ope.removeDevice(dev);
122 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800123 log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
124 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700125 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700126 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800127 log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
128 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800129 }
130
Teru87cd2da2013-06-15 20:33:08 -0700131 /***
132 * This function is for getting the Device from the DB by Mac address of the device.
133 * @param mac The device mac address you want to get from the DB.
134 * @return IDeviceObject you want to get.
135 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800136 @Override
137 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700138 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800139 }
140
Teru87cd2da2013-06-15 20:33:08 -0700141 /***
142 * This function is for getting the Device from the DB by IP address of the device.
143 * @param ip The device ip address you want to get from the DB.
144 * @return IDeviceObject you want to get.
145 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800146 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700147 public IDeviceObject getDeviceByIP(int ipv4Address) {
148 try {
149 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
150 if (ipv4AddressVertex != null) {
151 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700152 }
153 return null;
154 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700155 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700156 log.error("DeviceStorage:getDeviceByIP:{} failed");
157 return null;
158 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800159 }
160
Teru87cd2da2013-06-15 20:33:08 -0700161 /***
162 * This function is for changing the Device attachment point.
163 * @param device The device you want change the attachment point
164 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800165 @Override
166 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800167 IDeviceObject obj = null;
168 try {
Teru87cd2da2013-06-15 20:33:08 -0700169 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700170 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800171 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700172 ope.commit();
173 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700174 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800175 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700176 }
177 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700178 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800179 log.error(":addDevice mac:{} failed", device.getMACAddressString());
180 }
181 }
182
Teru87cd2da2013-06-15 20:33:08 -0700183 /***
184 * This function is for changing the Device attachment point.
185 * @param device The new device you want change the attachment point
186 * @param obj The old device IDeviceObject that is going to change the attachment point.
187 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800188 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
189 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
190 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700191
192 for (SwitchPort ap : attachmentPoints) {
193 //Check if there is the port
194 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
195 (short) ap.getPort());
196 log.debug("New Switch Port is {},{}",
197 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
198
199 if (port != null){
200 if (attachedPorts.contains(port)) {
201 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
202 //This port will be remained, so remove from the removed port lists.
203 attachedPorts.remove(port);
204 } else {
205 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
206 port.setDevice(obj);
207 }
208
209 log.debug("port number is {}", port.getNumber().toString());
210 log.debug("port desc is {}", port.getDesc());
211 }
212 }
213
214 for (IPortObject port: attachedPorts) {
215 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
216 port.removeDevice(obj);
217 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800218 }
219
Teru87cd2da2013-06-15 20:33:08 -0700220 /***
221 * This function is for changing the Device IPv4 address.
222 * @param device The new device you want change the ipaddress
223 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800224 @Override
225 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700226 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
227 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800228 IDeviceObject obj;
229 try {
Teru87cd2da2013-06-15 20:33:08 -0700230 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700231 for (int ipv4Address : device.getIPv4Addresses()) {
232 if (obj.getIpv4Address(ipv4Address) == null) {
233 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
234 obj.addIpv4Address(dbIpv4Address);
235 }
Teru87cd2da2013-06-15 20:33:08 -0700236 }
237
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700238 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
239 for (IIpv4Address dbIpv4Address : obj.getIpv4Addresses()) {
240 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
241 obj.removeIpv4Address(dbIpv4Address);
242 }
243 }
Teru87cd2da2013-06-15 20:33:08 -0700244
245 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800246 } else {
247 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700248 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800249 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700250 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700251 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800252 }
253 }
254
Pankaj Berdeb6031342013-02-19 18:51:51 -0800255}