Support TOSCA in verigraph (gRPC service)
[parser.git] / verigraph / src / it / polito / verigraph / grpc / test / GrpcTest.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 package it.polito.verigraph.grpc.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.fail;
14 import java.util.ArrayList;
15 import java.util.Comparator;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.TreeSet;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.FixMethodOrder;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.JUnit4;
27 import org.junit.runners.MethodSorters;
28 import it.polito.verigraph.grpc.ConfigurationGrpc;
29 import it.polito.verigraph.grpc.GraphGrpc;
30 import it.polito.verigraph.grpc.NeighbourGrpc;
31 import it.polito.verigraph.grpc.NewGraph;
32 import it.polito.verigraph.grpc.NewNeighbour;
33 import it.polito.verigraph.grpc.NewNode;
34 import it.polito.verigraph.grpc.NodeGrpc;
35 import it.polito.verigraph.grpc.client.Client;
36 import it.polito.verigraph.grpc.server.Service;
37
38 @RunWith(JUnit4.class)
39 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
40 public class GrpcTest {
41     private Service server;
42     private Client client;
43
44     @Before
45     public void setUpBeforeClass() throws Exception {
46         client = new Client("localhost" , 50051);
47         server = new Service(50051);
48
49         server.start();
50     }
51
52     @After
53     public void tearDown() throws Exception {
54         server.stop();
55         client.shutdown();
56     }
57
58     // method for comparing two non-null strings
59     private void compareString(String rs, String ts, String meaning) {
60         assertNotNull("NULL "+meaning, ts);
61         assertEquals("Wrong "+meaning, rs, ts);
62     }
63
64     public void deleteGraphs() {
65         for(GraphGrpc graph : client.getGraphs()){
66             client.deleteGraph(graph.getId());
67         }
68
69     }
70
71     @Test
72     public final void test1Load() throws Exception{
73         System.out.println("[DEBUG] test1Load starts");
74         deleteGraphs();
75         String funcType1 = "vpnaccess";
76         String funcType2 = "vpnexit";
77
78         // load an existing graph with 2 nodes
79         Map<String,String> map = new HashMap<String,String>();
80         map.put("vpnexit", "Node2");
81         ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
82         NodeGrpc node1 = Client.createNodeGrpc("Node1", funcType1, null, conf);
83         List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
84         NeighbourGrpc nb = Client.createNeighbourGrpc("Node1");
85         neighbours.add(nb);
86         map.clear();
87         map.put("vpnaccess", "Node1");
88         conf = Client.createConfigurationGrpc(map, null, null, null);
89         NodeGrpc node2 = Client.createNodeGrpc("Node2", funcType2, neighbours, conf);
90         List<NodeGrpc> nodes = new ArrayList<NodeGrpc>();
91         nodes.add(node1);
92         nodes.add(node2);
93         GraphGrpc graph = Client.createGraphGrpc(nodes);
94         //createGraph
95         graph = client.createGraph(graph).getGraph();
96
97         //getGraph
98         GraphGrpc retreivedGraph = client.getGraph(graph.getId());
99
100         assertNotNull("NULL Graph ",retreivedGraph);
101         assertEquals(graph.getId(), retreivedGraph.getId());
102
103         // check the name of first node and the id
104         compareString(retreivedGraph.getNodeList().get(0).getName(), graph.getNodeList().get(0).getName(), "node name");
105         assertEquals(retreivedGraph.getNodeList().get(0).getId(), graph.getNodeList().get(0).getId());
106
107         // check the name of second node and the id
108         compareString(retreivedGraph.getNodeList().get(1).getName(), graph.getNodeList().get(1).getName(), "node name");
109         assertEquals(retreivedGraph.getNodeList().get(1).getId(), graph.getNodeList().get(1).getId());
110
111         //updateGraph
112         GraphGrpc updatedGraph = GraphGrpc.newBuilder().build();
113         NewGraph response = client.updateGraph(graph.getId(),updatedGraph);
114
115         assertEquals(response.getSuccess(),true);
116     }
117
118     @Test
119     public final void test2LoadWithError() throws Exception{
120         System.out.println("[DEBUG] test2Load starts");
121         deleteGraphs();
122         // try to load a graph with node without functionalType
123         NodeGrpc node = null;
124         try{
125             node = Client.createNodeGrpc("Node1", null, null, null);
126             fail( "createNodeGrpc didn't throw when I expected it to" );
127         }
128         catch(Exception ex){
129         }
130         List<NodeGrpc> nodes = new ArrayList<NodeGrpc>();
131         if(node != null)
132             nodes.add(node);
133
134         GraphGrpc graph = Client.createGraphGrpc(nodes);
135         GraphGrpc g = client.createGraph(graph).getGraph();
136
137         GraphGrpc get_graph= client.getGraph(g.getId());
138         assertEquals(g.getErrorMessage(), get_graph.getErrorMessage());
139
140         //getGraphs
141         List<GraphGrpc> graph_list = client.getGraphs();
142         TreeSet<GraphGrpc> pts = new TreeSet<GraphGrpc>(new GraphGrpcComparator());
143         pts.addAll(graph_list);
144         Iterator<GraphGrpc> graphs = pts.iterator();
145
146         //assertEquals(graphs.next().getId(), g.getId());
147         if(graphs.hasNext())
148             assertEquals(graphs.next(), g);
149
150         //deleteGraph
151         boolean resp= client.deleteGraph(g.getId());
152         assertEquals(resp, true);
153
154         List<GraphGrpc> listGraphs = client.getGraphs();
155
156         assertEquals(listGraphs.size(), 0);
157         //assertEquals(listGraphs.get(0).getId(), 1);
158     }
159
160     @Test
161     public void test3Node() throws Exception {
162         System.out.println("[DEBUG] test3Load starts");
163         deleteGraphs();
164
165         NodeGrpc ufoundedGraph = NodeGrpc.newBuilder()
166                 .setErrorMessage("There is no Graph whose Id is '1'").build();
167
168         // graph not found in the server
169         NodeGrpc node = client.getNode(1, 1);//id not present
170
171         assertEquals(ufoundedGraph, node);
172
173         // graph found in the server, but first add it
174         NodeGrpc addedNode = Client.createNodeGrpc("Node4", "firewall", null, null);
175         GraphGrpc addedgraph = Client.createGraphGrpc(null);
176         NewGraph response_graph = client.createGraph(addedgraph);
177         NewNode response = client.createNode(addedNode, response_graph.getGraph().getId());
178         addedNode = response.getNode();
179         node = client.getNode(response_graph.getGraph().getId(), addedNode.getId());
180
181         assertEquals(addedNode.getId(), node.getId());
182
183         //updateNode
184         NodeGrpc updatedNode = Client.createNodeGrpc("Node9", "endhost", null, null);
185         response = client.updateNode(response_graph.getGraph().getId(), addedNode.getId(), updatedNode);
186
187         assertEquals(response.getSuccess(),true);
188
189         //configureNode
190         //this configuration is valid only on endhost!
191         Map<String,String> params = new HashMap<String,String>();
192         params.put("url", "www.facebook.com");
193         params.put("body", "word");
194         params.put("destination","server");
195         params.put("protocol", "HTTP_REQUEST");
196         ConfigurationGrpc configuration = Client.createConfigurationGrpc(params, null, null, null);
197
198         boolean status = client.configureNode(response_graph.getGraph().getId(), addedNode.getId(), configuration);
199
200         assertEquals(status,true);
201     }
202
203     @Test
204     public void test4Nodes() throws Exception {
205         System.out.println("[DEBUG] test4Load starts");
206         // setup
207         GraphGrpc graph = Client.createGraphGrpc(null);
208         //createGraph
209         graph = client.createGraph(graph).getGraph();
210
211         List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
212         NeighbourGrpc nb = Client.createNeighbourGrpc("Node6");
213         neighbours.add(nb);
214         NodeGrpc n1 = Client.createNodeGrpc("Node6", "mailserver", null, null);
215         NodeGrpc n2 = Client.createNodeGrpc("Node9", "endhost", neighbours, null);
216         Map<String,String> map = new HashMap<String,String>();
217         map.put("mailserver", "Node6");
218         ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
219         NodeGrpc n3 = Client.createNodeGrpc("Node10", "mailclient", null, conf);
220         NodeGrpc n4 = Client.createNodeGrpc("Node11", "nat", null, null);
221
222         NewNode nw1 = client.createNode(n1, graph.getId());
223         NewNode nw2 = client.createNode(n2, graph.getId());
224         NewNode nw3 = client.createNode(n3, graph.getId());
225         NewNode nw4 = client.createNode(n4, graph.getId());
226         assertEquals(nw1.getSuccess(),true);
227         assertEquals(nw2.getSuccess(),true);
228         assertEquals(nw3.getSuccess(),true);
229         assertEquals(nw4.getSuccess(),true);
230         n1 = NodeGrpc.newBuilder(n1).setId(nw1.getNode().getId()).build();
231         n2 = NodeGrpc.newBuilder(n2).setId(nw2.getNode().getId()).build();
232         n3 = NodeGrpc.newBuilder(n3).setId(nw3.getNode().getId()).build();
233         n4 = NodeGrpc.newBuilder(n4).setId(nw4.getNode().getId()).build();
234
235         // getNodes
236         List<NodeGrpc> node_list = client.getNodes(graph.getId());
237         TreeSet<NodeGrpc> pts = new TreeSet<NodeGrpc>(new NodeGrpcComparator());
238         pts.addAll(node_list);
239         Iterator<NodeGrpc> nodes = pts.iterator();
240         //sorted by name
241         if(nodes.hasNext()){
242             assertEquals(nodes.next().getName(), n3.getName());
243             assertEquals(nodes.next().getName(), n4.getName());
244             assertEquals(nodes.next().getName(), n1.getName());
245             assertEquals(nodes.next().getName(), n2.getName());
246         }
247
248         //deleteNode
249         client.deleteNode(graph.getId(), n1.getId());
250         // run
251         node_list = client.getNodes(graph.getId());
252         pts = new TreeSet<NodeGrpc>(new NodeGrpcComparator());
253         pts.addAll(node_list);
254         nodes = pts.iterator();
255
256         assertEquals(nodes.next().getName(), n3.getName());
257         assertEquals(nodes.next().getName(), n4.getName());
258         assertEquals(nodes.next().getName(), n2.getName());
259     }
260
261     @Test
262     public void test5Neighbours() throws Exception {
263         System.out.println("[DEBUG] test5Load starts");
264         NeighbourGrpc ufoundedNeighbour = NeighbourGrpc.newBuilder()
265                 .setErrorMessage("There is no Graph whose Id is '1'").build();;
266
267                 // Neighbour not found in the server
268                 NeighbourGrpc neighbour = client.getNeighbour(1, 1, 1);//id not present
269
270                 assertEquals(ufoundedNeighbour, neighbour);
271
272                 GraphGrpc graph = Client.createGraphGrpc(null);
273                 graph = client.createGraph(graph).getGraph();
274
275                 List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
276                 NeighbourGrpc nb = Client.createNeighbourGrpc("Node1");
277                 neighbours.add(nb);
278                 NodeGrpc n1 = Client.createNodeGrpc("Node1", "antispam", null, null);
279                 NodeGrpc n2 = Client.createNodeGrpc("Node2", "endhost", neighbours, null);
280                 NodeGrpc n3 = Client.createNodeGrpc("Node3", "endhost", null, null);
281                 NodeGrpc n4 = Client.createNodeGrpc("Node4", "endpoint", null, null);
282                 NodeGrpc n5 = Client.createNodeGrpc("Node5", "webserver", null, null);
283                 Map<String,String> map = new HashMap<String,String>();
284                 map.put("webserver", "Node5");
285                 ConfigurationGrpc conf = Client.createConfigurationGrpc(map, null, null, null);
286                 NodeGrpc n6 = Client.createNodeGrpc("Node6", "webclient", null, conf);
287                 NodeGrpc n7 = Client.createNodeGrpc("Node7", "cache", null, null);
288                 NodeGrpc n8 = Client.createNodeGrpc("Node8", "firewall", null, null);
289                 NodeGrpc n9 = Client.createNodeGrpc("Node9", "fieldmodifier", null, null);
290                 NodeGrpc n10 = Client.createNodeGrpc("Node10", "dpi", null, null);
291                 NewNode nw1 = client.createNode(n1, graph.getId());
292                 NewNode nw2 = client.createNode(n2, graph.getId());
293                 NewNode nw3 = client.createNode(n3, graph.getId());
294                 NewNode nw4 = client.createNode(n4, graph.getId());
295                 NewNode nw5 = client.createNode(n5, graph.getId());
296                 NewNode nw6 = client.createNode(n6, graph.getId());
297                 NewNode nw7 = client.createNode(n7, graph.getId());
298                 NewNode nw8 = client.createNode(n8, graph.getId());
299                 NewNode nw9 = client.createNode(n9, graph.getId());
300                 NewNode nw10 = client.createNode(n10, graph.getId());
301                 assertEquals(nw1.getSuccess(),true);
302                 assertEquals(nw2.getSuccess(),true);
303                 assertEquals(nw3.getSuccess(),true);
304                 assertEquals(nw4.getSuccess(),true);
305                 assertEquals(nw5.getSuccess(),true);
306                 assertEquals(nw6.getSuccess(),true);
307                 assertEquals(nw7.getSuccess(),true);
308                 assertEquals(nw8.getSuccess(),true);
309                 assertEquals(nw9.getSuccess(),true);
310                 assertEquals(nw10.getSuccess(),true);
311
312                 // getNeighbour, but first add it
313                 NeighbourGrpc addedNeighbour = Client.createNeighbourGrpc("Node9");
314                 NewNeighbour response = client.createNeighbour(addedNeighbour, graph.getId(), nw1.getNode().getId());
315                 addedNeighbour = response.getNeighbour();
316                 neighbour = client.getNeighbour(graph.getId(), nw1.getNode().getId(), addedNeighbour.getId());
317
318                 assertEquals(addedNeighbour.getId(), neighbour.getId());
319
320                 //updateNeighbour
321                 NeighbourGrpc updatedNeighbour = Client.createNeighbourGrpc("Node10");
322
323                 response = client.updateNeighbour(graph.getId(), nw1.getNode().getId(), addedNeighbour.getId(),updatedNeighbour);
324
325                 assertEquals(response.getSuccess(),true);
326                 assertEquals(response.getNeighbour().getName(),"Node10");
327     }
328
329     @Test
330     public void test6Neighbours() throws Exception {
331         System.out.println("[DEBUG] test6Load starts");
332         // setup
333         GraphGrpc graph = Client.createGraphGrpc(null);
334         //createGraph
335         graph = client.createGraph(graph).getGraph();
336
337         NodeGrpc n1 = Client.createNodeGrpc("Node1", "antispam", null, null);
338         NodeGrpc n2 = Client.createNodeGrpc("Node2", "endhost", null, null);
339         NodeGrpc n3 = Client.createNodeGrpc("Node3", "endhost", null, null);
340         NodeGrpc n4 = Client.createNodeGrpc("Node4", "endpoint", null, null);
341         NewNode nw1 = client.createNode(n1, graph.getId());
342         NewNode nw2 = client.createNode(n2, graph.getId());
343         NewNode nw3 = client.createNode(n3, graph.getId());
344         NewNode nw4 = client.createNode(n4, graph.getId());
345         assertEquals(nw1.getSuccess(),true);
346         assertEquals(nw2.getSuccess(),true);
347         assertEquals(nw3.getSuccess(),true);
348         assertEquals(nw4.getSuccess(),true);
349
350         //createNeighbour
351         NeighbourGrpc nn1 = Client.createNeighbourGrpc("Node2");
352         NewNeighbour addedNeighbour1 = client.createNeighbour(nn1, graph.getId(), nw1.getNode().getId());
353         assertEquals(addedNeighbour1.getSuccess(),true);
354         NeighbourGrpc nn2 = Client.createNeighbourGrpc("Node3");
355         NewNeighbour addedNeighbour2 = client.createNeighbour(nn2, graph.getId(), nw1.getNode().getId());
356         assertEquals(addedNeighbour2.getSuccess(),true);
357         NeighbourGrpc nn3 = Client.createNeighbourGrpc("Node4");
358         NewNeighbour addedNeighbour3 = client.createNeighbour(nn3, graph.getId(), nw1.getNode().getId());
359         assertEquals(addedNeighbour3.getSuccess(),true);
360
361         nn1 = NeighbourGrpc.newBuilder(nn1).setId(1).build();
362         nn2 = NeighbourGrpc.newBuilder(nn2).setId(2).build();
363         nn3 = NeighbourGrpc.newBuilder(nn3).setId(3).build();
364         // run
365         List<NeighbourGrpc> node_list = client.getNeighbours(graph.getId(), nw1.getNode().getId());
366         TreeSet<NeighbourGrpc> pts = new TreeSet<NeighbourGrpc>(new NeighbourGrpcComparator());
367         pts.addAll(node_list);
368         Iterator<NeighbourGrpc> neighbours = pts.iterator();
369
370         while(neighbours.hasNext()){
371             neighbours.next();
372         }
373
374         if(neighbours.hasNext()){
375             assertEquals(neighbours.next(), addedNeighbour1.getNeighbour());
376             assertEquals(neighbours.next(), addedNeighbour2.getNeighbour());
377             assertEquals(neighbours.next(), addedNeighbour3.getNeighbour());
378         }
379
380         //deleteNeighbour
381         boolean succ = client.deleteNeighbour(graph.getId(), nw1.getNode().getId(), addedNeighbour1.getNeighbour().getId());
382         assertEquals(succ, true);
383         // run
384         node_list = client.getNeighbours(graph.getId(), nw1.getNode().getId());
385         pts = new TreeSet<NeighbourGrpc>(new NeighbourGrpcComparator());
386         pts.addAll(node_list);
387         neighbours = pts.iterator();
388
389         while(neighbours.hasNext()){
390             neighbours.next();
391         }
392
393
394         if(neighbours.hasNext()){
395             assertEquals(neighbours.next(), addedNeighbour2.getNeighbour());
396             assertEquals(neighbours.next(), addedNeighbour3.getNeighbour());
397         }
398     }
399 }
400
401
402 class NodeGrpcComparator implements Comparator<NodeGrpc> {
403     public int compare(NodeGrpc n0, NodeGrpc n1) {
404         return n0.getName().compareTo(n1.getName());
405     }
406 }
407
408 class NeighbourGrpcComparator implements Comparator<NeighbourGrpc> {
409     public int compare(NeighbourGrpc n0, NeighbourGrpc n1) {
410         return n0.getName().compareTo(n1.getName());
411     }
412 }
413
414 class GraphGrpcComparator implements Comparator<GraphGrpc> {
415     public int compare(GraphGrpc n0, GraphGrpc n1) {
416         if(n0.getId() == n1.getId())
417             return 0;
418         else if (n0.getId() > n1.getId())
419             return 1;
420         else return -1;
421     }
422 }