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