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