Merge "Add verigraph code base"
[parser.git] / verigraph / src / main / java / it / polito / escape / verify / test / Scalability.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9
10 package it.polito.escape.verify.test;
11
12 import java.sql.Timestamp;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.ObjectMapper;
20 import com.fasterxml.jackson.databind.node.ArrayNode;
21 import com.fasterxml.jackson.databind.node.ObjectNode;
22
23 import it.polito.escape.verify.client.VerifyClient;
24 import it.polito.escape.verify.client.VerifyClientException;
25 import it.polito.escape.verify.model.Configuration;
26 import it.polito.escape.verify.model.Graph;
27 import it.polito.escape.verify.model.Neighbour;
28 import it.polito.escape.verify.model.Node;
29 import it.polito.escape.verify.model.Verification;
30
31 public class Scalability {
32
33         private VerifyClient client = new VerifyClient("http://localhost:8080/verify/api");
34
35         public static void main(String[] args) throws VerifyClientException {
36                 Scalability s = new Scalability();
37
38                 reachabilityTest(s, 10);
39
40                 isolationTest(s,1500);
41                 traversalTest(s,600);
42         }
43
44         private static void reachabilityTest(Scalability s, int n) throws VerifyClientException {
45                 System.out.printf("Reachability test with N=" + n + ": ");
46                 printTimestamp();
47                 Graph graph = generateNatScenario(n);
48                 Graph createdGraph = s.client.createGraph(graph).readEntity(Graph.class);
49                 Verification result = s.client.getReachability(createdGraph.getId(), "client", "server");
50                 System.out.println("Test returned " + result.getResult());
51                 System.out.printf("Finished reachability test with N=" + n + ": ");
52                 printTimestamp();
53                 System.out.println();
54         }
55
56         private static void isolationTest(Scalability s, int n) throws VerifyClientException {
57                 System.out.printf("Isolation test with N=" + n + ": ");
58                 printTimestamp();
59                 Graph graph = generateNatScenario(n);
60                 Graph createdGraph = s.client.createGraph(graph).readEntity(Graph.class);
61                 Verification result = s.client.getIsolation(createdGraph.getId(), "client", "server", "firewall");
62                 System.out.println("Test returned " + result.getResult());
63                 System.out.printf("Finished isolation test with N=" + n + ": ");
64                 printTimestamp();
65                 System.out.println();
66         }
67
68         private static void traversalTest(Scalability s, int n) throws VerifyClientException {
69                 System.out.printf("Traversal test with N=" + n + ": ");
70                 printTimestamp();
71                 Graph graph = generateNatScenario(n);
72                 Graph createdGraph = s.client.createGraph(graph).readEntity(Graph.class);
73                 Verification result = s.client.getTraversal(createdGraph.getId(), "client", "server", "firewall");
74                 System.out.println("Test returned " + result.getResult());
75 //              System.out.println("Result explanation: " + result.getComment());
76                 System.out.printf("Finished traversal test with N=" + n + ": ");
77                 printTimestamp();
78                 System.out.println();
79         }
80
81         private static void printTimestamp() {
82                 java.util.Date date= new java.util.Date();
83                 System.out.println(new Timestamp(date.getTime()));
84         }
85
86         private static Graph generateNatScenario(int n) {
87                 List<Node> nodes = new ArrayList<Node>();
88
89                 Node client = new Node();
90                 client.setName("client");
91                 client.setFunctional_type("endhost");
92                 ArrayNode clientConfigArray = new ObjectMapper().createArrayNode();
93                 JsonNode clientConfig = new ObjectMapper().createObjectNode();
94                 ((ObjectNode)clientConfig).put("url", "www.facebook.com");
95                 ((ObjectNode)clientConfig).put("body", "word");
96                 ((ObjectNode)clientConfig).put("destination","server");
97                 ((ObjectNode)clientConfig).put("protocol", "HTTP_REQUEST");
98                 clientConfigArray.add(clientConfig);
99                 client.setConfiguration(new Configuration(client.getName(),"", clientConfigArray));
100
101                 Map<Long, Neighbour> clientNeighbours = new HashMap<Long, Neighbour>();
102                 clientNeighbours.put(1L, new Neighbour(1L, "nat1"));
103                 client.setNeighbours(clientNeighbours );
104                 //add client to list
105                 nodes.add(client);
106
107                 for(int i=0; i< n;i++){
108                         Node nat = new Node();
109                         nat.setId(i+1);
110                         nat.setName("nat" + (i+1));
111                         nat.setFunctional_type("nat");
112                         ArrayNode configArray = new ObjectMapper().createArrayNode();
113
114                         Map<Long, Neighbour> natNeighbours = new HashMap<Long, Neighbour>();
115
116                         //set left neighbour for each node except the first
117                         if(nat.getId() != 1){
118                                 natNeighbours.put(1L, new Neighbour(1L, "nat" + i));
119                                 configArray.add("client");
120                                 for (int j=1; j <= i; j++){
121                                         configArray.add("nat" + j);
122                                 }
123                         }
124                         //first nat: set only client as neighbour and natted node
125                         else{
126                                 natNeighbours.put(1L, new Neighbour(1L, "client"));
127                                 configArray.add("client");
128                         }
129                         //set right neighbour for each node except the last
130                         if(nat.getId() != n){
131                                 natNeighbours.put(2L, new Neighbour(1L, "nat" + (i+2)));
132                         }
133                         //last nat: set server as neighbour
134                         else{
135                                 natNeighbours.put(2L, new Neighbour(1L, "server"));
136                         }
137
138                         nat.setNeighbours(natNeighbours);
139                         nat.setConfiguration(new Configuration(nat.getName(),"", configArray));
140                         
141                         //add nat to list
142                         nodes.add(nat);
143                 }
144
145                 Node server = new Node();
146                 server.setName("server");
147                 server.setFunctional_type("webserver");
148                 ArrayNode serverConfigArray = new ObjectMapper().createArrayNode();
149                 server.setConfiguration(new Configuration(server.getName(),"", serverConfigArray));
150
151                 Map<Long, Neighbour> serverNeighbours = new HashMap<Long, Neighbour>();
152                 serverNeighbours.put(1L, new Neighbour(1L, "nat" + (n)));
153                 server.setNeighbours(serverNeighbours );
154
155                 //add server to list
156                 nodes.add(server);
157
158                 //create graph
159                 Graph g = new Graph();
160                 Map<Long, Node> graphNodes = new HashMap<Long, Node>();
161                 long index = 1L;
162                 for (Node node : nodes){
163                         graphNodes.put(index, node);
164                         index++;
165                 }
166                 g.setNodes(graphNodes);
167
168                 return g;
169         }
170
171         private static Graph generateFirewallScenario(int n) {
172                 List<Node> nodes = new ArrayList<Node>();
173
174                 Node client = new Node();
175                 client.setName("client");
176                 client.setFunctional_type("endhost");
177                 ArrayNode clientConfigArray = new ObjectMapper().createArrayNode();
178                 JsonNode clientConfig = new ObjectMapper().createObjectNode();
179                 ((ObjectNode)clientConfig).put("url", "www.facebook.com");
180                 ((ObjectNode)clientConfig).put("body", "word");
181                 ((ObjectNode)clientConfig).put("destination","server");
182                 ((ObjectNode)clientConfig).put("protocol", "HTTP_REQUEST");
183                 clientConfigArray.add(clientConfig);
184                 client.setConfiguration(new Configuration(client.getName(),"", clientConfigArray));
185
186                 Map<Long, Neighbour> clientNeighbours = new HashMap<Long, Neighbour>();
187                 clientNeighbours.put(1L, new Neighbour(1L, "firewall1"));
188                 client.setNeighbours(clientNeighbours );
189                 //add client to list
190                 nodes.add(client);
191
192                 for(int i=0; i< n;i++){
193                         Node firewall = new Node();
194                         firewall.setId(i+1);
195                         firewall.setName("firewall" + (i+1));
196                         firewall.setFunctional_type("firewall");
197                         ArrayNode configArray = new ObjectMapper().createArrayNode();
198
199                         Map<Long, Neighbour> natNeighbours = new HashMap<Long, Neighbour>();
200
201                         //set left neighbour for each node except the first
202                         if(firewall.getId() != 1){
203                                 natNeighbours.put(1L, new Neighbour(1L, "firewall" + i));
204                         }
205                         //first firewall: set only client as neighbour and natted node
206                         else{
207                                 natNeighbours.put(1L, new Neighbour(1L, "client"));
208                         }
209                         //set right neighbour for each node except the last
210                         if(firewall.getId() != n){
211                                 natNeighbours.put(2L, new Neighbour(1L, "firewall" + (i+2)));
212                         }
213                         //last firewall: set server as neighbour
214                         else{
215                                 natNeighbours.put(2L, new Neighbour(1L, "server"));
216                         }
217
218                         firewall.setNeighbours(natNeighbours);
219                         firewall.setConfiguration(new Configuration(firewall.getName(),"", configArray));
220
221                         //add nat to list
222                         nodes.add(firewall);
223                 }
224
225                 Node server = new Node();
226                 server.setName("server");
227                 server.setFunctional_type("webserver");
228                 ArrayNode serverConfigArray = new ObjectMapper().createArrayNode();
229                 server.setConfiguration(new Configuration(server.getName(),"", serverConfigArray));
230
231                 Map<Long, Neighbour> serverNeighbours = new HashMap<Long, Neighbour>();
232                 serverNeighbours.put(1L, new Neighbour(1L, "firewall" + (n)));
233                 server.setNeighbours(serverNeighbours );
234
235                 //add server to list
236                 nodes.add(server);
237
238                 //create graph
239                 Graph g = new Graph();
240                 Map<Long, Node> graphNodes = new HashMap<Long, Node>();
241                 long index = 1L;
242                 for (Node node : nodes){
243                         graphNodes.put(index, node);
244                         index++;
245                 }
246                 g.setNodes(graphNodes);
247
248                 return g;
249         }
250
251         private static Graph generateScenario(int n) {
252                 List<Node> nodes = new ArrayList<Node>();
253
254                 Node firewall = new Node();
255                 firewall.setName("firewall");
256                 firewall.setFunctional_type("firewall");
257                 ArrayNode firewallConfigArray = new ObjectMapper().createArrayNode();
258                 Map<Long, Neighbour> firewallNeighbours = new HashMap<Long, Neighbour>();
259
260                 for (int i=0; i < n; i++){
261                         if(i!=0){
262                                 JsonNode firewallEntry = new ObjectMapper().createObjectNode();
263                                 ((ObjectNode) firewallEntry).put("server", "client" + (i+1));
264                                 firewallConfigArray.add(firewallEntry);
265                         }
266                         firewallNeighbours.put(new Long(i+1), new Neighbour(new Long(i+1), "client" + (i+1)));
267                 }
268
269                 firewallNeighbours.put(new Long(n+1), new Neighbour(new Long(n+1), "server"));
270                 
271                 firewall.setConfiguration(new Configuration(firewall.getName(),"", firewallConfigArray));
272
273
274                 firewall.setNeighbours(firewallNeighbours );
275                 //add client to list
276                 nodes.add(firewall);
277
278                 for(int i=0; i< n;i++){
279                         Node client = new Node();
280                         client.setId(i+1);
281                         client.setName("client" + (i+1));
282                         client.setFunctional_type("endhost");
283                         ArrayNode clientConfigArray = new ObjectMapper().createArrayNode();
284                         JsonNode clientConfig = new ObjectMapper().createObjectNode();
285                         ((ObjectNode)clientConfig).put("url", "www.facebook.com");
286                         ((ObjectNode)clientConfig).put("body", "word");
287                         ((ObjectNode)clientConfig).put("destination","server");
288                         ((ObjectNode)clientConfig).put("protocol", "HTTP_REQUEST");
289                         clientConfigArray.add(clientConfig);
290
291                         Map<Long, Neighbour> clientNeighbours = new HashMap<Long, Neighbour>();
292
293                         clientNeighbours.put(1L, new Neighbour(1L, "firewall"));
294                                                                         
295                         client.setNeighbours(clientNeighbours);
296                         client.setConfiguration(new Configuration(client.getName(),"", clientConfigArray));
297
298                         //add client to list
299                         nodes.add(client);
300                 }
301
302                 Node server = new Node();
303                 server.setName("server");
304                 server.setFunctional_type("webserver");
305                 ArrayNode serverConfigArray = new ObjectMapper().createArrayNode();
306                 server.setConfiguration(new Configuration(server.getName(),"", serverConfigArray));
307
308                 Map<Long, Neighbour> serverNeighbours = new HashMap<Long, Neighbour>();
309                 serverNeighbours.put(1L, new Neighbour(1L, "firewall"));
310                 server.setNeighbours(serverNeighbours );
311
312                 //add server to list
313                 nodes.add(server);
314
315                 //create graph
316                 Graph g = new Graph();
317                 Map<Long, Node> graphNodes = new HashMap<Long, Node>();
318                 long index = 1L;
319                 for (Node node : nodes){
320                         graphNodes.put(index, node);
321                         index++;
322                 }
323                 g.setNodes(graphNodes);
324
325                 return g;
326         }
327
328         private static Graph generateDpiScenario(int n) {
329                 List<Node> nodes = new ArrayList<Node>();
330
331                 Node client = new Node();
332                 client.setName("client");
333                 client.setFunctional_type("endhost");
334                 ArrayNode clientConfigArray = new ObjectMapper().createArrayNode();
335                 JsonNode clientConfig = new ObjectMapper().createObjectNode();
336                 ((ObjectNode)clientConfig).put("url", "www.facebook.com");
337                 ((ObjectNode)clientConfig).put("body", "word");
338                 ((ObjectNode)clientConfig).put("destination","server");
339                 ((ObjectNode)clientConfig).put("protocol", "HTTP_REQUEST");
340                 clientConfigArray.add(clientConfig);
341                 client.setConfiguration(new Configuration(client.getName(),"", clientConfigArray));
342
343                 Map<Long, Neighbour> clientNeighbours = new HashMap<Long, Neighbour>();
344                 clientNeighbours.put(1L, new Neighbour(1L, "dpi1"));
345                 client.setNeighbours(clientNeighbours );
346                 //add client to list
347                 nodes.add(client);
348
349                 for(int i=0; i< n;i++){
350                         Node dpi = new Node();
351                         dpi.setId(i+1);
352                         dpi.setName("dpi" + (i+1));
353                         dpi.setFunctional_type("dpi");
354                         ArrayNode configArray = new ObjectMapper().createArrayNode();
355                         configArray.add("drug");
356
357                         Map<Long, Neighbour> natNeighbours = new HashMap<Long, Neighbour>();
358
359                         //set left neighbour for each node except the first
360                         if(dpi.getId() != 1){
361                                 natNeighbours.put(1L, new Neighbour(1L, "dpi" + i));
362                         }
363                         //first firewall: set only client as neighbour and natted node
364                         else{
365                                 natNeighbours.put(1L, new Neighbour(1L, "client"));
366                         }
367                         //set right neighbour for each node except the last
368                         if(dpi.getId() != n){
369                                 natNeighbours.put(2L, new Neighbour(1L, "dpi" + (i+2)));
370                         }
371                         //last firewall: set server as neighbour
372                         else{
373                                 natNeighbours.put(2L, new Neighbour(1L, "server"));
374                         }
375
376                         dpi.setNeighbours(natNeighbours);
377                         dpi.setConfiguration(new Configuration(dpi.getName(),"", configArray));
378
379                         //add nat to list
380                         nodes.add(dpi);
381                 }
382
383                 Node server = new Node();
384                 server.setName("server");
385                 server.setFunctional_type("webserver");
386                 ArrayNode serverConfigArray = new ObjectMapper().createArrayNode();
387                 server.setConfiguration(new Configuration(server.getName(),"", serverConfigArray));
388
389                 Map<Long, Neighbour> serverNeighbours = new HashMap<Long, Neighbour>();
390                 serverNeighbours.put(1L, new Neighbour(1L, "dpi" + (n)));
391                 server.setNeighbours(serverNeighbours );
392
393                 //add server to list
394                 nodes.add(server);
395
396                 //create graph
397                 Graph g = new Graph();
398                 Map<Long, Node> graphNodes = new HashMap<Long, Node>();
399                 long index = 1L;
400                 for (Node node : nodes){
401                         graphNodes.put(index, node);
402                         index++;
403                 }
404                 g.setNodes(graphNodes);
405
406                 return g;
407         }
408
409 }