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 | */ |
| 35 | public class SouthboundNetworkGraph { |
| 36 | private static final Logger log = LoggerFactory.getLogger(SouthboundNetworkGraph.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 | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 42 | public SouthboundNetworkGraph(NetworkGraphImpl graph) { |
| 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); |
| 136 | rcSwitch.addPortId(rcPort.getId()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 137 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 138 | writeObject(rcPort); |
| 139 | writeObject(rcSwitch); |
| 140 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 141 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 142 | public void deactivatePort(Port port) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 143 | log.debug("Deactivating port {}", port); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 144 | RCPort rcPort = new RCPort(port.getSwitch().getDpid(), (long)port.getNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 145 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 146 | for (int i = 0; i < NUM_RETRIES; i++) { |
| 147 | try { |
| 148 | rcPort.read(); |
| 149 | } catch (ObjectDoesntExistException e) { |
| 150 | // oh well, we were deactivating anyway |
| 151 | log.warn("Trying to deactivate a port that doesn't exist: {}", port); |
| 152 | return; |
| 153 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 154 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 155 | rcPort.setStatus(RCPort.STATUS.INACTIVE); |
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 | try { |
| 158 | rcPort.update(); |
| 159 | break; |
| 160 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 161 | // retry |
| 162 | } |
| 163 | } |
| 164 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 165 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 166 | public void addLink(Link link) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 167 | log.debug("Adding link {}", link); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 168 | RCLink rcLink = new RCLink(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber(), |
| 169 | link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 170 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 171 | RCPort rcSrcPort = new RCPort(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber()); |
| 172 | RCPort rcDstPort = new RCPort(link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 173 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 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 {}", link); |
| 185 | return; |
| 186 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 187 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 188 | rcSrcPort.addLinkId(rcLink.getId()); |
| 189 | rcDstPort.addLinkId(rcLink.getId()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 190 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 191 | rcLink.setStatus(RCLink.STATUS.ACTIVE); |
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 | 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 | } |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 203 | |
| 204 | // Publish event to in-memory cache |
| 205 | graph.addLink(link); |
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 | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 208 | public void removeLink(Link link) { |
Jonathan Hart | 4b5bbb5 | 2014-02-06 10:09:31 -0800 | [diff] [blame] | 209 | log.debug("Removing link {}", link); |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 210 | RCLink rcLink = new RCLink(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber(), |
| 211 | link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 212 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 213 | RCPort rcSrcPort = new RCPort(link.getSourceSwitchDpid(), (long)link.getSourcePortNumber()); |
| 214 | RCPort rcDstPort = new RCPort(link.getDestinationSwitchDpid(), (long)link.getDestinationPortNumber()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 215 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 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 {}", link, e); |
| 223 | return; |
| 224 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 225 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 226 | rcSrcPort.removeLinkId(rcLink.getId()); |
| 227 | rcDstPort.removeLinkId(rcLink.getId()); |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 228 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 229 | try { |
| 230 | rcSrcPort.update(); |
| 231 | rcDstPort.update(); |
| 232 | rcLink.delete(); |
| 233 | } catch (ObjectDoesntExistException e) { |
| 234 | log.error("Remove link failed {}", link, e); |
| 235 | return; |
| 236 | } catch (WrongVersionException e) { |
| 237 | // retry |
| 238 | } |
| 239 | } |
| 240 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 241 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 242 | public void updateDevice(Device device) { |
| 243 | // TODO implement |
| 244 | } |
Yuta HIGUCHI | b4335ad | 2014-02-05 09:56:07 -0800 | [diff] [blame] | 245 | |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 246 | public void removeDevice(Device device) { |
| 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 | } |