blob: 6438ffdeb173cd91e94079c9bfd32b60f409f02a [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);
80 /*for (Integer intIpv4Address : device.getIPv4Addresses()) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070081 obj.addIpv4Address(ope.ensureIpv4Address(intIpv4Address));
Jonathan Hartc8ae36c2013-11-03 19:06:40 -080082 }*/
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070083
84 obj.setMACAddress(device.getMACAddressString());
85 obj.setType("device");
86 obj.setState("ACTIVE");
87 ope.commit();
88
89 //log.debug("Adding device {}",device.getMACAddressString());
90 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -070091 ope.rollback();
Pankaj Berdeb6031342013-02-19 18:51:51 -080092 log.error(":addDevice mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -070093 obj = null;
Jonathan Hartd6ed62b2013-11-01 13:18:25 -070094 }
95
96 return obj;
Pankaj Berdeb6031342013-02-19 18:51:51 -080097 }
98
Teru87cd2da2013-06-15 20:33:08 -070099 /***
100 * This function is for updating the Device properties.
101 * @param device The device you want to add into the DB.
102 * @return IDeviceObject which was added in the DB.
103 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800104 @Override
105 public IDeviceObject updateDevice(IDevice device) {
Pankaj Berdeb6031342013-02-19 18:51:51 -0800106 return addDevice(device);
107 }
108
Teru87cd2da2013-06-15 20:33:08 -0700109 /***
110 * This function is for removing the Device from the DB.
111 * @param device The device you want to delete from the DB.
112 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800113 @Override
Pankaj Berdeda809572013-02-22 15:31:20 -0800114 public void removeDevice(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800115 IDeviceObject dev;
116 try {
Teru87cd2da2013-06-15 20:33:08 -0700117 if ((dev = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hart4fce4ed2013-11-01 21:29:21 -0700118 for (IIpv4Address ipv4AddressVertex : dev.getIpv4Addresses()) {
119 ope.removeIpv4Address(ipv4AddressVertex);
120 }
121
Teru87cd2da2013-06-15 20:33:08 -0700122 ope.removeDevice(dev);
123 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800124 log.error("DeviceStorage:removeDevice mac:{} done", device.getMACAddressString());
125 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700126 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700127 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800128 log.error("DeviceStorage:removeDevice mac:{} failed", device.getMACAddressString());
129 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800130 }
131
Teru87cd2da2013-06-15 20:33:08 -0700132 /***
133 * This function is for getting the Device from the DB by Mac address of the device.
134 * @param mac The device mac address you want to get from the DB.
135 * @return IDeviceObject you want to get.
136 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800137 @Override
138 public IDeviceObject getDeviceByMac(String mac) {
Teru87cd2da2013-06-15 20:33:08 -0700139 return ope.searchDevice(mac);
Pankaj Berdeb6031342013-02-19 18:51:51 -0800140 }
141
Teru87cd2da2013-06-15 20:33:08 -0700142 /***
143 * This function is for getting the Device from the DB by IP address of the device.
144 * @param ip The device ip address you want to get from the DB.
145 * @return IDeviceObject you want to get.
146 */
Pankaj Berdeb6031342013-02-19 18:51:51 -0800147 @Override
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700148 public IDeviceObject getDeviceByIP(int ipv4Address) {
149 try {
150 IIpv4Address ipv4AddressVertex = ope.searchIpv4Address(ipv4Address);
151 if (ipv4AddressVertex != null) {
152 return ipv4AddressVertex.getDevice();
Teru87cd2da2013-06-15 20:33:08 -0700153 }
154 return null;
155 }
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700156 catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700157 log.error("DeviceStorage:getDeviceByIP:{} failed");
158 return null;
159 }
Pankaj Berdeb6031342013-02-19 18:51:51 -0800160 }
161
Teru87cd2da2013-06-15 20:33:08 -0700162 /***
163 * This function is for changing the Device attachment point.
164 * @param device The device you want change the attachment point
165 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800166 @Override
167 public void changeDeviceAttachments(IDevice device) {
Pankaj Berdeda809572013-02-22 15:31:20 -0800168 IDeviceObject obj = null;
169 try {
Teru87cd2da2013-06-15 20:33:08 -0700170 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700171 log.debug("Changing device ports {}: found existing device", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800172 changeDeviceAttachments(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700173 ope.commit();
174 } else {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700175 log.debug("failed to search device...now adding {}", device.getMACAddressString());
Pankaj Berdeda809572013-02-22 15:31:20 -0800176 addDevice(device);
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700177 }
178 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700179 ope.rollback();
Pankaj Berdeda809572013-02-22 15:31:20 -0800180 log.error(":addDevice mac:{} failed", device.getMACAddressString());
181 }
182 }
183
Teru87cd2da2013-06-15 20:33:08 -0700184 /***
185 * This function is for changing the Device attachment point.
186 * @param device The new device you want change the attachment point
187 * @param obj The old device IDeviceObject that is going to change the attachment point.
188 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800189 public void changeDeviceAttachments(IDevice device, IDeviceObject obj) {
190 SwitchPort[] attachmentPoints = device.getAttachmentPoints();
191 List<IPortObject> attachedPorts = Lists.newArrayList(obj.getAttachedPorts());
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700192
193 for (SwitchPort ap : attachmentPoints) {
194 //Check if there is the port
195 IPortObject port = ope.searchPort(HexString.toHexString(ap.getSwitchDPID()),
196 (short) ap.getPort());
197 log.debug("New Switch Port is {},{}",
198 HexString.toHexString(ap.getSwitchDPID()), (short) ap.getPort());
199
200 if (port != null){
201 if (attachedPorts.contains(port)) {
202 log.debug("This is the port you already attached {}: do nothing", device.getMACAddressString());
203 //This port will be remained, so remove from the removed port lists.
204 attachedPorts.remove(port);
205 } else {
206 log.debug("Adding device {}: attaching to port", device.getMACAddressString());
207 port.setDevice(obj);
208 }
209
210 log.debug("port number is {}", port.getNumber().toString());
211 log.debug("port desc is {}", port.getDesc());
212 }
213 }
214
215 for (IPortObject port: attachedPorts) {
216 log.debug("Detaching the device {}: detaching from port", device.getMACAddressString());
217 port.removeDevice(obj);
218 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800219 }
220
Teru87cd2da2013-06-15 20:33:08 -0700221 /***
222 * This function is for changing the Device IPv4 address.
223 * @param device The new device you want change the ipaddress
224 */
Pankaj Berdeda809572013-02-22 15:31:20 -0800225 @Override
226 public void changeDeviceIPv4Address(IDevice device) {
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700227 log.debug("Changing IP address for {} to {}", device.getMACAddressString(),
228 device.getIPv4Addresses());
Pankaj Berdeda809572013-02-22 15:31:20 -0800229 IDeviceObject obj;
230 try {
Teru87cd2da2013-06-15 20:33:08 -0700231 if ((obj = ope.searchDevice(device.getMACAddressString())) != null) {
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800232 changeDeviceIpv4Addresses(device, obj);
Teru87cd2da2013-06-15 20:33:08 -0700233
234 ope.commit();
Pankaj Berdeda809572013-02-22 15:31:20 -0800235 } else {
236 log.error(":changeDeviceIPv4Address mac:{} failed", device.getMACAddressString());
Teru87cd2da2013-06-15 20:33:08 -0700237 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800238 } catch (TitanException e) {
Teru87cd2da2013-06-15 20:33:08 -0700239 ope.rollback();
Jonathan Hartd6ed62b2013-11-01 13:18:25 -0700240 log.error(":changeDeviceIPv4Address mac:{} failed due to exception {}", device.getMACAddressString(), e);
Pankaj Berdeda809572013-02-22 15:31:20 -0800241 }
242 }
Jonathan Hartc8ae36c2013-11-03 19:06:40 -0800243
244 private void changeDeviceIpv4Addresses(IDevice device, IDeviceObject deviceObject) {
245 for (int ipv4Address : device.getIPv4Addresses()) {
246 if (deviceObject.getIpv4Address(ipv4Address) == null) {
247 IIpv4Address dbIpv4Address = ope.ensureIpv4Address(ipv4Address);
248 deviceObject.addIpv4Address(dbIpv4Address);
249 }
250 }
251
252 List<Integer> deviceIpv4Addresses = Arrays.asList(device.getIPv4Addresses());
253 for (IIpv4Address dbIpv4Address : deviceObject.getIpv4Addresses()) {
254 if (!deviceIpv4Addresses.contains(dbIpv4Address.getIpv4Address())) {
255 deviceObject.removeIpv4Address(dbIpv4Address);
256 }
257 }
258 }
Pankaj Berdeda809572013-02-22 15:31:20 -0800259
Pankaj Berdeb6031342013-02-19 18:51:51 -0800260}