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 | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 8 | import net.onrc.onos.datastore.topology.RCLink; |
| 9 | import net.onrc.onos.datastore.topology.RCPort; |
| 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; |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 39 | |
| 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 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 46 | public void addSwitch(Switch 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 | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 58 | for (Port port : sw.getPorts()) { |
| 59 | RCPort rcPort = new RCPort(sw.getDpid(), (long)port.getNumber()); |
| 60 | rcPort.setStatus(RCPort.STATUS.ACTIVE); |
| 61 | rcSwitch.addPortId(rcPort.getId()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 62 | |
| 63 | groupOp.add(WriteOp.ForceCreate(rcPort)); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 64 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 65 | |
| 66 | boolean failed = RCObject.multiWrite( groupOp ); |
| 67 | |
| 68 | if ( failed ) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 69 | log.error("Adding Switch {} and its ports failed.", sw.getDpid()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 70 | for ( WriteOp op : groupOp ) { |
| 71 | log.debug("Operation:{} for {} - Result:{}", op.getOp(), op.getObject(), op.getStatus() ); |
| 72 | |
| 73 | // If we changed the operation from ForceCreate to |
| 74 | // Conditional operation (Create/Update) then we should retry here. |
| 75 | } |
| 76 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 77 | else { |
| 78 | // Publish event to the in-memory cache |
| 79 | graph.addSwitch(sw); |
| 80 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 81 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 82 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 83 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 84 | public void deactivateSwitch(Switch sw) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 85 | log.debug("Deactivating switch {}", sw); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 86 | RCSwitch rcSwitch = new RCSwitch(sw.getDpid()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 87 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 88 | List<RCObject> objectsToDeactive = new ArrayList<RCObject>(); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 89 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 90 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 91 | try { |
| 92 | rcSwitch.read(); |
| 93 | rcSwitch.setStatus(RCSwitch.STATUS.INACTIVE); |
| 94 | objectsToDeactive.add(rcSwitch); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 95 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 96 | for (Port p : sw.getPorts()) { |
| 97 | RCPort rcPort = new RCPort(sw.getDpid(), (long)p.getNumber()); |
| 98 | rcPort.read(); |
| 99 | rcPort.setStatus(RCPort.STATUS.INACTIVE); |
| 100 | objectsToDeactive.add(rcPort); |
| 101 | } |
| 102 | } catch (ObjectDoesntExistException e) { |
| 103 | log.warn("Trying to deactivate an object that doesn't exist", e); |
| 104 | // We don't care to much if the object wasn't there, it's |
| 105 | // being deactivated anyway |
| 106 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 107 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 108 | try { |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 109 | for (RCObject rcObject : objectsToDeactive) { |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 110 | rcObject.update(); |
| 111 | } |
| 112 | break; |
| 113 | } catch (ObjectDoesntExistException e) { |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 114 | // Unlikely, and we don't care anyway. |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 115 | // TODO But, this will cause everything else to fail |
| 116 | log.warn("Trying to deactivate object that doesn't exist", e); |
| 117 | } catch (WrongVersionException e) { |
| 118 | // Need to re-read and retry |
| 119 | } |
| 120 | } |
| 121 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 122 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 123 | public void addPort(Switch sw, Port port) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 124 | log.debug("Adding port {}", port); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 125 | RCSwitch rcSwitch = new RCSwitch(sw.getDpid()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 126 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 127 | try { |
| 128 | rcSwitch.read(); |
| 129 | } catch (ObjectDoesntExistException e) { |
| 130 | log.warn("Add port failed because switch {} doesn't exist", sw.getDpid(), e); |
| 131 | return; |
| 132 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 133 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 134 | RCPort rcPort = new RCPort(port.getSwitch().getDpid(), (long)port.getNumber()); |
| 135 | rcPort.setStatus(RCPort.STATUS.ACTIVE); |
Jonathan Hart | 891d050 | 2014-02-10 10:04:08 -0800 | [diff] [blame] | 136 | // TODO add description into RCPort |
| 137 | //rcPort.setDescription(port.getDescription()); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 138 | rcSwitch.addPortId(rcPort.getId()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 139 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 140 | writeObject(rcPort); |
| 141 | writeObject(rcSwitch); |
| 142 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 143 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 144 | public void deactivatePort(Port port) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 145 | log.debug("Deactivating port {}", port); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 146 | RCPort rcPort = new RCPort(port.getSwitch().getDpid(), (long)port.getNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 147 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 148 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 149 | try { |
| 150 | rcPort.read(); |
| 151 | } catch (ObjectDoesntExistException e) { |
| 152 | // oh well, we were deactivating anyway |
| 153 | log.warn("Trying to deactivate a port that doesn't exist: {}", port); |
| 154 | return; |
| 155 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 156 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 157 | rcPort.setStatus(RCPort.STATUS.INACTIVE); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 158 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 159 | try { |
| 160 | rcPort.update(); |
| 161 | break; |
| 162 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 163 | // retry |
| 164 | } |
| 165 | } |
| 166 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 167 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 168 | public void addLink(Link link) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 169 | log.debug("Adding link {}", link); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 170 | RCLink rcLink = new RCLink(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber(), |
| 171 | link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 172 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 173 | RCPort rcSrcPort = new RCPort(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber()); |
| 174 | RCPort rcDstPort = new RCPort(link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 175 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 176 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 177 | try { |
| 178 | rcSrcPort.read(); |
| 179 | rcDstPort.read(); |
| 180 | rcLink.create(); |
| 181 | } catch (ObjectDoesntExistException e) { |
| 182 | // port doesn't exist |
| 183 | log.error("Add link failed {}", link, e); |
| 184 | return; |
| 185 | } catch (ObjectExistsException e) { |
| 186 | log.debug("Link already exists {}", link); |
| 187 | return; |
| 188 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 189 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 190 | rcSrcPort.addLinkId(rcLink.getId()); |
| 191 | rcDstPort.addLinkId(rcLink.getId()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 192 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 193 | rcLink.setStatus(RCLink.STATUS.ACTIVE); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 194 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 195 | try { |
| 196 | rcLink.update(); |
| 197 | rcSrcPort.update(); |
| 198 | rcDstPort.update(); |
| 199 | break; |
| 200 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 201 | log.debug(" ", e); |
| 202 | // retry |
| 203 | } |
| 204 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 205 | |
| 206 | // Publish event to in-memory cache |
| 207 | graph.addLink(link); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 208 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 209 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 210 | public void removeLink(Link link) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 211 | log.debug("Removing link {}", link); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 212 | RCLink rcLink = new RCLink(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber(), |
| 213 | link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 214 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 215 | RCPort rcSrcPort = new RCPort(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber()); |
| 216 | RCPort rcDstPort = new RCPort(link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 217 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 218 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 219 | try { |
| 220 | rcSrcPort.read(); |
| 221 | rcDstPort.read(); |
| 222 | rcLink.read(); |
| 223 | } catch (ObjectDoesntExistException e) { |
| 224 | log.error("Remove link failed {}", link, e); |
| 225 | return; |
| 226 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 227 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 228 | rcSrcPort.removeLinkId(rcLink.getId()); |
| 229 | rcDstPort.removeLinkId(rcLink.getId()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 230 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 231 | try { |
| 232 | rcSrcPort.update(); |
| 233 | rcDstPort.update(); |
| 234 | rcLink.delete(); |
| 235 | } catch (ObjectDoesntExistException e) { |
| 236 | log.error("Remove link failed {}", link, e); |
| 237 | return; |
| 238 | } catch (WrongVersionException e) { |
| 239 | // retry |
| 240 | } |
| 241 | } |
| 242 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 243 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 244 | public void updateDevice(Device device) { |
| 245 | // TODO implement |
| 246 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 247 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 248 | public void removeDevice(Device device) { |
| 249 | // TODO implement |
| 250 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 251 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 252 | // TODO what happens if this fails? why could it fail? |
| 253 | private void writeObject(RCObject object) { |
| 254 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 255 | try { |
| 256 | object.create(); |
| 257 | } catch (ObjectExistsException e) { |
| 258 | try { |
| 259 | object.read(); |
| 260 | } catch (ObjectDoesntExistException e1) { |
| 261 | // TODO Auto-generated catch block |
| 262 | log.error(" ", e); |
| 263 | return; |
| 264 | } |
| 265 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 266 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 267 | try { |
| 268 | // TODO check API for writing without caring what's there |
| 269 | object.update(); |
| 270 | break; |
| 271 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 272 | log.debug(" ", e); |
| 273 | // re-read and retry |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |