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