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