blob: 256008a5d0fac32458df2b72e352a163baee1fb9 [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) {
117 ope.removeDevice(dev);
118 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800119 log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
120 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700121 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700122 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800123 log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
124 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800125 }
126
Teru87cd2da2013-06-15 20:33:08 -0700127 /***
128 * This function is for getting the Device from the DB by Mac address of the device.
129 * @param mac The device mac address you want to get from the DB.
130 * @return IDeviceObject you want to get.
131 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800132 @Override
133 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700134 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800135 }
136
Teru87cd2da2013-06-15 20:33:08 -0700137 /***
138 * This function is for getting the Device from the DB by IP address of the device.
139 * @param ip The device ip address you want to get from the DB.
140 * @return IDeviceObject you want to get.
141 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800142 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700143 public IDeviceObject getDeviceByIP(int ipv4Address) {
144 try {
145 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
146 if (ipv4AddressVertex != null) {
147 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700148 }
149 return null;
150 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700151 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700152 log.error("DeviceStorage:getDeviceByIP:{} failed");
153 return null;
154 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800155 }
156
Teru87cd2da2013-06-15 20:33:08 -0700157 /***
158 * This function is for changing the Device attachment point.
159 * @param device The device you want change the attachment point
160 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800161 @Override
162 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800163 IDeviceObject obj = null;
164 try {
Teru87cd2da2013-06-15 20:33:08 -0700165 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700166 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800167 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700168 ope.commit();
169 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700170 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800171 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700172 }
173 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700174 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800175 log.error(":addDevice mac:{} failed", device.getMACAddressString());
176 }
177 }
178
Teru87cd2da2013-06-15 20:33:08 -0700179 /***
180 * This function is for changing the Device attachment point.
181 * @param device The new device you want change the attachment point
182 * @param obj The old device IDeviceObject that is going to change the attachment point.
183 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800184 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
185 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
186 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700187
188 for (SwitchPort ap : attachmentPoints) {
189 //Check if there is the port
190 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
191 (short) ap.getPort());
192 log.debug("New Switch Port is {},{}",
193 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
194
195 if (port != null){
196 if (attachedPorts.contains(port)) {
197 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
198 //This port will be remained, so remove from the removed port lists.
199 attachedPorts.remove(port);
200 } else {
201 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
202 port.setDevice(obj);
203 }
204
205 log.debug("port number is {}", port.getNumber().toString());
206 log.debug("port desc is {}", port.getDesc());
207 }
208 }
209
210 for (IPortObject port: attachedPorts) {
211 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
212 port.removeDevice(obj);
213 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800214 }
215
Teru87cd2da2013-06-15 20:33:08 -0700216 /***
217 * This function is for changing the Device IPv4 address.
218 * @param device The new device you want change the ipaddress
219 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800220 @Override
221 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700222 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
223 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800224 IDeviceObject obj;
225 try {
Teru87cd2da2013-06-15 20:33:08 -0700226 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700227 for (int ipv4Address : device.getIPv4Addresses()) {
228 if (obj.getIpv4Address(ipv4Address) == null) {
229 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
230 obj.addIpv4Address(dbIpv4Address);
231 }
Teru87cd2da2013-06-15 20:33:08 -0700232 }
233
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700234 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
235 for (IIpv4Address dbIpv4Address : obj.getIpv4Addresses()) {
236 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
237 obj.removeIpv4Address(dbIpv4Address);
238 }
239 }
Teru87cd2da2013-06-15 20:33:08 -0700240
241 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800242 } else {
243 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700244 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800245 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700246 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700247 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800248 }
249 }
250
Pankaj Berdeb6031342013-02-19 18:51:51 -0800251}