yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package net.onrc.onos.graph; |
| 6 | |
| 7 | import com.thinkaurelius.titan.core.TitanGraph; |
| 8 | import com.tinkerpop.blueprints.Vertex; |
| 9 | import com.tinkerpop.frames.FramedGraph; |
| 10 | import com.tinkerpop.frames.structures.FramedVertexIterable; |
| 11 | import com.tinkerpop.gremlin.java.GremlinPipeline; |
| 12 | import java.util.ArrayList; |
| 13 | import java.util.Iterator; |
| 14 | import java.util.List; |
| 15 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects; |
| 16 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject; |
| 17 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 18 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 19 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 20 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 21 | import net.onrc.onos.ofcontroller.core.ISwitchStorage; |
| 22 | import net.onrc.onos.ofcontroller.util.FlowId; |
| 23 | |
| 24 | /** |
| 25 | * |
| 26 | * @author nickkaranatsios |
| 27 | */ |
| 28 | public abstract class DBOperation implements IDBOperation { |
| 29 | |
| 30 | protected DBConnection conn; |
| 31 | |
| 32 | @Override |
| 33 | public ISwitchObject searchActiveSwitch(String dpid) { |
| 34 | ISwitchObject sw = searchSwitch(dpid); |
| 35 | if ((sw != null) |
| 36 | && sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) { |
| 37 | return sw; |
| 38 | } |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public ISwitchObject newSwitch(final String dpid) { |
| 44 | ISwitchObject obj = (ISwitchObject) conn.getFramedGraph().addVertex(null, ISwitchObject.class); |
| 45 | if (obj != null) { |
| 46 | obj.setType("switch"); |
| 47 | obj.setDPID(dpid); |
| 48 | } |
| 49 | return obj; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Iterable<ISwitchObject> getAllSwitches() { |
| 54 | Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class); |
| 55 | return switches; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Iterable<ISwitchObject> getInactiveSwitches() { |
| 60 | Iterable<ISwitchObject> switches = conn.getFramedGraph().getVertices("type", "switch", ISwitchObject.class); |
| 61 | List<ISwitchObject> inactiveSwitches = new ArrayList<ISwitchObject>(); |
| 62 | |
| 63 | for (ISwitchObject sw : switches) { |
| 64 | if (sw.getState().equals(ISwitchStorage.SwitchState.INACTIVE.toString())) { |
| 65 | inactiveSwitches.add(sw); |
| 66 | } |
| 67 | } |
| 68 | return inactiveSwitches; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Iterable<INetMapTopologyObjects.IFlowEntry> getAllSwitchNotUpdatedFlowEntries() { |
| 73 | //TODO: Should use an enum for flow_switch_state |
| 74 | return conn.getFramedGraph().getVertices("switch_state", "FE_SWITCH_NOT_UPDATED", INetMapTopologyObjects.IFlowEntry.class); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void removeSwitch(ISwitchObject sw) { |
| 80 | conn.getFramedGraph().removeVertex(sw.asVertex()); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public IPortObject newPort(String dpid, Short portNum) { |
| 85 | IPortObject obj = (IPortObject) conn.getFramedGraph().addVertex(null, IPortObject.class); |
| 86 | if (obj != null) { |
| 87 | obj.setType("port"); |
| 88 | String id = dpid + portNum.toString(); |
| 89 | obj.setPortId(id); |
| 90 | obj.setNumber(portNum); |
| 91 | } |
| 92 | return obj; |
| 93 | } |
| 94 | |
| 95 | public IPortObject searchPort(String dpid, Short number, final FramedGraph fg) { |
| 96 | String id = dpid + number.toString(); |
| 97 | return (fg != null && fg.getVertices("port_id", id).iterator().hasNext()) |
| 98 | ? (IPortObject) fg.getVertices("port_id", id, IPortObject.class).iterator().next() : null; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public IDeviceObject newDevice() { |
| 104 | IDeviceObject obj = (IDeviceObject) conn.getFramedGraph().addVertex(null, IDeviceObject.class); |
| 105 | if (obj != null) { |
| 106 | obj.setType("device"); |
| 107 | } |
| 108 | return obj; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public IFlowPath newFlowPath() { |
| 113 | IFlowPath flowPath = (IFlowPath)conn.getFramedGraph().addVertex(null, IFlowPath.class); |
| 114 | if (flowPath != null) { |
| 115 | flowPath.setType("flow"); |
| 116 | } |
| 117 | return flowPath; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public IFlowPath getFlowPathByFlowEntry(INetMapTopologyObjects.IFlowEntry flowEntry) { |
| 122 | GremlinPipeline<Vertex, IFlowPath> pipe = new GremlinPipeline<Vertex, IFlowPath>(); |
| 123 | pipe.start(flowEntry.asVertex()); |
| 124 | pipe.out("flow"); |
| 125 | FramedVertexIterable<IFlowPath> r = new FramedVertexIterable(conn.getFramedGraph(), (Iterable) pipe, IFlowPath.class); |
| 126 | return r.iterator().hasNext() ? r.iterator().next() : null; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | protected ISwitchObject searchSwitch(final String dpid, final FramedGraph fg) { |
| 132 | return (fg != null && fg.getVertices("dpid", dpid).iterator().hasNext()) |
| 133 | ? (ISwitchObject) (fg.getVertices("dpid", dpid, ISwitchObject.class).iterator().next()) : null; |
| 134 | } |
| 135 | |
| 136 | protected Iterable<ISwitchObject> getActiveSwitches(final FramedGraph fg) { |
| 137 | Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class); |
| 138 | List<ISwitchObject> activeSwitches = new ArrayList<ISwitchObject>(); |
| 139 | |
| 140 | for (ISwitchObject sw : switches) { |
| 141 | if (sw.getState().equals(ISwitchStorage.SwitchState.ACTIVE.toString())) { |
| 142 | activeSwitches.add(sw); |
| 143 | } |
| 144 | } |
| 145 | return activeSwitches; |
| 146 | } |
| 147 | |
| 148 | protected Iterable<ISwitchObject> getAllSwitches(final FramedGraph fg) { |
| 149 | Iterable<ISwitchObject> switches = fg.getVertices("type", "switch", ISwitchObject.class); |
| 150 | return switches; |
| 151 | } |
| 152 | |
| 153 | protected IDeviceObject searchDevice(String macAddr, final FramedGraph fg) { |
| 154 | return (fg != null && fg.getVertices("dl_addr", macAddr).iterator().hasNext()) |
| 155 | ? (IDeviceObject) fg.getVertices("dl_addr", macAddr, IDeviceObject.class).iterator().next() : null; |
| 156 | |
| 157 | } |
| 158 | |
| 159 | protected IFlowPath searchFlowPath(final FlowId flowId, final FramedGraph fg) { |
| 160 | return fg.getVertices("flow_id", flowId.toString()).iterator().hasNext() |
| 161 | ? (IFlowPath) fg.getVertices("flow_id", flowId.toString(), |
| 162 | IFlowPath.class).iterator().next() : null; |
| 163 | } |
| 164 | |
| 165 | protected Iterable<IFlowPath> getAllFlowPaths(final FramedGraph fg) { |
| 166 | Iterable<IFlowPath> flowPaths = fg.getVertices("type", "flow", IFlowPath.class); |
| 167 | |
| 168 | List<IFlowPath> nonNullFlows = new ArrayList<IFlowPath>(); |
| 169 | |
| 170 | for (IFlowPath fp : flowPaths) { |
| 171 | if (fp.getFlowId() != null) { |
| 172 | nonNullFlows.add(fp); |
| 173 | } |
| 174 | } |
| 175 | return nonNullFlows; |
| 176 | } |
| 177 | |
| 178 | protected IFlowEntry newFlowEntry(final FramedGraph fg) { |
| 179 | IFlowEntry flowEntry = (IFlowEntry) fg.addVertex(null, IFlowEntry.class); |
| 180 | if (flowEntry != null) { |
| 181 | flowEntry.setType("flow_entry"); |
| 182 | } |
| 183 | return flowEntry; |
| 184 | } |
| 185 | |
| 186 | } |