Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -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.ofcontroller.flowmanager; |
| 6 | |
| 7 | import com.tinkerpop.blueprints.Direction; |
| 8 | import com.tinkerpop.blueprints.Vertex; |
| 9 | import com.tinkerpop.blueprints.Edge; |
| 10 | import java.util.ArrayList; |
| 11 | import java.util.Collection; |
| 12 | import java.util.HashMap; |
| 13 | import java.util.Iterator; |
| 14 | import java.util.Map; |
| 15 | import net.onrc.onos.graph.DBOperation; |
| 16 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IBaseObject; |
| 17 | import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph; |
| 18 | import com.tinkerpop.blueprints.impls.ramcloud.RamCloudVertex; |
| 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 22 | public class FlowEntity implements FlowEntityManager { |
| 23 | private String primaryKey; |
Nick Karanatsios | f9cd37c | 2014-01-15 10:24:39 -0800 | [diff] [blame] | 24 | // TODO: Should remove since not implemented. |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 25 | private Class<?> hasMany; |
| 26 | private Collection<?> many = new ArrayList<>(); |
| 27 | private Map<String, Object> properties; |
| 28 | private Map<String, Map<String, Object>> operations = new HashMap<>(); |
| 29 | private ArrayList<Object> children = new ArrayList<>(); |
| 30 | private ArrayList<Object> edges = new ArrayList<>(); |
| 31 | private int opCount; |
| 32 | public Direction dir; |
| 33 | |
| 34 | public FlowEntity() { |
| 35 | opCount = 0; |
| 36 | } |
| 37 | |
| 38 | private class EntityEdge { |
| 39 | private Object src; |
| 40 | private Object dst; |
| 41 | private Direction dir; |
| 42 | private String label; |
| 43 | private DBOperationType op; |
| 44 | |
| 45 | public EntityEdge(Object src, Object dst, DBOperationType op, Direction dir, String label) { |
| 46 | this.src = src; |
| 47 | this.dst = dst; |
| 48 | this.dir = dir; |
| 49 | this.label = label; |
| 50 | this.op = op; |
| 51 | } |
| 52 | |
| 53 | public EntityEdge(Object src, Object dst, String label) { |
| 54 | this.src = src; |
| 55 | this.dst = dst; |
| 56 | this.label = label; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public String toString() { |
| 61 | return "EntityEdge: " + src + " " + dst + " " + label; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private class RamCloudEdgeEntity implements Edge { |
| 66 | private Vertex src; |
| 67 | private Vertex dst; |
| 68 | private Direction direction; |
| 69 | private String label; |
| 70 | |
| 71 | public RamCloudEdgeEntity(Vertex src, Vertex dst, Direction direction, String label) { |
| 72 | this.src = src; |
| 73 | this.dst = dst; |
| 74 | this.direction = direction; |
| 75 | this.label = label; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Vertex getVertex(com.tinkerpop.blueprints.Direction dir) throws IllegalArgumentException { |
| 80 | if (dir == Direction.IN) { |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 81 | return dst; |
| 82 | } else if (dir == Direction.OUT) { |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 83 | return src; |
| 84 | } |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public String getLabel() { |
| 90 | return this.label; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public <T> T getProperty(String key) { |
| 95 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public Set<String> getPropertyKeys() { |
| 100 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void setProperty(String key, Object value) { |
| 105 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public <T> T removeProperty(String key) { |
| 110 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void remove() { |
| 115 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Object getId() { |
| 120 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void setPrimaryKey(String key) { |
| 127 | primaryKey = key; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String getPrimaryKey() { |
| 132 | return primaryKey; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void hasMany(Class<?> cClass) { |
| 137 | hasMany = cClass; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void operationBegin(String opName) { |
| 142 | properties = new HashMap<>(); |
| 143 | operations.put(getOpKey(opName), properties); |
| 144 | opCount++; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void operationEnd(String opName) { |
Nick Karanatsios | f9cd37c | 2014-01-15 10:24:39 -0800 | [diff] [blame] | 149 | // TODO: This method is implemented in case we need to reset any variables. |
| 150 | /* |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 151 | String opKey = getOpKey(opName); |
| 152 | if (operations.containsKey(opKey)) { |
| 153 | System.out.println(operations); |
| 154 | } |
Nick Karanatsios | f9cd37c | 2014-01-15 10:24:39 -0800 | [diff] [blame] | 155 | */ |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | private String getOpKey(String opName) { |
| 160 | return opName + new Integer(opCount).toString(); |
| 161 | |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public void setProperty(String propertyName, Object value) { |
| 166 | properties.put(propertyName, value); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public FlowEntityManager append(Object entity) { |
| 171 | children.add(entity); |
| 172 | return this; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Object getProperty(String propertyName) { |
| 177 | if (properties.containsKey(propertyName)) { |
| 178 | return properties.get(propertyName); |
| 179 | } |
| 180 | return null; |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void persist(DBOperation dbHandler) { |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 185 | // get a hold of all the flow entries for the current flowpath. |
| 186 | if (children.size() > 0) { |
| 187 | int noOfChildren = children.size(); |
| 188 | if (noOfChildren > 0) { |
| 189 | // construct a list of null ids for creating vertices for all |
| 190 | // flow entries. |
Nick Karanatsios | b032ec8 | 2014-01-14 23:33:40 -0800 | [diff] [blame] | 191 | ArrayList<Object> ids = new ArrayList<>(noOfChildren); |
| 192 | // set properties |
| 193 | Map<RamCloudVertex, Map<String, Object>> propertiesToSet = new HashMap<>(); |
| 194 | |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 195 | RamCloudGraph graph = (RamCloudGraph)dbHandler.getDBConnection().getFramedGraph().getBaseGraph(); |
| 196 | for (int i = 0; i < noOfChildren; i++) { |
Nick Karanatsios | b032ec8 | 2014-01-14 23:33:40 -0800 | [diff] [blame] | 197 | ids.add(null); |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 198 | } |
Nick Karanatsios | b032ec8 | 2014-01-14 23:33:40 -0800 | [diff] [blame] | 199 | List<RamCloudVertex> addedVertices = graph.addVertices(ids); |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 200 | //Iterable<Vertex> vertices = dbHandler.setVertices(ids); |
| 201 | //Iterator vi = vertices.iterator(); |
| 202 | // get source and destination edge match vertex v construct list |
| 203 | // of edges |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 204 | ArrayList<Edge> edgesToSet = new ArrayList<>(); |
| 205 | for (int i = 0; i < noOfChildren; i++) { |
| 206 | FlowEntity childEntity = (FlowEntity)children.get(i); |
Nick Karanatsios | b032ec8 | 2014-01-14 23:33:40 -0800 | [diff] [blame] | 207 | Vertex srcVertex = addedVertices.get(i); |
| 208 | propertiesToSet.put((RamCloudVertex)srcVertex, childEntity.properties); |
Nick Karanatsios | b032ec8 | 2014-01-14 23:33:40 -0800 | [diff] [blame] | 209 | |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 210 | if (srcVertex == null) continue; |
| 211 | for (int j = 0; j < childEntity.edges.size(); j++) { |
| 212 | EntityEdge edge = (EntityEdge) childEntity.edges.get(j); |
Yoshi Muroi | 5804ce9 | 2014-02-08 03:58:04 -0800 | [diff] [blame^] | 213 | if (edge !=null) { |
| 214 | edgesToSet.add(new RamCloudEdgeEntity(srcVertex, ((IBaseObject) edge.dst).asVertex(), edge.dir, edge.label)); |
| 215 | } |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | graph.addEdges(edgesToSet); |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 219 | graph.setProperties(propertiesToSet); |
| 220 | } |
| 221 | } |
Nick Karanatsios | 758df8d | 2014-01-14 22:16:32 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | private Vertex getVertexEdge(Iterator vi, int idx) { |
| 225 | int i = 0; |
| 226 | while (vi.hasNext()) { |
| 227 | Vertex v = (Vertex)vi.next(); |
| 228 | if (i == idx) { |
| 229 | return v; |
| 230 | } |
| 231 | i++; |
| 232 | } |
| 233 | return null; |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public Map<String, Object> getProperties() { |
| 238 | return properties; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void addEdge(Object dst, Direction dir, String label) { |
| 243 | edges.add(new EntityEdge(this, dst, DBOperationType.ADD, dir, label)); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public void removeEdge(Object src, Object dst, Direction dir, String label) { |
| 248 | edges.add(new EntityEdge(src, dst, DBOperationType.REMOVE, dir, label)); |
| 249 | } |
| 250 | } |