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; |
| 5 | |
| 6 | import net.onrc.onos.datastore.RCObject; |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 7 | import net.onrc.onos.datastore.RCObject.WriteOp; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 8 | import net.onrc.onos.datastore.topology.RCLink; |
Jonathan Hart | 69864df | 2014-02-13 10:44:12 -0800 | [diff] [blame] | 9 | import net.onrc.onos.datastore.topology.RCPort; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 10 | import net.onrc.onos.datastore.topology.RCSwitch; |
| 11 | |
| 12 | import org.slf4j.Logger; |
| 13 | import org.slf4j.LoggerFactory; |
| 14 | |
| 15 | import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException; |
| 16 | import edu.stanford.ramcloud.JRamCloud.ObjectExistsException; |
| 17 | import edu.stanford.ramcloud.JRamCloud.WrongVersionException; |
| 18 | |
| 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 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 38 | private static final int NUM_RETRIES = 10; |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 39 | |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 40 | private final NetworkGraphImpl graph; |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 41 | |
Jonathan Hart | fa01c24 | 2014-02-11 10:03:03 -0800 | [diff] [blame] | 42 | public NetworkGraphDatastore(NetworkGraphImpl graph) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 43 | this.graph = graph; |
| 44 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 45 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 46 | public void addSwitch(SwitchEvent sw) { |
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 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 50 | RCSwitch rcSwitch = new RCSwitch(sw.getDpid()); |
| 51 | rcSwitch.setStatus(RCSwitch.STATUS.ACTIVE); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 52 | |
| 53 | // XXX Is ForceCreating Switch on DB OK here? |
| 54 | // If ForceCreating, who ever is calling this method needs |
| 55 | // to assure that DPID is unique cluster-wide, etc. |
| 56 | groupOp.add(WriteOp.ForceCreate(rcSwitch)); |
| 57 | |
Jonathan Hart | 69864df | 2014-02-13 10:44:12 -0800 | [diff] [blame] | 58 | //for (Port port : sw.getPorts()) { |
| 59 | for (PortEvent portEvent : sw.getPorts()) { |
| 60 | RCPort rcPort = new RCPort(sw.getDpid(), portEvent.getNumber()); |
| 61 | rcPort.setStatus(RCPort.STATUS.ACTIVE); |
| 62 | //rcSwitch.addPortId(rcPort.getId()); |
| 63 | |
| 64 | groupOp.add(WriteOp.ForceCreate(rcPort)); |
| 65 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 66 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 67 | boolean failed = RCObject.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 | } |
| 78 | |
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 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 81 | public void deactivateSwitch(SwitchEvent sw) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 82 | log.debug("Deactivating switch {}", sw); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 83 | RCSwitch rcSwitch = new RCSwitch(sw.getDpid()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 84 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 85 | List<RCObject> objectsToDeactive = new ArrayList<RCObject>(); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 86 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 87 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 88 | try { |
| 89 | rcSwitch.read(); |
| 90 | rcSwitch.setStatus(RCSwitch.STATUS.INACTIVE); |
| 91 | objectsToDeactive.add(rcSwitch); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 92 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 93 | // for (Port p : sw.getPorts()) { |
| 94 | // RCPort rcPort = new RCPort(sw.getDpid(), (long)p.getNumber()); |
| 95 | // rcPort.read(); |
| 96 | // rcPort.setStatus(RCPort.STATUS.INACTIVE); |
| 97 | // objectsToDeactive.add(rcPort); |
| 98 | // } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 99 | } catch (ObjectDoesntExistException e) { |
| 100 | log.warn("Trying to deactivate an object that doesn't exist", e); |
| 101 | // We don't care to much if the object wasn't there, it's |
| 102 | // being deactivated anyway |
| 103 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 104 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 105 | try { |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 106 | for (RCObject rcObject : objectsToDeactive) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 107 | rcObject.update(); |
| 108 | } |
| 109 | break; |
| 110 | } catch (ObjectDoesntExistException e) { |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 111 | // Unlikely, and we don't care anyway. |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 112 | // TODO But, this will cause everything else to fail |
| 113 | log.warn("Trying to deactivate object that doesn't exist", e); |
| 114 | } catch (WrongVersionException e) { |
| 115 | // Need to re-read and retry |
| 116 | } |
| 117 | } |
| 118 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 119 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 120 | public void addPort(PortEvent port) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 121 | log.debug("Adding port {}", port); |
Jonathan Hart | 4c26327 | 2014-02-13 17:41:05 -0800 | [diff] [blame] | 122 | //RCSwitch rcSwitch = new RCSwitch(sw.getDpid()); |
| 123 | |
| 124 | //try { |
| 125 | //rcSwitch.read(); |
| 126 | //} catch (ObjectDoesntExistException e) { |
| 127 | //log.warn("Add port failed because switch {} doesn't exist", sw.getDpid(), e); |
| 128 | //return; |
| 129 | //} |
| 130 | |
| 131 | RCPort rcPort = new RCPort(port.getDpid(), port.getNumber()); |
| 132 | rcPort.setStatus(RCPort.STATUS.ACTIVE); |
| 133 | // TODO add description into RCPort |
| 134 | //rcPort.setDescription(port.getDescription()); |
| 135 | //rcSwitch.addPortId(rcPort.getId()); |
| 136 | |
| 137 | writeObject(rcPort); |
| 138 | //writeObject(rcSwitch); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 139 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 140 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 141 | public void deactivatePort(PortEvent port) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 142 | log.debug("Deactivating port {}", port); |
Jonathan Hart | 4c26327 | 2014-02-13 17:41:05 -0800 | [diff] [blame] | 143 | RCPort rcPort = new RCPort(port.getDpid(), port.getNumber()); |
| 144 | |
| 145 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 146 | try { |
| 147 | rcPort.read(); |
| 148 | } catch (ObjectDoesntExistException e) { |
| 149 | // oh well, we were deactivating anyway |
| 150 | log.warn("Trying to deactivate a port that doesn't exist: {}", port); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | rcPort.setStatus(RCPort.STATUS.INACTIVE); |
| 155 | |
| 156 | try { |
| 157 | rcPort.update(); |
| 158 | break; |
| 159 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 160 | // retry |
| 161 | } |
| 162 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 163 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 164 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 165 | public void addLink(LinkEvent linkEvent) { |
| 166 | log.debug("Adding link {}", linkEvent); |
| 167 | |
| 168 | RCLink rcLink = new RCLink(linkEvent.getSrc().getDpid(), linkEvent.getSrc().getNumber(), |
| 169 | linkEvent.getDst().getDpid(), linkEvent.getDst().getNumber()); |
| 170 | |
| 171 | //RCPort rcSrcPort = new RCPort(link.getSrc().getDpid(), link.getSrc().getNumber()); |
| 172 | //RCPort rcDstPort = new RCPort(link.getDst().getDpid(), link.getDst().getNumber()); |
| 173 | |
| 174 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 175 | try { |
| 176 | //rcSrcPort.read(); |
| 177 | //rcDstPort.read(); |
| 178 | rcLink.create(); |
| 179 | //} catch (ObjectDoesntExistException e) { |
| 180 | // port doesn't exist |
| 181 | //log.error("Add link failed {}", link, e); |
| 182 | //return; |
| 183 | } catch (ObjectExistsException e) { |
| 184 | log.debug("Link already exists {}", linkEvent); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | //rcSrcPort.addLinkId(rcLink.getId()); |
| 189 | //rcDstPort.addLinkId(rcLink.getId()); |
| 190 | |
| 191 | rcLink.setStatus(RCLink.STATUS.ACTIVE); |
| 192 | |
| 193 | try { |
| 194 | rcLink.update(); |
| 195 | //rcSrcPort.update(); |
| 196 | //rcDstPort.update(); |
| 197 | break; |
| 198 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 199 | log.debug(" ", e); |
| 200 | // retry |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Publish event to in-memory cache |
| 205 | graph.putLink(linkEvent); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 206 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 207 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 208 | public void removeLink(LinkEvent linkEvent) { |
| 209 | log.debug("Removing link {}", linkEvent); |
| 210 | RCLink rcLink = new RCLink(linkEvent.getSrc().getDpid(), linkEvent.getSrc().getNumber(), |
| 211 | linkEvent.getDst().getDpid(), linkEvent.getDst().getNumber()); |
| 212 | |
| 213 | //RCPort rcSrcPort = new RCPort(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber()); |
| 214 | //RCPort rcDstPort = new RCPort(link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
| 215 | |
| 216 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 217 | try { |
| 218 | //rcSrcPort.read(); |
| 219 | //rcDstPort.read(); |
| 220 | rcLink.read(); |
| 221 | } catch (ObjectDoesntExistException e) { |
| 222 | log.error("Remove link failed {}", linkEvent, e); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | //rcSrcPort.removeLinkId(rcLink.getId()); |
| 227 | //rcDstPort.removeLinkId(rcLink.getId()); |
| 228 | |
| 229 | try { |
| 230 | //rcSrcPort.update(); |
| 231 | //rcDstPort.update(); |
| 232 | rcLink.delete(); |
| 233 | } catch (ObjectDoesntExistException e) { |
| 234 | log.error("Remove link failed {}", linkEvent, e); |
| 235 | return; |
| 236 | } catch (WrongVersionException e) { |
| 237 | // retry |
| 238 | } |
| 239 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 240 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 241 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 242 | public void updateDevice(DeviceEvent device) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 243 | // TODO implement |
| 244 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 245 | |
Yuta HIGUCHI | cb95198 | 2014-02-11 13:31:44 -0800 | [diff] [blame] | 246 | public void removeDevice(DeviceEvent device) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 247 | // TODO implement |
| 248 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 249 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 250 | // TODO what happens if this fails? why could it fail? |
| 251 | private void writeObject(RCObject object) { |
| 252 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 253 | try { |
| 254 | object.create(); |
| 255 | } catch (ObjectExistsException e) { |
| 256 | try { |
| 257 | object.read(); |
| 258 | } catch (ObjectDoesntExistException e1) { |
| 259 | // TODO Auto-generated catch block |
| 260 | log.error(" ", e); |
| 261 | return; |
| 262 | } |
| 263 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 264 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 265 | try { |
| 266 | // TODO check API for writing without caring what's there |
| 267 | object.update(); |
| 268 | break; |
| 269 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 270 | log.debug(" ", e); |
| 271 | // re-read and retry |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |