blob: f4d2dc95c1fc48354b137f62787434c961989234 [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.
196 //ArrayList<Object> ids = new ArrayList<>(noOfChildren);
197 List<RamCloudVertex> addedVertices = new ArrayList<>();
198 RamCloudGraph graph = (RamCloudGraph)dbHandler.getDBConnection().getFramedGraph().getBaseGraph();
199 for (int i = 0; i < noOfChildren; i++) {
200 // ids.add(null);
201 addedVertices.add((RamCloudVertex) graph.addVertex(null));
202 }
203 // List<RamCloudVertex> addedVertices = graph.addVertices(ids);
204 System.out.println("Added vertices " + addedVertices);
205 // call setVertices()
206 //Iterable<Vertex> vertices = dbHandler.setVertices(ids);
207 //Iterator vi = vertices.iterator();
208 // get source and destination edge match vertex v construct list
209 // of edges
210
211 ArrayList<Edge> edgesToSet = new ArrayList<>();
212 for (int i = 0; i < noOfChildren; i++) {
213 FlowEntity childEntity = (FlowEntity)children.get(i);
214 //Vertex srcVertex = getVertexEdge(vi, i);
215 Vertex srcVertex = addedVertices.get(i);
216 if (srcVertex == null) continue;
217 for (int j = 0; j < childEntity.edges.size(); j++) {
218 EntityEdge edge = (EntityEdge) childEntity.edges.get(j);
219 edgesToSet.add(new RamCloudEdgeEntity(srcVertex, ((IBaseObject) edge.dst).asVertex(), edge.dir, edge.label));
220 }
221 }
222 graph.addEdges(edgesToSet);
223 ArrayList<Map<Vertex, Map<String, Object>>> allProperties = new ArrayList<>();
224 // set properties
225 Map<RamCloudVertex, Map<String, Object>> propertiesToSet = new HashMap<>();
226 for (int i = 0; i < noOfChildren; i++) {
227 FlowEntity childEntity = (FlowEntity)children.get(i);
228 propertiesToSet.put((RamCloudVertex)addedVertices.get(i), childEntity.properties);
229 }
230 graph.setProperties(propertiesToSet);
231 }
232 }
233 for (int i = 0; i < children.size(); i++) {
234 FlowEntityManager entity = (FlowEntityManager)children.get(i);
235 System.out.println(entity.getProperties());
236 }
237 }
238
239 private Vertex getVertexEdge(Iterator vi, int idx) {
240 int i = 0;
241 while (vi.hasNext()) {
242 Vertex v = (Vertex)vi.next();
243 if (i == idx) {
244 return v;
245 }
246 i++;
247 }
248 return null;
249 }
250
251 @Override
252 public Map<String, Object> getProperties() {
253 return properties;
254 }
255
256 @Override
257 public void addEdge(Object dst, Direction dir, String label) {
258 edges.add(new EntityEdge(this, dst, DBOperationType.ADD, dir, label));
259 }
260
261 @Override
262 public void removeEdge(Object src, Object dst, Direction dir, String label) {
263 edges.add(new EntityEdge(src, dst, DBOperationType.REMOVE, dir, label));
264 }
265}