summaryrefslogtreecommitdiffstats
path: root/verigraph/src/it/polito/verigraph/grpc/client/Client.java
blob: 0fc76efc1534664705d2c160fb33d52efb48898e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*******************************************************************************
 * Copyright (c) 2017 Politecnico di Torino and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0
 *******************************************************************************/
package it.polito.verigraph.grpc.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.google.protobuf.Message;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import it.polito.verigraph.grpc.ConfigurationGrpc;
import it.polito.verigraph.grpc.GetRequest;
import it.polito.verigraph.grpc.GraphGrpc;
import it.polito.verigraph.grpc.NeighbourGrpc;
import it.polito.verigraph.grpc.NewGraph;
import it.polito.verigraph.grpc.NewNeighbour;
import it.polito.verigraph.grpc.NewNode;
import it.polito.verigraph.grpc.NodeGrpc;
import it.polito.verigraph.grpc.NodeGrpc.FunctionalType;
import it.polito.verigraph.grpc.Policy;
import it.polito.verigraph.grpc.Policy.PolicyType;
import it.polito.verigraph.grpc.RequestID;
import it.polito.verigraph.grpc.Status;
import it.polito.verigraph.grpc.VerificationGrpc;
import it.polito.verigraph.grpc.VerigraphGrpc;

public class Client {

    private final ManagedChannel channel;
    private final VerigraphGrpc.VerigraphBlockingStub blockingStub;

