Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.networkgraph; |
| 2 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 3 | import java.net.Inet4Address; |
| 4 | import java.net.InetAddress; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 5 | import java.util.ArrayList; |
| 6 | import java.util.List; |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 7 | import java.util.Collection; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 8 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.datastore.DataStoreClient; |
| 10 | import net.onrc.onos.core.datastore.IKVClient; |
| 11 | import net.onrc.onos.core.datastore.topology.KVDevice; |
| 12 | import net.onrc.onos.core.datastore.topology.KVLink; |
| 13 | import net.onrc.onos.core.datastore.topology.KVPort; |
| 14 | import net.onrc.onos.core.datastore.topology.KVSwitch; |
| 15 | import net.onrc.onos.core.datastore.topology.KVPort.STATUS; |
| 16 | import net.onrc.onos.core.datastore.utils.KVObject; |
| 17 | import net.onrc.onos.core.datastore.utils.KVObject.WriteOp; |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 18 | import net.onrc.onos.ofcontroller.networkgraph.PortEvent.SwitchPort; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 19 | |
| 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 23 | import com.google.common.net.InetAddresses; |
| 24 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 25 | /** |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 26 | * The southbound interface to the network graph which allows clients to |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 27 | * mutate the graph. This class will maintain the invariants of the network |
| 28 | * graph. The southbound discovery modules will use this interface to update |
| 29 | * the network graph as they learn about the state of the network. |
| 30 | * |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 31 | * Modification to the Network Map by this module will: |
| 32 | * 1. Writes to Cluster-wide DataStore. |
| 33 | * 2. Update ONOS instance In-memory Network Map. |
| 34 | * 3. Send-out Notification. (TBD) |
| 35 | * (XXX: To update other instances In-memory Network Map, |
| 36 | * notification should be triggered here. |
| 37 | * But if we want to aggregate notification to minimize notification, |
| 38 | * It might be better for the caller to trigger notification.) |
| 39 | * |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 40 | */ |
Jonathan Hart | fa01c24 | 2014-02-11 10:03:03 -0800 | [diff] [blame] | 41 | public class NetworkGraphDatastore { |
| 42 | private static final Logger log = LoggerFactory.getLogger(NetworkGraphDatastore.class); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 43 | |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Add a switch to the database. |
| 46 | * |
| 47 | * @param sw the switch to add. |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 48 | * @param portEvents the corresponding switch ports to add. |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 49 | * @return true on success, otherwise false. |
| 50 | */ |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 51 | public boolean addSwitch(SwitchEvent sw, |
| 52 | Collection<PortEvent> portEvents) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 53 | log.debug("Adding switch {}", sw); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 54 | ArrayList<WriteOp> groupOp = new ArrayList<>(); |
| 55 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 56 | KVSwitch rcSwitch = new KVSwitch(sw.getDpid()); |
| 57 | rcSwitch.setStatus(KVSwitch.STATUS.ACTIVE); |
| 58 | |
| 59 | IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 60 | |
| 61 | // XXX Is ForceCreating Switch on DB OK here? |
| 62 | // If ForceCreating, who ever is calling this method needs |
| 63 | // to assure that DPID is unique cluster-wide, etc. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 64 | groupOp.add(rcSwitch.forceCreateOp(client)); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 65 | |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 66 | for (PortEvent portEvent : portEvents) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 67 | KVPort rcPort = new KVPort(sw.getDpid(), portEvent.getNumber()); |
| 68 | rcPort.setStatus(KVPort.STATUS.ACTIVE); |
Jonathan Hart | 69864df | 2014-02-13 10:44:12 -0800 | [diff] [blame] | 69 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 70 | groupOp.add(rcPort.forceCreateOp(client)); |
Jonathan Hart | 69864df | 2014-02-13 10:44:12 -0800 | [diff] [blame] | 71 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 72 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 73 | boolean failed = KVObject.multiWrite(groupOp); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 74 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 75 | if (failed) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 76 | log.error("Adding Switch {} and its ports failed.", sw.getDpid()); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 77 | for (WriteOp op : groupOp) { |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 78 | log.debug("Operation:{} for {} - Result:{}", op.getOp(), op.getObject(), op.getStatus() ); |
| 79 | |
| 80 | // If we changed the operation from ForceCreate to |
| 81 | // Conditional operation (Create/Update) then we should retry here. |
| 82 | } |
| 83 | } |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 84 | return !failed; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 85 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 86 | |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 87 | /** |
| 88 | * Update a switch as inactive in the database. |
| 89 | * |
| 90 | * @param sw the switch to update. |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 91 | * @param portEvents the corresponding switch ports to update. |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 92 | * @return true on success, otherwise false. |
| 93 | */ |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 94 | public boolean deactivateSwitch(SwitchEvent sw, |
| 95 | Collection<PortEvent> portEvents) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 96 | log.debug("Deactivating switch {}", sw); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 97 | KVSwitch rcSwitch = new KVSwitch(sw.getDpid()); |
| 98 | |
| 99 | IKVClient client = DataStoreClient.getClient(); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 100 | |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 101 | List<WriteOp> groupOp = new ArrayList<>(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 102 | rcSwitch.setStatus(KVSwitch.STATUS.INACTIVE); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 103 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 104 | groupOp.add(rcSwitch.forceCreateOp(client)); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 105 | |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 106 | for (PortEvent portEvent : portEvents) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 107 | KVPort rcPort = new KVPort(sw.getDpid(), portEvent.getNumber()); |
| 108 | rcPort.setStatus(KVPort.STATUS.INACTIVE); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 109 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 110 | groupOp.add(rcPort.forceCreateOp(client)); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 111 | } |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 112 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 113 | boolean failed = KVObject.multiWrite(groupOp); |
Pavlin Radoslavov | 018d533 | 2014-02-19 23:08:35 -0800 | [diff] [blame] | 114 | |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 115 | return !failed; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 116 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 117 | |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 118 | /** |
| 119 | * Add a port to the database. |
| 120 | * |
| 121 | * @param port the port to add. |
| 122 | * @return true on success, otherwise false. |
| 123 | */ |
| 124 | public boolean addPort(PortEvent port) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 125 | log.debug("Adding port {}", port); |
Jonathan Hart | 4c26327 | 2014-02-13 17:41:05 -0800 | [diff] [blame] | 126 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 127 | KVPort rcPort = new KVPort(port.getDpid(), port.getNumber()); |
| 128 | rcPort.setStatus(KVPort.STATUS.ACTIVE); |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 129 | rcPort.forceCreate(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 130 | // TODO add description into KVPort |
Jonathan Hart | 4c26327 | 2014-02-13 17:41:05 -0800 | [diff] [blame] | 131 | //rcPort.setDescription(port.getDescription()); |
Jonathan Hart | 4c26327 | 2014-02-13 17:41:05 -0800 | [diff] [blame] | 132 | |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 133 | return true; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 134 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 135 | |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 136 | /** |
| 137 | * Update a port as inactive in the database. |
| 138 | * |
| 139 | * @param port the port to update. |
| 140 | * @return true on success, otherwise false. |
| 141 | */ |
| 142 | public boolean deactivatePort(PortEvent port) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 143 | log.debug("Deactivating port {}", port); |
Yuta HIGUCHI | 5d2d8d4 | 2014-02-20 22:22:53 -0800 | [diff] [blame] | 144 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 145 | KVPort rcPort = new KVPort(port.getDpid(), port.getNumber()); |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 146 | rcPort.setStatus(STATUS.INACTIVE); |
Yuta HIGUCHI | 5d2d8d4 | 2014-02-20 22:22:53 -0800 | [diff] [blame] | 147 | |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 148 | rcPort.forceCreate(); |
Jonathan Hart | 4c26327 | 2014-02-13 17:41:05 -0800 | [diff] [blame] | 149 | |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 150 | return true; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 151 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 152 | |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Add a link to the database. |
| 155 | * |
| 156 | * @param link the link to add. |
| 157 | * @return true on success, otherwise false. |
| 158 | */ |
| 159 | public boolean addLink(LinkEvent link) { |
| 160 | log.debug("Adding link {}", link); |
Yuta HIGUCHI | 0a4bd19 | 2014-02-17 13:52:34 -0800 | [diff] [blame] | 161 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 162 | KVLink rcLink = new KVLink(link.getSrc().getDpid(), |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 163 | link.getSrc().getNumber(), |
| 164 | link.getDst().getDpid(), |
| 165 | link.getDst().getNumber()); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 166 | |
Yuta HIGUCHI | ef47967 | 2014-02-19 09:14:39 -0800 | [diff] [blame] | 167 | // XXX This method is called only by discovery, |
| 168 | // which means what we are trying to write currently is the truth |
| 169 | // so we can force write here |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 170 | // |
| 171 | // TODO: We need to check for errors |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 172 | rcLink.setStatus(KVLink.STATUS.ACTIVE); |
Yuta HIGUCHI | ef47967 | 2014-02-19 09:14:39 -0800 | [diff] [blame] | 173 | rcLink.forceCreate(); |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 174 | |
| 175 | return true; // Success |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 176 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 177 | |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 178 | public boolean removeLink(LinkEvent linkEvent) { |
| 179 | log.debug("Removing link {}", linkEvent); |
Yuta HIGUCHI | 5d2d8d4 | 2014-02-20 22:22:53 -0800 | [diff] [blame] | 180 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 181 | KVLink rcLink = new KVLink(linkEvent.getSrc().getDpid(), linkEvent.getSrc().getNumber(), |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 182 | linkEvent.getDst().getDpid(), linkEvent.getDst().getNumber()); |
| 183 | rcLink.forceDelete(); |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 184 | |
Jonathan Hart | daea86f | 2014-02-19 15:28:42 -0800 | [diff] [blame] | 185 | return true; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 186 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 187 | |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Add a device to the database. |
| 190 | * |
| 191 | * @param device the device to add. |
| 192 | * @return true on success, otherwise false. |
| 193 | */ |
Pavlin Radoslavov | 50cd148 | 2014-02-19 16:57:03 -0800 | [diff] [blame] | 194 | public boolean addDevice(DeviceEvent device) { |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 195 | log.debug("Adding device into DB. mac {}", device.getMac()); |
| 196 | |
| 197 | KVDevice rcDevice = new KVDevice(device.getMac().toBytes()); |
| 198 | rcDevice.setLastSeenTime(device.getLastSeenTime()); |
| 199 | |
| 200 | for(SwitchPort sp : device.getAttachmentPoints()) { |
| 201 | byte[] portId = KVPort.getPortID(sp.getDpid(), sp.getNumber()); |
| 202 | rcDevice.addPortId(portId); |
| 203 | } |
| 204 | |
| 205 | for(InetAddress addr : device.getIpAddresses()) { |
| 206 | //It assume only one ip on a device now. |
| 207 | rcDevice.setIp(InetAddresses.coerceToInteger(addr)); |
| 208 | } |
| 209 | |
| 210 | rcDevice.forceCreate(); |
| 211 | |
| 212 | return true; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 213 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 214 | |
Pavlin Radoslavov | ca52907 | 2014-02-19 14:07:52 -0800 | [diff] [blame] | 215 | /** |
| 216 | * Remove a device from the database. |
| 217 | * |
| 218 | * @param device the device to remove. |
| 219 | * @return true on success, otherwise false. |
| 220 | */ |
| 221 | public boolean removeDevice(DeviceEvent device) { |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 222 | log.debug("Removing device into DB. mac {}", device.getMac()); |
| 223 | |
| 224 | KVDevice rcDevice = new KVDevice(device.getMac().toBytes()); |
| 225 | rcDevice.forceDelete(); |
| 226 | |
| 227 | return true; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 228 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 229 | } |