    public Client(String host, int port) {
        this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(true));
    }

    /** Construct client for accessing RouteGuide server using the existing channel. */
    public Client(ManagedChannelBuilder<?> channelBuilder) {
        channel = channelBuilder.build();
        blockingStub = VerigraphGrpc.newBlockingStub(channel);
    }

    /** Get array of graphs */
    public List<GraphGrpc> getGraphs() {
        List<GraphGrpc> graphsRecveived = new ArrayList<GraphGrpc>();
        GetRequest request = GetRequest.newBuilder().build();
        Iterator<GraphGrpc> graphs;
        try {
            graphs = blockingStub.getGraphs(request);
            while (graphs.hasNext()) {
                GraphGrpc graph = graphs.next();
                System.out.println("[getGraphs] Graph id : "+graph.getId());
                if(graph.getErrorMessage().equals("")){
                    System.out.println("[getGraphs] Node id : "+graph.getId());
                    graphsRecveived.add(graph);
                }else{
                    System.out.println("[getGraphs] Error : " + graph.getErrorMessage());
                    return graphsRecveived;
                }
            }
        } catch (StatusRuntimeException ex) {
            System.err.println("[getGraphs] RPC failed: " + ex.getStatus());
        }
        return graphsRecveived;
    }

    /** Create new Graph */
    public NewGraph createGraph(GraphGrpc gr) {
        NewGraph response;
        try {
            response = blockingStub.createGraph(gr);
            if(response.getSuccess()){
                System.out.println("[createGraph] Successful operation ");
            }else{
                System.out.println("[createGraph] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[createGraph] RPC failed: " + e.getStatus());
            return NewGraph.newBuilder().setSuccess(false).build();
        }
        return response;
    }

    /** Delete a Graph */
    public boolean deleteGraph(long idGraph) {

        RequestID id = RequestID.newBuilder().setIdGraph(idGraph).build();
        Status response;
        try {
            response = blockingStub.deleteGraph(id);
            if(response.getSuccess()){
                System.out.println("[deleteGraph] Successful operation ");
                return true;
            }else{
                System.out.println("[deleteGraph] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[deleteGraph] RPC failed: " + e.getStatus());
        }
        return false;
    }

    /** Edits a single graph */
    public NewGraph updateGraph(long idGraph, GraphGrpc newGraph) {

        GraphGrpc gr = GraphGrpc.newBuilder(newGraph).setId(idGraph).build();
        NewGraph response;
        try {
            response = blockingStub.updateGraph(gr);
            if(response.getSuccess()){
                System.out.println("[updateGraph] Successful operation ");
            }else{
                System.out.println("[updateGraph] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[updateGraph] RPC failed: " + e.getStatus());
            return NewGraph.newBuilder().setSuccess(false).build();
        }
        return response;
    }

    /** Get a single graph*/
    public GraphGrpc getGraph(long idGraph) {

        RequestID request = RequestID.newBuilder().setIdGraph(idGraph).build() ;
        try {
            GraphGrpc graph = blockingStub.getGraph(request);
            if(!graph.getErrorMessage().equals("")){
                System.out.println("[getGraph] Error in operation: " + graph.getErrorMessage());
            }
            return graph;
        } catch (StatusRuntimeException ex) {
            System.err.println("[getGraph] RPC failed: " + ex.getStatus());
            return null;
        }
    }

    /** Verify a given policy*/
    public VerificationGrpc verify(Policy policy) {

        VerificationGrpc response;
        try {
            response = blockingStub.verifyPolicy(policy);
            if(!response.getErrorMessage().equals("")){
                System.out.println("[verify] Error in operation: " + response.getErrorMessage());
            }
            System.out.println("[verify] Result : "+response.getResult());
            System.out.println("[verify] Comment : "+response.getComment());
            //uncomment if you want to print the paths
            /*for(TestGrpc test:response.getTestList()){
            System.out.println("Test : "+test.getResult()+". Traversed nodes:");
            for(NodeGrpc node:test.getNodeList()){
            //prints only the name
            System.out.println("Node "+node.getName());
            }
            }*/
            return response;
        } catch (StatusRuntimeException e) {
            System.err.println("[verify] RPC failed: " + e.getStatus());
            return null;
        }
    }

    /*Node part*/

    /** Get array of nodes */
    public List<NodeGrpc> getNodes(long idGraph) {
        List<NodeGrpc> nodesReceived = new ArrayList<NodeGrpc>();
        RequestID request = RequestID.newBuilder().setIdGraph(idGraph).build();
        Iterator<NodeGrpc> nodes;
        try {
            nodes = blockingStub.getNodes(request);
            while (nodes.hasNext()) {
                NodeGrpc node = nodes.next();
                if(node.getErrorMessage().equals("")){
                    System.out.println("[getNodes] Node id : "+node.getId());
                    nodesReceived.add(node);
                }else{
                    System.out.println("[getNodes] Error : " + node.getErrorMessage());
                    return nodesReceived;
                }
            }
        } catch (StatusRuntimeException ex) {
            System.err.println("[getNodes] RPC failed: " + ex.getStatus());
        }
        return nodesReceived;
    }

    /** Create new Node */
    public NewNode createNode(NodeGrpc node, long idGraph) {

        NewNode response;
        try {
            NodeGrpc n = NodeGrpc.newBuilder(node).setIdGraph(idGraph).build();
            response = blockingStub.createNode(n);
            if(response.getSuccess()){
                System.out.println("[createNode] Successful operation ");
            }else{
                System.out.println("[createNode] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[createNode] RPC failed: " + e.getStatus());
            return NewNode.newBuilder().setSuccess(false).build();
        }
        return response;
    }

    /** Delete a Node */
    public boolean deleteNode(long idGraph, long idNode) {

        RequestID id = RequestID.newBuilder().setIdGraph(idGraph).setIdNode(idNode).build();
        Status response;
        try {
            response = blockingStub.deleteNode(id);
            if(response.getSuccess()){
                System.out.println("[deleteNode] Successful operation ");
                return true;
            }else{
                System.out.println("[deleteNode] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[deleteNode] RPC failed: " + e.getStatus());
        }
        return false;
    }

    /** Edits a single Node */
    public NewNode updateNode(long idGraph, long idNode, NodeGrpc node) {

        NodeGrpc nu = NodeGrpc.newBuilder(node).setIdGraph(idGraph).setId(idNode).build();
        NewNode response;
        try {
            response = blockingStub.updateNode(nu);
            if(response.getSuccess()){
                System.out.println("[updateNode] Successful operation ");
            }else{
                System.out.println("[updateNode] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[updateNode] RPC failed: " + e.getStatus());
            return NewNode.newBuilder().setSuccess(false).build();
        }
        return response;
    }

    /** Get a single Node*/
    public NodeGrpc getNode(long idGraph, long idNode) {

        RequestID request = RequestID.newBuilder().setIdGraph(idGraph).setIdNode(idNode).build() ;
        try {
            NodeGrpc node = blockingStub.getNode(request);
            if(!node.getErrorMessage().equals("")){
                System.out.println("[getNode] Error in operation: " + node.getErrorMessage());
            }
            return node;
        } catch (StatusRuntimeException ex) {
            System.err.println("[getNode] RPC failed: " + ex.getStatus());
            return null;
        }
    }

    /** Configure a single Node*/
    public boolean configureNode(long idGraph, long idNode, ConfigurationGrpc configuration) {

        try {
            ConfigurationGrpc request = ConfigurationGrpc.newBuilder(configuration)
                    .setIdGraph(idGraph).setIdNode(idNode).build() ;

            Status response = blockingStub.configureNode(request);
            if(response.getSuccess()){
                System.out.println("[configureNode] Successful operation ");
                return true;
            }else{
                System.out.println("[configureNode] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[configureNode] RPC failed: " + e.getStatus());
        }
        return false;
    }

    /*Neighbour part*/

    /** Get array of neighbours */
    public List<NeighbourGrpc> getNeighbours(long idGraph, long idNode) {

        List<NeighbourGrpc> neighboursReceived = new ArrayList<NeighbourGrpc>();
        RequestID request = RequestID.newBuilder().setIdGraph(idGraph).setIdNode(idNode).build() ;
        Iterator<NeighbourGrpc> neighbours;
        try {
            neighbours = blockingStub.getNeighbours(request);
            while (neighbours.hasNext()) {
                NeighbourGrpc neighbour = neighbours.next();
                if(neighbour.getErrorMessage().equals("")){
                    System.out.println("[getNeighbours] Neighbour id : "+neighbour.getId());
                    neighboursReceived.add(neighbour);
                }else{
                    System.out.println("[getNeighbours] Error : " + neighbour.getErrorMessage());
                    return neighboursReceived;
                }
            }
        } catch (StatusRuntimeException ex) {
            System.err.println("[getNeighbours] RPC failed: " + ex.getStatus());
        }
        return neighboursReceived;
    }

    /** Create new Neighbour */
    public NewNeighbour createNeighbour(NeighbourGrpc neighbour, long idGraph, long idNode) {

        NewNeighbour response;
        try {
            NeighbourGrpc n = NeighbourGrpc.newBuilder(neighbour).setIdGraph(idGraph)
                    .setIdNode(idNode).build();
            response = blockingStub.createNeighbour(n);
            if(response.getSuccess()){
                System.out.println("[createNeighbour] Successful operation ");
            }else{
                System.out.println("[createNeighbour] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[createNeighbour] RPC failed: " + e.getStatus());
            return NewNeighbour.newBuilder().setSuccess(false).build();
        }
        return response;
    }

    /** Delete a Neighbour */
    public boolean deleteNeighbour(long idGraph, long idNode, long idNeighbour) {

        RequestID id = RequestID.newBuilder().setIdGraph(idGraph).setIdNode(idNode).setIdNeighbour(idNeighbour).build();
        Status response;
        try {
            response = blockingStub.deleteNeighbour(id);
            if(response.getSuccess()){
                System.out.println("[deleteNeighbour] Successful operation ");
                return true;
            }else{
                System.out.println("[deleteNeighbour] Unsuccesful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[deleteNeighbour] RPC failed: " + e.getStatus());
        }
        return false;
    }

    /** Edits a single Neighbour */
    public NewNeighbour updateNeighbour(long idGraph, long idNode, long idNeighbour, NeighbourGrpc neighbour) {

        NeighbourGrpc nu = NeighbourGrpc.newBuilder(neighbour).setIdGraph(idGraph).setIdNode(idNode)
                .setId(idNeighbour).build();
        NewNeighbour response;
        try {
            response = blockingStub.updateNeighbour(nu);
            if(response.getSuccess()){
                System.out.println("[updateNeighbour] Successful operation ");
            }else{
                System.out.println("[updateNeighbour] Unsuccessful operation: " + response.getErrorMessage());
            }
        } catch (StatusRuntimeException e) {
            System.err.println("[updateNeighbour] RPC failed: " + e.getStatus());
            return NewNeighbour.newBuilder().setSuccess(false).build();
        }
        return response;
    }

    /** Get a single Neighbour*/
    public NeighbourGrpc getNeighbour(long idGraph, long idNode, long idNeighbour) {

        RequestID request = RequestID.newBuilder().setIdGraph(idGraph).setIdNode(idNode).setIdNeighbour(idNeighbour).build() ;
        try {
            NeighbourGrpc neighbour = blockingStub.getNeighbour(request);
            if(!neighbour.getErrorMessage().equals("")){
                System.out.println("[getNeighbour] Error in operation: " + neighbour.getErrorMessage());
            }
            return neighbour;
        } catch (StatusRuntimeException ex) {
            System.err.println("[getNeighbour] RPC failed: " + ex.getStatus());
            return null;
        }
    }

    public void shutdown() throws InterruptedException {
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }

    /** Test on the Server. */
    public static void main(String[] args) throws Exception {

        List<Long> listGraph = new ArrayList<Long>();//list of ID

        Client client = new Client("localhost" , 50051);
        try {
            NodeGrpc node1 = createNodeGrpc("Node1", "endhost", null, null);
            List<NeighbourGrpc> neighbours = new ArrayList<NeighbourGrpc>();
            NeighbourGrpc nb = createNeighbourGrpc("Node1");
            neighbours.add(nb);
            NodeGrpc node2 = createNodeGrpc("Node2", "endpoint", neighbours, null);
            List<NodeGrpc> nodes = new ArrayList<NodeGrpc>();
            nodes.add(node1);
            nodes.add(node2);

            GraphGrpc graph = createGraphGrpc(nodes);

            NewGraph createdGraph = client.createGraph(graph);
            if(createdGraph.getSuccess() == true){
                listGraph.add(createdGraph.getGraph().getId());
                System.out.println("Created graph with id :"+ createdGraph.getGraph().getId());
            }

            for(GraphGrpc g : client.getGraphs()){
                long graph_id = g.getId();
                System.out.println("graph id: "+graph_id);
                for (NodeGrpc n: g.getNodeList()){
                    long node_id = n.getId();
                    System.out.println("node id: "+node_id);
                    NodeGrpc nodenew = createNodeGrpc("Newnode","endhost",null,null);
                    //NewNode node = client.updateNode(graph_id, node_id, nodenew);
                    //System.out.println("Client updates node: "+node.getSuccess());
                    System.out.println("end");
                }
            }


        } catch(Exception ex){
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        }finally {
            client.shutdown();
        }
    }

    public static NeighbourGrpc createNeighbourGrpc(String name){
        return NeighbourGrpc.newBuilder().setName(name).build();
    }

    public static NodeGrpc createNodeGrpc(String name, String functionalType, List<NeighbourGrpc> neighbours, ConfigurationGrpc conf) throws Exception{
        NodeGrpc.Builder nb = NodeGrpc.newBuilder();

        if(name != null)
            nb.setName(name);
        else
            throw new Exception("Node must have a name");

        if(functionalType != null)
            nb.setFunctionalType(FunctionalType.valueOf(functionalType));
        else
            throw new Exception("Node must have a functional type");

        if( neighbours!= null){
            for(NeighbourGrpc value:neighbours)
                nb.addNeighbour(value);
        }
        if(conf == null){
            try{
                conf = createConfigurationGrpc(null, null, null, null);
            }catch(Exception e){
                throw new Exception(e.getMessage());
            }
        }
        nb.setConfiguration(conf);
        return nb.build();
    }

    public static GraphGrpc createGraphGrpc(List<NodeGrpc> nodes){
        GraphGrpc.Builder gb = GraphGrpc.newBuilder();
        if(nodes != null){
            for(NodeGrpc value:nodes)
                gb.addNode(value);
        }
        return gb.build();
    }

    public static Policy createPolicy(String src, String dst, String type, String middlebox, long idGraph) throws IllegalArgumentException{
        if(!validMiddlebox(type, middlebox))
            throw new IllegalArgumentException("Not valid middlebox valid with this type");
        Policy.Builder policy = Policy.newBuilder();
        policy.setIdGraph(idGraph);
        if(src != null)
            policy.setSource(src);
        else{
            throw new IllegalArgumentException("Please insert source field");
        }
        if(dst != null)
            policy.setDestination(dst);
        else{
            throw new IllegalArgumentException("Please insert destination field");
        }
        if(type != null)
            policy.setType(PolicyType.valueOf(type));
        if(middlebox != null)
            policy.setMiddlebox(middlebox);
        return policy.build();
    }

    public static ConfigurationGrpc createConfigurationGrpc(Map<String,String> parameters, List<String> lists, String id, String description) throws Exception{
        ConfigurationGrpc.Builder cb = ConfigurationGrpc.newBuilder();
        StringBuilder sb = new StringBuilder("[");
        if(parameters != null && lists == null){
            int i = 0;
            sb.append("{");
            for (String key: parameters.keySet()) {
                sb.append("\"");
                sb.append(key);
                sb.append("\":\"");
                sb.append(parameters.get(key));
                sb.append("\"");
                if((i+1)<parameters.keySet().size()){
                    sb.append(", ");
                }
                i++;
            }
            sb.append("}");
        }
        else if(parameters == null && lists != null){
            int i = 0;
            for (String value: lists) {
                sb.append("\"");
                sb.append(value);
                sb.append("\"");
                if((i+1)<lists.size()){
                    sb.append(", ");
                }
                i++;
            }
        }
        else if(parameters != null && lists != null){
            throw new Exception("Error, configuration must contains or a sequence name:value or sequence"
                    + "of string, but not both");
        }
        sb.append("]");
        cb.setConfiguration(sb.toString());
        if(id != null)
            cb.setId(id);
        if(description != null)
            cb.setDescription(description);
        return cb.build();
    }

    private static boolean validMiddlebox(String type, String middlebox) {
        if(type == null)
            return false;
        if(type.equals("reachability") && (middlebox == null || middlebox.equals("")))
            return true;
        if(type.equals("isolation") && !(middlebox == null || middlebox.equals("")))
            return true;
        if(type.equals("traversal") && !(middlebox == null || middlebox.equals("")))
            return true;
        return false;
    }
}